-
Notifications
You must be signed in to change notification settings - Fork 23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
PDX-356: Move POST DP API to GET /arena/rankings API #8
Merged
Changes from 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
855de0d
Add local appsettings to .gitignore
XxshiftxX f8cf7f2
Add MongoDB.Driver
XxshiftxX b458232
Add DatabaseOption
XxshiftxX d06afcd
Add sample mongodb query for GET /arena/ranking API
XxshiftxX c0ab058
Fix dynamic to ArenaRanking
XxshiftxX 9578aa1
Add base offset for avatarAddress query
XxshiftxX a582e06
Add cp calculation on arena info API
XxshiftxX d89e216
Add EOF newline
XxshiftxX File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -284,3 +284,5 @@ obj/ | |
|
||
# StrawberryShake | ||
Generated | ||
|
||
appsettings.local.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
using NineChroniclesUtilBackend.Models.Agent; | ||
|
||
namespace NineChroniclesUtilBackend.Models.Arena; | ||
|
||
public class ArenaRanking( | ||
string AvatarAddress, | ||
string ArenaAddress, | ||
int Win, | ||
int Lose, | ||
long Rank, | ||
int Ticket, | ||
int TicketResetCount, | ||
int PurchasedTicketCount, | ||
int Score, | ||
Avatar Avatar) | ||
{ | ||
public string AvatarAddress { get; set; } = AvatarAddress; | ||
public string ArenaAddress { get; set; } = ArenaAddress; | ||
public int Win { get; set; } = Win; | ||
public int Lose { get; set; } = Lose; | ||
public long Rank { get; set; } = Rank; | ||
public int Ticket { get; set; } = Ticket; | ||
public int TicketResetCount { get; set; } = TicketResetCount; | ||
public int PurchasedTicketCount { get; set; } = PurchasedTicketCount; | ||
public int Score { get; set; } = Score; | ||
public Avatar Avatar { get; set; } = Avatar; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace NineChroniclesUtilBackend.Options; | ||
|
||
public class DatabaseOption | ||
{ | ||
public string ConnectionString { get; set; } | ||
public string DatabaseName { get; set; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 110 additions & 0 deletions
110
NineChroniclesUtilBackend/Repositories/ArenaRankingRespository.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
using MongoDB.Bson; | ||
using MongoDB.Driver; | ||
using NineChroniclesUtilBackend.Models.Agent; | ||
using NineChroniclesUtilBackend.Models.Arena; | ||
using NineChroniclesUtilBackend.Services; | ||
|
||
namespace NineChroniclesUtilBackend.Repositories; | ||
|
||
public class ArenaRankingRepository(MongoDBCollectionService mongoDBCollectionService) | ||
{ | ||
private readonly IMongoCollection<dynamic> ArenaCollection = mongoDBCollectionService.GetCollection<dynamic>("arena_0_10"); | ||
|
||
public async Task<long> GetRankByAvatarAddress(string avatarAddress) | ||
{ | ||
var pipelines = new BsonDocument[] | ||
{ | ||
new("$sort", new BsonDocument("ArenaScore.Score", -1)), | ||
new("$group", new BsonDocument | ||
{ | ||
{ "_id", BsonNull.Value }, | ||
{ "docs", new BsonDocument("$push", "$$ROOT") }, | ||
}), | ||
new("$unwind", new BsonDocument | ||
{ | ||
{ "path", "$docs" }, | ||
{ "includeArrayIndex", "Rank" }, | ||
}), | ||
new("$match", new BsonDocument("docs.AvatarAddress", avatarAddress)), | ||
new("$replaceRoot", new BsonDocument | ||
{ | ||
{ "newRoot", new BsonDocument | ||
{ | ||
{ "$mergeObjects", new BsonArray | ||
{ | ||
"$docs", | ||
new BsonDocument("Rank", "$Rank"), | ||
} }, | ||
} }, | ||
}), | ||
}; | ||
|
||
var aggregation = await ArenaCollection.Aggregate<dynamic>(pipelines).ToListAsync(); | ||
|
||
return aggregation.First().Rank + 1; | ||
} | ||
|
||
public async Task<List<ArenaRanking>> GetRanking(long limit, long offset) | ||
{ | ||
// TODO: Implement CP Calculation | ||
var pipelines = new BsonDocument[] | ||
{ | ||
new("$sort", new BsonDocument("ArenaScore.Score", -1)), | ||
new("$group", new BsonDocument | ||
{ | ||
{ "_id", BsonNull.Value }, | ||
{ "docs", new BsonDocument("$push", "$$ROOT") }, | ||
}), | ||
new("$unwind", new BsonDocument | ||
{ | ||
{ "path", "$docs" }, | ||
{ "includeArrayIndex", "Rank" }, | ||
}), | ||
new("$skip", offset), | ||
new("$limit", limit), | ||
new("$replaceRoot", new BsonDocument | ||
{ | ||
{ "newRoot", new BsonDocument | ||
{ | ||
{ "$mergeObjects", new BsonArray | ||
{ | ||
"$docs", | ||
new BsonDocument("Rank", "$Rank"), | ||
} }, | ||
} }, | ||
}), | ||
new("$lookup", new BsonDocument | ||
{ | ||
{ "from", "avatars" }, | ||
{ "localField", "AvatarAddress" }, | ||
{ "foreignField", "address" }, | ||
{ "as", "Avatar" }, | ||
}), | ||
new("$unwind", new BsonDocument | ||
{ | ||
{ "path", "$Avatar" }, | ||
}), | ||
}; | ||
|
||
|
||
var aggregation = await ArenaCollection.Aggregate<dynamic>(pipelines).ToListAsync(); | ||
var result = aggregation.Select(x => new ArenaRanking( | ||
x.AvatarAddress, | ||
x.ArenaInfo.Address, | ||
x.ArenaInfo.Win, | ||
x.ArenaInfo.Lose, | ||
x.Rank + 1, | ||
x.ArenaInfo.Ticket, | ||
x.ArenaInfo.TicketResetCount, | ||
x.ArenaInfo.PurchasedTicketCount, | ||
x.ArenaScore.Score, | ||
new Avatar( | ||
x.Avatar.address, | ||
x.Avatar.name, | ||
x.Avatar.level | ||
) | ||
)).ToList(); | ||
|
||
return result; | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
NineChroniclesUtilBackend/Services/MongoDBCollectionService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
using Microsoft.Extensions.Options; | ||
using MongoDB.Driver; | ||
using NineChroniclesUtilBackend.Options; | ||
|
||
namespace NineChroniclesUtilBackend.Services; | ||
|
||
public class MongoDBCollectionService(IOptions<DatabaseOption> databaseOption) | ||
{ | ||
private readonly IOptions<DatabaseOption> _databaseOption = databaseOption; | ||
|
||
public IMongoCollection<T> GetCollection<T>(string collectionName) | ||
{ | ||
var client = new MongoClient(_databaseOption.Value.ConnectionString); | ||
var database = client.GetDatabase(_databaseOption.Value.DatabaseName); | ||
return database.GetCollection<T>(collectionName); | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please insert newline at the EOFs. |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems like a case to use
record
type. (maybe)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
At first, I thought adding cp calculation part to getter (or static method) of ArenaRanking or Avatar. Because it's kind of (Domain) Entity and cp calculation code is domain logic. I will apply these refactor on following Pull Request and ticket.