public void Join(Player joiningPlayer, string password = "") { Logger.Log(this, "Player {0} joined room {1}", joiningPlayer.Data.Name, RoomData.ID); // Prevent join if password is incorrect if (joiningPlayer.Data.ID != Owner.Data.ID && password != RoomData.Password) { Logger.Log(this, "Player {0} provided incorrect password {1}. (Is {2})", joiningPlayer.Data.Name, password, RoomData.Password); joiningPlayer.SendRoomJoinNotice(RoomNotice.InvalidPassword); return; } // Prevent join if room is full if (Players.Count == Settings.Values.Server.MaxPlayers) { Logger.Log(this, "Player {0} attempt to join the room {1} but that room is full", joiningPlayer.Data.Name, RoomData.ID); joiningPlayer.SendRoomJoinNotice(RoomNotice.RoomFull); return; } // Prevent join if game has started if (RoomData.GameStarted) { Logger.Log(this, "Player {0} attempt to join the room {1} but the game has already started", joiningPlayer.Data.Name, RoomData.ID); joiningPlayer.SendRoomJoinNotice(RoomNotice.GameAlreadyStarted); return; } if (Players.Contains(joiningPlayer)) { // TODO: Handle player rejoining room after disconnection? Logger.Log(this, "Already contains player {0}", joiningPlayer.Data.Name); joiningPlayer.SendRoomJoinNotice(RoomNotice.AlreadyInRoom); } else { joiningPlayer.SendRoomJoinNotice(RoomNotice.None); // Assign colour var color = RoomIdPool.GetValue(); joiningPlayer.AssignRoomId(color); Players.Add(joiningPlayer); RoomData.Players.Add(joiningPlayer.Data); // Add callbacks joiningPlayer.OnConnectionClosed += OnPlayerConnectionClosed; joiningPlayer.OnAwayStatusChanged += OnPlayerAwayStatusChanged; // Send message to joining player EchoActionToAll(joiningPlayer.Data, PlayerAction.Joined); SendUpdateToAll(); } }
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); } }); }
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); } }); }