Пример #1
0
        public IActionResult SaveMatch(SaveMatchesRequest saveMatchesRequest)
        {
            if (saveMatchesRequest == null)
            {
                return(BadRequest());
            }
            //var validated = _userRepository.Validate(saveMatchesRequest.User);
            //if (!validated)
            //{
            //    return Unauthorized();
            //}

            var seasons = _seasonLogic.GetSeasons();

            if (seasons.All(x => x.EndDate != null))
            {
                return(BadRequest("No active seaons"));
            }

            var currentSeason = seasons.Single(x => x.EndDate == null);

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

            //Sat i AddMatch java
            foreach (var match in matches)
            {
                if (match.TimeStampUtc == DateTime.MinValue)
                {
                    match.TimeStampUtc = DateTime.UtcNow;
                }

                match.SeasonName = currentSeason.Name;

                var leaderboards = _leaderboardService.GetLatestLeaderboardViews();

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

                _leaderboardService.AddMatchToLeaderboard(activeLeaderboard, match);

                _matchRepository.Upsert(match);

                _leaderboardViewRepository.Upsert(activeLeaderboard);
            }

            return(Ok());
        }
Пример #2
0
        public LeaderboardView RecalculateLeaderboard(string season)
        {
            var matches         = _matchRepository.GetMatches(season).OrderBy(x => x.TimeStampUtc);
            var leaderboardView = new LeaderboardView {
                SeasonName = season
            };

            foreach (var match in matches)
            {
                AddMatchToLeaderboard(leaderboardView, match);
                _matchRepository.Upsert(match);
            }
            leaderboardView.Entries = leaderboardView.Entries.OrderByDescending(x => x.EloRating).ToList();

            _leaderboardViewRepository.Upsert(leaderboardView);
            return(leaderboardView);
        }
Пример #3
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);
        }
Пример #4
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());
        }