コード例 #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();
        }
コード例 #2
0
        public void FinishMatch(Guid matchId)
        {
            DbMatch dbMatch = _context
                              .Matches
                              .Where(x => x.Id == matchId)
                              .FirstOrDefault();

            if (dbMatch == null)
            {
                return;
            }

            User winner = _context.Users.Find(dbMatch.CurrentPlayer);

            if (winner != null)
            {
                dbMatch.Winner = winner;
            }

            dbMatch.Finished = true;
            dbMatch.Discharge.Clear();
            dbMatch.Deck.Clear();

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

            hands.ForEach(h => _context.Hands.Remove(h));

            _context.SaveChanges();
        }
コード例 #3
0
 public static MatchToAssociateGvVM ToMatchToAssociateGvVM(DbMatch dbMatch)
 {
     return(new MatchToAssociateGvVM
     {
         Id = dbMatch.Id,
         LocalTimestamp = dbMatch.Date.ToExtendedTime().ToLocal(),
         LeagueName = dbMatch.League.Name,
         MatchHomeName = dbMatch.Home.Name,
         MatchAwayName = dbMatch.Away.Name,
     });
 }
コード例 #4
0
        public void CreateMatch(CoreNewMatch coreMatch)
        {
            var dbMatch = new DbMatch
            {
                MatchweekId = coreMatch.MatchweekId,
                HomeTeamId  = coreMatch.HomeTeamId,
                AwayTeamId  = coreMatch.AwayTeamId
            };

            _context.DbMatchs.Add(dbMatch);
            _context.SaveChanges();
        }
コード例 #5
0
        public UnoMatch FindMatch(Guid matchId)
        {
            DbMatch match = _context.Matches
                            .Where(x => x.Id == matchId)
                            .FirstOrDefault();

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

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

            return(new UnoMatch(match, hands));
        }
コード例 #6
0
        public static DbMatch CopyWithoutNavigationProperties(DbMatch dbMatch)
        {
            return(new DbMatch
            {
                Id = dbMatch.Id,
                Date = dbMatch.Date,
                Status = dbMatch.Status,
                MatchDay = dbMatch.MatchDay,
                HomeScore = dbMatch.HomeScore,
                AwayScore = dbMatch.AwayScore,
                HomeScoreHalf = dbMatch.HomeScoreHalf,
                AwayScoreHalf = dbMatch.AwayScoreHalf,

                HomeId = dbMatch.HomeId,
                AwayId = dbMatch.AwayId,
                LeagueId = dbMatch.LeagueId
            });
        }
コード例 #7
0
ファイル: MatchRepository.cs プロジェクト: s4xack/Jinder
        public Match Add(Match match)
        {
            DbMatch dbMatch = DbMatch.FromModel(match);

            dbMatch = _context.Matches
                      .Add(dbMatch)
                      .Entity;

            try
            {
                _context.SaveChanges();
            }
            catch (DbUpdateException)
            {
                throw new ArgumentException("Unable to create Match with such data!");
            }

            return(dbMatch.ToModel());
        }
コード例 #8
0
ファイル: MatchRepository.cs プロジェクト: s4xack/Jinder
        public Match Update(Match match)
        {
            DbMatch dbMatch = _context.Matches
                              .SingleOrDefault(m => m.Id == match.Id) ??
                              throw new ArgumentException($"No natch with id {match.Id}!");

            dbMatch.Status = match.Status;

            dbMatch = _context.Matches
                      .Update(dbMatch)
                      .Entity;

            try
            {
                _context.SaveChanges();
            }
            catch (DbUpdateException)
            {
                throw new ArgumentException("Unable to create summary suggestions with such data!");
            }

            return(dbMatch.ToModel());
        }
コード例 #9
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);
        }