public async Task <IActionResult> GuessNextCard(Guid deckId, GuessDirection guessDirection) { var result = await _gameControlService.GuessNextCardAsync(deckId, guessDirection); return(Ok(new GuessResults( sucess: result.Item1, oldCard: new CardResults( color: result.Item2.Color, suit: result.Item2.Suit, value: result.Item2.Value, displayName: result.Item2.DisplayName), newCard: new CardResults( color: result.Item3.Color, suit: result.Item3.Suit, value: result.Item3.Value, displayName: result.Item3.DisplayName)))); }
public async Task <(bool, Card, Card)> GuessNextCardAsync(Guid deckId, GuessDirection guessDirection) { var deck = await GetDeckAsync(deckId); var newDeckCard = deck.Cards?.ToList().FirstOrDefault(); if (newDeckCard == null) { throw new Exception("this Deck contains no cards"); } if (deck.LastCardValue == 0) { throw new Exception("game did not started yet"); } deck.Cards.Remove(newDeckCard); //calculate the Guess Result var GuessResult = ( (guessDirection == GuessDirection.Higher && deck.LastCardValue <= newDeckCard.Value) || (guessDirection == GuessDirection.Lower && deck.LastCardValue >= newDeckCard.Value) ); //now the top card will be on the deck deck.LastCardValue = newDeckCard.Value; var oldCard = JsonConvert.DeserializeObject <Card>(deck.LastCardJson); deck.LastCardJson = JsonConvert.SerializeObject(newDeckCard); //get the next player turn deck.PlayerTurn = (deck.PlayerTurn + 1) % (deck.NPlayers + 1); await _deckRepository.UpdateDeckCardAsync(deck); //if the guess is true end the game if (GuessResult) { await CancelDeckAsync(deckId); } return(GuessResult, oldCard, newDeckCard); }