Skip to content

Commit 4e733b0

Browse files
committed
#10 fix leaderboards
1 parent d4556da commit 4e733b0

File tree

4 files changed

+25
-11
lines changed

4 files changed

+25
-11
lines changed

requirements.txt

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@ pytest-order==1.3.0
1212
flake8==7.1.1
1313
black==24.10.0
1414
pytest-timeout==2.3.1
15-
pytest-xdist==3.6.1
15+
pytest-xdist==3.6.1
16+
numpy==2.2.2

src/threads/get_matches.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
logger = create_logger("get_matches")
1515

16+
MAX_RETRIES = 5
17+
1618

1719
class GetMatchesThread(AbstractThread):
1820
def __init__(self):
@@ -138,7 +140,8 @@ def run_insert_matches_loop(self):
138140

139141
def handle(self):
140142
while True:
141-
if self.tries > 5:
143+
if self.tries > MAX_RETRIES:
144+
logger.info(f"Exceeding {MAX_RETRIES} tries. Skipping to next id.")
142145
self.tries = 0
143146
self.match_id += 1
144147
self.run_insert_matches_loop()

src/threads/update_big_queries.py

+11-3
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,15 @@
99

1010
from models import Player, PlayerGame, Game, Season, Zone, PlayerSeason, Map
1111
from src.log_utils import create_logger
12-
from src.utils import CustomJSONEncoder, RANKS, calculate_points
12+
from src.utils import CustomJSONEncoder, RANKS, distribute_points
1313
from src.threads.abstract_thread import AbstractThread
1414

1515
logger = create_logger("update_big_queries")
1616

1717

1818
def get_countries_leaderboard(season_id):
1919
total_players = 1000
20+
points_repartition = distribute_points(max_points=10000, num_people=total_players)
2021
player_seasons = (
2122
PlayerSeason.select(
2223
Player.uuid,
@@ -37,7 +38,10 @@ def get_countries_leaderboard(season_id):
3738
objs = [o for o in player_seasons]
3839
countries = {}
3940
for o in objs:
40-
computed_points = calculate_points(o["rank"], total_players=total_players)
41+
if o["rank"] > len(points_repartition):
42+
computed_points = 0
43+
else:
44+
computed_points = int(points_repartition[o["rank"] - 1])
4145
if not o["country"] in countries:
4246
countries[o["country"]] = {
4347
"name": o["name"],
@@ -62,6 +66,7 @@ def get_countries_leaderboard(season_id):
6266

6367
def get_clubs_leaderboard(season_id):
6468
total_players = 1000
69+
points_repartition = distribute_points(max_points=10000, num_people=total_players)
6570
player_seasons = (
6671
PlayerSeason.select(
6772
Player.uuid,
@@ -78,7 +83,10 @@ def get_clubs_leaderboard(season_id):
7883
objs = [o for o in player_seasons]
7984
club_tags = {}
8085
for o in objs:
81-
computed_points = calculate_points(o["rank"], total_players=total_players)
86+
if o["rank"] > len(points_repartition):
87+
computed_points = 0
88+
else:
89+
computed_points = int(points_repartition[o["rank"] - 1])
8290
if not o["club_tag"] in club_tags:
8391
club_tags[o["club_tag"]] = {"name": o["club_tag"], "points": 0, "count": 0}
8492
if club_tags[o["club_tag"]]["count"] < 10:

src/utils.py

+8-6
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import decimal
33
import inspect
44
import json
5-
import math
5+
import numpy as np
66
from datetime import datetime
77
from http.server import BaseHTTPRequestHandler
88
from inspect import Parameter
@@ -235,11 +235,13 @@ def routes(cls):
235235
return formatted_routes
236236

237237

238-
def calculate_points(rank, max_points=10000, min_points=1000, total_players=5000):
239-
scale_factor = (max_points - min_points) / (math.log(2) - math.log(total_players + 1))
240-
points = int(max_points - scale_factor * (math.log(rank + 1) - math.log(2)))
241-
242-
return max(min_points, points)
238+
def distribute_points(num_people=1000, max_points=10000):
239+
log_values = np.log2(np.arange(1, num_people + 1))
240+
scaled_points = (log_values - log_values.min()) / (log_values.max() - log_values.min())
241+
scaled_points = scaled_points * max_points
242+
rounded_points = np.round(scaled_points, 0)
243+
rounded_points = map(lambda x: max_points - x, rounded_points)
244+
return list(rounded_points)
243245

244246

245247
def get_trackmaster_limit():

0 commit comments

Comments
 (0)