Beispiel #1
0
        public void SaveMove(UnoMatch match)
        {
            DbMatch dbMatch = _context
                              .Matches
                              .Where(x => x.Id == match.Id)
                              .FirstOrDefault();

            if (dbMatch == null)
            {
                return;
            }

            dbMatch.Backlog       = match.Backlog;
            dbMatch.CurrentColor  = match.CurrentColor;
            dbMatch.CurrentPlayer = dbMatch.Players.Where(x => x.ExternalId == match.CurrentPlayer).FirstOrDefault();
            dbMatch.Deck          = match.Deck;
            dbMatch.Discharge     = match.Discharge;

            List <DbHand> hands = _context
                                  .Hands
                                  .Where(h => h.Match.Id == match.Id)
                                  .ToList();

            hands.ForEach(hand =>
            {
                if (match.Hands.ContainsKey(hand.User.ExternalId))
                {
                    hand.Hand = match.Hands[hand.User.ExternalId];
                }
            });

            _context.SaveChanges();
        }
Beispiel #2
0
 public void OnGet(Guid id)
 {
     Id           = id;
     CurrentMatch = _matchesStorageService.FindMatch(Id);
 }
Beispiel #3
0
        public Guid?TryStartMatch(Guid user, Guid?opponent)
        {
            User initiatior = _context.Users.Where(x => x.ExternalId == user).FirstOrDefault();

            if (initiatior == null)
            {
                return(null);
            }

            if (_matchedUsers.Contains(user))
            {
                DbMatch existing = _context.Matches
                                   .Where(x => x.Players.Any(player => player.Equals(user)))
                                   .FirstOrDefault();

                return(existing?.Id);
            }

            if (_matchQueue.Count == 1)
            {
                return(null);
            }

            if (!opponent.HasValue || !_matchQueue.Contains(opponent.Value))
            {
                int  opponentIndex = 0;
                bool matchOverflow = false;

                do
                {
                    if (_matchQueue[opponentIndex] == initiatior.ExternalId)
                    {
                        opponentIndex++;
                        continue;
                    }

                    opponent = _matchQueue[opponentIndex];

                    matchOverflow = _context
                                    .Matches
                                    .Where(match =>
                                           match.Finished &&
                                           match.Players.Any(dbuser => dbuser.ExternalId == opponent) &&
                                           match.Players.Any(dbuser => dbuser.ExternalId == user)
                                           )
                                    .Count() == MAXIMUM_MATCH_COUNT;
                    opponentIndex++;
                } while (matchOverflow || opponentIndex < _matchQueue.Count);
            }

            User opponentor = _context.Users.Where(x => x.ExternalId == opponent.Value).FirstOrDefault();

            UnoMatch match = new UnoMatch(Guid.NewGuid(), new List <Guid> {
                user, opponent.Value
            });
            DbMatch dbMatch = new DbMatch
            {
                Id        = match.Id,
                Finished  = false,
                Deck      = match.Deck,
                Discharge = match.Discharge,
                Players   = new List <User> {
                    initiatior, opponentor
                },
                CurrentPlayer = initiatior
            };

            _context.Matches.Add(dbMatch);

            match.Hands
            .Select(mHand => new DbHand
            {
                Id    = Guid.NewGuid(),
                Match = dbMatch,
                User  = mHand.Key == initiatior.ExternalId ? initiatior : opponentor,
                Hand  = mHand.Value
            })
            .ToList()
            .ForEach(hand => _context.Hands.Add(hand));

            _context.SaveChanges();

            return(dbMatch.Id);
        }