diff --git a/Application/Game/Rank/GetGlobalScoreRankQuery.cs b/Application/Game/Rank/GetGlobalScoreRankQuery.cs index 62e6759..8ccb292 100644 --- a/Application/Game/Rank/GetGlobalScoreRankQuery.cs +++ b/Application/Game/Rank/GetGlobalScoreRankQuery.cs @@ -9,7 +9,7 @@ using Microsoft.EntityFrameworkCore; namespace Application.Game.Rank; -public record GetGlobalScoreRankQuery() : IRequestWrapper; +public record GetGlobalScoreRankQuery(string Param) : IRequestWrapper; public class GetGlobalScoreRankQueryHandler : IRequestHandlerWrapper { @@ -21,6 +21,59 @@ public class GetGlobalScoreRankQueryHandler : IRequestHandlerWrapper> Handle(GetGlobalScoreRankQuery request, CancellationToken cancellationToken) + { + var param = request.Param.DeserializeCardData(); + if (param.CardId == 0) + { + return await GetAllRanks(cancellationToken); + } + return await GetCardRank(param.CardId, cancellationToken); + } + + private async Task> 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(), + Status = new RankStatus + { + TableName = "GlobalScoreRank", + StartDate = TimeHelper.DateToString(DateTime.Today), + EndDate = TimeHelper.DateToString(DateTime.Today), + Rows = 1, + Status = 1 + } + }; + return new ServiceResult(container.SerializeCardData()); + } + + var dto = rank.ScoreRankToDto(); + dto.Id = 0; + container = new GlobalScoreRankContainer + { + Ranks = new List + { + dto + }, + Status = new RankStatus + { + TableName = "GlobalScoreRank", + StartDate = TimeHelper.DateToString(DateTime.Today), + EndDate = TimeHelper.DateToString(DateTime.Today), + Rows = 1, + Status = 1 + } + }; + return new ServiceResult(container.SerializeCardData()); + } + + private async Task> GetAllRanks(CancellationToken cancellationToken) { var ranks = await cardDbContext.GlobalScoreRanks.OrderBy(rank => rank.Rank) .Take(30).ToListAsync(cancellationToken: cancellationToken); @@ -32,7 +85,7 @@ public class GetGlobalScoreRankQueryHandler : IRequestHandlerWrapper; +public record GetTenpoScoreRankQuery(int TenpoId, string Param) : IRequestWrapper; public class GetTenpoScoreRankQueryHandler : IRequestHandlerWrapper { @@ -23,7 +23,60 @@ public class GetTenpoScoreRankQueryHandler : IRequestHandlerWrapper> Handle(GetTenpoScoreRankQuery request, CancellationToken cancellationToken) { - var ranks = await cardDbContext.GlobalScoreRanks.Where(rank => rank.LastPlayTenpoId == request.TenpoId) + var param = request.Param.DeserializeCardData(); + if (param.CardId == 0) + { + return await GetAllRanks(request.TenpoId, cancellationToken); + } + return await GetCardRank(param.CardId, request.TenpoId, cancellationToken); + } + + private async Task> 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(), + Status = new RankStatus + { + TableName = "TenpoScoreRank", + StartDate = TimeHelper.DateToString(DateTime.Today), + EndDate = TimeHelper.DateToString(DateTime.Today), + Rows = 1, + Status = 1 + } + }; + return new ServiceResult(container.SerializeCardData()); + } + + var dto = rank.ScoreRankToDto(); + dto.Id = 0; + container = new TenpoScoreRankContainer + { + Ranks = new List + { + dto + }, + Status = new RankStatus + { + TableName = "TenpoScoreRankContainer", + StartDate = TimeHelper.DateToString(DateTime.Today), + EndDate = TimeHelper.DateToString(DateTime.Today), + Rows = 1, + Status = 1 + } + }; + return new ServiceResult(container.SerializeCardData()); + } + + private async Task> GetAllRanks(int tenpoId, CancellationToken cancellationToken) + { + var ranks = await cardDbContext.GlobalScoreRanks.Where(rank => rank.LastPlayTenpoId == tenpoId) .OrderByDescending(rank => rank.TotalScore) .Take(30) .ToListAsync(cancellationToken: cancellationToken); @@ -32,7 +85,7 @@ public class GetTenpoScoreRankQueryHandler : IRequestHandlerWrapper { var dto = rank.ScoreRankToDto(); @@ -40,7 +93,7 @@ public class GetTenpoScoreRankQueryHandler : IRequestHandlerWrapper 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); diff --git a/MainServer/Controllers/Game/RankingController.cs b/MainServer/Controllers/Game/RankingController.cs index a759776..64f7a50 100644 --- a/MainServer/Controllers/Game/RankingController.cs +++ b/MainServer/Controllers/Game/RankingController.cs @@ -12,18 +12,19 @@ public class RankingController : BaseController { [HttpGet("ranking.php")] public async Task> 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!") };