/** * * @param {!Client} client Websocket client that's connected to * the game. * @param {!RelayServer} relayserver relayserver the game is * connected to. * @param {Game~GameOptions} data Data sent from the game which * includes */ public HFTGame AssignClient(HFTSocket client, HFTRuntimeOptions data) { // If there are no games make one // If multiple games are allowed make one // If multiple games are not allowed re-assign string newGameId = !String.IsNullOrEmpty(data.id) ? data.id : ("_hft_" + nextGameId_++); HFTGame game = new HFTGame(newGameId, this, data); // Add it to 'games' immediately because if we remove the old game games would go to 0 // for a moment and that would trigger this GameGroup getting removed because there'd be no games if (masterGame_ == null) { masterGame_ = game; } HFTGame oldGame = null; // See if there's an old game with the same id then replace it games_.TryGetValue(newGameId, out oldGame); games_[newGameId] = game; if (oldGame != null) { log_.Info("tell old game to quit"); oldGame.SendQuit(); oldGame.Close(); } log_.Info("add game: num games = " + games_.Count); game.AssignClient(client, data); if (data.master) { masterGame_ = game; } return game; }
public void RemoveGame(HFTGame game) { List<string> itemsToRemove = new List<string>(); foreach (var pair in games_.GetAll()) { if (pair.Value == game) { itemsToRemove.Add(pair.Key); } } foreach (var item in itemsToRemove) { games_.Remove(item); } if (game == masterGame_) { // Pick a new master masterGame_ = GetAnyGame(); } foreach (var otherGame in games_.Values) { otherGame.SendGameDisconnect(game); } log_.Info("remove game: num games = " + games_.Count); if (games_.Count == 0) { relayServer_.RemoveGameGroup(gameId_); } }