public async Task <GameResponse> JoinGameAsync(string connectionId, Guid gameId, string password) { if (!UsersByConnectionId.ContainsKey(connectionId)) { Logger.LogError($"Got join game message for unknown connection id '{connectionId}'"); return(GameResponse.Failure("Connection not found")); } var user = UsersByConnectionId[connectionId]; if (!GamesById.ContainsKey(gameId)) { Logger.LogError($"Got join game message for unknown game id '{gameId}'"); return(GameResponse.Failure("Game not found")); } var game = GamesById[gameId]; var joinResponse = game.Join(user, password); if (!joinResponse.Success) { return(joinResponse); } await subscriber.PublishAsync(RedisChannels.UpdateGame, JsonConvert.SerializeObject(game)); return(joinResponse); }
public async Task <GameResponse> StartGameAsync(string connectionId) { if (!UsersByConnectionId.ContainsKey(connectionId)) { Logger.LogError($"Got start game message for unknown connection id '{connectionId}'"); return(GameResponse.Failure("Connection not found")); } var user = UsersByConnectionId[connectionId]; var game = FindGameForUser(user.Name); if (game == null) { Logger.LogError($"No games found to start for {user.Name}"); return(GameResponse.Failure("Game not found")); } if (game.Owner != user.Name) { return(GameResponse.Failure("You are not the game owner and cannot start the game.")); } await Task.Delay(1); return(GameResponse.Succeeded(game)); }
public GameResponse SelectDeck(string connectionId, PlayerDeck deck) { if (!UsersByConnectionId.ContainsKey(connectionId)) { Logger.LogError($"Got select deck message for unknown connection id '{connectionId}'"); return(GameResponse.Failure("An error occurred selecting your deck. Please try again later.")); } var user = UsersByConnectionId[connectionId]; var game = FindGameForUser(user.Name); if (game == null) { Logger.LogError($"Got select deck message for user '{user.Name}' with no game"); return(GameResponse.Failure("Could not select your deck because your game was not found. Please restart your game.")); } if (!game.GetPlayers().ContainsKey(user.Name)) { Logger.LogError($"Got select deck message for user '{user.Name}' game '{game.Id}' which they are not in."); return(GameResponse.Failure("An error occurred selecting your deck. Please try again later.")); } var player = game.GetPlayers()[user.Name]; var customData = JsonConvert.DeserializeObject <PlayerCustomData>(player.CustomData) ?? new PlayerCustomData(); customData.Deck = deck; player.CustomData = JsonConvert.SerializeObject(customData); return(GameResponse.Succeeded(game)); }
private async Task <GameResponse> StartNewGameInternalAsync(string connectionId, StartNewGameRequest request) { if (!UsersByConnectionId.ContainsKey(connectionId)) { Logger.LogError($"Got new game message for unknown connection id '{connectionId}'"); return(GameResponse.Failure("Connection not found")); } var user = UsersByConnectionId[connectionId]; var existingGame = FindGameForUser(user.Name); if (existingGame != null) { Logger.LogError($"Got new game message for user already in game '{user.Name}' '{existingGame.Name}'"); return(GameResponse.Failure("You are already in a game so cannot start a new one")); } if (request.QuickJoin) { var pendingGame = GamesById.Values.OrderBy(g => g.Started).FirstOrDefault(game => game.CanQuickJoin(game.GameType)); if (pendingGame != null) { } return(GameResponse.Succeeded(pendingGame)); } var newGame = new LobbyGame(user.Name, request); newGame.NewGame(user); GamesById.Add(newGame.Id, newGame); await subscriber.PublishAsync(RedisChannels.NewGame, JsonConvert.SerializeObject(newGame)); return(GameResponse.Succeeded(newGame)); }