2023-02-16 09:53:02 +01:00
|
|
|
|
using Application.Common.Helpers;
|
2023-02-14 19:14:19 +01:00
|
|
|
|
|
|
|
|
|
namespace Application.Game.Rank;
|
|
|
|
|
|
2023-02-16 09:04:38 +01:00
|
|
|
|
public record GetTenpoScoreRankQuery(int TenpoId, string Param) : IRequestWrapper<string>;
|
2023-02-14 19:14:19 +01:00
|
|
|
|
|
|
|
|
|
public class GetTenpoScoreRankQueryHandler : IRequestHandlerWrapper<GetTenpoScoreRankQuery, string>
|
|
|
|
|
{
|
2023-02-16 08:26:13 +01:00
|
|
|
|
private readonly ICardDbContext cardDbContext;
|
|
|
|
|
|
|
|
|
|
public GetTenpoScoreRankQueryHandler(ICardDbContext cardDbContext)
|
|
|
|
|
{
|
|
|
|
|
this.cardDbContext = cardDbContext;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<ServiceResult<string>> Handle(GetTenpoScoreRankQuery request, CancellationToken cancellationToken)
|
2023-02-14 19:14:19 +01:00
|
|
|
|
{
|
2023-02-16 09:04:38 +01:00
|
|
|
|
var param = request.Param.DeserializeCardData<RankParam>();
|
|
|
|
|
if (param.CardId == 0)
|
|
|
|
|
{
|
|
|
|
|
return await GetAllRanks(request.TenpoId, cancellationToken);
|
|
|
|
|
}
|
|
|
|
|
return await GetCardRank(param.CardId, request.TenpoId, cancellationToken);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task<ServiceResult<string>> GetCardRank(long cardId, int tenpoId, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
var rank = await cardDbContext.GlobalScoreRanks.FirstOrDefaultAsync(scoreRank => scoreRank.CardId == cardId &&
|
|
|
|
|
scoreRank.LastPlayTenpoId == tenpoId,
|
|
|
|
|
cancellationToken: cancellationToken);
|
2023-02-16 09:53:02 +01:00
|
|
|
|
var container = new TenpoScoreRankContainer
|
2023-02-16 09:04:38 +01:00
|
|
|
|
{
|
2023-02-16 09:53:02 +01:00
|
|
|
|
Ranks = new List<ScoreRankDto>(),
|
2023-02-16 09:04:38 +01:00
|
|
|
|
Status = new RankStatus
|
|
|
|
|
{
|
2023-02-16 09:53:02 +01:00
|
|
|
|
TableName = "TenpoScoreRank",
|
2023-02-16 09:04:38 +01:00
|
|
|
|
StartDate = TimeHelper.DateToString(DateTime.Today),
|
|
|
|
|
EndDate = TimeHelper.DateToString(DateTime.Today),
|
2023-02-16 09:53:02 +01:00
|
|
|
|
Rows = 0,
|
2023-02-16 09:04:38 +01:00
|
|
|
|
Status = 1
|
|
|
|
|
}
|
|
|
|
|
};
|
2023-02-16 09:53:02 +01:00
|
|
|
|
if (rank is null)
|
|
|
|
|
{
|
|
|
|
|
return new ServiceResult<string>(container.SerializeCardData());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var dto = rank.ScoreRankToDto();
|
|
|
|
|
dto.Id = 0;
|
|
|
|
|
container.Ranks.Add(dto);
|
|
|
|
|
container.Status.Rows++;
|
2023-02-16 09:04:38 +01:00
|
|
|
|
return new ServiceResult<string>(container.SerializeCardData());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task<ServiceResult<string>> GetAllRanks(int tenpoId, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
var ranks = await cardDbContext.GlobalScoreRanks.Where(rank => rank.LastPlayTenpoId == tenpoId)
|
2023-02-16 08:26:13 +01:00
|
|
|
|
.OrderByDescending(rank => rank.TotalScore)
|
|
|
|
|
.Take(30)
|
|
|
|
|
.ToListAsync(cancellationToken: cancellationToken);
|
|
|
|
|
ranks = ranks.Select((rank, i) =>
|
|
|
|
|
{
|
|
|
|
|
rank.Rank = i + 1;
|
|
|
|
|
return rank;
|
|
|
|
|
}).ToList();
|
2023-02-16 09:04:38 +01:00
|
|
|
|
|
2023-02-16 08:26:13 +01:00
|
|
|
|
var dtoList = ranks.Select((rank, i) =>
|
|
|
|
|
{
|
|
|
|
|
var dto = rank.ScoreRankToDto();
|
|
|
|
|
dto.Id = i;
|
|
|
|
|
dto.Rank2 = dto.Rank;
|
|
|
|
|
return dto;
|
|
|
|
|
}).ToList();
|
2023-02-16 09:04:38 +01:00
|
|
|
|
|
2023-02-14 19:14:19 +01:00
|
|
|
|
var container = new TenpoScoreRankContainer
|
|
|
|
|
{
|
2023-02-16 08:26:13 +01:00
|
|
|
|
Ranks = dtoList,
|
2023-02-14 19:14:19 +01:00
|
|
|
|
Status = new RankStatus
|
|
|
|
|
{
|
2023-10-07 20:12:10 +02:00
|
|
|
|
TableName = "CardTenpoScoreRank",
|
2023-02-14 19:14:19 +01:00
|
|
|
|
StartDate = TimeHelper.DateToString(DateTime.Today),
|
|
|
|
|
EndDate = TimeHelper.DateToString(DateTime.Today),
|
2023-02-16 08:26:13 +01:00
|
|
|
|
Rows = dtoList.Count,
|
|
|
|
|
Status = 1
|
2023-02-14 19:14:19 +01:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2023-02-16 08:26:13 +01:00
|
|
|
|
return new ServiceResult<string>(container.SerializeCardData());
|
2023-02-14 19:14:19 +01:00
|
|
|
|
}
|
2023-10-07 20:12:10 +02:00
|
|
|
|
}
|