1
0
mirror of synced 2024-11-24 06:20:12 +01:00

Add single rank

This commit is contained in:
asesidaa 2023-02-16 16:04:38 +08:00
parent 7b3fae51b7
commit ef11e98da7
5 changed files with 130 additions and 10 deletions

View File

@ -9,7 +9,7 @@ using Microsoft.EntityFrameworkCore;
namespace Application.Game.Rank;
public record GetGlobalScoreRankQuery() : IRequestWrapper<string>;
public record GetGlobalScoreRankQuery(string Param) : IRequestWrapper<string>;
public class GetGlobalScoreRankQueryHandler : IRequestHandlerWrapper<GetGlobalScoreRankQuery, string>
{
@ -21,6 +21,59 @@ public class GetGlobalScoreRankQueryHandler : IRequestHandlerWrapper<GetGlobalSc
}
public async Task<ServiceResult<string>> Handle(GetGlobalScoreRankQuery request, CancellationToken cancellationToken)
{
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);
GlobalScoreRankContainer container;
if (rank is null)
{
container = new GlobalScoreRankContainer
{
Ranks = new List<ScoreRankDto>(),
Status = new RankStatus
{
TableName = "GlobalScoreRank",
StartDate = TimeHelper.DateToString(DateTime.Today),
EndDate = TimeHelper.DateToString(DateTime.Today),
Rows = 1,
Status = 1
}
};
return new ServiceResult<string>(container.SerializeCardData());
}
var dto = rank.ScoreRankToDto();
dto.Id = 0;
container = new GlobalScoreRankContainer
{
Ranks = new List<ScoreRankDto>
{
dto
},
Status = new RankStatus
{
TableName = "GlobalScoreRank",
StartDate = TimeHelper.DateToString(DateTime.Today),
EndDate = TimeHelper.DateToString(DateTime.Today),
Rows = 1,
Status = 1
}
};
return new ServiceResult<string>(container.SerializeCardData());
}
private async Task<ServiceResult<string>> GetAllRanks(CancellationToken cancellationToken)
{
var ranks = await cardDbContext.GlobalScoreRanks.OrderBy(rank => rank.Rank)
.Take(30).ToListAsync(cancellationToken: cancellationToken);

View File

@ -10,7 +10,7 @@ using Microsoft.EntityFrameworkCore;
namespace Application.Game.Rank;
public record GetTenpoScoreRankQuery(int TenpoId) : IRequestWrapper<string>;
public record GetTenpoScoreRankQuery(int TenpoId, string Param) : IRequestWrapper<string>;
public class GetTenpoScoreRankQueryHandler : IRequestHandlerWrapper<GetTenpoScoreRankQuery, string>
{
@ -23,7 +23,60 @@ public class GetTenpoScoreRankQueryHandler : IRequestHandlerWrapper<GetTenpoScor
public async Task<ServiceResult<string>> Handle(GetTenpoScoreRankQuery request, CancellationToken cancellationToken)
{
var ranks = await cardDbContext.GlobalScoreRanks.Where(rank => rank.LastPlayTenpoId == request.TenpoId)
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);
TenpoScoreRankContainer container;
if (rank is null)
{
container = new TenpoScoreRankContainer
{
Ranks = new List<ScoreRankDto>(),
Status = new RankStatus
{
TableName = "TenpoScoreRank",
StartDate = TimeHelper.DateToString(DateTime.Today),
EndDate = TimeHelper.DateToString(DateTime.Today),
Rows = 1,
Status = 1
}
};
return new ServiceResult<string>(container.SerializeCardData());
}
var dto = rank.ScoreRankToDto();
dto.Id = 0;
container = new TenpoScoreRankContainer
{
Ranks = new List<ScoreRankDto>
{
dto
},
Status = new RankStatus
{
TableName = "TenpoScoreRankContainer",
StartDate = TimeHelper.DateToString(DateTime.Today),
EndDate = TimeHelper.DateToString(DateTime.Today),
Rows = 1,
Status = 1
}
};
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)
.OrderByDescending(rank => rank.TotalScore)
.Take(30)
.ToListAsync(cancellationToken: cancellationToken);

View File

@ -0,0 +1,11 @@
using System.ComponentModel;
using System.Xml.Serialization;
namespace Application.Game.Rank;
public class RankParam
{
[XmlElement(ElementName = "card_id")]
[DefaultValue("0")]
public long CardId { get; set; }
}

View File

@ -22,11 +22,13 @@ public class MaintainTenpoIdJob : IJob
[SuppressMessage("ReSharper.DPA", "DPA0007: Large number of DB records",
Justification = "All details might be read")]
[SuppressMessage("ReSharper.DPA", "DPA0006: Large number of DB commands")]
public async Task Execute(IJobExecutionContext context)
{
logger.LogInformation("Starting changing null values in card detail table");
var details = await cardDbContext.CardDetails.Where(detail => detail.LastPlayTenpoId == null).ToListAsync();
var details = await cardDbContext.CardDetails.Where(detail => detail.LastPlayTenpoId == null ||
detail.LastPlayTenpoId == "GC local server").ToListAsync();
details.ForEach(detail => detail.LastPlayTenpoId="1337");
cardDbContext.CardDetails.UpdateRange(details);

View File

@ -12,18 +12,19 @@ public class RankingController : BaseController<RankingController>
{
[HttpGet("ranking.php")]
public async Task<ActionResult<string>> Ranking([FromQuery(Name = "cmd_type")] int rankType,
[FromQuery(Name = "tenpo_id")] int tenpoId)
[FromQuery(Name = "tenpo_id")] int tenpoId,
[FromQuery(Name = "param")] string param)
{
var type = (RankingCommandType)rankType;
type.Throw().IfOutOfRange();
var result = type switch
{
RankingCommandType.GlobalRank => await Mediator.Send(new GetGlobalScoreRankQuery()),
RankingCommandType.GlobalRank => await Mediator.Send(new GetGlobalScoreRankQuery(param)),
RankingCommandType.PlayNumRank => await Mediator.Send(new GetPlayNumRankQuery()),
RankingCommandType.EventRank => await Mediator.Send(new GetEventRankQuery()),
RankingCommandType.MonthlyRank => await Mediator.Send(new GetMonthlyScoreRankQuery()),
RankingCommandType.ShopRank => await Mediator.Send(new GetTenpoScoreRankQuery(tenpoId)),
RankingCommandType.ShopRank => await Mediator.Send(new GetTenpoScoreRankQuery(tenpoId, param)),
_ => throw new ArgumentOutOfRangeException(nameof(type), type, "Should not happen!")
};