1
0
mirror of synced 2024-12-21 18:55:53 +01:00
GC-local-server-rewrite/Application/Game/Rank/GetGlobalScoreRankQuery.cs

97 lines
3.1 KiB
C#
Raw Normal View History

2023-02-16 09:53:02 +01:00
using Application.Common.Helpers;
namespace Application.Game.Rank;
2023-02-16 09:04:38 +01:00
public record GetGlobalScoreRankQuery(string Param) : IRequestWrapper<string>;
public class GetGlobalScoreRankQueryHandler : IRequestHandlerWrapper<GetGlobalScoreRankQuery, string>
{
2023-02-16 08:26:13 +01:00
private readonly ICardDbContext cardDbContext;
public GetGlobalScoreRankQueryHandler(ICardDbContext cardDbContext)
{
this.cardDbContext = cardDbContext;
}
public async Task<ServiceResult<string>> Handle(GetGlobalScoreRankQuery request, CancellationToken cancellationToken)
2023-02-16 09:04:38 +01:00
{
var param = request.Param.DeserializeCardData<RankParam>();
if (param.CardId == 0)
{
return await GetAllRanks(cancellationToken);
}
return await GetCardRank(param.CardId, cancellationToken);
}
private async Task<ServiceResult<string>> GetCardRank(long cardId,
CancellationToken cancellationToken)
{
var rank = await cardDbContext.GlobalScoreRanks.FirstOrDefaultAsync(scoreRank => scoreRank.CardId == cardId,
cancellationToken: cancellationToken);
2023-02-16 09:53:02 +01:00
var container = new GlobalScoreRankContainer
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
{
TableName = "GlobalScoreRank",
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(CancellationToken cancellationToken)
{
2023-02-16 08:26:13 +01:00
var ranks = await cardDbContext.GlobalScoreRanks.OrderBy(rank => rank.Rank)
.Take(30).ToListAsync(cancellationToken: cancellationToken);
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
var container = new GlobalScoreRankContainer
{
2023-02-16 08:26:13 +01:00
Ranks = dtoList,
Status = new RankStatus
{
2023-02-16 08:26:13 +01:00
TableName = "GlobalScoreRank",
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-16 08:26:13 +01:00
return new ServiceResult<string>(container.SerializeCardData());
}
}
[XmlRoot("root")]
public class GlobalScoreRankContainer
{
[XmlArray(ElementName = "score_rank")]
[XmlArrayItem(ElementName = "record")]
// ReSharper disable once UnusedAutoPropertyAccessor.Global
2023-02-16 08:26:13 +01:00
public List<ScoreRankDto> Ranks { get; init; } = new();
[XmlElement("ranking_status")]
public RankStatus Status { get; set; } = new();
}