Example #1
0
 static void BroadcastPlayerPosition(Player player)
 {
     foreach (KeyValuePair<string, Player> sp in players_byname)
     {
         PlayerPositionUpdate msg_sc = new PlayerPositionUpdate();
         msg_sc.ClientID = player.ClientID;
         msg_sc.Position = new float[2];
         msg_sc.Position[0] = player.Position.X;
         msg_sc.Position[1] = player.Position.Y;
         msg_sc.State = (int)CharacterState.Idle;
         msg_sc.Dir = player.Direction;
         ProtobufAdapter.Send(sp.Value.Channel, msg_sc);
     }
 }
Example #2
0
 public void DelNetPlayer(Player ch)
 {
     netplayers.Remove(ch);
 }
Example #3
0
        static void OnMessageReceive(PacketRecieveMessagerArgs e)
        {
            ProtobufAdapter adapter = (ProtobufAdapter)e.Message;
            Type type = adapter.Message.GetType();
            switch (type.Name)
            {
                case "PlayerLoginRequestMsg":
                    {
                        PlayerLoginRequestMsg plm_cs = (PlayerLoginRequestMsg)adapter.Message;
                        if (players_byname.ContainsKey(plm_cs.Name))
                        {
                            PlayerLoginResultMsg plrm = new PlayerLoginResultMsg();
                            plrm.Result = LoginResult.Failed_AlreadyLogin;
                            ProtobufAdapter.Send(e.Channel, plrm);
                        }
                        else
                        {
                            //get accout infomation
                            PlayerSync ps = null;
                            try
                            {
                                using (FileStream fs = new FileStream(string.Format(@"accounts/{0:s}.xml", plm_cs.Name), FileMode.Open))
                                {
                                    XmlSerializer formatter = new XmlSerializer(typeof(PlayerSync));
                                    ps = formatter.Deserialize(fs) as PlayerSync;
                                }
                            }
                            catch (FileNotFoundException)
                            {
                                ps = null;
                            }
                            catch (FileLoadException)
                            {
                                ps = null;
                            }
                            if (ps != null)
                            {
                                //check password
                                if (ps.Password != plm_cs.Password)
                                {
                                    PlayerLoginResultMsg plrm = new PlayerLoginResultMsg();
                                    plrm.Result = LoginResult.Failed_Password;
                                    ProtobufAdapter.Send(e.Channel, plrm);
                                    break;
                                }
                                else
                                {
                                    Player pl = new Player(plm_cs.Name, scene);
                                    pl.Channel = e.Channel;
                                    pl.ClientID = e.Channel.ClientID;

                                    //pl.Position = new Vector2((float)random.NextDouble() * 1000.0f + 100.0f,
                                    //                                    (float)random.NextDouble() * 1000.0f + 100.0f);
                                    // pl.Speed = 200.0f;
                                    pl.Position = new Vector2(ps.Position[0], ps.Position[1]);
                                    pl.Speed = ps.Speed;
                                    pl.HP = ps.HP;
                                    pl.MaxHP = ps.MaxHP;
                                    pl.Name = ps.Name;
                                    pl.ATK = ps.ATK;
                                    pl.DEF = ps.DEF;

                                    //send succeed msg
                                    PlayerLoginResultMsg plrm = new PlayerLoginResultMsg();
                                    plrm.Result = LoginResult.Succeed;
                                    plrm.ClientID = e.Channel.ClientID;
                                    ProtobufAdapter.Send(e.Channel, plrm);
                                    //add player to list
                                    lock (lockobject)
                                    {
                                        players_byname[pl.Name] = pl;
                                        players_byid[pl.ClientID] = pl;
                                    }

                                    PlayerLoginSelfMsg plms_sc = new PlayerLoginSelfMsg();
                                    plms_sc.Position = new float[2];
                                    plms_sc.ClientID = pl.ClientID;
                                    plms_sc.Name = pl.Name;
                                    plms_sc.Position[0] = pl.Position.X;
                                    plms_sc.Position[1] = pl.Position.Y;
                                    plms_sc.Speed = pl.Speed;
                                    plms_sc.ATK = pl.ATK;
                                    plms_sc.DEF = pl.DEF;
                                    plms_sc.HP = pl.HP;
                                    plms_sc.MaxHP = pl.MaxHP;
                                    ProtobufAdapter.Send(e.Channel, plms_sc);

                                    foreach (KeyValuePair<string, Player> sp in players_byname)
                                    {
                                        if (sp.Value.ClientID != pl.ClientID)
                                        {
                                            PlayerLoginMsg plm_sc = new PlayerLoginMsg();
                                            plm_sc.Position = new float[2];
                                            plm_sc.ClientID = sp.Value.ClientID;
                                            plm_sc.Name = sp.Value.Name;
                                            plm_sc.Position[0] = sp.Value.Position.X;
                                            plm_sc.Position[1] = sp.Value.Position.Y;
                                            plm_sc.Speed = sp.Value.Speed;
                                            plm_sc.ATK = sp.Value.ATK;
                                            plm_sc.DEF = sp.Value.DEF;
                                            plm_sc.HP = sp.Value.HP;
                                            plm_sc.MaxHP = sp.Value.MaxHP;
                                            ProtobufAdapter.Send(e.Channel, plm_sc);
                                        }
                                    }
                                    //send player login to other client
                                    foreach (KeyValuePair<string, Player> sp in players_byname)
                                    {
                                        if (sp.Value.ClientID != pl.ClientID)
                                        {
                                            PlayerLoginMsg plm_sc = new PlayerLoginMsg();
                                            plm_sc.Position = new float[2];
                                            plm_sc.ClientID = pl.ClientID;
                                            plm_sc.Name = pl.Name;
                                            plm_sc.Position[0] = pl.Position.X;
                                            plm_sc.Position[1] = pl.Position.Y;
                                            plm_sc.Speed = pl.Speed;
                                            plm_sc.ATK = pl.ATK;
                                            plm_sc.DEF = pl.DEF;
                                            plm_sc.HP = pl.HP;
                                            plm_sc.MaxHP = pl.MaxHP;
                                            ProtobufAdapter.Send(sp.Value.Channel, plm_sc);
                                        }
                                    }
                                }
                            }
                            else
                            {
                                PlayerLoginResultMsg plrm = new PlayerLoginResultMsg();
                                plrm.Result = LoginResult.Failed_Notfound;
                                ProtobufAdapter.Send(e.Channel, plrm);
                            }
                        }
                        break;
                    }
                case "PlayerTargetChanged":
                    {
                        PlayerTargetChanged msg_cs = (PlayerTargetChanged)adapter.Message;
                        if (players_byid.ContainsKey(e.Channel.ClientID))
                        {

                            Player player = players_byid[e.Channel.ClientID];
                            Console.WriteLine("Player {0:s} PlayerTargetChanged ", player.Name);
                            if (player.State != CharacterState.Moving)
                            {
                                CorrectPlayerPoistionToOrigin(player);
                                return;
                            }
                            player.Timer.Stop();
                            double dur = player.Timer.Duration;
                            Vector2 dir = player.Position - player.PositionBackup;
                            float s = dir.Length();
                            float time = s / player.Speed;
                            if (time - (float)dur > 0.1)
                            {
                                Console.ForegroundColor = ConsoleColor.Red;
                                Console.WriteLine("PTC : Player {0:s} error is {1:f}", player.Name, time - (float)dur);
                                Console.ResetColor();
                                CorrectPlayerPoistionToOrigin(player);
                                return;
                            }
                            player.Target = new Vector2(msg_cs.Target[0], msg_cs.Target[1]);
                            player.Position = new Vector2(msg_cs.Position[0], msg_cs.Position[1]);
                            player.PushPosition();

                            foreach (KeyValuePair<string, Player> sp in players_byname)
                            {
                                if (sp.Value.ClientID != player.ClientID)
                                {
                                    PlayerTargetChanged msg_sc = new PlayerTargetChanged();
                                    msg_sc.ClientID = player.ClientID;
                                    msg_sc.Target = new float[2];
                                    msg_sc.Target[0] = player.Target.X;
                                    msg_sc.Target[1] = player.Target.Y;
                                    msg_sc.Position = new float[2];
                                    msg_sc.Position[0] = player.Position.X;
                                    msg_sc.Position[1] = player.Position.Y;
                                    ProtobufAdapter.Send(sp.Value.Channel, msg_sc);
                                }
                            }
                            player.Timer.Start();
                        }
                        break;
                    }
                case "PlayerMoveRequest":
                    {
                        PlayerMoveRequest msg_cs = (PlayerMoveRequest)adapter.Message;
                        if (players_byid.ContainsKey(e.Channel.ClientID))
                        {
                            Player player = players_byid[e.Channel.ClientID];
                            player.State = CharacterState.Moving;

                            Vector2 p = new Vector2(msg_cs.Position[0], msg_cs.Position[1]);
                            if (Vector2.Distance(p, player.Position) > 5.0f)
                            {
                                Console.ForegroundColor = ConsoleColor.Red;
                                Console.WriteLine("PMR : Player {0:s}", player.Name);
                                Console.ResetColor();
                                CorrectPlayerPoistionToOrigin(player);
                                return;
                            }
                            player.Target = new Vector2(msg_cs.Target[0], msg_cs.Target[1]);

                            Console.WriteLine("Player {0:s} PlayerMoveRequest ", player.Name);
                            player.PushPosition();

                            //fixme:
                            //verifying the position and target
                            foreach (KeyValuePair<string, Player> sp in players_byname)
                            {
                                if (sp.Value.ClientID != player.ClientID)
                                {
                                    PlayerMoveRequest msg_sc = new PlayerMoveRequest();
                                    msg_sc.ClientID = player.ClientID;
                                    msg_sc.Target = new float[2];
                                    msg_sc.Target[0] = player.Target.X;
                                    msg_sc.Target[1] = player.Target.Y;
                                    ProtobufAdapter.Send(sp.Value.Channel, msg_sc);
                                }
                            }
                            player.Timer.Start();
                        }
                        break;
                    }
                case "PlayerStopRequest":
                    {
                        PlayerStopRequest msg_cs = (PlayerStopRequest)adapter.Message;
                        if (players_byid.ContainsKey(e.Channel.ClientID))
                        {
                            Player player = players_byid[e.Channel.ClientID];
                            Console.WriteLine("Player {0:s} PlayerStopRequest ", player.Name);
                            if (player.State != CharacterState.Moving)
                            {
                                CorrectPlayerPoistionToOrigin(player);
                                return;
                            }
                            player.Timer.Stop();
                            double dur = player.Timer.Duration;
                            Vector2 dir = player.Target - player.PositionBackup;
                            float s = dir.Length() - 15.0f;  //扣除到达目标点误差
                            float time = s / player.Speed;
                            if (time - (float)dur > 0.1)
                            {
                                Console.ForegroundColor = ConsoleColor.Red;
                                Console.WriteLine("PSR : Player {0:s}", player.Name);
                                Console.ResetColor();
                                CorrectPlayerPoistionToOrigin(player);
                                return;
                            }
                            player.Direction = dir.X >= 0 ? 1 : -1;
                            player.Position = player.Target;
                            BroadcastPlayerPosition(player);
                        }
                        break;
                    }
                case "PlayerPositioReport":
                    {
                        /*Messages.PlayerPositioReport msg_cs = (Messages.PlayerPositioReport)adapter.Message;
                        if (players_byid.ContainsKey(e.Channel.ClientID))
                        {
                            Player player = players_byid[e.Channel.ClientID];
                            if (player.State != CharacterState.Moving)
                            {
                                CorrectPlayerPoistionToOrigin(player);
                                return;
                            }
                            double dur = player.Timer.GetTotalDuration();
                            Vector2 dir = player.Target - player.PositionBackup;
                            float s = dir.Length();
                            float time = s / player.Speed;
                            if (time < (float)dur)
                            {
                                Console.ForegroundColor = ConsoleColor.Red;
                                Console.WriteLine("PPR : Player {0:s} ", player.Name);
                                Console.ResetColor();
                                CorrectPlayerPoistionToOrigin(player);
                                return;
                            }
                            player.Position = new Vector2(msg_cs.Position[0], msg_cs.Position[1]);
                            BroadcastPlayerPosition(player);
                        }*/
                        break;
                    }
            }
        }
Example #4
0
 public void AddNetPlayer(Player ch)
 {
     netplayers.Add(ch);
 }