コード例 #1
0
        public void CalculationTest()
        {
            // Arrange
            var player1 = new ApplicationUser
            {
                Id = "1"
            };
            var player2 = new ApplicationUser
            {
                Id = "2"
            };
            var eloStat = new EloStat();

            // Act
            eloStat.AddMatch(new Match
            {
                FirstPlayerScore  = 7,
                SecondPlayerScore = 5,
                FirstPlayer       = player1,
                SecondPlayer      = player2
            });
            eloStat.AddMatch(new Match
            {
                FirstPlayerScore  = 7,
                SecondPlayerScore = 2,
                FirstPlayer       = player1,
                SecondPlayer      = player2
            });
            eloStat.AddMatch(new Match
            {
                FirstPlayerScore  = 1,
                SecondPlayerScore = 7,
                FirstPlayer       = player1,
                SecondPlayer      = player2
            });

            // Assert
            eloStat[player1].Should().Be(1407);
            eloStat[player2].Should().Be(1393);
        }
コード例 #2
0
        // GET: Leagues/Rating/5
        public async Task <IActionResult> Rating(Guid?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            var currentUser = await User.GetApplicationUser(_userManager);

            var league = _leaguesRepository.GetUserAuthorizedLeague(currentUser, id.Value);

            if (league == null)
            {
                return(NotFound());
            }

            league =
                _context.League.Include(l => l.Matches)
                .ThenInclude(m => m.FirstPlayer)
                .Include(l => l.Matches)
                .ThenInclude(m => m.SecondPlayer)
                .Single(m => m.Id == id);

            var notBlockedUserIds =
                new HashSet <string>(
                    _context.LeaguePlayers.Where(lp => lp.LeagueId == league.Id && !lp.IsBlocked)
                    .Select(lp => lp.UserId));
            var activeUsers = new HashSet <ApplicationUser>();
            var elo         = new EloStat();
            var stats       = new List <IStat>
            {
                elo,
                new WinRateStat(),
                new GoalsForAgainstStat(),
                new StreakStat(true),
                new StreakStat(false)
            };

            foreach (var match in league.Matches.OrderBy(m => m.Date))
            {
                if (notBlockedUserIds.Contains(match.FirstPlayerId))
                {
                    activeUsers.Add(match.FirstPlayer);
                }
                if (notBlockedUserIds.Contains(match.SecondPlayerId))
                {
                    activeUsers.Add(match.SecondPlayer);
                }

                foreach (var stat in stats)
                {
                    stat.AddMatch(match);
                }
            }

            var userList = new List <ApplicationUser>(activeUsers);
            var forecast = new Dictionary <string, Dictionary <string, string> >();

            foreach (var appUser in userList)
            {
                var dict = new Dictionary <string, string>();

                foreach (var t in userList)
                {
                    var userRating = elo[appUser];
                    dict[t.Id] = (new Elo(userRating, elo[t], 1, 0).NewRatingAPlayer - userRating).ToString();
                }

                forecast[appUser.Id] = dict;
            }

            var lastMatches = _context.Match.Where(m => m.LeagueId == league.Id).OrderByDescending(m => m.Date).Take(5);

            return
                (View(new RatingViewModel(stats, activeUsers.OrderByDescending(u => elo.GetResult(u)), forecast,
                                          lastMatches)));
        }