Skip to content

Commit

Permalink
Refactor top_countries
Browse files Browse the repository at this point in the history
  • Loading branch information
Lekuruu committed Nov 10, 2023
1 parent bef2f9e commit 08ad62e
Showing 1 changed file with 43 additions and 15 deletions.
58 changes: 43 additions & 15 deletions cache/leaderboards.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,37 +209,65 @@ def top_players(

return [(int(id), score) for id, score in players]

def top_countries(
mode: int,
type: str = 'performance'
) -> List[Tuple[str, float]]:
total_scores = []
def top_countries(mode: int) -> List[dict]:
"""Get a list of the top countries"""
country_rankings = []

for country in countries.keys():
if country == 'XX':
continue

scores = app.session.redis.zrevrangebyscore(
f'bancho:{type}:{mode}:{country.lower()}',
country_performance = app.session.redis.zrevrangebyscore(
f'bancho:performance:{mode}:{country.lower()}',
'+inf',
'-inf',
withscores=True
)

if not scores:
if not country_performance:
continue

total_scores.append((
country,
sum(score for member, score in scores)
))
country_rscore = app.session.redis.zrevrangebyscore(
f'bancho:rscore:{mode}:{country.lower()}',
'+inf',
'-inf',
withscores=True
)

if not country_rscore:
continue

country_tscore = app.session.redis.zrevrangebyscore(
f'bancho:tscore:{mode}:{country.lower()}',
'+inf',
'-inf',
withscores=True
)

if not country_tscore:
continue

total_scores.sort(
key=lambda x: x[1],
total_performance = sum(score for member, score in country_performance)
total_rscore = sum(score for member, score in country_rscore)
total_tscore = sum(score for member, score in country_tscore)
total_users = len(country_performance)
average_pp = total_performance / total_users

country_rankings.append({
'name': country.lower(),
'total_performance': total_performance,
'total_rscore': total_rscore,
'total_tscore': total_tscore,
'total_users': total_users,
'average_pp': average_pp
})

country_rankings.sort(
key=lambda x: x['total_performance'],
reverse=True
)

return total_scores
return country_rankings

def player_count(
mode: int,
Expand Down

0 comments on commit 08ad62e

Please sign in to comment.