示例#1
0
文件: LobbyMenu.cs 项目: corefan/yad2
 private void HouseChangeUpdate()
 {
     if (GlobalData.me.userID > 0)
     {
         if (!Network.isHost)
         {
             PlayersListMessage pl = new PlayersListMessage();
             List <User>        l  = new List <User>();
             l.Add(GlobalData.me);
             pl.players = l;
             Network.SendMessage(pl);
         }
         else
         {
             GlobalData.usersList[0].House = GlobalData.me.House;
             PlayersListMessage pl = new PlayersListMessage();
             pl.players = GlobalData.usersList;
             Network.SendMessage(pl);
         }
     }
     UpdatePlayerList();
 }
示例#2
0
文件: Network.cs 项目: corefan/yad2
        public void ServerHandleConnection(object state)
        {
            NetworkStream ns = client.GetStream();

            BinaryFormatter bf = new BinaryFormatter();
            NetworkMessage  nm;

            try
            {
                while (true)
                {
                    nm = (NetworkMessage)bf.Deserialize(ns);
                    if (nm == null)
                    {
                        continue;
                    }

                    if (nm is IntroduceMessage)
                    {
                        IntroduceMessage im = nm as IntroduceMessage;

                        int userID = User.GetNewID();
                        Network.connections.Add(client, userID);

                        ResponseMessage rm = new ResponseMessage();
                        rm.senderID = GlobalData.me.userID;
                        rm.NewID    = userID;
                        bf.Serialize(ns, rm);

                        User u = new User();
                        u.name   = im.playerNick;
                        u.House  = im.house;
                        u.userID = userID;

                        GlobalData.usersList.Add(u);
                        //PlayersListMessage playerList = new PlayersListMessage();
                        //playerList.players = GlobalData.usersList;
                        //bf.Serialize(ns, playerList);
                        PlayersListMessage pl = new PlayersListMessage();
                        pl.players  = GlobalData.usersList;
                        pl.senderID = GlobalData.me.userID;
                        Network.SendMessage(pl);

                        if (PlayersUpdate != null)
                        {
                            PlayersUpdate(this, new EventArgs());
                        }
                    }
                    if (nm is TextMessage)
                    {
                        lock (Network.messages)
                        {
                            Network.messages.Enqueue(nm);
                        }
                        if (NewMessage != null)
                        {
                            NewMessage(this, new EventArgs());
                        }
                        Network.SendMessage(nm);
                    }
                    if (nm is PlayersListMessage)
                    {
                        if (((PlayersListMessage)nm).players.Count == 1)
                        {
                            User u = ((PlayersListMessage)nm).players[0];
                            int  i;
                            for (i = 0; i < GlobalData.usersList.Count; i++)
                            {
                                if (GlobalData.usersList[i].userID == u.userID)
                                {
                                    GlobalData.usersList[i].name  = u.name;
                                    GlobalData.usersList[i].House = u.House;
                                    break;
                                }
                            }
                            //tego nie powinno byæ
                            if (i == GlobalData.usersList.Count)
                            {
                                client.Close();
                                Network.connections.Remove(client);
                                throw new Exception(); //Not on the list!
                            }
                            PlayersListMessage pl = new PlayersListMessage();
                            pl.players  = GlobalData.usersList;
                            pl.senderID = GlobalData.me.userID;
                            Network.SendMessage(pl);

                            if (PlayersUpdate != null)
                            {
                                PlayersUpdate(this, new EventArgs());
                            }
                        }
                    }
                    if (nm is ReadyToPlayMessage)
                    {
                        int u = Network.connections[client];
                        for (int i = 0; i < GlobalData.usersList.Count; i++)
                        {
                            if (u == GlobalData.usersList[i].userID)
                            {
                                GlobalData.playersReadyToPlay[i - 1] = true;
                                break;
                            }
                        }
                    }
                    if (nm is CreateMessage)
                    {
                        if (GlobalData.isPlaying)
                        {
                            lock (Network.messages)
                            {
                                Network.messages.Enqueue((CreateMessage)nm);
                            }
                            //Network.SendMessage(nm);
                        }
                    }
                    if (nm is DestroyMessage)
                    {
                        if (GlobalData.isPlaying)
                        {
                            lock (Network.messages)
                            {
                                Network.messages.Enqueue((DestroyMessage)nm);
                            }
                            Network.SendMessage(nm);
                        }
                    }
                    if (nm is MoveMessage)
                    {
                        if (GlobalData.isPlaying)
                        {
                            lock (Network.messages)
                            {
                                Network.messages.Enqueue((MoveMessage)nm);
                            }
                        }
                    }
                    if (nm is AttackMessage)
                    {
                        if (GlobalData.isPlaying)
                        {
                            lock (Network.messages)
                            {
                                Network.messages.Enqueue((AttackMessage)nm);
                            }
                            Network.SendMessage(nm);
                        }
                    }
                }
            }
            catch (Exception)
            {
                //MessageBox.Show(exc.Message);
            }
            finally
            {
                ns.Close();
                AfterDisconnection();

                PlayersListMessage pl = new PlayersListMessage();
                pl.players  = GlobalData.usersList;
                pl.senderID = GlobalData.me.userID;
                Network.SendMessage(pl);
            }
        }