public async Task <List <CardInHandModel> > GetCardHandModelListForPlayer(int userId, bool allCards, int trump) { List <CardInHandModel> listOfCards = new List <CardInHandModel>(); var player = await _playerRepository.GetPlayerByUserIdAsync(userId); if (string.IsNullOrEmpty(player.Hand)) { return(listOfCards); } var cards = CardsManager.GetCardListFromHandString(player.Hand); if (allCards) { cards = cards.OrderCards(); var trumpSuit = (CardSuit)trump; var belaAvailable = cards.Exists(c => c.Value == CardValue.King && c.Suit == trumpSuit) && cards.Exists(c => c.Value == CardValue.Queen && c.Suit == trumpSuit); var trumpSuitString = trumpSuit.GetStringValue(); listOfCards = cards .Select(c => new CardInHandModel() { CardString = c.ToString(), CardUrl = c.ImgPath, AskForBela = belaAvailable && c.Suit == trumpSuit && (c.Value == CardValue.Queen || c.Value == CardValue.King) }).ToList(); } else { var firstSix = cards.Take(6).ToList().OrderCards(); listOfCards = firstSix .Select(c => new CardInHandModel() { CardString = c.ToString(), CardUrl = c.ImgPath, AskForBela = false }).ToList(); listOfCards.Add(new CardInHandModel() { CardString = "", CardUrl = CardsManager.GetBackgroundCardImgPath() }); listOfCards.Add(new CardInHandModel() { CardString = "", CardUrl = CardsManager.GetBackgroundCardImgPath() }); } return(listOfCards); }
public async Task <Result> PlayACard(string playedCardString, int roundId, int userId, PlayerPosition position, List <string> cardsInHandStrings, bool belaCalled) { Round round = await _gameRepository.GetRoundById(roundId); Player player = await _playerRepository.GetPlayerByUserIdAsync(userId); var cardsPlayed = round.CardsPlayed.Where(cp => cp.RoundPhase == round.RoundPhase) .OrderBy(cp => cp.DateCreated) .Select(cp => CardsManager.GetCardFromString(cp.CardString)) .ToList(); var cardsInHand = CardsManager.GetCardListFromHandString(string.Join(",", cardsInHandStrings)); var playedCard = CardsManager.GetCardFromString(playedCardString); var validCards = ValidCardsHelper.GetValidCardsForPlay(cardsInHand, round.CurrentTrump.Value, cardsPlayed); if (validCards.Contains(playedCard)) { var result = Result.Success(); round.CardsPlayed.Add(new CardPlayed(round.RoundPhase, position, playedCard, round.CurrentTrump.Value)); cardsInHand.Remove(playedCard); cardsInHand = cardsInHand.OrderCards(); player.Hand = string.Join(",", cardsInHand); Game game = null; var roundAlert = ""; var isLastTurn = false; var isGameOver = false; if (belaCalled) { if (player.Team.Value == Team.FirstTeam) { round.FirstTeamCalls += 20; round.FirstTeamRoundTotal += 20; } if (player.Team.Value == Team.SecondTeam) { round.SecondTeamCalls += 20; round.SecondTeamRoundTotal += 20; } } if (round.CardsPlayed.Where(cp => cp.RoundPhase == round.RoundPhase).ToList().Count == 4) { isLastTurn = round.RoundPhase == RoundPhase.EighthCard; game = await _gameRepository.GetGameWithPlayersById(round.GameId); ResolveTurn(round, game, isLastTurn); if (isLastTurn) { round.CurrentPlayerToPlay = round.FirstPlayerToPlay.GetNextPosition(); roundAlert = await ResolveRound(round, game); if (IsGameOver(game)) { isGameOver = true; await SetGameOverDataAsync(game); } else { AddNextRound(game, round); var players = game.PlayerGames.Select(pg => pg.Player).ToList(); FillPlayersHands(players); } } else { round.RoundPhase = round.RoundPhase.GetNextPhase(); } } else { round.CurrentPlayerToPlay = round.CurrentPlayerToPlay.GetNextPosition(); } if (await _gameRepository.SaveAsync()) { var currentRoundId = round.Id; var currentPhase = round.RoundPhase; if (isLastTurn) { var newRound = game.Rounds.OrderByDescending(r => r.Id).FirstOrDefault(); currentRoundId = newRound.Id; currentPhase = newRound.RoundPhase; } result.Values = GetResultObjForCardPlayed(round, position, playedCard, roundAlert, currentRoundId, currentPhase, isLastTurn, isGameOver, belaCalled); return(result); } return(Result.Fail(new string[] { "Dogodila se greška" })); } return(Result.Fail(new string[] { "Nevažeća karta" })); }