public void OnJoinLobby(JoinLobby joinLobby) { _log.Output($"Player [{joinLobby.ClientId}] joining lobby [{joinLobby.LobbyId}]"); LobbyState state = GetLobbyById(joinLobby.LobbyId); if (state == null) { // try geting lobby by name, will return the first match state = GetLobbyByName(joinLobby.LobbyId); if (state == null) { // TODO - need to send feed back to player that cannot connect return; } } // Add player if not already added if (!state.ContainsPlayerId(joinLobby.ClientId)) { state.AddPlayer(joinLobby.ClientId, joinLobby.PlayerDisplayName); } // send all players connected to this lobby a lobby update Broadcast(state, new UpdateLobbyState(state)); }
public override void Connected(BoltConnection connection) { Bolt.IProtocolToken token = connection.ConnectToken; //if this is a development build or in the editor, user authorization is not required //this should allow for much faster debugging and testing if ((Debug.isDebugBuild || Application.isEditor) && token == null) { var newToken = new ConnectionRequestData(); newToken.Password = ServerSideData.Password; string baseusername = "******"; int suffix = 0; while (PlayerRegistry.UserConnected(baseusername + suffix)) { suffix++; } string name = baseusername + suffix; newToken.PlayerName = name; token = newToken; DebugNameEvent evnt = DebugNameEvent.Create(connection, Bolt.ReliabilityModes.ReliableOrdered); evnt.NewName = name; evnt.Send(); } if (token != null && token is ConnectionRequestData) { ConnectionRequestData data = (ConnectionRequestData)token; Debug.Log("connection request with token of type " + token.GetType().Name); if (data.Password != ServerSideData.Password) { connection.Disconnect(new DisconnectReason("Server Refused Connection", "Incorrect Password")); } else if (PlayerRegistry.UserConnected(data.PlayerName)) { connection.Disconnect(new DisconnectReason("Server Refused Connection", "A player with that name is already connected")); } else if (GameManager.instance.CurrentGameState == GameManager.GameState.IN_GAME) { if (!IndexMap.ContainsPlayer(data.PlayerName)) { connection.Disconnect(new DisconnectReason("Server Refused Connection", "Game already in progress")); } } else { PlayerRegistry.CreatePlayer(connection, data.PlayerName); lobby.AddPlayer(data.PlayerName); lobby.SetPlayerStatIndex(data.PlayerName, IndexMap.AddPlayer(data.PlayerName)); //player connected successfully! } } else { connection.Disconnect(new DisconnectReason("Server Refused Connection", "Invalid Connection Token")); } }