Skip to content

Commit 50128e9

Browse files
committed
list players with 0 list points and accepted legacy records
1 parent 6c3329f commit 50128e9

File tree

3 files changed

+144
-4
lines changed

3 files changed

+144
-4
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
UPDATE players
2+
SET score = 0.0
3+
WHERE score IS NULL;
4+
5+
ALTER TABLE players
6+
ALTER COLUMN score
7+
SET NOT NULL;
8+
9+
CREATE OR REPLACE FUNCTION record_score(progress FLOAT, demon FLOAT, list_size FLOAT, requirement FLOAT) RETURNS FLOAT AS
10+
$record_score$
11+
SELECT CASE
12+
WHEN progress = 100 THEN
13+
CASE
14+
15+
WHEN 55 < demon AND demon <= 150 THEN
16+
(56.191 * EXP(LN(2) * ((54.147 - (demon + 3.2)) * LN(50.0)) / 99.0)) + 6.273
17+
WHEN 35 < demon AND demon <= 55 THEN
18+
212.61 * (EXP(LN(1.036) * (1 - demon))) + 25.071
19+
WHEN 20 < demon AND demon <= 35 THEN
20+
(250 - 83.389) * (EXP(LN(1.0099685) * (2 - demon))) - 31.152
21+
WHEN demon <= 20 THEN
22+
(250 - 100.39) * (EXP(LN(1.168) * (1 - demon))) + 100.39
23+
24+
END
25+
26+
WHEN progress < requirement THEN
27+
0.0
28+
ELSE
29+
CASE
30+
31+
WHEN 55 < demon AND demon <= 150 THEN
32+
((56.191 * EXP(LN(2) * ((54.147 - (demon + 3.2)) * LN(50.0)) / 99.0)) + 6.273) * (EXP(LN(5) * (progress - requirement) / (100 - requirement))) / 10
33+
WHEN 35 < demon AND demon <= 55 THEN
34+
(212.61 * (EXP(LN(1.036) * (1 - demon))) + 25.071) * (EXP(LN(5) * (progress - requirement) / (100 - requirement))) / 10
35+
WHEN 20 < demon AND demon <= 35 THEN
36+
((250 - 83.389) * (EXP(LN(1.0099685) * (2 - demon))) - 31.152) * (EXP(LN(5) * (progress - requirement) / (100 - requirement))) / 10
37+
WHEN demon <= 20 THEN
38+
((250 - 100.39) * (EXP(LN(1.168) * (1 - demon))) + 100.39) * (EXP(LN(5) * (progress - requirement) / (100 - requirement))) / 10
39+
40+
END
41+
END;
42+
$record_score$
43+
LANGUAGE SQL IMMUTABLE;
44+
45+
CREATE OR REPLACE FUNCTION recompute_player_scores() RETURNS void AS $$
46+
UPDATE players
47+
SET score = coalesce(q.score, 0)
48+
FROM players p
49+
LEFT OUTER JOIN (
50+
SELECT player, SUM(record_score(progress, position, 150, requirement)) as score
51+
FROM score_giving
52+
GROUP BY player
53+
) q
54+
ON q.player = p.id
55+
WHERE players.id = p.id;
56+
$$ LANGUAGE SQL;
57+
58+
CREATE OR REPLACE VIEW ranked_players AS
59+
SELECT
60+
ROW_NUMBER() OVER(ORDER BY players.score DESC, id) AS index,
61+
RANK() OVER(ORDER BY players.score DESC) AS rank,
62+
id, name, players.score, subdivision,
63+
nationalities.iso_country_code,
64+
nationalities.nation,
65+
nationalities.continent
66+
FROM players
67+
LEFT OUTER JOIN nationalities
68+
ON players.nationality = nationalities.iso_country_code
69+
WHERE NOT players.banned AND players.score > 0.0;
70+
71+
SELECT recompute_player_scores();
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
ALTER TABLE players
2+
ALTER COLUMN score
3+
DROP NOT NULL;
4+
5+
CREATE OR REPLACE FUNCTION record_score(progress FLOAT, demon FLOAT, list_size FLOAT, requirement FLOAT) RETURNS FLOAT AS
6+
$record_score$
7+
SELECT CASE
8+
WHEN demon > 150 THEN 0.0
9+
10+
WHEN progress = 100 THEN
11+
CASE
12+
13+
WHEN 55 < demon AND demon <= 150 THEN
14+
(56.191 * EXP(LN(2) * ((54.147 - (demon + 3.2)) * LN(50.0)) / 99.0)) + 6.273
15+
WHEN 35 < demon AND demon <= 55 THEN
16+
212.61 * (EXP(LN(1.036) * (1 - demon))) + 25.071
17+
WHEN 20 < demon AND demon <= 35 THEN
18+
(250 - 83.389) * (EXP(LN(1.0099685) * (2 - demon))) - 31.152
19+
WHEN demon <= 20 THEN
20+
(250 - 100.39) * (EXP(LN(1.168) * (1 - demon))) + 100.39
21+
22+
END
23+
24+
WHEN progress < requirement THEN
25+
0.0
26+
ELSE
27+
CASE
28+
29+
WHEN 55 < demon AND demon <= 150 THEN
30+
((56.191 * EXP(LN(2) * ((54.147 - (demon + 3.2)) * LN(50.0)) / 99.0)) + 6.273) * (EXP(LN(5) * (progress - requirement) / (100 - requirement))) / 10
31+
WHEN 35 < demon AND demon <= 55 THEN
32+
(212.61 * (EXP(LN(1.036) * (1 - demon))) + 25.071) * (EXP(LN(5) * (progress - requirement) / (100 - requirement))) / 10
33+
WHEN 20 < demon AND demon <= 35 THEN
34+
((250 - 83.389) * (EXP(LN(1.0099685) * (2 - demon))) - 31.152) * (EXP(LN(5) * (progress - requirement) / (100 - requirement))) / 10
35+
WHEN demon <= 20 THEN
36+
((250 - 100.39) * (EXP(LN(1.168) * (1 - demon))) + 100.39) * (EXP(LN(5) * (progress - requirement) / (100 - requirement))) / 10
37+
38+
END
39+
END;
40+
$record_score$
41+
LANGUAGE SQL IMMUTABLE;
42+
43+
CREATE OR REPLACE FUNCTION recompute_player_scores() RETURNS void AS $$
44+
UPDATE players
45+
SET score = q.score
46+
FROM players p
47+
LEFT OUTER JOIN (
48+
SELECT player, SUM(record_score(progress, position, 150, requirement)) as score
49+
FROM score_giving
50+
GROUP BY player
51+
) q
52+
ON q.player = p.id
53+
WHERE players.id = p.id;
54+
$$ LANGUAGE SQL;
55+
56+
CREATE OR REPLACE VIEW ranked_players AS
57+
SELECT
58+
ROW_NUMBER() OVER(ORDER BY players.score DESC, id) AS index,
59+
RANK() OVER(ORDER BY players.score DESC) AS rank,
60+
id, name, players.score, subdivision,
61+
nationalities.iso_country_code,
62+
nationalities.nation,
63+
nationalities.continent
64+
FROM players
65+
LEFT OUTER JOIN nationalities
66+
ON players.nationality = nationalities.iso_country_code
67+
WHERE NOT players.banned AND players.score IS NOT NULL;
68+
69+
SELECT recompute_player_scores();

pointercrate-demonlist/src/player/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ pub struct Player {
5858
/// - Player updates
5959
/// * Player banned
6060
/// * Player objects merged
61-
pub score: f64,
61+
pub score: Option<f64>,
6262
pub nationality: Option<Nationality>,
6363
}
6464

@@ -67,7 +67,7 @@ pub struct Player {
6767
impl Hash for Player {
6868
fn hash<H: Hasher>(&self, state: &mut H) {
6969
self.base.hash(state);
70-
((self.score * 100f64) as u64).hash(state);
70+
((self.score.unwrap_or(0.0) * 100f64) as u64).hash(state);
7171
self.nationality.hash(state);
7272
}
7373
}
@@ -82,10 +82,10 @@ impl Taggable for FullPlayer {
8282

8383
impl DatabasePlayer {
8484
/// Recomputes this player's score and updates it in the database.
85-
pub async fn update_score(&self, connection: &mut PgConnection) -> Result<f64, CoreError> {
85+
pub async fn update_score(&self, connection: &mut PgConnection) -> Result<Option<f64>, CoreError> {
8686
// No need to specially handle banned players - they have no approved records, so `score_of_player` will return 0
8787
let new_score = sqlx::query!(
88-
"UPDATE players SET score = coalesce(score_of_player($1), 0) WHERE id = $1 RETURNING score",
88+
"UPDATE players SET score = score_of_player($1) WHERE id = $1 RETURNING score",
8989
self.id
9090
)
9191
.fetch_one(&mut *connection)

0 commit comments

Comments
 (0)