forked from stadust/pointercrate
-
Notifications
You must be signed in to change notification settings - Fork 1
/
20240504194923_cached_points.down.sql
183 lines (165 loc) · 6.78 KB
/
20240504194923_cached_points.down.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
-- Add down migration script here
DROP VIEW ranked_players;
DROP VIEW ranked_nations;
ALTER TABLE players DROP COLUMN score;
ALTER TABLE nationalities DROP COLUMN score;
ALTER TABLE subdivisions DROP COLUMN score;
DROP FUNCTION recompute_player_scores();
DROP FUNCTION score_of_player(player_id INTEGER);
DROP FUNCTION recompute_nation_scores();
DROP FUNCTION score_of_nation(iso_country_code VARCHAR(2));
DROP FUNCTION recompute_subdivision_scores();
DROP FUNCTION score_of_subdivision(iso_country_code VARCHAR(2), iso_code VARCHAR(3));
DROP VIEW score_giving;
ALTER TABLE players DROP CONSTRAINT nation_subdivions_fkey;
-- Copied from 20210419002933.up
CREATE VIEW players_with_score AS
SELECT players.id,
players.name,
RANK() OVER(ORDER BY scores.total_score DESC) AS rank,
CASE WHEN scores.total_score IS NULL THEN 0.0::FLOAT ELSE scores.total_score END AS score,
ROW_NUMBER() OVER(ORDER BY scores.total_score DESC) AS index,
nationalities.iso_country_code,
nationalities.nation,
players.subdivision,
nationalities.continent
FROM
(
SELECT pseudo_records.player,
SUM(record_score(pseudo_records.progress::FLOAT, pseudo_records.position::FLOAT, 100::FLOAT, pseudo_records.requirement)) as total_score
FROM (
SELECT player,
progress,
position,
CASE WHEN demons.position > 75 THEN 100 ELSE requirement END AS requirement
FROM records
INNER JOIN demons
ON demons.id = demon
WHERE demons.position <= 150 AND status_ = 'APPROVED' AND (demons.position <= 75 OR progress = 100)
UNION
SELECT verifier as player,
CASE WHEN demons.position > 150 THEN 0.0::FLOAT ELSE 100.0::FLOAT END as progress,
position,
100.0::FLOAT
FROM demons
UNION
SELECT publisher as player,
0.0::FLOAT as progress,
position,
100.0::FLOAT
FROM demons
UNION
SELECT creator as player,
0.0::FLOAT as progress,
1.0::FLOAT as position, -- doesn't matter
100.0::FLOAT
FROM creators
) AS pseudo_records
GROUP BY player
) scores
INNER JOIN players
ON scores.player = players.id
LEFT OUTER JOIN nationalities
ON players.nationality = nationalities.iso_country_code
WHERE NOT players.banned AND players.id != 1534;
-- Copied from 20210726174613
CREATE VIEW nations_with_score AS
SELECT RANK() OVER(ORDER BY scores.total_score DESC) AS rank,
scores.total_score AS score,
nationalities.iso_country_code,
nationalities.nation,
nationalities.continent
FROM (
SELECT nationality,
SUM(record_score(pseudo_records.progress::FLOAT, pseudo_records.position::FLOAT,
100::FLOAT, pseudo_records.requirement)) as total_score
FROM (
select distinct on (nationality, demon)
nationality,
progress,
position,
CASE WHEN demons.position > 75 THEN 100 ELSE requirement END AS requirement
from (
select demon, player, progress
from records
where status_='APPROVED'
union
select id, verifier, 100
from demons
) records
inner join demons
on demons.id = records.demon
inner join players
on players.id=records.player
inner join nationalities
on iso_country_code=players.nationality
where position <= 150 and not players.banned
order by nationality, demon, progress desc
) AS pseudo_records
GROUP BY nationality
) scores
INNER JOIN nationalities
ON nationalities.iso_country_code = scores.nationality;
-- Copied from 20210903174349
CREATE OR REPLACE FUNCTION best_records_local(country VARCHAR(2), the_subdivision VARCHAR(3))
RETURNS TABLE (LIKE records)
AS
$body$
WITH grp AS (
SELECT records.*,
RANK() OVER (PARTITION BY demon ORDER BY demon, progress DESC) AS rk
FROM records
INNER JOIN players
ON players.id = player
WHERE status_='APPROVED' AND players.nationality = country AND players.subdivision = the_subdivision
)
SELECT id, progress, video, status_, player, submitter, demon
FROM grp
WHERE rk = 1;
$body$
LANGUAGE SQL;
CREATE OR REPLACE FUNCTION subdivision_ranking_of(country VARCHAR(2))
RETURNS TABLE (
rank BIGINT,
score FLOAT,
subdivision_code VARCHAR(3),
name TEXT
)
AS
$body$
SELECT RANK() OVER(ORDER BY scores.total_score DESC) AS rank,
scores.total_score AS score,
iso_code,
name
FROM (
SELECT iso_code, name,
SUM(record_score(pseudo_records.progress::FLOAT, pseudo_records.position::FLOAT,
100::FLOAT, pseudo_records.requirement)) as total_score
FROM (
select distinct on (iso_code, demon)
iso_code,
subdivisions.name,
progress,
position,
CASE WHEN demons.position > 75 THEN 100 ELSE requirement END AS requirement
from (
select demon, player, progress
from records
where status_='APPROVED'
union
select id, verifier, 100
from demons
) records
inner join demons
on demons.id = records.demon
inner join players
on players.id=records.player
inner join subdivisions
on (iso_code=players.subdivision and players.nationality = nation)
where position <= 150 and not players.banned and nation = country
order by iso_code, demon, progress desc
) AS pseudo_records
GROUP BY iso_code, name
) scores;
$body$
LANGUAGE SQL;