예제 #1
0
        public void OwnerListen(Client OwnerFun)
        {
            while (OwnerThreadFlag)
            {
                if (OwnerFun.Stream.DataAvailable)
                {
                    //int RoomId;
                    string[] ClientMsg = OwnerFun.Br.ReadString().Split(',');
                    bool     flag      = true;
                    switch (ClientMsg[0])
                    {
                    case "1":
                        Opponent.Bw.Write("letter," + ClientMsg[1]);
                        PlayingNow  = ClientMsg[2];
                        CurrentWord = ClientMsg[3];
                        History.Append(ClientMsg[1] + ",");
                        foreach (Client watcher in Watchers.Values)
                        {
                            // watcher.ThreadClient.Suspend();
                            watcher.Bw.Write("1," + ClientMsg[1] + "," + PlayingNow + "," + CurrentWord);
                            //watcher.ThreadClient.Resume();
                        }

                        break;

                    case "2":
                        if (PlayingNow == "OwnerPlaying")
                        {
                            ownerCount++;
                        }
                        else
                        {
                            OpponentCount++;
                        }
                        while (flag)
                        {
                            if (OpponentReplyFlag != null)
                            {
                                SelectedWord();
                                flag = false;
                                History.Clear();
                                if ((bool)OpponentReplyFlag)
                                {
                                    Opponent.Bw.Write("2accepted," + Word);
                                    Owner.Bw.Write("2accepted," + Word);
                                }
                                else
                                {
                                    Score();
                                    Owner.Bw.Write("1accepted," + Word);
                                    Opponent        = null;
                                    NumOfPlayers    = 1;
                                    Join            = true;
                                    OwnerThreadFlag = false;

                                    Owner.ThreadClient.Resume();
                                }
                                OpponentReplyFlag = null;
                            }
                        }

                        break;

                    case "3":
                        if (PlayingNow == "OwnerPlaying")
                        {
                            ownerCount++;
                        }
                        else
                        {
                            OpponentCount++;
                        }
                        Score();
                        Owner.ThreadClient.Resume();
                        NumOfPlayers = 1;
                        Join         = true;
                        History.Clear();
                        while (flag)
                        {
                            if (OpponentReplyFlag != null)
                            {
                                flag = false;
                                if ((bool)OpponentReplyFlag)
                                {
                                    OpponentThreadFlag = false;
                                    OwnerThreadFlag    = false;
                                    SelectedWord();
                                    Opponent.Bw.Write("Opponentaccepted," + Word);
                                    // Owner.ThreadClient.Resume();
                                    Opponent.ThreadClient.Resume();
                                    Owner        = Opponent;
                                    Opponent     = null;
                                    NumOfPlayers = 1;
                                    Join         = true;
                                }
                                else
                                {
                                    Rooms.Remove(Id);
                                    OwnerThreadFlag = false;
                                }
                            }
                        }
                        OpponentReplyFlag = null;
                        break;

                    case "4":
                        OpponentCount++;
                        Score();
                        foreach (Client watcher in Watchers.Values)
                        {
                            watcher.Bw.Write("2," + Owner.Name + " leave the room");
                        }
                        SelectedWord();
                        OpponentThreadFlag = false;
                        OwnerThreadFlag    = false;
                        Opponent.Bw.Write("Ownerleave," + Word);
                        Opponent.ThreadClient.Resume();
                        Owner.ThreadClient.Resume();
                        Owner        = Opponent;
                        Opponent     = null;
                        NumOfPlayers = 1;
                        Join         = true;
                        History.Clear();
                        //Rooms.Remove(Id);
                        break;
                    }
                }
            }
        }
 public async Task LeaveRoom()
 {
     Rooms.removeGuest(g);
     await this.InvokeTo(c => c.g.inRoom() && c.g.roomName == g.roomName, g.sid, "clientLeft");
 }
예제 #3
0
 Room FindRoomContainingPlayer(PlayerData player)
 {
     return(Rooms.Find(x => x.HasPlayer(player)));
 }
예제 #4
0
        void IConnectionMessageHandler.HandleMessage(Player player, string json)
        {
            Message.IsType <ClientMessage.RequestRoomList>(json, (data) =>
            {
                Logger.Log(this, "Recieved a request from {0} for a list a rooms.", player);
                var rooms = Rooms.Select(x => x.RoomData).ToList();
                player.SendMessage(new ServerMessage.RoomList(rooms));
            });

            Message.IsType <ClientMessage.JoinRoom>(json, (data) =>
            {
                Logger.Log(this, "Recieved a request from {0} to join room {1}.", player.Data, data.RoomId);

                var roomHasPlayer = Rooms.Find(x => x.HasPlayer(player.Data));
                if (roomHasPlayer != null)
                {
                    roomHasPlayer.Leave(player.Data);
                }

                var targetRoom = Rooms.Find(x => x.RoomData.ID == data.RoomId);

                if (targetRoom != null)
                {
                    targetRoom.Join(player, data.Password);
                }
                else
                {
                    player.SendRoomJoinNotice(RoomNotice.RoomDoesNotExist);
                }
            });

            Message.IsType <ClientMessage.LeaveRoom>(json, (data) =>
            {
                var containingRoom = Rooms.Find(x => x.HasPlayer(player.Data));

                if (containingRoom != null)
                {
                    Logger.Log(this, "Remove {0} from room", player);
                    containingRoom.Leave(player.Data);
                }
                else
                {
                    Logger.Warn(this, "Cannot remove {0} from room as they are not in that room", player);
                }
            });

            Message.IsType <ClientMessage.CreateRoom>(json, (data) =>
            {
                if (Rooms.Count != SettingsLoader.Values.Server.MaxRooms)
                {
                    Logger.Log(this, "Create room for {0} with password {1}", player, data.Password);

                    var playerCurrentRoom = FindRoomContainingPlayer(player.Data);
                    if (playerCurrentRoom != null)
                    {
                        playerCurrentRoom.Leave(player.Data);
                    }

                    var room = new Room(ConnectionsHandler, player, SettingsLoader, data.Password);

                    room.OnEmpty += OnRoomEmpty;

                    Rooms.Add(room);
                }
                else
                {
                    Logger.Log(this, "Cannot create room for {0} as the maximum room limit has been reached", player);
                    player.SendRoomJoinNotice(RoomNotice.MaxRoomsLimitReached);
                }
            });
        }
예제 #5
0
 void OnRoomEmpty(object sender, Room e)
 {
     e.OnEmpty -= OnRoomEmpty;
     Rooms.Remove(e);
 }