コード例 #1
0
        public IHttpActionResult SaveMatch(List <Match> matches)
        {
            matches = matches.OrderBy(x => x.TimeStampUtc).ToList();

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

                LeaderboardView currentLeaderboard = _leaderboardService.GetLatestLeaderboardView();

                _leaderboardService.AddMatchToLeaderboard(currentLeaderboard, match);

                _matchRepository.SaveMatch(match);

                _leaderboardViewRepository.SaveLeaderboardView(currentLeaderboard);
            }

            //TODO Run validation

            return(Ok());
        }
コード例 #2
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());
        }
コード例 #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());
        }