-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Count legacy RF replays properly when it comes to ranking. (#519)
- Loading branch information
Showing
2 changed files
with
279 additions
and
0 deletions.
There are no files selected for viewing
223 changes: 223 additions & 0 deletions
223
project/thscoreboard/replays/migrations/0043_replay_legacy_rank.py
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,223 @@ | ||
# Generated by Django 4.2.4 on 2025-01-13 19:10 | ||
|
||
from django.db import migrations | ||
|
||
|
||
class Migration(migrations.Migration): | ||
"""Fix rank view to include RF replays.""" | ||
|
||
dependencies = [ | ||
("replays", "0042_unique_rank_view"), | ||
] | ||
|
||
operations = [ | ||
migrations.RunSQL( | ||
sql=""" | ||
CREATE OR REPLACE VIEW replays_rank | ||
AS | ||
SELECT | ||
row_number() over () as id, | ||
replay, | ||
score, | ||
shot_id, | ||
difficulty, | ||
route_id, | ||
category, | ||
place | ||
FROM | ||
( | ||
SELECT | ||
replay, | ||
score, | ||
shot_id, | ||
difficulty, | ||
route_id, | ||
category, | ||
created, | ||
rank() OVER ( | ||
PARTITION BY shot_id, | ||
difficulty, | ||
route_id, | ||
category | ||
ORDER BY | ||
score DESC, | ||
created, | ||
replay | ||
) as place | ||
FROM | ||
( | ||
SELECT | ||
replay, | ||
score, | ||
shot_id, | ||
difficulty, | ||
route_id, | ||
category, | ||
created, | ||
user_id, | ||
imported_username | ||
FROM | ||
( | ||
-- Per-user bests | ||
( | ||
SELECT | ||
id as replay, | ||
score, | ||
shot_id, | ||
difficulty, | ||
route_id, | ||
category, | ||
created, | ||
user_id, | ||
NULL as imported_username, | ||
rank() OVER ( | ||
PARTITION BY shot_id, | ||
difficulty, | ||
route_id, | ||
category, | ||
user_id | ||
ORDER BY | ||
score DESC, | ||
created, | ||
id | ||
) as per_user_place | ||
FROM | ||
replays_replay | ||
WHERE | ||
replay_type = 1 -- FULL_GAME | ||
AND category = 1 -- STANDARD | ||
AND user_id IS NOT NULL | ||
) | ||
UNION ALL | ||
-- Per-username bests for imported, unowned Royalflare replays | ||
( | ||
SELECT | ||
id as replay, | ||
score, | ||
shot_id, | ||
difficulty, | ||
route_id, | ||
category, | ||
created, | ||
NULL as user_id, | ||
imported_username, | ||
rank() OVER ( | ||
PARTITION BY shot_id, | ||
difficulty, | ||
route_id, | ||
category, | ||
imported_username | ||
ORDER BY | ||
score DESC, | ||
created, | ||
id | ||
) as per_user_place | ||
FROM | ||
replays_replay | ||
WHERE | ||
replay_type = 1 -- FULL_GAME | ||
AND category = 1 -- STANDARD | ||
AND user_id IS NULL | ||
AND imported_username IS NOT NULL | ||
) | ||
) | ||
AS replays_ranked_per_user | ||
WHERE | ||
per_user_place = 1 | ||
) AS top_replays_per_user | ||
) AS top_replays | ||
WHERE | ||
place <= 3 | ||
ORDER BY | ||
shot_id, | ||
difficulty, | ||
route_id, | ||
category, | ||
place DESC; | ||
""", | ||
reverse_sql=""" | ||
CREATE OR REPLACE VIEW replays_rank | ||
AS | ||
SELECT | ||
row_number() over () as id, | ||
replay, | ||
score, | ||
shot_id, | ||
difficulty, | ||
route_id, | ||
category, | ||
place | ||
FROM | ||
( | ||
SELECT | ||
replay, | ||
score, | ||
shot_id, | ||
difficulty, | ||
route_id, | ||
category, | ||
created, | ||
rank() OVER ( | ||
PARTITION BY shot_id, | ||
difficulty, | ||
route_id, | ||
category | ||
ORDER BY | ||
score DESC, | ||
created, | ||
replay | ||
) as place | ||
FROM | ||
( | ||
SELECT | ||
replay, | ||
score, | ||
shot_id, | ||
difficulty, | ||
route_id, | ||
category, | ||
created, | ||
user_id | ||
FROM | ||
( | ||
SELECT | ||
id as replay, | ||
score, | ||
shot_id, | ||
difficulty, | ||
route_id, | ||
category, | ||
created, | ||
user_id, | ||
rank() OVER ( | ||
PARTITION BY shot_id, | ||
difficulty, | ||
route_id, | ||
category, | ||
user_id | ||
ORDER BY | ||
score DESC, | ||
created, | ||
id | ||
) as per_user_place | ||
FROM | ||
replays_replay | ||
WHERE | ||
replay_type = 1 -- FULL_GAME | ||
AND category = 1 -- STANDARD | ||
) AS replays_ranked_per_user | ||
WHERE | ||
per_user_place = 1 | ||
) AS top_replays_per_user | ||
) AS top_replays | ||
WHERE | ||
place <= 3 | ||
ORDER BY | ||
shot_id, | ||
difficulty, | ||
route_id, | ||
category, | ||
place DESC; | ||
""", | ||
) | ||
] |
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