Skip to content

Commit

Permalink
add database country rank
Browse files Browse the repository at this point in the history
  • Loading branch information
arily committed Mar 7, 2024
1 parent affdaf0 commit 98b109c
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
27 changes: 25 additions & 2 deletions src/server/backend/bancho.py/server/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -361,15 +361,27 @@ class DBUserProvider extends Base<Id, ScoreId> implements Base<Id, ScoreId> {
const sq = this.drizzle.select({
id: schema.stats.id,
mode: schema.stats.mode,

ppv2Rank: sql`RANK() OVER(PARTITION BY ${schema.stats.mode} ORDER BY ${schema.stats.pp} DESC)`
.mapWith(Number)
.as('ppRank'),
ppv2CountryRank: sql`RANK() OVER(PARTITION BY ${schema.stats.mode}, ${schema.users.country} ORDER BY ${schema.stats.pp} DESC)`
.mapWith(Number)
.as('ppCountryRank'),

totalScoreRank: sql`RANK() OVER(PARTITION BY ${schema.stats.mode} ORDER BY ${schema.stats.totalScore} DESC)`
.mapWith(Number)
.as('tscoreRank'),
totalScoreCountryRank: sql`RANK() OVER(PARTITION BY ${schema.stats.mode}, ${schema.users.country} ORDER BY ${schema.stats.totalScore} DESC)`
.mapWith(Number)
.as('tscoreCountryRank'),

rankedScoreRank: sql`RANK() OVER(PARTITION BY ${schema.stats.mode} ORDER BY ${schema.stats.rankedScore} DESC)`
.mapWith(Number)
.as('rscoreRank'),
rankedScoreCountryRank: sql`RANK() OVER(PARTITION BY ${schema.stats.mode}, ${schema.users.country} ORDER BY ${schema.stats.rankedScore} DESC)`
.mapWith(Number)
.as('rscoreCountryRank'),
})
.from(schema.stats)
.innerJoin(schema.users, and(
Expand All @@ -380,8 +392,13 @@ class DBUserProvider extends Base<Id, ScoreId> implements Base<Id, ScoreId> {
const s2 = aliasedTable(schema.stats, 's2')
const mq = this.drizzle.select({
ppv2Rank: sq.ppv2Rank,
ppv2CountryRank: sq.ppv2CountryRank,

totalScoreRank: sq.totalScoreRank,
totalScoreCountryRank: sq.totalScoreCountryRank,

rankedScoreRank: sq.rankedScoreRank,
rankedScoreCountryRank: sq.rankedScoreCountryRank,
stat: s2,
}).from(sq)
.innerJoin(s2,
Expand Down Expand Up @@ -680,8 +697,14 @@ class DBUserProvider extends Base<Id, ScoreId> implements Base<Id, ScoreId> {
async _toStatistics(
results: ({
stat: typeof schema.stats.$inferSelect

ppv2Rank: number
ppv2CountryRank: number

totalScoreCountryRank: number
totalScoreRank: number

rankedScoreCountryRank: number
rankedScoreRank: number
})[],
livePPRank?: Awaited<ReturnType<RedisUserProvider['getRedisRanks']>>,
Expand Down Expand Up @@ -794,14 +817,14 @@ class DBUserProvider extends Base<Id, ScoreId> implements Base<Id, ScoreId> {
}

export class RedisUserProvider extends DBUserProvider {
redisClient?: ReturnType<typeof redisClient>
redisClient: ReturnType<typeof redisClient>
constructor() {
super()
this.redisClient = redisClient()
}

async getLiveRank(id: number, mode: number, country: string) {
if (this.redisClient?.isReady) {
if (this.redisClient.isReady) {
return {
rank: await this.redisClient.zRevRank(
`bancho:leaderboard:${mode}`,
Expand Down
10 changes: 9 additions & 1 deletion src/server/backend/bancho.py/transforms/ruleset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,15 @@ export function createRulesetData<RankingSystem extends LeaderboardRankingSystem
}: {
databaseResult?: {
stat: typeof schema.stats.$inferSelect

ppv2Rank: number
ppv2CountryRank: number

totalScoreRank: number
totalScoreCountryRank: number

rankedScoreRank: number
rankedScoreCountryRank: number
}
livePPRank?: {
rank: number | null
Expand Down Expand Up @@ -56,15 +62,17 @@ export function createRulesetData<RankingSystem extends LeaderboardRankingSystem
return {
[Rank.PPv2]: {
rank: livePPRank?.rank || dbResult.ppv2Rank || undefined,
countryRank: livePPRank?.countryRank || undefined,
countryRank: livePPRank?.countryRank || dbResult.ppv2CountryRank || undefined,
performance: dbResult.stat.pp,
},
[Rank.RankedScore]: {
rank: dbResult.rankedScoreRank || undefined,
countryRank: dbResult.rankedScoreCountryRank || undefined,
score: dbResult.stat.rankedScore,
},
[Rank.TotalScore]: {
rank: dbResult.totalScoreRank || undefined,
countryRank: dbResult.totalScoreCountryRank || undefined,
score: dbResult.stat.totalScore,
},
playCount: dbResult.stat.plays,
Expand Down

0 comments on commit 98b109c

Please sign in to comment.