コード例 #1
0
        public LeaderboardView GetLatestLeaderboardView()
        {
            var result = _leaderboardViewRepository.GetLeaderboardView();

            if (result == null)
            {
                return(RecalculateLeaderboard());
            }

            result.Entries = result.Entries.OrderByDescending(x => x.EloRating).ToList();

            return(result);
        }
コード例 #2
0
ファイル: LeaderboardService.cs プロジェクト: Mech0z/Foosball
        public async Task <LeaderboardView> RecalculateLeaderboard(Season season)
        {
            var seasons = await _seasonLogic.GetSeasons();

            await _playerRankHistoryRepository.RemovePlayerHistoryFromSeason(season.Name);

            var playerRankHistories = await _playerRankHistoryRepository.GetPlayerRankHistories(season.Name);

            var matches =
                (await _matchRepository.GetMatches(season.StartDate,
                                                   HelperMethods.GetNextSeason(seasons, season)?.StartDate))
                .OrderBy(x => x.TimeStampUtc);

            var existingLeaderboard = await _leaderboardViewRepository.GetLeaderboardView(season);

            var leaderboardView = existingLeaderboard ?? new LeaderboardView(season.Name);

            leaderboardView.Entries = new List <LeaderboardViewEntry>(); //Clear old matches if existing leaderboard exists

            foreach (var match in matches)
            {
                var matchPointsChanged = AddMatchToLeaderboard(leaderboardView, match);
                UpdatePlayerRanks(playerRankHistories,
                                  leaderboardView.Entries.OrderByDescending(x => x.EloRating).ToList(), season.Name,
                                  match.TimeStampUtc);
                if (matchPointsChanged)
                {
                    await _matchRepository.Upsert(match);
                }
            }
            leaderboardView.Entries = leaderboardView.Entries.OrderByDescending(x => x.EloRating).ToList();

            await _leaderboardViewRepository.Upsert(leaderboardView);

            //Only update those who played this season
            foreach (PlayerRankSeasonEntry playerRankHistory in playerRankHistories.Where(x =>
                                                                                          leaderboardView.Entries.Select(x => x.UserName).Contains(x.Email)))
            {
                await _playerRankHistoryRepository.Upsert(playerRankHistory);
            }

            return(leaderboardView);
        }
コード例 #3
0
        public async Task <List <PartnerPercentResult> > GetPartnerWinPercent(List <Season> seasons, string email,
                                                                              Season season)
        {
            var leaderboard = await _leaderboardViewRepository.GetLeaderboardView(season);

            double?normalWinRate = null;

            if (leaderboard != null)
            {
                // ReSharper disable once RedundantCast
                normalWinRate = leaderboard.Entries.Where(e => e.UserName == email).Select(e => Math.Round((((double)e.Wins / (double)e.NumberOfGames) * 100), 2)).FirstOrDefault();
            }

            var result = new List <PartnerPercentResult>();

            foreach (User user in await _userRepository.GetUsersAsync())
            {
                if (user.Email != email)
                {
                    result.Add(new PartnerPercentResult
                    {
                        Username           = user.Username,
                        Email              = user.Email,
                        UsersNormalWinrate = normalWinRate
                    });
                }
            }

            var matches = await _matchRepository.GetMatches(season.StartDate, HelperMethods.GetNextSeason(seasons, season)?.StartDate);

            foreach (Match match in matches)
            {
                ManipulatePartnerResults(email, match, result, 0, 1, 2, 3);
                ManipulatePartnerResults(email, match, result, 1, 0, 2, 3);
                ManipulatePartnerResults(email, match, result, 2, 3, 0, 1);
                ManipulatePartnerResults(email, match, result, 3, 2, 0, 1);
            }

            return(result
                   .Where(x => x.MatchesTogether > 0 || x.MatchesAgainst > 0)
                   .OrderByDescending(x => (double)x.WinsTogether / (double)x.MatchesTogether)
                   .ToList());
        }
コード例 #4
0
        public async Task <AchievementsView> GetAchievementsView(List <Season> seasons, Season season)
        {
            var leaderboardView = await _leaderboardViewRepository.GetLeaderboardView(season);

            var matches =
                (await _matchRepository.GetMatches(season.StartDate,
                                                   HelperMethods.GetNextSeason(seasons, season)?.StartDate))
                .OrderBy(m => m.TimeStampUtc).ToList();

            GetStreak(matches, leaderboardView.Entries, true, out var winStreak, out var playerWin);

            GetStreak(matches, leaderboardView.Entries, false, out var lossStreak, out var playerLoss);

            var mostGames = leaderboardView.Entries.OrderByDescending(e => e.NumberOfGames).First();
            var mostWins  = leaderboardView.Entries.OrderByDescending(e => e.Wins).First();
            var bestRatio = leaderboardView.Entries.OrderByDescending(e => e.Wins / e.NumberOfGames).First();


            var flawlessVictoryWinners = new List <string>();
            var flawlessVictoryLoosers = new List <string>();

            GetFlawlessVictory(matches, flawlessVictoryWinners, flawlessVictoryLoosers);

            var view = new AchievementsView
            {
                Achievements = new List <Achievement>()
                {
                    new Achievement()
                    {
                        Headline = "Most games",
                        UserName = mostGames.UserName,
                        Count    = mostGames.NumberOfGames.ToString(),
                        Type     = "Games"
                    },
                    new Achievement()
                    {
                        Headline = "Most wins",
                        UserName = mostWins.UserName,
                        Count    = mostWins.Wins.ToString(),
                        Type     = "Games"
                    },
                    new Achievement()
                    {
                        Headline = "Best win ratio",
                        UserName = bestRatio.UserName,
                        Count    =
                            ((int)((double)((double)bestRatio.Wins / (double)bestRatio.NumberOfGames) * 100)).ToString(),
                        Type = "Ratio"
                    },
                    new Achievement()
                    {
                        Headline = "Longest win streak",
                        UserName = playerWin.UserName,
                        Count    = winStreak.ToString(),
                        Type     = "Games"
                    },
                    new Achievement()
                    {
                        Headline = "Longest loss streak",
                        UserName = playerLoss.UserName,
                        Count    = lossStreak.ToString(),
                        Type     = "Games"
                    },
                    //new Achievement()
                    //{
                    //    Headline = "Most points for single match",
                    //    UserName = "******",
                    //    Count = "",
                    //    Type = "Points"
                    //},
                    new Achievement()
                    {
                        Headline = "Flawless victory (10-0 win)",
                        UserName = "******" + string.Join(" and ", flawlessVictoryWinners.ToArray()),
                        Count    = string.Join(" and ", flawlessVictoryLoosers.ToArray()),
                        Type     = "Loosers"
                    }
                }
            };

            return(view);
        }
コード例 #5
0
        public List <PartnerPercentResult> GetPartnerWinPercent(string email, string season)
        {
            var    leaderboard   = _leaderboardViewRepository.GetLeaderboardView(season);
            double?normalWinRate = null;

            if (leaderboard != null)
            {
                normalWinRate = leaderboard.Entries.Where(e => e.UserName == email).Select(e => Math.Round((((double)e.Wins / (double)e.NumberOfGames) * 100), 2)).FirstOrDefault();
            }

            var result = new List <PartnerPercentResult>();

            foreach (User user in _userRepository.GetUsers())
            {
                if (user.Email.NormalizedValue.ToLower() != email.ToLower())
                {
                    result.Add(new PartnerPercentResult
                    {
                        Username           = user.UserName,
                        Email              = user.LowerEmail,
                        UsersNormalWinrate = normalWinRate
                    });
                }
            }

            var matches = _matchRepository.GetMatches(season);

            foreach (Match match in matches)
            {
                if (match.PlayerList[0] == email)
                {
                    PartnerPercentResult resultToManipulate = result.Single(x => x.Email == match.PlayerList[1]);
                    resultToManipulate.Matches++;
                    if (match.MatchResult.Team1Won)
                    {
                        resultToManipulate.Wins++;
                    }
                }

                if (match.PlayerList[1] == email)
                {
                    PartnerPercentResult resultToManipulate = result.Single(x => x.Email == match.PlayerList[0]);
                    resultToManipulate.Matches++;
                    if (match.MatchResult.Team1Won)
                    {
                        resultToManipulate.Wins++;
                    }
                }

                if (match.PlayerList[2] == email)
                {
                    PartnerPercentResult resultToManipulate = result.Single(x => x.Email == match.PlayerList[3]);
                    resultToManipulate.Matches++;
                    if (!match.MatchResult.Team1Won)
                    {
                        resultToManipulate.Wins++;
                    }
                }

                if (match.PlayerList[3] == email)
                {
                    PartnerPercentResult resultToManipulate = result.Single(x => x.Email == match.PlayerList[2]);
                    resultToManipulate.Matches++;
                    if (!match.MatchResult.Team1Won)
                    {
                        resultToManipulate.Wins++;
                    }
                }
            }

            return(result
                   .Where(x => x.Matches > 0)
                   .OrderByDescending(x => (double)x.Wins / (double)x.Matches)
                   .ToList());
        }
コード例 #6
0
 public LeaderboardView GetLeaderboardView(string seasonName)
 {
     return(_leaderboardViewRepository.GetLeaderboardView(seasonName));
 }