/// <summary> /// Create a new player, register it, and assign an ID. /// </summary> /// <param name="connection">The connection this player is associated with.</param> /// <param name="gameBase">The game server that this player will be registered under.</param> /// <param name="playerID">PlayerID to pass in. This only needs to be defined by clients.</param> public Player(ConnectionID connection, GameBase gameBase, int playerID = -1) { ConnectionID = connection; CharacterSelection = -1; CharacterLocked = false; PlayerReady = false; if (playerID < 0) { PlayerID = gameBase.RegisterPlayer(this); } else { PlayerID = playerID; gameBase.RegisterPlayer(this); } return; }
/// <summary> /// Retrieve a player from a connection ID. /// </summary> /// <param name="connectionID">The connection ID to fetch a player ID for. Null will return the LocalPlayer.</param> /// <returns>A player mapped to the provided connection ID. If the connection was not mapped to a player, returns null.</returns> public Player ConnectionIDToPlayer(ConnectionID connectionID) { if (connectionID == null) { return LocalPlayer; } else if (ConnectionIDToPlayerMap.ContainsKey(connectionID)) { return ConnectionIDToPlayerMap[connectionID]; } else { return null; } }
/// <summary> /// Send a unit of game data to a particular client. /// </summary> /// <param name="gameData">The unit of game data to send.</param> /// <param name="connectionID">The ID of the client to send the game data to.</param> public void SendGameData(GameData gameData, ConnectionID connectionID) { networkWorker.SendPacket(new GameDataPacket(connectionID.IPEndPoint, gameData)); return; }
/// <summary> /// Send a list of game data to a particular client. /// </summary> /// <param name="gameDataList">The list of game data to send.</param> /// <param name="connectionID">The ID of the client to send the game data to.</param> public void SendGameData(List<GameData> gameDataList, ConnectionID connectionID) { for (int i = 0; i < gameDataList.Count; i += 1) { SendGameData(gameDataList[i], connectionID); } return; }