示例#1
0
        public async Task UpdateStandings_Newcomer_PointsUpdated()
        {
            var league         = CreateLeague(2);
            var player1        = league.Members[0];
            var originalPoints = player1.Points;

            var set = SetUtility.Create(dbContext, player1.ID, league.Members[1].ID, league.ID);

            DbContextUtility.UpdateAndSave(dbContext, set, () => { set.IsComplete = true; });

            pointService.CalculatePointDeltas(0, 0, false).ReturnsForAnyArgs((1, -1));

            await testObj.UpdateStandings(league.ID);

            Assert.AreNotEqual(originalPoints, player1.Points);
        }
示例#2
0
        public async Task <League> UpdateStandings(int leagueID)
        {
            var league = await dbContext.Leagues
                         .Include(l => l.Members)
                         .FirstOrDefaultAsync(l => l.ID == leagueID);

            if (league == null)
            {
                throw new NotFoundException(typeof(League), leagueID);
            }

            var playedSets = await dbContext.Sets.Where(s => s.LeagueID == leagueID &&
                                                        s.IsComplete &&
                                                        !s.IsLocked).ToArrayAsync();

            dbContext.Sets.UpdateRange(playedSets);
            dbContext.UpdateRange(league.Members);

            UpdatePoints();
            UpdateRanks();

            await dbContext.SaveChangesAsync();

            return(league);

            void UpdatePoints()
            {
                var pointsPerMember = new Dictionary <int, int>();

                foreach (var set in playedSets)
                {
                    var player1Won = set.WinnerID == set.Player1ID;
                    var(p1Points, p2Points) = pointService.CalculatePointDeltas(set.Player1.Points, set.Player2.Points, player1Won);
                    // TODO: beginner multiplier
                    if (!pointsPerMember.ContainsKey(set.Player1ID))
                    {
                        pointsPerMember.Add(set.Player1ID, 0);
                    }

                    if (!pointsPerMember.ContainsKey(set.Player2ID))
                    {
                        pointsPerMember.Add(set.Player2ID, 0);
                    }

                    pointsPerMember[set.Player1ID] += p1Points;
                    pointsPerMember[set.Player2ID] += p2Points;

                    set.IsLocked = true;
                }

                foreach (var member in league.Members)
                {
                    if (pointsPerMember.TryGetValue(member.ID, out var eloDelta))
                    {
                        member.Points += eloDelta;
                    }
                }
            }

            void UpdateRanks()
            {
                var activeMembers = league.Members.OrderByDescending(lu => lu.Points).ToList();

                var rank       = 0;
                var lastPoints = -1;

                for (var i = 0; i < activeMembers.Count; i++)
                {
                    var member = activeMembers[i];
                    if (league.IsMemberNew(member))
                    {
                        member.Rank = 0;
                        continue;
                    }

                    if (member.Points != lastPoints)
                    {
                        lastPoints = member.Points;
                        rank       = i + 1;
                    }

                    member.Rank = rank;
                }
            }
        }