Skip to content

Commit

Permalink
update ability ranking query
Browse files Browse the repository at this point in the history
  • Loading branch information
area363 committed Nov 25, 2024
1 parent fff1d24 commit a74ea9d
Showing 1 changed file with 26 additions and 4 deletions.
30 changes: 26 additions & 4 deletions NineChronicles.DataProvider/Store/MySqlStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2190,22 +2190,44 @@ public IEnumerable<AbilityRankingModel> GetAbilityRanking(
int? limit = null)
{
using NineChroniclesContext ctx = _dbContextFactory.CreateDbContext();

// Query without ROW_NUMBER()
var query = ctx.Set<AbilityRankingModel>()
.FromSqlRaw("SELECT `Address` as `AvatarAddress`, `AgentAddress`, `Name`, `TitleId`, `AvatarLevel`, `ArmorId`, `Cp`, " +
"row_number() over(ORDER BY `Cp` DESC) `Ranking` " +
.FromSqlRaw("SELECT `Address` as `AvatarAddress`, `AgentAddress`, `Name`, `TitleId`, `AvatarLevel`, `ArmorId`, `Cp` " +
"FROM `Avatars` ");

// Apply filter for a specific avatar address
if (avatarAddress is { } avatarAddressNotNull)
{
query = query.Where(s => s.AvatarAddress == avatarAddressNotNull.ToString());
}

// Execute the query
var result = query
.OrderByDescending(s => s.Cp) // Order by Cp descending
.ToList();

// Calculate rankings in memory
var rankedResult = result
.Select((item, index) => new AbilityRankingModel
{
AvatarAddress = item.AvatarAddress,
AgentAddress = item.AgentAddress,
Name = item.Name,
TitleId = item.TitleId,
AvatarLevel = item.AvatarLevel,
ArmorId = item.ArmorId,
Cp = item.Cp,
Ranking = index + 1, // Assign rank based on position
});

// Apply limit if specified
if (limit is { } limitNotNull)
{
query = query.Take(limitNotNull);
rankedResult = rankedResult.Take(limitNotNull);
}

return query.ToList();
return rankedResult;
}

public IEnumerable<AgentModel> GetDau(string date)
Expand Down

0 comments on commit a74ea9d

Please sign in to comment.