public static GameOperation PlayCard(string userId, string cardId, GameState gameState) { if (!ObjectId.IsValidId(cardId)) { return(GameOperation.Failure("Invalid cardId.")); } if (!ObjectId.IdsMatch(gameState.NextPlayerUserId, userId)) { return(GameOperation.Failure("It's not your turn.")); } bool foundPlayer = TryGetPlayer(userId, gameState, out Player player, out Player opponent); if (!foundPlayer) { return(GameOperation.Failure("You're not playing this game.")); } bool foundCard = PlayHandCard(cardId, player, gameState); if (!foundCard) { return(GameOperation.Failure("Invalid cardId.")); } if (opponent.HandCards.Length > 0) { PlayHandCard(opponent.HandCards[0].CardId, opponent, gameState); } gameState.LastUpdatedDateTimeUtc = DateTime.UtcNow; return(GameOperation.Success(gameState)); }
public static GameOperation StartGame(string userId, string userName, Deck deck) { if (!IsPlayableDeck(deck)) { return(GameOperation.Failure(FormattableString.Invariant($"Unplayable deck. A deck must contain {HandSize} or more cards."))); } var gameState = new GameState(); gameState.GameId = ObjectId.GetNewId(); gameState.NextPlayerUserId = userId; gameState.Player1 = new Player { DisplayName = userName, DeckDisplayName = deck.DisplayName, HandCards = GetRandomHandFromDeck(deck), UserId = userId }; gameState.Player2 = new Player { DisplayName = "Mr. Roboto", HandCards = GetRandomHandForAIPlayer(), UserId = ObjectId.ComputerPlayerId }; gameState.LastUpdatedDateTimeUtc = DateTime.UtcNow; return(GameOperation.Success(gameState)); }
public static GameOperation CompleteGame(string userId, GameState gameState) { bool foundPlayer = TryGetPlayer(userId, gameState, out Player player, out Player opponent); if (!foundPlayer) { return(GameOperation.Failure("You're not playing this game.")); } if (!player.Completed) { player.Completed = true; gameState.LastUpdatedDateTimeUtc = DateTime.UtcNow; } return(GameOperation.Success(gameState)); }
public async Task <GameOperation> PlayCardAsync(string userId, string gameId, string cardId) { GameState gameState = await GetGameStateAsync(userId, gameId, sanitizeGameState : false); if (gameState == null) { return(GameOperation.Failure("No such game.")); } GameOperation operation = GameHandler.PlayCard(userId, cardId, gameState); if (operation.Succeeded) { await PersistGameToDatabaseAsync(userId, operation.GameState); // sanitize game state for user RemovePrivateGameStateInfo(userId, operation.GameState); } return(operation); }
public async Task <GameOperation> StartGameAsync(string userId, string userName, string deckId, string authToken) { var redisDb = this.redis.GetDatabase(); Deck deck = await GetDeckAsync(deckId, authToken); if (deck == null) { return(GameOperation.Failure("No such deck")); } GameOperation operation = GameHandler.StartGame(userId, userName, deck); if (operation.Succeeded) { await PersistGameToDatabaseAsync(userId, operation.GameState); // sanitize game state for user RemovePrivateGameStateInfo(userId, operation.GameState); } return(operation); }