Exemplo n.º 1
0
        /// <summary>
        /// Thêm người chơi vào phòng
        /// </summary>
        /// <param name="client"></param>
        /// <param name="mes"></param>
        void AddPlayer(Socket client, Message mes)
        {
            Player player = new Player(mes.Sender, client);

            listPlayer[player.Name] = player;
            if (listRoom.ContainsKey(mes.Payload))
            {
                //Phòng đã tồn tại
                if (!listRoom[mes.Payload].AddPlayer(player))
                {
                    //Phòng đầy người
                    Message send_buff = new Message(202, "Server", "");
                    server.Send(client, send_buff.ToString());
                    client.Close();
                }
                else
                {
                    //Tham gia phòng thành công
                    Message send_buff = new Message(201, "Server", "");
                    player.RoomID = mes.Payload;
                    server.Send(client, send_buff.ToString());
                    List <Player> lPlayer = listRoom[player.RoomID].ListPlayer;

                    for (int i = 0; i < lPlayer.Count; i++)
                    {
                        Message send_buff1 = new Message(203, lPlayer[i].Name, (i + 1).ToString());
                        server.Send(client, send_buff1.ToString());
                    }

                    Message send_buff2 = new Message(203, player.Name, lPlayer.Count.ToString());
                    server.SendRoom(listRoom[mes.Payload], client, send_buff2.ToString());
                }
            }
            else
            {
                //Phòng chưa tồn tại
                Room room = new Room(mes.Payload);
                room.AddPlayer(player);
                listRoom.Add(room.Id, room);

                player.RoomID = mes.Payload;

                Message send_buff = new Message(201, player.Name, "");
                server.Send(client, send_buff.ToString());
                Message send_buff1 = new Message(203, player.Name, "1");
                server.Send(client, send_buff1.ToString());
            }
        }