Skip to content

Commit 079bd11

Browse files
committed
Fix place update
1 parent 3a6dd25 commit 079bd11

File tree

2 files changed

+77
-19
lines changed

2 files changed

+77
-19
lines changed

services/app/apps/codebattle/lib/codebattle/user/points_and_rank_update.ex

Lines changed: 59 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,71 @@ defmodule Codebattle.User.PointsAndRankUpdate do
77
"""
88

99
alias Codebattle.Repo
10+
alias Codebattle.Season
1011
alias Ecto.Adapters.SQL
1112

1213
@doc """
1314
Updates user points and rankings based on tournament performance in current season.
1415
15-
Points are already calculated in tournament_user_results table based on tournament grade and place.
16-
This function aggregates those points for the current season and updates user rankings.
17-
18-
Updates the current season points in points field and rank.
16+
Points and places are calculated in season_results based on current season tournaments.
17+
This function aligns user points and rank with the current season standings.
1918
"""
20-
def update do
19+
def update(season \\ nil) do
20+
case season || Season.get_current_season() do
21+
%Season{id: season_id} ->
22+
update_from_season_results(season_id)
23+
24+
nil ->
25+
update_from_date_ranges()
26+
end
27+
end
28+
29+
defp update_from_season_results(season_id) do
30+
sql = """
31+
WITH season_results_data AS (
32+
SELECT
33+
sr.user_id,
34+
sr.total_points,
35+
sr.place
36+
FROM season_results sr
37+
WHERE sr.season_id = $1
38+
),
39+
max_place AS (
40+
SELECT COALESCE(MAX(place), 0) AS max_place
41+
FROM season_results
42+
WHERE season_id = $1
43+
),
44+
fallback_rankings AS (
45+
SELECT
46+
u.id AS user_id,
47+
(SELECT max_place FROM max_place)
48+
+ DENSE_RANK() OVER (ORDER BY u.rating DESC, u.id ASC) AS fallback_rank
49+
FROM users u
50+
LEFT JOIN season_results_data sr ON sr.user_id = u.id
51+
WHERE u.is_bot = false AND sr.user_id IS NULL
52+
),
53+
user_rankings AS (
54+
SELECT
55+
u.id AS user_id,
56+
COALESCE(sr.total_points, 0) AS season_points,
57+
COALESCE(sr.place, fr.fallback_rank) AS new_rank
58+
FROM users u
59+
LEFT JOIN season_results_data sr ON sr.user_id = u.id
60+
LEFT JOIN fallback_rankings fr ON fr.user_id = u.id
61+
WHERE u.is_bot = false
62+
)
63+
UPDATE users
64+
SET
65+
points = ur.season_points,
66+
rank = ur.new_rank
67+
FROM user_rankings ur
68+
WHERE users.id = ur.user_id;
69+
"""
70+
71+
SQL.query!(Repo, sql, [season_id])
72+
end
73+
74+
defp update_from_date_ranges do
2175
{current_season, season_start, season_end} = get_current_season_info()
2276

2377
sql = """

services/app/apps/codebattle/lib/codebattle/users_points_and_rank_server.ex

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -65,22 +65,26 @@ defmodule Codebattle.UsersPointsAndRankServer do
6565
def handle_info(_, state), do: {:noreply, state}
6666

6767
defp do_work do
68-
case Codebattle.Season.get_current_season() do
69-
nil ->
70-
Logger.warning("No current season found, skipping season results update")
71-
72-
season ->
73-
case Codebattle.SeasonResult.aggregate_season_results(season.id) do
74-
{:ok, num_rows} ->
75-
Logger.debug("Season results aggregated: #{num_rows} users updated")
76-
77-
{:error, error} ->
78-
Logger.error("Error aggregating season results: #{inspect(error)}")
79-
end
80-
end
68+
season =
69+
case Codebattle.Season.get_current_season() do
70+
nil ->
71+
Logger.warning("No current season found, skipping season results update")
72+
nil
73+
74+
season ->
75+
case Codebattle.SeasonResult.aggregate_season_results(season.id) do
76+
{:ok, num_rows} ->
77+
Logger.debug("Season results aggregated: #{num_rows} users updated")
78+
79+
{:error, error} ->
80+
Logger.error("Error aggregating season results: #{inspect(error)}")
81+
end
82+
83+
season
84+
end
8185

8286
# Keep updating user points/ranks for backward compatibility
83-
Codebattle.User.PointsAndRankUpdate.update()
87+
Codebattle.User.PointsAndRankUpdate.update(season)
8488
Logger.debug("Points has been recalculated")
8589
end
8690
end

0 commit comments

Comments
 (0)