private async ValueTask <(bool Success, Game?Game)> TryCreateAsync(GameOptionsData options, GameCode?desiredGameCode = null) { GameCode gameCode = desiredGameCode ?? _gameCodeFactory.Create(); var gameCodeStr = gameCode.Code; var game = ActivatorUtilities.CreateInstance <Game>(_serviceProvider, _publicIp, gameCode, options); if (await _nodeLocator.ExistsAsync(gameCodeStr) || !_games.TryAdd(gameCode, game)) { return(false, null); } await _nodeLocator.SaveAsync(gameCodeStr, _publicIp); _logger.LogDebug("Created game with code {0}.", game.Code); await _eventManager.CallAsync(new GameCreatedEvent(game)); return(true, game); }
public GameCode Get() { lock (_sync) { if (_codes.Count == 0) { _logger.LogWarning("Boot.Codes: Ran out of codes!"); return(_codeFactory.Create()); } var index = StrongRandom.Next(0, _codes.Count); var code = _codes[index]; _codes.RemoveAt(index); _inUse.Add(code); return(code); } }