public UnoMatch(DbMatch dbMatch, List <DbHand> hands) { Id = dbMatch.Id; Players = dbMatch.Players.Select(x => x.ExternalId).ToList(); Deck = dbMatch.Deck.ToList(); Discharge = dbMatch.Discharge.ToList(); Hands = hands.ToDictionary(hand => hand.User.ExternalId, hand => hand.Hand.ToList()); CurrentPlayer = dbMatch.CurrentPlayer.ExternalId; CurrentColor = dbMatch.CurrentColor; Backlog = dbMatch.Backlog.ToList(); }
private void TakeCard(Guid user, DbMatch dbMatch, List <DbHand> hands) { if (Deck.Count == 0) { Deck = Discharge.Shuffle().ToList(); dbMatch.Deck = Deck; Discharge.Clear(); dbMatch.Discharge.Clear(); } DbHand userHand = hands.Where(x => x.User.ExternalId == user).FirstOrDefault(); userHand.Hand.Add(dbMatch.Deck.First()); Hands[user].Add(dbMatch.Deck.First()); dbMatch.Deck.RemoveAt(0); Deck.RemoveAt(0); }
public MoveStatus Move( List <UnoCard> cards, UnoCardColor color, IdentityContext context) { DbMatch dbMatch = context.Matches.Where(x => x.Id == Id).FirstOrDefault(); List <DbHand> hands = context.Hands.Where(x => x.Match.Id == Id).ToList(); if (dbMatch == null) { return(MoveStatus.WRONG_MOVE); } User player; if (cards.Count == 0) { bool hasMove = false; foreach (var card in Hands[CurrentPlayer]) { //hasMove = hasMove || ValidateMove(new List<UnoCard> { card }); } if (hasMove) { return(MoveStatus.FAKE_EMPTY_MOVE); } else { TakeCard(CurrentPlayer, dbMatch, hands); CurrentPlayer = NextPlayer(); player = context.Users.Where(x => x.ExternalId == CurrentPlayer).FirstOrDefault(); dbMatch.CurrentPlayer = player; return(MoveStatus.SUCCESS); } } if (!ValidateMove(cards)) { return(MoveStatus.WRONG_MOVE); } UnoMatchMove move = new UnoMatchMove { DeckCard = GetCurrentCard(), PlayerId = CurrentPlayer, Move = cards, SelecteColor = color }; Backlog.Add(move); dbMatch.Backlog.Add(move); DbHand currentPlayerHand = hands.Where(x => x.User.ExternalId == CurrentPlayer).FirstOrDefault(); if (currentPlayerHand == null) { return(MoveStatus.WRONG_MOVE); } cards.ForEach(card => { Hands[CurrentPlayer].Remove(card); var dbCard = currentPlayerHand .Hand .Where(x => x.Color == card.Color && x.NumberValue == card.NumberValue && x.Type == card.Type) .FirstOrDefault(); currentPlayerHand.Hand.Remove(dbCard); }); if (Hands[CurrentPlayer].Count == 0) { return(MoveStatus.ENDGAME); } cards.ForEach(card => { Discharge.Add(card); dbMatch.Discharge.Add(card); }); switch (GetCurrentCard().Type) { case UnoCardType.Numeric: CurrentPlayer = NextPlayer(); break; case UnoCardType.Reverse: case UnoCardType.Skip: break; case UnoCardType.TakeTwo: TakeCard(NextPlayer(), dbMatch, hands); TakeCard(NextPlayer(), dbMatch, hands); break; case UnoCardType.TakeFourChooseColor: for (var i = 0; i < 4; i++) { TakeCard(NextPlayer(), dbMatch, hands); } CurrentColor = color; break; case UnoCardType.ChooseColor: CurrentColor = color; CurrentPlayer = NextPlayer(); break; } player = context.Users.Where(x => x.ExternalId == CurrentPlayer).FirstOrDefault(); dbMatch.CurrentPlayer = player; dbMatch.CurrentColor = color; return(MoveStatus.SUCCESS); }