Ejemplo n.º 1
0
        public async Task <List <PlayerRankSeasonEntry> > GetPlayerRanksAsync(string seasonName)
        {
            var data = await _playerRankHistoryRepository.GetPlayerRankHistories(seasonName);

            var filteredData  = GetLastEntryOfEachDay(data);
            var filledOutData = FillOutPlayerRankBlanks(filteredData);

            return(filledOutData);
        }
Ejemplo n.º 2
0
        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);
        }
Ejemplo n.º 3
0
        public async Task <IActionResult> SaveMatch(SaveMatchesRequest saveMatchesRequest)
        {
            if (saveMatchesRequest == null)
            {
                throw new ArgumentNullException(nameof(saveMatchesRequest));
            }
            if (saveMatchesRequest.Email == null)
            {
                throw new ArgumentNullException(nameof(saveMatchesRequest.Email));
            }
            if (saveMatchesRequest.Matches == null)
            {
                throw new ArgumentNullException(nameof(saveMatchesRequest.Matches));
            }

            var hasAdminClaim = HttpContext.User.HasClaim(x => x.Type == "Role" && x.Value == ClaimRoles.Admin.ToString());

            foreach (var match in saveMatchesRequest.Matches)
            {
                if (!match.PlayerList.Contains(saveMatchesRequest.Email) && !hasAdminClaim)
                {
                    return(Unauthorized());
                }

                if (!ValidateScore(match))
                {
                    return(Forbid($"Invalid match score {match.MatchResult.Team1Score} - {match.MatchResult.Team2Score}"));
                }
            }

            var seasons = await _seasonLogic.GetSeasons();

            var currentSeason = HelperMethods.GetCurrentSeason(seasons);

            if (currentSeason == null)
            {
                return(BadRequest()); //"No active seaons"
            }

            var isEdit             = saveMatchesRequest.Matches.Any(x => x.Id != Guid.Empty);
            var fromPreviousSeason = saveMatchesRequest.Matches.Any(x => x.TimeStampUtc < currentSeason.StartDate);

            if (isEdit && fromPreviousSeason && !hasAdminClaim)
            {
                return(Forbid());
            }

            var matches = saveMatchesRequest.Matches.OrderBy(x => x.TimeStampUtc).ToList();

            foreach (var match in matches)
            {
                match.SubmittedBy = saveMatchesRequest.Email;
                if (match.TimeStampUtc == DateTime.MinValue)
                {
                    match.TimeStampUtc = DateTime.UtcNow;
                }

                var leaderboards = await _leaderboardService.GetLatestLeaderboardViews();

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

                var activeLeaderboard = leaderboards.SingleOrDefault(x => x.SeasonName == currentSeason.Name);

                if (!isEdit)
                {
                    _leaderboardService.AddMatchToLeaderboard(activeLeaderboard, match);
                }

                await _matchRepository.Upsert(match);

                _leaderboardService.UpdatePlayerRanks(playerRankHistories,
                                                      activeLeaderboard.Entries.OrderByDescending(x => x.EloRating).ToList(), currentSeason.Name,
                                                      match.TimeStampUtc);

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

                if (!isEdit)
                {
                    await _leaderboardViewRepository.Upsert(activeLeaderboard);
                }
            }

            if (isEdit)
            {
                if (fromPreviousSeason)
                {
                    var season = HelperMethods.GetSeasonOfDate(seasons, matches.First().TimeStampUtc);
                    await _leaderboardService.RecalculateLeaderboard(season);
                }
                else
                {
                    await _leaderboardService.RecalculateLeaderboard(currentSeason);
                }
            }

            await _hubContext.Clients.All.MatchAdded();

            return(Ok());
        }