player mvp calculation

This commit is contained in:
Tomasi - Developing 2025-01-07 09:44:06 +01:00
parent bb735a68aa
commit c1b67c721e
4 changed files with 83 additions and 0 deletions

View File

@ -54,6 +54,29 @@ namespace Api.Controllers.v1
} }
} }
[AllowAnonymous]
[HttpGet("[action]/{allianceId:guid}")]
public async Task<ActionResult<List<PlayerMvpDto>>> GetAllianceMvpPlayers(Guid allianceId,
CancellationToken cancellationToken)
{
try
{
var alliancePlayersResult =
await playerRepository.GetAlliancePlayersMvp(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

@ -0,0 +1,11 @@
namespace Application.DataTransferObjects.Player;
public class PlayerMvpDto
{
public required string PlayerName { get; set; }
public required string Rank { get; set; }
public long TotalVsDuelPoints { get; set; }
public int MarshalGuardParticipationCount { get; set; }
public int DesertStormParticipationCount { get; set; }
public decimal MvpPoints { get; set; }
}

View File

@ -9,6 +9,8 @@ public interface IPlayerRepository
Task<Result<List<PlayerDto>>> GetAlliancePlayersAsync(Guid allianceId, CancellationToken cancellationToken); Task<Result<List<PlayerDto>>> GetAlliancePlayersAsync(Guid allianceId, CancellationToken cancellationToken);
Task<Result<List<PlayerMvpDto>>> GetAlliancePlayersMvp(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

@ -36,6 +36,53 @@ public class PlayerRepository(ApplicationContext context, IMapper mapper, ILogge
return Result.Success(alliancePlayers); return Result.Success(alliancePlayers);
} }
public async Task<Result<List<PlayerMvpDto>>> GetAlliancePlayersMvp(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);