From 9ccfc034da52e40ea46a67333fda935079c3ebd0 Mon Sep 17 00:00:00 2001 From: Tomasi - Developing Date: Tue, 7 Jan 2025 10:44:09 +0100 Subject: [PATCH] MVP for R4 and R5 --- Api/Controllers/v1/PlayersController.cs | 23 ++++++++++ Application/Interfaces/IPlayerRepository.cs | 2 + Application/Repositories/PlayerRepository.cs | 46 ++++++++++++++++++++ 3 files changed, 71 insertions(+) diff --git a/Api/Controllers/v1/PlayersController.cs b/Api/Controllers/v1/PlayersController.cs index 004605b..70f7c28 100644 --- a/Api/Controllers/v1/PlayersController.cs +++ b/Api/Controllers/v1/PlayersController.cs @@ -77,6 +77,29 @@ namespace Api.Controllers.v1 } } + [AllowAnonymous] + [HttpGet("[action]/{allianceId:guid}")] + public async Task>> GetAllianceLeadershipMvp(Guid allianceId, + CancellationToken cancellationToken) + { + try + { + var alliancePlayersResult = + await playerRepository.GetAllianceLeadershipMvp(allianceId, cancellationToken); + + if (alliancePlayersResult.IsFailure) return BadRequest(alliancePlayersResult.Error); + + return alliancePlayersResult.Value.Count > 0 + ? Ok(alliancePlayersResult.Value) + : NoContent(); + } + catch (Exception e) + { + logger.LogError(e, e.Message); + return StatusCode(StatusCodes.Status500InternalServerError); + } + } + [HttpPost] public async Task> CreatePlayer(CreatePlayerDto createPlayerDto, CancellationToken cancellationToken) diff --git a/Application/Interfaces/IPlayerRepository.cs b/Application/Interfaces/IPlayerRepository.cs index 11ff7d9..8b3a7b9 100644 --- a/Application/Interfaces/IPlayerRepository.cs +++ b/Application/Interfaces/IPlayerRepository.cs @@ -11,6 +11,8 @@ public interface IPlayerRepository Task>> GetAlliancePlayersMvp(Guid allianceId, CancellationToken cancellationToken); + Task>> GetAllianceLeadershipMvp(Guid allianceId, CancellationToken cancellationToken); + Task> CreatePlayerAsync(CreatePlayerDto createPlayerDto, string createdBy, CancellationToken cancellationToken); Task> UpdatePlayerAsync(UpdatePlayerDto updatePlayerDto, string modifiedBy, CancellationToken cancellationToken); diff --git a/Application/Repositories/PlayerRepository.cs b/Application/Repositories/PlayerRepository.cs index 86bcd16..9be4114 100644 --- a/Application/Repositories/PlayerRepository.cs +++ b/Application/Repositories/PlayerRepository.cs @@ -83,6 +83,52 @@ public class PlayerRepository(ApplicationContext context, IMapper mapper, ILogge return playerMvps; } + public async Task>> GetAllianceLeadershipMvp(Guid allianceId, CancellationToken cancellationToken) + { + var currentDate = DateTime.Now; + var threeWeeksAgo = currentDate.AddDays(-21); + + var playerMvps = await context.Players + .Where(p => p.AllianceId == allianceId && (p.Rank.Name == "R4" || p.Rank.Name == "R5")) + .Select(p => new + { + p.Id, + p.PlayerName, + Rank = p.Rank.Name, + + VsDuels = context.VsDuelParticipants + .Where(vp => vp.PlayerId == p.Id && vp.VsDuel.EventDate <= currentDate && !vp.VsDuel.IsInProgress) + .OrderByDescending(vp => vp.VsDuel.EventDate) + .Take(3) + .Sum(vp => vp.WeeklyPoints), + + MarshalGuardParticipationCount = context.MarshalGuardParticipants + .Count(mpg => mpg.PlayerId == p.Id && mpg.Participated && mpg.MarshalGuard.EventDate > threeWeeksAgo), + + DessertStormParticipationCount = context.DesertStormParticipants + .Count(dsp => dsp.PlayerId == p.Id && dsp.Participated && dsp.DesertStorm.EventDate > threeWeeksAgo) + }) + .Select(p => new PlayerMvpDto() + { + PlayerName = p.PlayerName, + Rank = p.Rank, + TotalVsDuelPoints = p.VsDuels, + MarshalGuardParticipationCount = p.MarshalGuardParticipationCount, + DesertStormParticipationCount = p.DessertStormParticipationCount, + MvpPoints = Math.Round( + (decimal)((p.VsDuels / 1000000.0 * 0.7) + + (p.MarshalGuardParticipationCount * 15) + + (p.DessertStormParticipationCount * 10)) + ) + }) + .OrderByDescending(p => p.MvpPoints) + .ThenByDescending(p => p.TotalVsDuelPoints) + .ThenByDescending(p => p.MarshalGuardParticipationCount) + .ThenBy(p => p.PlayerName) + .ToListAsync(cancellationToken); + + return playerMvps; + } public async Task> CreatePlayerAsync(CreatePlayerDto createPlayerDto, string createdBy, CancellationToken cancellationToken) { var newPlayer = mapper.Map(createPlayerDto);