Skip to content

Commit

Permalink
Merge pull request #14 from planetarium/PDX-387
Browse files Browse the repository at this point in the history
PDX-387: Fix /arena API to fit on arena-win-rate-calculator
  • Loading branch information
moreal authored Mar 7, 2024
2 parents e4c0e6b + 98a33f9 commit 62ddcaf
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 12 deletions.
12 changes: 8 additions & 4 deletions NineChroniclesUtilBackend/Controllers/ArenaController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,16 @@ namespace NineChroniclesUtilBackend.Controllers;
[Route("arena")]
public class ArenaController(ArenaRankingRepository arenaRankingRepository) : ControllerBase
{
[HttpGet("ranking")]
public async Task<List<ArenaRanking>> GetRanking(int limit, int offset, string avatarAddress)
[HttpGet("ranking/{avatarAddress}/rank")]
public async Task<long> GetRankByAvatarAddress(string avatarAddress)
{
var addressOffset = await arenaRankingRepository.GetRankByAvatarAddress(avatarAddress);
return await arenaRankingRepository.GetRankByAvatarAddress(avatarAddress);
}

return await arenaRankingRepository.GetRanking(limit, offset + addressOffset);
[HttpGet("ranking")]
public async Task<List<ArenaRanking>> GetRanking(int limit, int offset)
{
return await arenaRankingRepository.GetRanking(limit, offset);
}

[HttpPost("simulate")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,21 +41,18 @@ public async Task<List<ArenaRanking>> GetRanking(long limit, long offset)
{
var pipelines = new[]
{
@"{ $sort: { 'Score.Score': -1 } }",
@"{ $group: { _id: null, docs: { $push: '$$ROOT' } } }",
@"{ $unwind: { path: '$docs', includeArrayIndex: 'Rank' } }",
@"{ $setWindowFields: { partitionBy: '', sortBy: { 'Score.Score': -1 }, output: { Rank: { $rank: {} } } } }",
$@"{{ $skip: {offset} }}",
$@"{{ $limit: {limit} }}",
@"{ $replaceRoot: { newRoot: { $mergeObjects: [ '$docs', { Rank: '$Rank' } ] } } }",
@"{ $lookup: { from: 'avatars', localField: 'AvatarAddress', foreignField: 'Avatar.address', as: 'Avatar' } }",
@"{ $unwind: { path: '$Avatar', preserveNullAndEmptyArrays: true } }",
@"{ $unset: ['Avatar.Avatar.inventory', 'Avatar.Avatar.mailBox', 'Avatar.Avatar.stageMap', 'Avatar.Avatar.monsterMap', 'Avatar.Avatar.itemMap', 'Avatar.Avatar.eventMap'] }",
}.Select(BsonDocument.Parse).ToArray();

var aggregation = await ArenaCollection.Aggregate<BsonDocument>(pipelines).ToListAsync();

var aggregation = ArenaCollection.Aggregate<BsonDocument>(pipelines).ToList();
var arenaRankings = await Task.WhenAll(aggregation.OfType<BsonDocument>().Select(BuildArenaRankingFromDocument));

return arenaRankings.ToList();
return [.. arenaRankings];
}

private async Task<ArenaRanking> BuildArenaRankingFromDocument(BsonDocument document)
Expand All @@ -65,7 +62,7 @@ private async Task<ArenaRanking> BuildArenaRankingFromDocument(BsonDocument docu
document["Information"]["Address"].AsString,
document["Information"]["Win"].AsInt32,
document["Information"]["Lose"].AsInt32,
document["Rank"].AsInt64 + 1,
document["Rank"].AsInt32 + 1,
document["Information"]["Ticket"].AsInt32,
document["Information"]["TicketResetCount"].AsInt32,
document["Information"]["PurchasedTicketCount"].AsInt32,
Expand All @@ -90,6 +87,7 @@ private async Task<ArenaRanking> BuildArenaRankingFromDocument(BsonDocument docu

var cp = await CpRepository.CalculateCp(avatar, characterId, equipmentids, costumeids, runeSlots);
arenaRanking.CP = cp;
Console.WriteLine($"CP Calculate {arenaRanking.ArenaAddress}");

return arenaRanking;
}
Expand Down

0 comments on commit 62ddcaf

Please sign in to comment.