MVP for R4 and R5

This commit is contained in:
Tomasi - Developing 2025-01-07 10:44:09 +01:00
parent c1b67c721e
commit 9ccfc034da
3 changed files with 71 additions and 0 deletions

View File

@ -77,6 +77,29 @@ namespace Api.Controllers.v1
} }
} }
[AllowAnonymous]
[HttpGet("[action]/{allianceId:guid}")]
public async Task<ActionResult<List<PlayerMvpDto>>> 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] [HttpPost]
public async Task<ActionResult<PlayerDto>> CreatePlayer(CreatePlayerDto createPlayerDto, public async Task<ActionResult<PlayerDto>> CreatePlayer(CreatePlayerDto createPlayerDto,
CancellationToken cancellationToken) CancellationToken cancellationToken)

View File

@ -11,6 +11,8 @@ public interface IPlayerRepository
Task<Result<List<PlayerMvpDto>>> GetAlliancePlayersMvp(Guid allianceId, CancellationToken cancellationToken); Task<Result<List<PlayerMvpDto>>> GetAlliancePlayersMvp(Guid allianceId, CancellationToken cancellationToken);
Task<Result<List<PlayerMvpDto>>> GetAllianceLeadershipMvp(Guid allianceId, CancellationToken cancellationToken);
Task<Result<PlayerDto>> CreatePlayerAsync(CreatePlayerDto createPlayerDto, string createdBy, CancellationToken cancellationToken); Task<Result<PlayerDto>> CreatePlayerAsync(CreatePlayerDto createPlayerDto, string createdBy, CancellationToken cancellationToken);
Task<Result<PlayerDto>> UpdatePlayerAsync(UpdatePlayerDto updatePlayerDto, string modifiedBy, CancellationToken cancellationToken); Task<Result<PlayerDto>> UpdatePlayerAsync(UpdatePlayerDto updatePlayerDto, string modifiedBy, CancellationToken cancellationToken);

View File

@ -83,6 +83,52 @@ public class PlayerRepository(ApplicationContext context, IMapper mapper, ILogge
return playerMvps; return playerMvps;
} }
public async Task<Result<List<PlayerMvpDto>>> 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<Result<PlayerDto>> CreatePlayerAsync(CreatePlayerDto createPlayerDto, string createdBy, CancellationToken cancellationToken) public async Task<Result<PlayerDto>> CreatePlayerAsync(CreatePlayerDto createPlayerDto, string createdBy, CancellationToken cancellationToken)
{ {
var newPlayer = mapper.Map<Player>(createPlayerDto); var newPlayer = mapper.Map<Player>(createPlayerDto);