/// <summary> /// Event Handler for Application Event AddPlayer /// If a HostPassword is set on the HostInstance, it is required to add a new player to that host /// </summary> /// <param name="netMsg"></param> void OnAddPlayer(NetworkMessage netMsg) { if (IsConnectionVerified(netMsg.conn)) { MessageTypes.CustomEventType result = MessageTypes.CustomEventType.UnknownError; var msg = netMsg.ReadMessage <MessageTypes.AddPlayerMessage>(); if (string.IsNullOrEmpty(msg.playerKey)) { result = MessageTypes.CustomEventType.KeyEmpty; } else if (!Players.IsPlayerCountValid(MaxPlayerLimitForAllHosts)) { result = MessageTypes.CustomEventType.MaxPlayersExceeded; } else if (!string.IsNullOrEmpty(msg.hostName) && ConnectedHosts.Connected.ContainsKey(msg.hostName)) { // check given host first for quick check, then check all hosts if (ConnectedHosts.FindPlayer(msg.hostName, msg.playerKey) != null) { result = MessageTypes.CustomEventType.DuplicateDetected; } else if (ConnectedHosts.FindPlayer(msg.playerKey) != null) { result = MessageTypes.CustomEventType.DuplicateDetected; } else if (!ConnectedHosts.Connected[msg.hostName].VerifyPassword(msg.hostPassword)) { result = MessageTypes.CustomEventType.HostPasswordFail; } else { result = MessageTypes.CustomEventType.NoError; ConnectedHosts.AddPlayer(msg.hostName, msg.playerKey, msg.playerValue, msg.flags); } } else { result = MessageTypes.CustomEventType.HostUnknown; } if (MasterServer.ViewDebugMessages) { Debug.Log("<MasterServer> OnAddPlayer result: " + result); } var response = new MessageTypes.AddPlayerResponseMessage(); response.resultCode = (int)result; response.playerKey = msg.playerKey; netMsg.conn.Send(MessageTypes.AddPlayerResponse, response); UpdateUI(); } }
/// <summary> /// Event Handler for Application Event PlayerChangeHost /// </summary> /// <param name="netMsg"></param> void OnUpdatePlayerHost(NetworkMessage netMsg) { if (IsConnectionVerified(netMsg.conn)) { var msg = netMsg.ReadMessage <MessageTypes.UpdatePlayerHostMessage>(); MessageTypes.CustomEventType result = MessageTypes.CustomEventType.UnknownError; if (string.IsNullOrEmpty(msg.playerKey)) { result = MessageTypes.CustomEventType.KeyEmpty; } else if (!string.IsNullOrEmpty(msg.hostName) && ConnectedHosts.Connected.ContainsKey(msg.hostName)) { PlayerInstance player = null; // check given host first for quick check, then check all hosts if (!ConnectedHosts.Connected[msg.hostName].VerifyPassword(msg.hostPassword)) { result = MessageTypes.CustomEventType.HostPasswordFail; } else if ((player = ConnectedHosts.RemovePlayer(msg.playerKey)) != null) { // successful removal, now add to new Host player.HostLocation = ConnectedHosts.Connected[msg.hostName]; player.HostLoginTime = System.DateTime.UtcNow; ConnectedHosts.AddPlayer(msg.hostName, player); result = MessageTypes.CustomEventType.NoError; } else { result = MessageTypes.CustomEventType.DuplicateDetected; } } else { result = MessageTypes.CustomEventType.HostUnknown; } if (MasterServer.ViewDebugMessages) { Debug.Log("<MasterServer> OnUpdatePlayerHost result: " + result); } var response = new MessageTypes.UpdatePlayerHostResponseMessage(); response.resultCode = (int)result; response.playerKey = msg.playerKey; netMsg.conn.Send((short)MessageTypes.UpdatePlayerHostResponse, response); UpdateUI(); } }