public async Task JoinLobby(LobbyConnection c, Lobby l) { log.Debug("connection " + c + " joining lobby " + l); using (var alock = await Data.Synchronizer.SetStateAsync(ModuleState.Writing)) { if (c.Lobby != null) { throw new ArgumentException("Connection has already joined a lobby!"); } if (l.LobbyState == LobbyState.Finished) { throw new LobbyStateException("Can not join finished game!"); } Data.AddLobbyConnection(l, c); try { var players = PlayerStorage.GetPlayerOfConnection(c.Connection); await AddPlayerToLobby(players, l); await Messenger.SendMessage(c.Connection, Messages.LobbyJoined(l)); await Messenger.SendMessage(c.Connection, Messages.GameModeChanged(l)); await Messenger.SendMessage(c.Connection, Messages.ReadyStates(l)); //await Messenger.SendMessage(c.Connection, Messages.LobbySettings(l)); await UpdateConnection(c); } catch (Exception ex) { log.Warn("connection " + c + " failed to join lobby " + l + ", reverting changes"); l.Connections.Remove(c.Connection.Id); c.Lobby = null; throw ex; } } }
public async Task LeaveLobby(LobbyConnection connection, bool silent) { log.Debug("removing connection " + connection + " from lobby " + connection.Lobby); using (var alock = await Data.Synchronizer.SetStateAsync(ModuleState.Writing)) { var lobby = connection.Lobby; if (lobby == null) { throw new LobbyStateException("Connection is not member in any lobby!"); } log.Debug("connection " + connection.Connection.Id + " leaving lobby " + lobby.Id); if (PlayerStorage.IsSynchronized(connection.Connection)) { var players = PlayerStorage.GetPlayerOfConnection(connection.Connection).Where(p => lobby.Players.Contains(p)).ToArray(); await RemovePlayerFromLobby(players, lobby, silent); } lobby.Connections.Remove(connection.Connection.Id); if (!silent && connection.Connection.State != ConnectionState.Disconnected) { await Messenger.SendMessage(connection.Connection, Messages.LobbyLeft()); } connection.Lobby = null; connection.PendingUpdates = 0; if (lobby.Host == connection) { await MigrateHost(lobby); } } }