diff --git a/api/.sqlx/query-2db4e3aaf2b526925f8faa606763bc72d7c526c65a9d1788b2197cb34747a015.json b/api/.sqlx/query-05845fdb39cec480cfa34441c591594827f4b38fa1cc1b81d2974fc8141fa7b5.json similarity index 77% rename from api/.sqlx/query-2db4e3aaf2b526925f8faa606763bc72d7c526c65a9d1788b2197cb34747a015.json rename to api/.sqlx/query-05845fdb39cec480cfa34441c591594827f4b38fa1cc1b81d2974fc8141fa7b5.json index cdf8918..5127cbf 100644 --- a/api/.sqlx/query-2db4e3aaf2b526925f8faa606763bc72d7c526c65a9d1788b2197cb34747a015.json +++ b/api/.sqlx/query-05845fdb39cec480cfa34441c591594827f4b38fa1cc1b81d2974fc8141fa7b5.json @@ -1,6 +1,6 @@ { "db_name": "PostgreSQL", - "query": "\n SELECT shown.map AS \"map!\", shown.track AS \"track!\", shown.time AS \"time!\",\n CASE WHEN NOT shown.legacy THEN 1 + (\n SELECT count(*) FROM personal_bests faster\n WHERE faster.map_id = shown.map_id AND faster.style = $2 AND faster.track = shown.track\n AND faster.time < shown.time) END AS rank,\n shown.legacy AS \"legacy!\", shown.id AS \"id!\", shown.finished_at\n FROM (\n SELECT DISTINCT ON (both_kinds.map, both_kinds.track) both_kinds.*\n FROM (\n SELECT m.name AS map, pb.map_id, pb.track, pb.time, false AS legacy, pb.run_id AS id,\n pb.finished_at\n FROM personal_bests pb\n JOIN maps m ON m.id = pb.map_id\n WHERE $3 AND pb.player_steamid64 = $1 AND pb.style = $2 AND pb.track = $4\n UNION ALL\n SELECT lm.name, NULL::bigint, $4::smallint, l.time, true, l.id::bigint, l.finished_at\n FROM legacy_personal_bests l\n JOIN legacy_maps lm ON lm.id = l.map_id\n WHERE l.player_steamid64 = $1 AND NOT EXISTS (\n SELECT 1 FROM bans\n WHERE bans.player_steamid64 = $1 AND bans.restrictions & $5 <> 0\n AND bans.lifted_at IS NULL AND (bans.ends_at IS NULL OR bans.ends_at > now())\n )\n ) both_kinds\n WHERE $6::text IS NULL OR (both_kinds.map, both_kinds.track) > ($6, $7::smallint)\n ORDER BY both_kinds.map, both_kinds.track, both_kinds.time, both_kinds.legacy\n LIMIT $8\n ) shown\n ORDER BY shown.map, shown.track\n ", + "query": "\n SELECT shown.map AS \"map!\", shown.track AS \"track!\", shown.time AS \"time!\",\n CASE WHEN NOT shown.legacy THEN 1 + (\n SELECT count(*) FROM personal_bests faster\n WHERE faster.map_id = shown.map_id AND faster.style = $2 AND faster.track = shown.track\n AND faster.time < shown.time) END AS rank,\n shown.legacy AS \"legacy!\", shown.id AS \"id!\", shown.finished_at\n FROM (\n SELECT DISTINCT ON (both_kinds.map, both_kinds.track) both_kinds.*\n FROM (\n SELECT m.name AS map, pb.map_id, pb.track, pb.time, false AS legacy, pb.run_id AS id,\n pb.finished_at\n FROM personal_bests pb\n JOIN maps m ON m.id = pb.map_id\n WHERE $3 AND pb.player_steamid64 = $1 AND pb.style = $2 AND pb.track = $4\n UNION ALL\n SELECT lm.name, NULL::bigint, $4::smallint, l.time, true, l.id::bigint, l.finished_at\n FROM legacy_personal_bests l\n JOIN legacy_maps lm ON lm.id = l.map_id\n WHERE l.player_steamid64 = $1 AND NOT EXISTS (SELECT 1 FROM records_bans b WHERE b.player_steamid64 = $1)\n ) both_kinds\n WHERE $5::text IS NULL OR (both_kinds.map, both_kinds.track) > ($5, $6::smallint)\n ORDER BY both_kinds.map, both_kinds.track, both_kinds.time, both_kinds.legacy\n LIMIT $7\n ) shown\n ORDER BY shown.map, shown.track\n ", "describe": { "columns": [ { @@ -52,7 +52,6 @@ "Text", "Bool", "Int2", - "Int8", "Text", "Int2", "Int8" @@ -68,5 +67,5 @@ null ] }, - "hash": "2db4e3aaf2b526925f8faa606763bc72d7c526c65a9d1788b2197cb34747a015" + "hash": "05845fdb39cec480cfa34441c591594827f4b38fa1cc1b81d2974fc8141fa7b5" } diff --git a/api/.sqlx/query-03c57545c970a54d832c8c103c7e0cbb6c90ef4c4aa18b0bcfa542fb35e46aec.json b/api/.sqlx/query-07baebf297e50075ed39f8adf4019c2a08cd219ba0591f7a97a746dc84b8d1a2.json similarity index 55% rename from api/.sqlx/query-03c57545c970a54d832c8c103c7e0cbb6c90ef4c4aa18b0bcfa542fb35e46aec.json rename to api/.sqlx/query-07baebf297e50075ed39f8adf4019c2a08cd219ba0591f7a97a746dc84b8d1a2.json index 8fa9229..8f494da 100644 --- a/api/.sqlx/query-03c57545c970a54d832c8c103c7e0cbb6c90ef4c4aa18b0bcfa542fb35e46aec.json +++ b/api/.sqlx/query-07baebf297e50075ed39f8adf4019c2a08cd219ba0591f7a97a746dc84b8d1a2.json @@ -1,6 +1,6 @@ { "db_name": "PostgreSQL", - "query": "SELECT count(DISTINCT map_id) AS \"count!\" FROM legacy_runs", + "query": "SELECT count(*) AS \"count!\" FROM legacy_maps m\n WHERE EXISTS (SELECT 1 FROM legacy_runs r WHERE r.map_id = m.id)", "describe": { "columns": [ { @@ -17,5 +17,5 @@ null ] }, - "hash": "03c57545c970a54d832c8c103c7e0cbb6c90ef4c4aa18b0bcfa542fb35e46aec" + "hash": "07baebf297e50075ed39f8adf4019c2a08cd219ba0591f7a97a746dc84b8d1a2" } diff --git a/api/.sqlx/query-07cad333640d67941225c6a4460ca571d81156888fd6095e31ce97381600e2df.json b/api/.sqlx/query-07cad333640d67941225c6a4460ca571d81156888fd6095e31ce97381600e2df.json new file mode 100644 index 0000000..5f3a590 --- /dev/null +++ b/api/.sqlx/query-07cad333640d67941225c6a4460ca571d81156888fd6095e31ce97381600e2df.json @@ -0,0 +1,14 @@ +{ + "db_name": "PostgreSQL", + "query": "\n WITH tiers AS (\n SELECT DISTINCT ON (map_id, track) map_id, track, tier FROM map_tiers\n WHERE $1 ORDER BY map_id, track, assigned_at DESC\n )\n INSERT INTO standings_boards (map_id, style, track, best, tier)\n SELECT b.map_id, b.style, b.track, b.best, t.tier\n FROM (SELECT map_id, style, track, min(time) AS best FROM personal_bests GROUP BY 1, 2, 3) b\n LEFT JOIN tiers t ON t.map_id = b.map_id AND t.track = b.track\n ", + "describe": { + "columns": [], + "parameters": { + "Left": [ + "Bool" + ] + }, + "nullable": [] + }, + "hash": "07cad333640d67941225c6a4460ca571d81156888fd6095e31ce97381600e2df" +} diff --git a/api/.sqlx/query-4ff782faf8d722d94b1863be1fcc7b2aac94c11e4d44a4efecde4c7edb510138.json b/api/.sqlx/query-0b22cc625fa4cae58f01ba5b2cb375a043e99ee0ecf6912a114c5801eacbcb12.json similarity index 73% rename from api/.sqlx/query-4ff782faf8d722d94b1863be1fcc7b2aac94c11e4d44a4efecde4c7edb510138.json rename to api/.sqlx/query-0b22cc625fa4cae58f01ba5b2cb375a043e99ee0ecf6912a114c5801eacbcb12.json index 7b33a7d..a8dedc1 100644 --- a/api/.sqlx/query-4ff782faf8d722d94b1863be1fcc7b2aac94c11e4d44a4efecde4c7edb510138.json +++ b/api/.sqlx/query-0b22cc625fa4cae58f01ba5b2cb375a043e99ee0ecf6912a114c5801eacbcb12.json @@ -1,6 +1,6 @@ { "db_name": "PostgreSQL", - "query": "\n SELECT style AS \"style!\", track AS \"track!\", rank AS \"rank!\", steamid64::text AS \"steamid64!\",\n name AS \"name!\", time AS \"time!\", run_id AS \"run_id!\", finished_at AS \"finished_at!\",\n stored AS \"has_replay!\", points AS \"points!\"\n FROM (\n SELECT pb.style, pb.track, pb.time, pb.run_id, pb.points, p.steamid64, p.name, r.finished_at,\n rp.stored_key IS NOT NULL AS stored,\n rank() OVER (PARTITION BY pb.style, pb.track ORDER BY pb.time) AS rank\n FROM personal_bests pb\n JOIN maps m ON m.id = pb.map_id\n JOIN players p ON p.steamid64 = pb.player_steamid64\n JOIN runs r ON r.id = pb.run_id\n LEFT JOIN replays rp ON rp.run_id = pb.run_id\n WHERE m.name = $1\n ) ranked\n WHERE rank <= $2\n ORDER BY style, track, rank, finished_at\n ", + "query": "\n SELECT style AS \"style!\", track AS \"track!\", rank AS \"rank!\", steamid64::text AS \"steamid64!\",\n name AS \"name!\", time AS \"time!\", run_id AS \"run_id!\", finished_at AS \"finished_at!\",\n stored AS \"has_replay!\", points AS \"points!\"\n FROM (\n SELECT pb.style, pb.track, pb.time, pb.run_id,\n personal_best_points(pb.map_id, pb.style, pb.track, pb.time) AS points,\n p.steamid64, p.name, r.finished_at,\n rp.stored_key IS NOT NULL AS stored,\n rank() OVER (PARTITION BY pb.style, pb.track ORDER BY pb.time) AS rank\n FROM personal_bests pb\n JOIN maps m ON m.id = pb.map_id\n JOIN players p ON p.steamid64 = pb.player_steamid64\n JOIN runs r ON r.id = pb.run_id\n LEFT JOIN replays rp ON rp.run_id = pb.run_id\n WHERE m.name = $1\n ) ranked\n WHERE rank <= $2\n ORDER BY style, track, rank, finished_at\n ", "describe": { "columns": [ { @@ -91,12 +91,7 @@ "ordinal": 9, "name": "points!", "type_info": "Float8", - "origin": { - "Table": { - "table": "personal_bests", - "name": "points" - } - } + "origin": "Expression" } ], "parameters": { @@ -115,8 +110,8 @@ false, false, null, - false + null ] }, - "hash": "4ff782faf8d722d94b1863be1fcc7b2aac94c11e4d44a4efecde4c7edb510138" + "hash": "0b22cc625fa4cae58f01ba5b2cb375a043e99ee0ecf6912a114c5801eacbcb12" } diff --git a/api/.sqlx/query-02235b882665d8454409c6721c6e7a34ffa7328ea8650f68b3ed0bad729c1e34.json b/api/.sqlx/query-0e5fd428486c3ff6a9210aa1813d243795a041590b0a94267c0f6ed61a0f7746.json similarity index 54% rename from api/.sqlx/query-02235b882665d8454409c6721c6e7a34ffa7328ea8650f68b3ed0bad729c1e34.json rename to api/.sqlx/query-0e5fd428486c3ff6a9210aa1813d243795a041590b0a94267c0f6ed61a0f7746.json index 988b044..c7e603b 100644 --- a/api/.sqlx/query-02235b882665d8454409c6721c6e7a34ffa7328ea8650f68b3ed0bad729c1e34.json +++ b/api/.sqlx/query-0e5fd428486c3ff6a9210aa1813d243795a041590b0a94267c0f6ed61a0f7746.json @@ -1,6 +1,6 @@ { "db_name": "PostgreSQL", - "query": "\n WITH listed AS (\n SELECT r.id, r.map_id, r.style, r.track, r.player_steamid64, r.time, r.finished_at, tier.tier\n FROM runs r\n LEFT JOIN LATERAL (SELECT tier FROM map_tiers WHERE map_id = r.map_id AND track = r.track\n ORDER BY assigned_at DESC LIMIT 1) tier ON $7\n WHERE r.invalidated_at IS NULL\n AND (NOT $4 OR r.was_record)\n AND ($1::text IS NULL OR r.style = $1)\n AND ($2::text IS NULL OR ($2 = 'main') = (r.track = 0))\n AND ($3::smallint IS NULL OR tier.tier >= $3)\n AND ($5::timestamptz IS NULL OR (r.finished_at, r.id) < ($5, $6::bigint))\n ORDER BY r.finished_at DESC, r.id DESC\n LIMIT $8 OFFSET $9\n )\n SELECT l.id, l.style, l.track, l.time, l.finished_at, l.tier AS \"tier?\", prior.best AS \"best?\",\n m.name AS map, place.placement AS \"placement?\",\n p.steamid64::text AS \"steamid64!\", p.name, p.avatar_url, p.country_code\n FROM listed l\n CROSS JOIN LATERAL (\n SELECT min(e.time) AS best FROM runs e\n WHERE e.map_id = l.map_id AND e.style = l.style AND e.track = l.track\n AND e.invalidated_at IS NULL AND (e.finished_at, e.id) < (l.finished_at, l.id)\n ) prior\n JOIN maps m ON m.id = l.map_id\n JOIN players p ON p.steamid64 = l.player_steamid64\n LEFT JOIN LATERAL (\n SELECT 1 + count(*) AS placement FROM (\n SELECT 1 FROM runs e\n WHERE e.map_id = l.map_id AND e.style = l.style AND e.track = l.track\n AND e.invalidated_at IS NULL AND (e.finished_at, e.id) < (l.finished_at, l.id)\n AND e.player_steamid64 <> l.player_steamid64\n GROUP BY e.player_steamid64\n HAVING min(e.time) < l.time\n ) faster\n ) place ON NOT EXISTS (\n SELECT 1 FROM runs e\n WHERE e.map_id = l.map_id AND e.style = l.style AND e.track = l.track\n AND e.invalidated_at IS NULL AND (e.finished_at, e.id) < (l.finished_at, l.id)\n AND e.player_steamid64 = l.player_steamid64 AND e.time <= l.time\n )\n ORDER BY l.finished_at DESC, l.id DESC\n ", + "query": "\n WITH listed AS (\n SELECT r.id, r.map_id, r.style, r.track, r.player_steamid64, r.time, r.finished_at, r.was_record,\n tier.tier\n FROM runs r\n LEFT JOIN LATERAL (SELECT tier FROM map_tiers WHERE map_id = r.map_id AND track = r.track\n ORDER BY assigned_at DESC LIMIT 1) tier ON $7\n WHERE r.invalidated_at IS NULL\n AND (NOT $4 OR r.was_record)\n AND ($1::text IS NULL OR r.style = $1)\n AND ($2::text IS NULL OR ($2 = 'main') = (r.track = 0))\n AND ($3::smallint IS NULL OR tier.tier >= $3)\n AND ($5::timestamptz IS NULL OR (r.finished_at, r.id) < ($5, $6::bigint))\n ORDER BY r.finished_at DESC, r.id DESC\n LIMIT $8 OFFSET $9\n )\n SELECT l.id, l.style, l.track, l.time, l.finished_at, l.was_record, l.tier AS \"tier?\", prior.best AS \"best?\",\n m.name AS map, place.placement AS \"placement?\",\n p.steamid64::text AS \"steamid64!\", p.name, p.avatar_url, p.country_code\n FROM listed l\n CROSS JOIN LATERAL (\n SELECT min(e.time) AS best FROM runs e\n WHERE e.map_id = l.map_id AND e.style = l.style AND e.track = l.track\n AND e.invalidated_at IS NULL AND (e.finished_at, e.id) < (l.finished_at, l.id)\n ) prior\n JOIN maps m ON m.id = l.map_id\n JOIN players p ON p.steamid64 = l.player_steamid64\n LEFT JOIN LATERAL (\n SELECT 1 + count(*) AS placement FROM (\n SELECT 1 FROM runs e\n WHERE e.map_id = l.map_id AND e.style = l.style AND e.track = l.track\n AND e.invalidated_at IS NULL AND (e.finished_at, e.id) < (l.finished_at, l.id)\n AND e.player_steamid64 <> l.player_steamid64\n GROUP BY e.player_steamid64\n HAVING min(e.time) < l.time\n ) faster\n ) place ON NOT EXISTS (\n SELECT 1 FROM runs e\n WHERE e.map_id = l.map_id AND e.style = l.style AND e.track = l.track\n AND e.invalidated_at IS NULL AND (e.finished_at, e.id) < (l.finished_at, l.id)\n AND e.player_steamid64 = l.player_steamid64 AND e.time <= l.time\n )\n ORDER BY l.finished_at DESC, l.id DESC\n ", "describe": { "columns": [ { @@ -60,6 +60,17 @@ }, { "ordinal": 5, + "name": "was_record", + "type_info": "Bool", + "origin": { + "Table": { + "table": "runs", + "name": "was_record" + } + } + }, + { + "ordinal": 6, "name": "tier?", "type_info": "Int2", "origin": { @@ -70,13 +81,13 @@ } }, { - "ordinal": 6, + "ordinal": 7, "name": "best?", "type_info": "Float8", "origin": "Expression" }, { - "ordinal": 7, + "ordinal": 8, "name": "map", "type_info": "Text", "origin": { @@ -87,19 +98,19 @@ } }, { - "ordinal": 8, + "ordinal": 9, "name": "placement?", "type_info": "Int8", "origin": "Expression" }, { - "ordinal": 9, + "ordinal": 10, "name": "steamid64!", "type_info": "Text", "origin": "Expression" }, { - "ordinal": 10, + "ordinal": 11, "name": "name", "type_info": "Text", "origin": { @@ -110,7 +121,7 @@ } }, { - "ordinal": 11, + "ordinal": 12, "name": "avatar_url", "type_info": "Text", "origin": { @@ -121,7 +132,7 @@ } }, { - "ordinal": 12, + "ordinal": 13, "name": "country_code", "type_info": "Text", "origin": { @@ -152,6 +163,7 @@ false, false, false, + false, null, false, null, @@ -161,5 +173,5 @@ true ] }, - "hash": "02235b882665d8454409c6721c6e7a34ffa7328ea8650f68b3ed0bad729c1e34" + "hash": "0e5fd428486c3ff6a9210aa1813d243795a041590b0a94267c0f6ed61a0f7746" } diff --git a/api/.sqlx/query-a927189daaef7d0749a7006110d97de034bfcee1fa1a824082cb0bc257bdc239.json b/api/.sqlx/query-0eba6af91c197b4a7c14d17531e8c815bfb0fed762575ba2069fee0dfe790f9c.json similarity index 64% rename from api/.sqlx/query-a927189daaef7d0749a7006110d97de034bfcee1fa1a824082cb0bc257bdc239.json rename to api/.sqlx/query-0eba6af91c197b4a7c14d17531e8c815bfb0fed762575ba2069fee0dfe790f9c.json index 953969e..362f45f 100644 --- a/api/.sqlx/query-a927189daaef7d0749a7006110d97de034bfcee1fa1a824082cb0bc257bdc239.json +++ b/api/.sqlx/query-0eba6af91c197b4a7c14d17531e8c815bfb0fed762575ba2069fee0dfe790f9c.json @@ -1,6 +1,6 @@ { "db_name": "PostgreSQL", - "query": "\n SELECT r.id, m.name AS map, r.style, r.track, r.time, r.sync, r.strafes, r.jumps, r.finished_at,\n r.player_steamid64::text AS \"steamid64!\", p.name AS player, s.name AS server_name,\n (SELECT st->>'name' FROM rulesets, jsonb_array_elements(document->'styles') st\n WHERE st->>'key' = r.style ORDER BY version DESC LIMIT 1) AS style_name,\n (SELECT min(o.time) FROM runs o\n WHERE o.map_id = r.map_id AND o.style = r.style AND o.track = r.track\n AND o.invalidated_at IS NULL AND o.id <> r.id AND o.finished_at <= r.finished_at)\n AS previous_record,\n (SELECT min(o.time) FROM runs o\n WHERE o.map_id = r.map_id AND o.style = r.style AND o.track = r.track\n AND o.player_steamid64 = r.player_steamid64\n AND o.invalidated_at IS NULL AND o.id <> r.id AND o.finished_at <= r.finished_at)\n AS previous_personal_best,\n (SELECT tier FROM map_tiers WHERE map_id = r.map_id AND track = r.track\n ORDER BY assigned_at DESC LIMIT 1) AS tier,\n (SELECT key FROM map_images WHERE map_id = r.map_id AND position = 0) AS image_key,\n (SELECT host(h.ip) || ':' || h.port FROM server_heartbeats h\n JOIN server_listings l ON l.ip = h.ip AND l.port = h.port\n WHERE h.server_id = s.id ORDER BY h.received_at DESC LIMIT 1) AS server_address\n FROM unnest($1::bigint[]) WITH ORDINALITY AS wanted (run_id, place)\n JOIN runs r ON r.id = wanted.run_id\n JOIN maps m ON m.id = r.map_id\n JOIN players p ON p.steamid64 = r.player_steamid64\n JOIN servers s ON s.id = r.server_id\n ORDER BY wanted.place\n ", + "query": "\n SELECT r.id, m.name AS map, r.style, r.track, r.time, r.sync, r.strafes, r.jumps, r.finished_at, r.was_record,\n r.player_steamid64::text AS \"steamid64!\", p.name AS player, s.name AS server_name,\n (SELECT st->>'name' FROM rulesets, jsonb_array_elements(document->'styles') st\n WHERE st->>'key' = r.style ORDER BY version DESC LIMIT 1) AS style_name,\n (SELECT min(o.time) FROM runs o\n WHERE o.map_id = r.map_id AND o.style = r.style AND o.track = r.track\n AND o.invalidated_at IS NULL AND (o.finished_at, o.id) < (r.finished_at, r.id))\n AS previous_record,\n (SELECT min(o.time) FROM runs o\n WHERE o.map_id = r.map_id AND o.style = r.style AND o.track = r.track\n AND o.player_steamid64 = r.player_steamid64\n AND o.invalidated_at IS NULL AND (o.finished_at, o.id) < (r.finished_at, r.id))\n AS previous_personal_best,\n (SELECT tier FROM map_tiers WHERE map_id = r.map_id AND track = r.track\n ORDER BY assigned_at DESC LIMIT 1) AS tier,\n (SELECT key FROM map_images WHERE map_id = r.map_id AND position = 0) AS image_key,\n (SELECT host(h.ip) || ':' || h.port FROM server_heartbeats h\n JOIN server_listings l ON l.ip = h.ip AND l.port = h.port\n WHERE h.server_id = s.id ORDER BY h.received_at DESC LIMIT 1) AS server_address\n FROM unnest($1::bigint[]) WITH ORDINALITY AS wanted (run_id, place)\n JOIN runs r ON r.id = wanted.run_id\n JOIN maps m ON m.id = r.map_id\n JOIN players p ON p.steamid64 = r.player_steamid64\n JOIN servers s ON s.id = r.server_id\n ORDER BY wanted.place\n ", "describe": { "columns": [ { @@ -104,12 +104,23 @@ }, { "ordinal": 9, + "name": "was_record", + "type_info": "Bool", + "origin": { + "Table": { + "table": "runs", + "name": "was_record" + } + } + }, + { + "ordinal": 10, "name": "steamid64!", "type_info": "Text", "origin": "Expression" }, { - "ordinal": 10, + "ordinal": 11, "name": "player", "type_info": "Text", "origin": { @@ -120,7 +131,7 @@ } }, { - "ordinal": 11, + "ordinal": 12, "name": "server_name", "type_info": "Text", "origin": { @@ -131,37 +142,37 @@ } }, { - "ordinal": 12, + "ordinal": 13, "name": "style_name", "type_info": "Text", "origin": "Expression" }, { - "ordinal": 13, + "ordinal": 14, "name": "previous_record", "type_info": "Float8", "origin": "Expression" }, { - "ordinal": 14, + "ordinal": 15, "name": "previous_personal_best", "type_info": "Float8", "origin": "Expression" }, { - "ordinal": 15, + "ordinal": 16, "name": "tier", "type_info": "Int2", "origin": "Expression" }, { - "ordinal": 16, + "ordinal": 17, "name": "image_key", "type_info": "Text", "origin": "Expression" }, { - "ordinal": 17, + "ordinal": 18, "name": "server_address", "type_info": "Text", "origin": "Expression" @@ -182,6 +193,7 @@ false, false, false, + false, null, false, false, @@ -193,5 +205,5 @@ null ] }, - "hash": "a927189daaef7d0749a7006110d97de034bfcee1fa1a824082cb0bc257bdc239" + "hash": "0eba6af91c197b4a7c14d17531e8c815bfb0fed762575ba2069fee0dfe790f9c" } diff --git a/api/.sqlx/query-d5e042f4114740ee6a5981caf1849873d6643a3b311ac96263a9eca3e5ace057.json b/api/.sqlx/query-17283e78f20e0bf0c5cb6e3c7a96d895539b7803969bf3e33f0a9f2b9c5f6184.json similarity index 79% rename from api/.sqlx/query-d5e042f4114740ee6a5981caf1849873d6643a3b311ac96263a9eca3e5ace057.json rename to api/.sqlx/query-17283e78f20e0bf0c5cb6e3c7a96d895539b7803969bf3e33f0a9f2b9c5f6184.json index 083e8bf..92adbba 100644 --- a/api/.sqlx/query-d5e042f4114740ee6a5981caf1849873d6643a3b311ac96263a9eca3e5ace057.json +++ b/api/.sqlx/query-17283e78f20e0bf0c5cb6e3c7a96d895539b7803969bf3e33f0a9f2b9c5f6184.json @@ -1,6 +1,6 @@ { "db_name": "PostgreSQL", - "query": "\n SELECT * FROM (\n SELECT b.id, b.restrictions, b.reason, b.banned_at, b.ends_at, b.lifted_at, b.lift_reason,\n b.lifted_at IS NULL AND (b.ends_at IS NULL OR b.ends_at > now()) AS \"active!\",\n p.steamid64::text AS \"steamid64!\", p.name, p.avatar_url, p.country_code,\n s.steamid64::text AS \"by_steamid64?\", s.name AS \"by_name?\",\n s.avatar_url AS by_avatar_url, s.country_code AS by_country_code,\n l.steamid64::text AS \"lifter_steamid64?\", l.name AS \"lifter_name?\",\n l.avatar_url AS lifter_avatar_url, l.country_code AS lifter_country_code\n FROM bans b\n JOIN players p ON p.steamid64 = b.player_steamid64\n LEFT JOIN players s ON s.steamid64 = b.banned_by\n LEFT JOIN players l ON l.steamid64 = b.lifted_by\n WHERE $1::bigint IS NULL OR b.player_steamid64 = $1\n ) bans\n WHERE $2::bool IS NULL OR \"active!\" = $2\n -- Active bans newest first; ended ones by when they ended.\n ORDER BY CASE WHEN \"active!\" THEN banned_at ELSE coalesce(lifted_at, ends_at) END DESC, id DESC\n LIMIT $3\n ", + "query": "\n SELECT * FROM (\n SELECT b.id, b.restrictions, b.reason, b.banned_at, b.ends_at, b.lifted_at, b.lift_reason,\n EXISTS (SELECT 1 FROM active_bans a WHERE a.id = b.id) AS \"active!\",\n p.steamid64::text AS \"steamid64!\", p.name, p.avatar_url, p.country_code,\n s.steamid64::text AS \"by_steamid64?\", s.name AS \"by_name?\",\n s.avatar_url AS by_avatar_url, s.country_code AS by_country_code,\n l.steamid64::text AS \"lifter_steamid64?\", l.name AS \"lifter_name?\",\n l.avatar_url AS lifter_avatar_url, l.country_code AS lifter_country_code\n FROM bans b\n JOIN players p ON p.steamid64 = b.player_steamid64\n LEFT JOIN players s ON s.steamid64 = b.banned_by\n LEFT JOIN players l ON l.steamid64 = b.lifted_by\n WHERE $1::bigint IS NULL OR b.player_steamid64 = $1\n ) bans\n WHERE $2::bool IS NULL OR \"active!\" = $2\n -- Active bans newest first; ended ones by when they ended.\n ORDER BY CASE WHEN \"active!\" THEN banned_at ELSE coalesce(lifted_at, ends_at) END DESC, id DESC\n LIMIT $3\n ", "describe": { "columns": [ { @@ -234,5 +234,5 @@ true ] }, - "hash": "d5e042f4114740ee6a5981caf1849873d6643a3b311ac96263a9eca3e5ace057" + "hash": "17283e78f20e0bf0c5cb6e3c7a96d895539b7803969bf3e33f0a9f2b9c5f6184" } diff --git a/api/.sqlx/query-1e4e08bfbbea80f5c47a62de5c4720d53ffff4ec25d0615eda0c7bf3af7681a4.json b/api/.sqlx/query-1e4e08bfbbea80f5c47a62de5c4720d53ffff4ec25d0615eda0c7bf3af7681a4.json new file mode 100644 index 0000000..58f48a4 --- /dev/null +++ b/api/.sqlx/query-1e4e08bfbbea80f5c47a62de5c4720d53ffff4ec25d0615eda0c7bf3af7681a4.json @@ -0,0 +1,14 @@ +{ + "db_name": "PostgreSQL", + "query": "\n WITH boards AS (SELECT DISTINCT map_id, style, track FROM runs WHERE id = ANY($1)),\n -- Each board's best time, from the `personal_bests_board` index: its holders are those who have it.\n bests AS (\n SELECT b.*, (SELECT min(pb.time) FROM personal_bests pb\n WHERE pb.map_id = b.map_id AND pb.style = b.style AND pb.track = b.track) AS time\n FROM boards b\n )\n INSERT INTO world_record_holders (player_steamid64, map_id, style, track)\n SELECT pb.player_steamid64, pb.map_id, pb.style, pb.track\n FROM bests b\n JOIN personal_bests pb ON pb.map_id = b.map_id AND pb.style = b.style AND pb.track = b.track\n AND pb.time = b.time\n ON CONFLICT DO NOTHING\n ", + "describe": { + "columns": [], + "parameters": { + "Left": [ + "Int8Array" + ] + }, + "nullable": [] + }, + "hash": "1e4e08bfbbea80f5c47a62de5c4720d53ffff4ec25d0615eda0c7bf3af7681a4" +} diff --git a/api/.sqlx/query-1ff6e1c4590fef89193f8bc106d6479dfbdc3c6c685fff0e13a8997e8aaea311.json b/api/.sqlx/query-1ff6e1c4590fef89193f8bc106d6479dfbdc3c6c685fff0e13a8997e8aaea311.json deleted file mode 100644 index a2bcc77..0000000 --- a/api/.sqlx/query-1ff6e1c4590fef89193f8bc106d6479dfbdc3c6c685fff0e13a8997e8aaea311.json +++ /dev/null @@ -1,16 +0,0 @@ -{ - "db_name": "PostgreSQL", - "query": "\n WITH tracks AS (\n SELECT map_id, track FROM runs WHERE id = ANY($1)\n UNION SELECT * FROM unnest($2::int8[], $3::int2[])\n ), affected AS (\n SELECT style, track > 0 AS bonus, player_steamid64 FROM personal_bests\n WHERE (map_id, track) IN (SELECT map_id, track FROM tracks)\n UNION SELECT style, track > 0, player_steamid64 FROM runs WHERE id = ANY($1)\n )\n DELETE FROM standings s USING affected a\n WHERE s.style = a.style AND s.bonus = a.bonus AND s.player_steamid64 = a.player_steamid64\n ", - "describe": { - "columns": [], - "parameters": { - "Left": [ - "Int8Array", - "Int8Array", - "Int2Array" - ] - }, - "nullable": [] - }, - "hash": "1ff6e1c4590fef89193f8bc106d6479dfbdc3c6c685fff0e13a8997e8aaea311" -} diff --git a/api/.sqlx/query-2078223a4e69921fd8ce58c6468d49a16fa5cfe07f43fa304dffc58f144245fa.json b/api/.sqlx/query-2078223a4e69921fd8ce58c6468d49a16fa5cfe07f43fa304dffc58f144245fa.json new file mode 100644 index 0000000..9d35a6f --- /dev/null +++ b/api/.sqlx/query-2078223a4e69921fd8ce58c6468d49a16fa5cfe07f43fa304dffc58f144245fa.json @@ -0,0 +1,12 @@ +{ + "db_name": "PostgreSQL", + "query": "DELETE FROM standings_boards", + "describe": { + "columns": [], + "parameters": { + "Left": [] + }, + "nullable": [] + }, + "hash": "2078223a4e69921fd8ce58c6468d49a16fa5cfe07f43fa304dffc58f144245fa" +} diff --git a/api/.sqlx/query-24c2857962550fd2c47dc0995babe98f508223b78aff1409d65c1563974b411c.json b/api/.sqlx/query-24c2857962550fd2c47dc0995babe98f508223b78aff1409d65c1563974b411c.json deleted file mode 100644 index f079241..0000000 --- a/api/.sqlx/query-24c2857962550fd2c47dc0995babe98f508223b78aff1409d65c1563974b411c.json +++ /dev/null @@ -1,14 +0,0 @@ -{ - "db_name": "PostgreSQL", - "query": "\n WITH boards AS (SELECT DISTINCT map_id, style, track FROM runs WHERE id = ANY($1))\n INSERT INTO world_record_holders (player_steamid64, map_id, style, track)\n SELECT pb.player_steamid64, pb.map_id, pb.style, pb.track\n FROM personal_bests pb\n JOIN boards b ON b.map_id = pb.map_id AND b.style = pb.style AND b.track = pb.track\n WHERE NOT EXISTS (\n SELECT 1 FROM personal_bests faster\n WHERE faster.map_id = pb.map_id AND faster.style = pb.style AND faster.track = pb.track\n AND faster.time < pb.time\n )\n ON CONFLICT DO NOTHING\n ", - "describe": { - "columns": [], - "parameters": { - "Left": [ - "Int8Array" - ] - }, - "nullable": [] - }, - "hash": "24c2857962550fd2c47dc0995babe98f508223b78aff1409d65c1563974b411c" -} diff --git a/api/.sqlx/query-2cf5a66c637ecd69e2b53a138cd71206887f3b5484d6d99a7da021d19d59c9b9.json b/api/.sqlx/query-2cf5a66c637ecd69e2b53a138cd71206887f3b5484d6d99a7da021d19d59c9b9.json new file mode 100644 index 0000000..e5d762a --- /dev/null +++ b/api/.sqlx/query-2cf5a66c637ecd69e2b53a138cd71206887f3b5484d6d99a7da021d19d59c9b9.json @@ -0,0 +1,21 @@ +{ + "db_name": "PostgreSQL", + "query": "WITH taken AS (DELETE FROM daily_totals_stale RETURNING day) SELECT min(day) FROM taken", + "describe": { + "columns": [ + { + "ordinal": 0, + "name": "min", + "type_info": "Date", + "origin": "Expression" + } + ], + "parameters": { + "Left": [] + }, + "nullable": [ + null + ] + }, + "hash": "2cf5a66c637ecd69e2b53a138cd71206887f3b5484d6d99a7da021d19d59c9b9" +} diff --git a/api/.sqlx/query-2fff32fac3b22df3a45fa70fb5a31b9d7a93405e1471586435be0c3d1ac91562.json b/api/.sqlx/query-2fff32fac3b22df3a45fa70fb5a31b9d7a93405e1471586435be0c3d1ac91562.json new file mode 100644 index 0000000..d6061f8 --- /dev/null +++ b/api/.sqlx/query-2fff32fac3b22df3a45fa70fb5a31b9d7a93405e1471586435be0c3d1ac91562.json @@ -0,0 +1,19 @@ +{ + "db_name": "PostgreSQL", + "query": "\n WITH affected AS (SELECT DISTINCT style, track > 0 AS bonus, player_steamid64 FROM runs WHERE id = ANY($1))\n INSERT INTO standings (style, bonus, player_steamid64, points, completed)\n SELECT a.style, a.bonus, a.player_steamid64, totals.points, totals.completed\n FROM affected a\n -- Each player's own personal bests, through the primary key: joined on the list instead, the planner\n -- can't tell how short it is and reads every personal best.\n CROSS JOIN LATERAL (\n SELECT sum(coalesce(points_for(b.tier, pb.time, b.best, pb.track > 0, $2, $3, $4, $5, $6), 0))\n AS points,\n count(*) AS completed\n FROM personal_bests pb\n LEFT JOIN standings_boards b ON b.map_id = pb.map_id AND b.style = pb.style AND b.track = pb.track\n WHERE pb.player_steamid64 = a.player_steamid64 AND pb.style = a.style AND (pb.track > 0) = a.bonus\n ) totals\n WHERE totals.completed > 0\n ", + "describe": { + "columns": [], + "parameters": { + "Left": [ + "Int8Array", + "Float8", + "Float8", + "Float8", + "Float8", + "Float8" + ] + }, + "nullable": [] + }, + "hash": "2fff32fac3b22df3a45fa70fb5a31b9d7a93405e1471586435be0c3d1ac91562" +} diff --git a/api/.sqlx/query-91d8048279050f09856dc16c8b5ebd9ba3f0e7522af183f305a45462831d1ad3.json b/api/.sqlx/query-328b92b82280713a4cb14d7e8a819c7a909025aeb1a06c9daf823f11baff2a1c.json similarity index 57% rename from api/.sqlx/query-91d8048279050f09856dc16c8b5ebd9ba3f0e7522af183f305a45462831d1ad3.json rename to api/.sqlx/query-328b92b82280713a4cb14d7e8a819c7a909025aeb1a06c9daf823f11baff2a1c.json index 7855e58..b0d4ba2 100644 --- a/api/.sqlx/query-91d8048279050f09856dc16c8b5ebd9ba3f0e7522af183f305a45462831d1ad3.json +++ b/api/.sqlx/query-328b92b82280713a4cb14d7e8a819c7a909025aeb1a06c9daf823f11baff2a1c.json @@ -1,6 +1,6 @@ { "db_name": "PostgreSQL", - "query": "\n WITH ranked AS (\n SELECT s.player_steamid64, s.points, s.completed,\n CASE WHEN $3 THEN rank() OVER (ORDER BY s.points DESC)\n ELSE rank() OVER (ORDER BY s.completed DESC) END AS rank,\n row_number() OVER (ORDER BY CASE WHEN $3 THEN s.points ELSE s.completed END DESC,\n lower(p.name)) AS position\n FROM standings s JOIN players p ON p.steamid64 = s.player_steamid64\n WHERE s.style = $1 AND s.bonus = $2 AND (NOT $3 OR s.points > 0)\n ), page AS (\n SELECT * FROM ranked WHERE position > $4 ORDER BY position LIMIT $5\n )\n -- The total comes in every row, and alone in one row for a page past the end.\n SELECT (SELECT count(*) FROM ranked) AS \"total!\", page.rank AS \"rank?\",\n round(page.points)::int8 AS \"points?\", page.completed::int8 AS \"completed?\",\n p.steamid64::text AS \"steamid64?\", p.name AS \"name?\", p.avatar_url AS \"avatar_url?\",\n p.country_code AS \"country_code?\", records.count AS \"world_records?\"\n FROM (SELECT 1) one\n LEFT JOIN page ON true\n LEFT JOIN players p ON p.steamid64 = page.player_steamid64\n LEFT JOIN LATERAL (\n SELECT count(*) FROM world_record_holders h\n WHERE h.player_steamid64 = page.player_steamid64 AND h.style = $1 AND (h.track > 0) = $2\n ) records ON true\n ORDER BY page.position\n ", + "query": "\n WITH ranked AS (\n SELECT s.player_steamid64, s.points, s.completed,\n CASE WHEN $3 THEN rank() OVER (ORDER BY s.points DESC)\n ELSE rank() OVER (ORDER BY s.completed DESC) END AS rank,\n -- Down to the SteamID, so equal points and names keep one order from page to page.\n row_number() OVER (ORDER BY CASE WHEN $3 THEN s.points ELSE s.completed END DESC,\n lower(p.name), s.player_steamid64) AS position\n FROM standings s JOIN players p ON p.steamid64 = s.player_steamid64\n WHERE s.style = $1 AND s.bonus = $2 AND (NOT $3 OR s.points > 0)\n ), page AS (\n SELECT * FROM ranked WHERE position > $4 ORDER BY position LIMIT $5\n )\n -- The total comes in every row, and alone in one row for a page past the end.\n SELECT (SELECT count(*) FROM ranked) AS \"total!\", page.rank AS \"rank?\",\n round(page.points)::int8 AS \"points?\", page.completed::int8 AS \"completed?\",\n p.steamid64::text AS \"steamid64?\", p.name AS \"name?\", p.avatar_url AS \"avatar_url?\",\n p.country_code AS \"country_code?\", records.count AS \"world_records?\"\n FROM (SELECT 1) one\n LEFT JOIN page ON true\n LEFT JOIN players p ON p.steamid64 = page.player_steamid64\n LEFT JOIN LATERAL (\n SELECT count(*) FROM world_record_holders h\n WHERE h.player_steamid64 = page.player_steamid64 AND h.style = $1 AND (h.track > 0) = $2\n ) records ON true\n ORDER BY page.position\n ", "describe": { "columns": [ { @@ -94,5 +94,5 @@ null ] }, - "hash": "91d8048279050f09856dc16c8b5ebd9ba3f0e7522af183f305a45462831d1ad3" + "hash": "328b92b82280713a4cb14d7e8a819c7a909025aeb1a06c9daf823f11baff2a1c" } diff --git a/api/.sqlx/query-63e1aa6c756d10a26ba3778ff61b4c462906cd062a3359190ee82129768d319b.json b/api/.sqlx/query-36c69e27d86b279df06be1d6cf260c2134c7f53622cf1f78008908a74598e1cb.json similarity index 70% rename from api/.sqlx/query-63e1aa6c756d10a26ba3778ff61b4c462906cd062a3359190ee82129768d319b.json rename to api/.sqlx/query-36c69e27d86b279df06be1d6cf260c2134c7f53622cf1f78008908a74598e1cb.json index caccc57..eb7892a 100644 --- a/api/.sqlx/query-63e1aa6c756d10a26ba3778ff61b4c462906cd062a3359190ee82129768d319b.json +++ b/api/.sqlx/query-36c69e27d86b279df06be1d6cf260c2134c7f53622cf1f78008908a74598e1cb.json @@ -1,6 +1,6 @@ { "db_name": "PostgreSQL", - "query": "\n WITH banned AS (\n SELECT player_steamid64 FROM bans\n WHERE restrictions & $2 <> 0 AND lifted_at IS NULL AND (ends_at IS NULL OR ends_at > now())\n )\n SELECT l.id, lm.name AS map, l.player_name, l.time, l.jumps, l.strafes, l.sync, l.finished_at,\n l.server_hostname,\n l.player_steamid64::text AS \"steamid64!\", coalesce(p.name, lp.name) AS \"name!\",\n coalesce(p.avatar_url, lp.avatar_url) AS avatar_url,\n coalesce(p.country_code, lp.country_code) AS country_code,\n (SELECT min(o.time) FROM legacy_runs o\n WHERE o.map_id = l.map_id\n AND NOT EXISTS (SELECT 1 FROM banned b WHERE b.player_steamid64 = o.player_steamid64)\n ) AS \"record_time!\"\n FROM legacy_runs l\n JOIN legacy_maps lm ON lm.id = l.map_id\n JOIN legacy_players lp ON lp.steamid64 = l.player_steamid64\n LEFT JOIN players p ON p.steamid64 = l.player_steamid64\n WHERE l.id = $1\n AND NOT EXISTS (SELECT 1 FROM banned b WHERE b.player_steamid64 = l.player_steamid64)\n ", + "query": "\n SELECT l.id, lm.name AS map, l.player_name, l.time, l.jumps, l.strafes, l.sync, l.finished_at,\n l.server_hostname,\n l.player_steamid64::text AS \"steamid64!\", coalesce(p.name, lp.name) AS \"name!\",\n coalesce(p.avatar_url, lp.avatar_url) AS avatar_url,\n coalesce(p.country_code, lp.country_code) AS country_code,\n (SELECT min(o.time) FROM legacy_runs o\n WHERE o.map_id = l.map_id\n AND NOT EXISTS (SELECT 1 FROM records_bans b WHERE b.player_steamid64 = o.player_steamid64)\n ) AS \"record_time!\"\n FROM legacy_runs l\n JOIN legacy_maps lm ON lm.id = l.map_id\n JOIN legacy_players lp ON lp.steamid64 = l.player_steamid64\n LEFT JOIN players p ON p.steamid64 = l.player_steamid64\n WHERE l.id = $1\n AND NOT EXISTS (SELECT 1 FROM records_bans b WHERE b.player_steamid64 = l.player_steamid64)\n ", "describe": { "columns": [ { @@ -135,8 +135,7 @@ ], "parameters": { "Left": [ - "Int4", - "Int8" + "Int4" ] }, "nullable": [ @@ -156,5 +155,5 @@ null ] }, - "hash": "63e1aa6c756d10a26ba3778ff61b4c462906cd062a3359190ee82129768d319b" + "hash": "36c69e27d86b279df06be1d6cf260c2134c7f53622cf1f78008908a74598e1cb" } diff --git a/api/.sqlx/query-38efe06275848926459e99554a19f3a5e893ccd9bbd1f40e026ccd5fe77add2c.json b/api/.sqlx/query-38efe06275848926459e99554a19f3a5e893ccd9bbd1f40e026ccd5fe77add2c.json deleted file mode 100644 index 04f7ec6..0000000 --- a/api/.sqlx/query-38efe06275848926459e99554a19f3a5e893ccd9bbd1f40e026ccd5fe77add2c.json +++ /dev/null @@ -1,22 +0,0 @@ -{ - "db_name": "PostgreSQL", - "query": "\n WITH tracks AS (\n SELECT map_id, track FROM runs WHERE id = ANY($1)\n UNION SELECT * FROM unnest($2::int8[], $3::int2[])\n ), tiers AS (\n SELECT DISTINCT ON (map_id, track) map_id, track, tier FROM map_tiers\n WHERE $4 AND (map_id, track) IN (SELECT map_id, track FROM tracks)\n ORDER BY map_id, track, assigned_at DESC\n ), scored AS (\n SELECT pb.player_steamid64, pb.map_id, pb.style, pb.track,\n coalesce(points_for(t.tier, pb.time,\n min(pb.time) OVER (PARTITION BY pb.map_id, pb.style, pb.track),\n pb.track > 0, $5, $6, $7, $8, $9), 0) AS points\n FROM personal_bests pb\n LEFT JOIN tiers t ON t.map_id = pb.map_id AND t.track = pb.track\n WHERE (pb.map_id, pb.track) IN (SELECT map_id, track FROM tracks)\n )\n UPDATE personal_bests pb SET points = scored.points FROM scored\n WHERE pb.player_steamid64 = scored.player_steamid64 AND pb.map_id = scored.map_id\n AND pb.style = scored.style AND pb.track = scored.track AND pb.points <> scored.points\n ", - "describe": { - "columns": [], - "parameters": { - "Left": [ - "Int8Array", - "Int8Array", - "Int2Array", - "Bool", - "Float8", - "Float8", - "Float8", - "Float8", - "Float8" - ] - }, - "nullable": [] - }, - "hash": "38efe06275848926459e99554a19f3a5e893ccd9bbd1f40e026ccd5fe77add2c" -} diff --git a/api/.sqlx/query-77c64d990e1161cff3b0e37583b1b0134030a11ed72f84c3e42a65fb82cbc4a0.json b/api/.sqlx/query-3bd2ea0a2ff5134a4e5b49f958c689a146cc33f8a50e22a9774babf6035601c5.json similarity index 51% rename from api/.sqlx/query-77c64d990e1161cff3b0e37583b1b0134030a11ed72f84c3e42a65fb82cbc4a0.json rename to api/.sqlx/query-3bd2ea0a2ff5134a4e5b49f958c689a146cc33f8a50e22a9774babf6035601c5.json index 4fac3bc..24e40f4 100644 --- a/api/.sqlx/query-77c64d990e1161cff3b0e37583b1b0134030a11ed72f84c3e42a65fb82cbc4a0.json +++ b/api/.sqlx/query-3bd2ea0a2ff5134a4e5b49f958c689a146cc33f8a50e22a9774babf6035601c5.json @@ -1,6 +1,6 @@ { "db_name": "PostgreSQL", - "query": "\n WITH board AS (\n SELECT DISTINCT ON (player) * FROM (\n SELECT false AS legacy, pb.player_steamid64 AS player, pb.time, r.finished_at,\n pb.run_id AS id, r.jumps, r.strafes, r.sync, pb.points, rp.stored_key IS NOT NULL AS stored\n FROM personal_bests pb\n JOIN runs r ON r.id = pb.run_id\n LEFT JOIN replays rp ON rp.run_id = pb.run_id\n WHERE $3 AND pb.map_id = $1 AND pb.style = $10 AND pb.track = $11\n UNION ALL\n SELECT true, l.player_steamid64, l.time, l.finished_at, l.id::bigint, l.jumps, l.strafes, l.sync, 0, false\n FROM legacy_personal_bests l\n -- By ID, not a join on name, so the view picks bests on this map alone, not across all runs.\n WHERE l.map_id = (SELECT id FROM legacy_maps WHERE name = $2) AND NOT EXISTS (\n SELECT 1 FROM bans\n WHERE bans.player_steamid64 = l.player_steamid64 AND bans.restrictions & $4 <> 0\n AND bans.lifted_at IS NULL AND (bans.ends_at IS NULL OR bans.ends_at > now())\n )\n ) both_kinds\n -- Each player's fastest run of either kind; on equal times, their 2.0 run.\n ORDER BY player, time, legacy\n ), ranked AS (\n SELECT *, rank() OVER (ORDER BY time) AS rank, count(*) OVER () AS total,\n min(time) OVER () AS fastest, coalesce(finished_at, 'infinity') AS sort_at\n FROM board\n )\n SELECT ranked.legacy AS \"legacy!\", ranked.time AS \"time!\", ranked.finished_at, ranked.id AS \"id!\",\n ranked.jumps AS \"jumps!\", ranked.strafes AS \"strafes!\", ranked.sync AS \"sync!\",\n ranked.points AS \"points!\", ranked.stored AS \"stored!\",\n ranked.rank AS \"rank!\", ranked.total AS \"total!\", ranked.fastest AS \"fastest!\",\n ranked.player::text AS \"steamid64!\", coalesce(p.name, lp.name) AS \"name!\",\n coalesce(p.avatar_url, lp.avatar_url) AS avatar_url,\n coalesce(p.country_code, lp.country_code) AS country_code\n FROM ranked\n LEFT JOIN players p ON p.steamid64 = ranked.player\n LEFT JOIN legacy_players lp ON lp.steamid64 = ranked.player\n WHERE $5::float8 IS NULL\n OR (ranked.time, ranked.sort_at, ranked.legacy, ranked.id)\n > ($5, coalesce($6::timestamptz, 'infinity'), $7::bool, $8::bigint)\n ORDER BY ranked.time, ranked.sort_at, ranked.legacy, ranked.id\n LIMIT $9\n ", + "query": "\n WITH board AS (\n SELECT DISTINCT ON (player) * FROM (\n SELECT false AS legacy, pb.player_steamid64 AS player, pb.time, r.finished_at,\n pb.run_id AS id, r.jumps, r.strafes, r.sync,\n personal_best_points(pb.map_id, pb.style, pb.track, pb.time) AS points,\n rp.stored_key IS NOT NULL AS stored\n FROM personal_bests pb\n JOIN runs r ON r.id = pb.run_id\n LEFT JOIN replays rp ON rp.run_id = pb.run_id\n WHERE $3 AND pb.map_id = $1 AND pb.style = $9 AND pb.track = $10\n UNION ALL\n SELECT true, l.player_steamid64, l.time, l.finished_at, l.id::bigint, l.jumps, l.strafes, l.sync, 0, false\n FROM legacy_personal_bests l\n -- By ID, not a join on name, so the view picks bests on this map alone, not across all runs.\n WHERE l.map_id = (SELECT id FROM legacy_maps WHERE name = $2) AND NOT EXISTS (\n SELECT 1 FROM records_bans b WHERE b.player_steamid64 = l.player_steamid64\n )\n ) both_kinds\n -- Each player's fastest run of either kind; on equal times, their 2.0 run.\n ORDER BY player, time, legacy\n ), ranked AS (\n SELECT *, rank() OVER (ORDER BY time) AS rank, count(*) OVER () AS total,\n min(time) OVER () AS fastest, coalesce(finished_at, 'infinity') AS sort_at\n FROM board\n )\n SELECT ranked.legacy AS \"legacy!\", ranked.time AS \"time!\", ranked.finished_at, ranked.id AS \"id!\",\n ranked.jumps AS \"jumps!\", ranked.strafes AS \"strafes!\", ranked.sync AS \"sync!\",\n ranked.points AS \"points!\", ranked.stored AS \"stored!\",\n ranked.rank AS \"rank!\", ranked.total AS \"total!\", ranked.fastest AS \"fastest!\",\n ranked.player::text AS \"steamid64!\", coalesce(p.name, lp.name) AS \"name!\",\n coalesce(p.avatar_url, lp.avatar_url) AS avatar_url,\n coalesce(p.country_code, lp.country_code) AS country_code\n FROM ranked\n LEFT JOIN players p ON p.steamid64 = ranked.player\n LEFT JOIN legacy_players lp ON lp.steamid64 = ranked.player\n WHERE $4::float8 IS NULL\n OR (ranked.time, ranked.sort_at, ranked.legacy, ranked.id)\n > ($4, coalesce($5::timestamptz, 'infinity'), $6::bool, $7::bigint)\n ORDER BY ranked.time, ranked.sort_at, ranked.legacy, ranked.id\n LIMIT $8\n ", "describe": { "columns": [ { @@ -105,7 +105,6 @@ "Int8", "Text", "Bool", - "Int8", "Float8", "Timestamptz", "Bool", @@ -134,5 +133,5 @@ null ] }, - "hash": "77c64d990e1161cff3b0e37583b1b0134030a11ed72f84c3e42a65fb82cbc4a0" + "hash": "3bd2ea0a2ff5134a4e5b49f958c689a146cc33f8a50e22a9774babf6035601c5" } diff --git a/api/.sqlx/query-9b55d192fb24284d21501e42626ab00a1135b7d2785f9a7b803ca7b19a6f7669.json b/api/.sqlx/query-4075ea3370538e687c71b3c907833752a77f04445567af621a78b96cc2fadb20.json similarity index 63% rename from api/.sqlx/query-9b55d192fb24284d21501e42626ab00a1135b7d2785f9a7b803ca7b19a6f7669.json rename to api/.sqlx/query-4075ea3370538e687c71b3c907833752a77f04445567af621a78b96cc2fadb20.json index 2f4c874..83666df 100644 --- a/api/.sqlx/query-9b55d192fb24284d21501e42626ab00a1135b7d2785f9a7b803ca7b19a6f7669.json +++ b/api/.sqlx/query-4075ea3370538e687c71b3c907833752a77f04445567af621a78b96cc2fadb20.json @@ -1,6 +1,6 @@ { "db_name": "PostgreSQL", - "query": "\n WITH RECURSIVE every_board AS (\n -- Each style and track with a personal best on the map, one index probe apiece.\n (SELECT style, track FROM personal_bests WHERE map_id = $1 ORDER BY map_id, style, track LIMIT 1)\n UNION ALL\n SELECT next.style, next.track\n FROM every_board b\n CROSS JOIN LATERAL (\n SELECT style, track FROM personal_bests\n WHERE map_id = $1 AND (style, track) > (b.style, b.track)\n ORDER BY map_id, style, track LIMIT 1\n ) next\n ), boards AS (\n SELECT $2::text AS style, $3::smallint AS track WHERE $2::text IS NOT NULL\n UNION ALL\n SELECT style, track FROM every_board WHERE $2::text IS NULL\n ), page AS (\n SELECT pb.style, pb.track, pb.player_steamid64, pb.time, pb.finished_at, pb.run_id, pb.points,\n rank() OVER board AS page_rank, min(pb.time) OVER board AS first_time\n FROM boards b\n CROSS JOIN LATERAL (\n SELECT * FROM personal_bests pb\n WHERE pb.map_id = $1 AND pb.style = b.style AND pb.track = b.track\n AND ($4::float8 IS NULL\n OR (pb.time, pb.finished_at, pb.run_id) > ($4, $5::timestamptz, $6::bigint))\n ORDER BY pb.time, pb.finished_at, pb.run_id\n LIMIT $7\n ) pb\n WINDOW board AS (PARTITION BY pb.style, pb.track ORDER BY pb.time)\n ), ahead AS MATERIALIZED (\n -- Materialized so each board counts once, not once per entry.\n SELECT b.style, b.track,\n (SELECT count(*) FROM personal_bests x\n WHERE x.map_id = $1 AND x.style = b.style AND x.track = b.track\n AND x.time < b.first_time) AS faster_than_first,\n -- Without a cursor the comparison is null, so nothing comes before the page.\n (SELECT count(*) FROM personal_bests x\n WHERE x.map_id = $1 AND x.style = b.style AND x.track = b.track\n AND (x.time, x.finished_at, x.run_id) <= ($4, $5, $6)) AS before_page\n FROM (SELECT DISTINCT style, track, first_time FROM page) b\n )\n SELECT p.style, p.track, p.time, p.run_id, p.finished_at,\n CASE WHEN p.time = p.first_time THEN a.faster_than_first + 1\n ELSE a.before_page + p.page_rank END AS \"rank!\",\n r.jumps, r.strafes, r.sync, p.points, rp.stored_key IS NOT NULL AS \"stored!\",\n pl.steamid64::text AS \"steamid64!\", pl.name, pl.avatar_url, pl.country_code\n FROM page p\n JOIN ahead a ON a.style = p.style AND a.track = p.track\n JOIN runs r ON r.id = p.run_id\n JOIN players pl ON pl.steamid64 = p.player_steamid64\n LEFT JOIN replays rp ON rp.run_id = p.run_id\n ORDER BY p.style, p.track, p.time, p.finished_at, p.run_id\n ", + "query": "\n WITH RECURSIVE every_board AS (\n -- Each style and track with a personal best on the map, one index probe apiece.\n (SELECT style, track FROM personal_bests WHERE map_id = $1 ORDER BY map_id, style, track LIMIT 1)\n UNION ALL\n SELECT next.style, next.track\n FROM every_board b\n CROSS JOIN LATERAL (\n SELECT style, track FROM personal_bests\n WHERE map_id = $1 AND (style, track) > (b.style, b.track)\n ORDER BY map_id, style, track LIMIT 1\n ) next\n ), boards AS (\n SELECT $2::text AS style, $3::smallint AS track WHERE $2::text IS NOT NULL\n UNION ALL\n SELECT style, track FROM every_board WHERE $2::text IS NULL\n ), page AS (\n SELECT pb.style, pb.track, pb.player_steamid64, pb.time, pb.finished_at, pb.run_id,\n rank() OVER board AS page_rank, min(pb.time) OVER board AS first_time\n FROM boards b\n CROSS JOIN LATERAL (\n SELECT * FROM personal_bests pb\n WHERE pb.map_id = $1 AND pb.style = b.style AND pb.track = b.track\n AND ($4::float8 IS NULL\n OR (pb.time, pb.finished_at, pb.run_id) > ($4, $5::timestamptz, $6::bigint))\n ORDER BY pb.time, pb.finished_at, pb.run_id\n LIMIT $7\n ) pb\n WINDOW board AS (PARTITION BY pb.style, pb.track ORDER BY pb.time)\n ), ahead AS MATERIALIZED (\n -- Materialized so each board counts once, not once per entry.\n SELECT b.style, b.track,\n (SELECT count(*) FROM personal_bests x\n WHERE x.map_id = $1 AND x.style = b.style AND x.track = b.track\n AND x.time < b.first_time) AS faster_than_first,\n -- Without a cursor the comparison is null, so nothing comes before the page.\n (SELECT count(*) FROM personal_bests x\n WHERE x.map_id = $1 AND x.style = b.style AND x.track = b.track\n AND (x.time, x.finished_at, x.run_id) <= ($4, $5, $6)) AS before_page\n FROM (SELECT DISTINCT style, track, first_time FROM page) b\n )\n SELECT p.style, p.track, p.time, p.run_id, p.finished_at,\n CASE WHEN p.time = p.first_time THEN a.faster_than_first + 1\n ELSE a.before_page + p.page_rank END AS \"rank!\",\n r.jumps, r.strafes, r.sync, personal_best_points($1, p.style, p.track, p.time) AS \"points!\",\n rp.stored_key IS NOT NULL AS \"stored!\",\n pl.steamid64::text AS \"steamid64!\", pl.name, pl.avatar_url, pl.country_code\n FROM page p\n JOIN ahead a ON a.style = p.style AND a.track = p.track\n JOIN runs r ON r.id = p.run_id\n JOIN players pl ON pl.steamid64 = p.player_steamid64\n LEFT JOIN replays rp ON rp.run_id = p.run_id\n ORDER BY p.style, p.track, p.time, p.finished_at, p.run_id\n ", "describe": { "columns": [ { @@ -99,14 +99,9 @@ }, { "ordinal": 9, - "name": "points", + "name": "points!", "type_info": "Float8", - "origin": { - "Table": { - "table": "personal_bests", - "name": "points" - } - } + "origin": "Expression" }, { "ordinal": 10, @@ -175,7 +170,7 @@ false, false, false, - false, + null, null, null, false, @@ -183,5 +178,5 @@ true ] }, - "hash": "9b55d192fb24284d21501e42626ab00a1135b7d2785f9a7b803ca7b19a6f7669" + "hash": "4075ea3370538e687c71b3c907833752a77f04445567af621a78b96cc2fadb20" } diff --git a/api/.sqlx/query-41c6777f22d2d65edfdcc4dfc8eca397ca4d226596a6617ab2c1573f375fc8b5.json b/api/.sqlx/query-41c6777f22d2d65edfdcc4dfc8eca397ca4d226596a6617ab2c1573f375fc8b5.json deleted file mode 100644 index 26bb0a5..0000000 --- a/api/.sqlx/query-41c6777f22d2d65edfdcc4dfc8eca397ca4d226596a6617ab2c1573f375fc8b5.json +++ /dev/null @@ -1,15 +0,0 @@ -{ - "db_name": "PostgreSQL", - "query": "UPDATE replays SET shavit_key = $2 WHERE run_id = $1", - "describe": { - "columns": [], - "parameters": { - "Left": [ - "Int8", - "Text" - ] - }, - "nullable": [] - }, - "hash": "41c6777f22d2d65edfdcc4dfc8eca397ca4d226596a6617ab2c1573f375fc8b5" -} diff --git a/api/.sqlx/query-45cd2d65e46893d0779c47dbfc979c25557ae102f4958972de900884f98bbb72.json b/api/.sqlx/query-45cd2d65e46893d0779c47dbfc979c25557ae102f4958972de900884f98bbb72.json new file mode 100644 index 0000000..055a7fb --- /dev/null +++ b/api/.sqlx/query-45cd2d65e46893d0779c47dbfc979c25557ae102f4958972de900884f98bbb72.json @@ -0,0 +1,52 @@ +{ + "db_name": "PostgreSQL", + "query": "SELECT DISTINCT map_id, style, track FROM runs WHERE id = ANY($1)", + "describe": { + "columns": [ + { + "ordinal": 0, + "name": "map_id", + "type_info": "Int8", + "origin": { + "Table": { + "table": "runs", + "name": "map_id" + } + } + }, + { + "ordinal": 1, + "name": "style", + "type_info": "Text", + "origin": { + "Table": { + "table": "runs", + "name": "style" + } + } + }, + { + "ordinal": 2, + "name": "track", + "type_info": "Int2", + "origin": { + "Table": { + "table": "runs", + "name": "track" + } + } + } + ], + "parameters": { + "Left": [ + "Int8Array" + ] + }, + "nullable": [ + false, + false, + false + ] + }, + "hash": "45cd2d65e46893d0779c47dbfc979c25557ae102f4958972de900884f98bbb72" +} diff --git a/api/.sqlx/query-d1289839119b57fc7c924a77555ec2da352d70ff6580d13e41cfef4435f990d1.json b/api/.sqlx/query-5249aee041e2ff089e7d8ffd930921103a2a0abd13130d52c2eb86e1829df8e6.json similarity index 74% rename from api/.sqlx/query-d1289839119b57fc7c924a77555ec2da352d70ff6580d13e41cfef4435f990d1.json rename to api/.sqlx/query-5249aee041e2ff089e7d8ffd930921103a2a0abd13130d52c2eb86e1829df8e6.json index 291682c..a482f7e 100644 --- a/api/.sqlx/query-d1289839119b57fc7c924a77555ec2da352d70ff6580d13e41cfef4435f990d1.json +++ b/api/.sqlx/query-5249aee041e2ff089e7d8ffd930921103a2a0abd13130d52c2eb86e1829df8e6.json @@ -1,6 +1,6 @@ { "db_name": "PostgreSQL", - "query": "\n SELECT r.style, r.track, r.id, r.time, r.finished_at,\n p.steamid64::text AS \"steamid64!\", p.name, p.avatar_url, p.country_code\n FROM (\n SELECT id, style, track, player_steamid64, time, finished_at,\n min(time) OVER (\n PARTITION BY style, track ORDER BY finished_at, id\n ROWS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING\n ) AS best_before\n FROM runs\n WHERE map_id = $1 AND invalidated_at IS NULL\n AND ($2::text IS NULL OR (style = $2 AND track = $3::smallint))\n ) r\n JOIN players p ON p.steamid64 = r.player_steamid64\n WHERE r.best_before IS NULL OR r.time < r.best_before\n ORDER BY r.style, r.track, r.finished_at, r.id\n ", + "query": "\n SELECT r.style, r.track, r.id, r.time, r.finished_at,\n p.steamid64::text AS \"steamid64!\", p.name, p.avatar_url, p.country_code\n FROM runs r\n JOIN players p ON p.steamid64 = r.player_steamid64\n WHERE r.map_id = $1 AND r.was_record\n AND ($2::text IS NULL OR (r.style = $2 AND r.track = $3::smallint))\n ORDER BY r.style, r.track, r.finished_at, r.id\n ", "describe": { "columns": [ { @@ -117,5 +117,5 @@ true ] }, - "hash": "d1289839119b57fc7c924a77555ec2da352d70ff6580d13e41cfef4435f990d1" + "hash": "5249aee041e2ff089e7d8ffd930921103a2a0abd13130d52c2eb86e1829df8e6" } diff --git a/api/.sqlx/query-53af948bd0d4ff5ca892d4e15d7ba49640274492658b50ccefccb03d5f1733b5.json b/api/.sqlx/query-53af948bd0d4ff5ca892d4e15d7ba49640274492658b50ccefccb03d5f1733b5.json deleted file mode 100644 index cb82c77..0000000 --- a/api/.sqlx/query-53af948bd0d4ff5ca892d4e15d7ba49640274492658b50ccefccb03d5f1733b5.json +++ /dev/null @@ -1,21 +0,0 @@ -{ - "db_name": "PostgreSQL", - "query": "SELECT min(id) AS \"id!\" FROM runs GROUP BY map_id, style, track", - "describe": { - "columns": [ - { - "ordinal": 0, - "name": "id!", - "type_info": "Int8", - "origin": "Expression" - } - ], - "parameters": { - "Left": [] - }, - "nullable": [ - null - ] - }, - "hash": "53af948bd0d4ff5ca892d4e15d7ba49640274492658b50ccefccb03d5f1733b5" -} diff --git a/api/.sqlx/query-58b16b0ae91f0ee7f789d3617275f2a3cdac3f5c4193ac1dda4846cb7c8f0161.json b/api/.sqlx/query-58b16b0ae91f0ee7f789d3617275f2a3cdac3f5c4193ac1dda4846cb7c8f0161.json deleted file mode 100644 index d62d82c..0000000 --- a/api/.sqlx/query-58b16b0ae91f0ee7f789d3617275f2a3cdac3f5c4193ac1dda4846cb7c8f0161.json +++ /dev/null @@ -1,12 +0,0 @@ -{ - "db_name": "PostgreSQL", - "query": "\n INSERT INTO standings (style, bonus, player_steamid64, points, completed)\n SELECT style, track > 0, player_steamid64, sum(points), count(*)\n FROM personal_bests GROUP BY style, track > 0, player_steamid64\n ", - "describe": { - "columns": [], - "parameters": { - "Left": [] - }, - "nullable": [] - }, - "hash": "58b16b0ae91f0ee7f789d3617275f2a3cdac3f5c4193ac1dda4846cb7c8f0161" -} diff --git a/api/.sqlx/query-5d4f220428ed510ebe16fe59043675be751f8d2d9a2df35f28e9063a9524a5cc.json b/api/.sqlx/query-5d4f220428ed510ebe16fe59043675be751f8d2d9a2df35f28e9063a9524a5cc.json new file mode 100644 index 0000000..93c0ce8 --- /dev/null +++ b/api/.sqlx/query-5d4f220428ed510ebe16fe59043675be751f8d2d9a2df35f28e9063a9524a5cc.json @@ -0,0 +1,28 @@ +{ + "db_name": "PostgreSQL", + "query": "SELECT log_id AS \"log_id!\" FROM records_bans\n WHERE player_steamid64 = $1 ORDER BY banned_at DESC, id DESC LIMIT 1", + "describe": { + "columns": [ + { + "ordinal": 0, + "name": "log_id!", + "type_info": "Int8", + "origin": { + "Table": { + "table": "records_bans", + "name": "log_id" + } + } + } + ], + "parameters": { + "Left": [ + "Int8" + ] + }, + "nullable": [ + true + ] + }, + "hash": "5d4f220428ed510ebe16fe59043675be751f8d2d9a2df35f28e9063a9524a5cc" +} diff --git a/api/.sqlx/query-61199db507be2420920cb714010a93a6e3ec837b247b16fd909f79052e6b747d.json b/api/.sqlx/query-61199db507be2420920cb714010a93a6e3ec837b247b16fd909f79052e6b747d.json deleted file mode 100644 index c199abd..0000000 --- a/api/.sqlx/query-61199db507be2420920cb714010a93a6e3ec837b247b16fd909f79052e6b747d.json +++ /dev/null @@ -1,190 +0,0 @@ -{ - "db_name": "PostgreSQL", - "query": "\n WITH listed AS (\n SELECT m.id, m.name, m.last_run_at, counts.players, counts.bonuses, tier.tier,\n row_number() OVER (ORDER BY\n -- How well the name matches: exactly, by a word's start (\"candy\" in bhop_candy), or within.\n CASE WHEN $6::text IS NULL OR m.name = $6 THEN 0\n WHEN starts_with(m.name, $6) OR strpos(m.name, '_' || $6) > 0 THEN 1\n WHEN m.name ILIKE $1 ESCAPE '\\' THEN 2 ELSE 3 END,\n CASE WHEN m.name NOT ILIKE $1 ESCAPE '\\' THEN word_similarity($7, m.name) END DESC NULLS LAST,\n CASE WHEN $2 = 'recent' THEN m.last_run_at END DESC,\n CASE WHEN $2 = 'newest' THEN m.listed_at END DESC NULLS LAST,\n CASE $2 WHEN 'popular' THEN -counts.players WHEN 'least_popular' THEN counts.players END,\n CASE $2 WHEN 'hardest' THEN -tier.tier WHEN 'easiest' THEN tier.tier END NULLS LAST,\n CASE $2 WHEN 'longest_record' THEN -record.time WHEN 'shortest_record' THEN record.time END\n NULLS LAST,\n CASE WHEN $2 = 'newest_record' THEN record.finished_at END DESC NULLS LAST,\n CASE WHEN $2 = 'oldest_record' THEN record.finished_at END NULLS LAST,\n CASE WHEN $2 = 'name_desc' THEN m.name END DESC,\n m.name\n ) AS position\n FROM maps m\n LEFT JOIN LATERAL (SELECT tier FROM map_tiers WHERE map_id = m.id AND track = 0\n ORDER BY assigned_at DESC LIMIT 1) tier ON true\n -- Every map's record, read only for the sorts that need it: the page below reads the page's own.\n LEFT JOIN LATERAL (\n SELECT pb.time, r.finished_at\n FROM personal_bests pb JOIN runs r ON r.id = pb.run_id\n WHERE $2 IN ('longest_record', 'shortest_record', 'newest_record', 'oldest_record') AND pb.map_id = m.id AND pb.style = $5 AND pb.track = 0\n ORDER BY pb.time, r.finished_at\n LIMIT 1\n ) record ON true\n CROSS JOIN LATERAL (\n SELECT count(DISTINCT player_steamid64) AS players,\n count(DISTINCT track) FILTER (WHERE track > 0) AS bonuses\n FROM personal_bests WHERE map_id = m.id\n ) counts\n WHERE m.last_run_at IS NOT NULL AND ($1::text IS NULL OR m.name ILIKE $1 ESCAPE '\\' OR $7 <% m.name)\n AND ($8::bigint IS NULL OR $9 = EXISTS (\n SELECT 1 FROM personal_bests WHERE player_steamid64 = $8 AND map_id = m.id AND style = $5 AND track = 0))\n AND ($10::smallint IS NULL OR tier.tier = $10)\n ), page AS (\n SELECT * FROM listed WHERE position > $4 ORDER BY position LIMIT $3\n )\n SELECT page.name, ci.id AS \"image_id?\", ci.key AS \"image_key?\", ci.focus AS \"image_focus?\", page.last_run_at AS \"last_run_at!\", page.players AS \"players!\",\n page.bonuses AS \"bonuses!\", page.tier AS \"tier?\",\n wr.time AS \"wr_time?\", wr.run_id AS \"wr_run_id?\", wr.finished_at AS \"wr_finished_at?\",\n p.steamid64::text AS \"wr_steamid64?\", p.name AS \"wr_name?\", p.avatar_url AS \"wr_avatar_url?\",\n p.country_code AS \"wr_country_code?\"\n FROM page\n LEFT JOIN map_images ci ON ci.map_id = page.id AND ci.position = 0\n LEFT JOIN LATERAL (\n SELECT pb.player_steamid64, pb.time, pb.run_id, r.finished_at\n FROM personal_bests pb JOIN runs r ON r.id = pb.run_id\n WHERE pb.map_id = page.id AND pb.style = $5 AND pb.track = 0\n ORDER BY pb.time, r.finished_at\n LIMIT 1\n ) wr ON true\n LEFT JOIN players p ON p.steamid64 = wr.player_steamid64\n ORDER BY page.position\n ", - "describe": { - "columns": [ - { - "ordinal": 0, - "name": "name", - "type_info": "Text", - "origin": { - "Table": { - "table": "maps", - "name": "name" - } - } - }, - { - "ordinal": 1, - "name": "image_id?", - "type_info": "Int8", - "origin": { - "Table": { - "table": "map_images", - "name": "id" - } - } - }, - { - "ordinal": 2, - "name": "image_key?", - "type_info": "Text", - "origin": { - "Table": { - "table": "map_images", - "name": "key" - } - } - }, - { - "ordinal": 3, - "name": "image_focus?", - "type_info": "Int2", - "origin": { - "Table": { - "table": "map_images", - "name": "focus" - } - } - }, - { - "ordinal": 4, - "name": "last_run_at!", - "type_info": "Timestamptz", - "origin": { - "Table": { - "table": "maps", - "name": "last_run_at" - } - } - }, - { - "ordinal": 5, - "name": "players!", - "type_info": "Int8", - "origin": "Expression" - }, - { - "ordinal": 6, - "name": "bonuses!", - "type_info": "Int8", - "origin": "Expression" - }, - { - "ordinal": 7, - "name": "tier?", - "type_info": "Int2", - "origin": { - "Table": { - "table": "map_tiers", - "name": "tier" - } - } - }, - { - "ordinal": 8, - "name": "wr_time?", - "type_info": "Float8", - "origin": { - "Table": { - "table": "personal_bests", - "name": "time" - } - } - }, - { - "ordinal": 9, - "name": "wr_run_id?", - "type_info": "Int8", - "origin": { - "Table": { - "table": "personal_bests", - "name": "run_id" - } - } - }, - { - "ordinal": 10, - "name": "wr_finished_at?", - "type_info": "Timestamptz", - "origin": { - "Table": { - "table": "runs", - "name": "finished_at" - } - } - }, - { - "ordinal": 11, - "name": "wr_steamid64?", - "type_info": "Text", - "origin": "Expression" - }, - { - "ordinal": 12, - "name": "wr_name?", - "type_info": "Text", - "origin": { - "Table": { - "table": "players", - "name": "name" - } - } - }, - { - "ordinal": 13, - "name": "wr_avatar_url?", - "type_info": "Text", - "origin": { - "Table": { - "table": "players", - "name": "avatar_url" - } - } - }, - { - "ordinal": 14, - "name": "wr_country_code?", - "type_info": "Text", - "origin": { - "Table": { - "table": "players", - "name": "country_code" - } - } - } - ], - "parameters": { - "Left": [ - "Text", - "Text", - "Int8", - "Int8", - "Text", - "Text", - "Text", - "Int8", - "Bool", - "Int2" - ] - }, - "nullable": [ - false, - true, - true, - true, - true, - null, - null, - false, - true, - true, - true, - null, - true, - true, - true - ] - }, - "hash": "61199db507be2420920cb714010a93a6e3ec837b247b16fd909f79052e6b747d" -} diff --git a/api/.sqlx/query-76a76384fa08304b4375bc1f64c01dbb0b4513e061dbbd6db50a1d12f578e260.json b/api/.sqlx/query-733f080103fb495fdbac6f2940a423806fc16af983802d2a6069f0e6e81c9b37.json similarity index 70% rename from api/.sqlx/query-76a76384fa08304b4375bc1f64c01dbb0b4513e061dbbd6db50a1d12f578e260.json rename to api/.sqlx/query-733f080103fb495fdbac6f2940a423806fc16af983802d2a6069f0e6e81c9b37.json index 3771a4c..94cd688 100644 --- a/api/.sqlx/query-76a76384fa08304b4375bc1f64c01dbb0b4513e061dbbd6db50a1d12f578e260.json +++ b/api/.sqlx/query-733f080103fb495fdbac6f2940a423806fc16af983802d2a6069f0e6e81c9b37.json @@ -1,6 +1,6 @@ { "db_name": "PostgreSQL", - "query": "SELECT lp.name, lp.avatar_url, lp.country_code,\n (SELECT count(*) FROM legacy_personal_bests l WHERE l.player_steamid64 = $1\n AND NOT EXISTS (\n SELECT 1 FROM bans\n WHERE bans.player_steamid64 = $1 AND bans.restrictions & $2 <> 0\n AND bans.lifted_at IS NULL AND (bans.ends_at IS NULL OR bans.ends_at > now())\n )) AS \"maps!\"\n FROM legacy_players lp WHERE lp.steamid64 = $1", + "query": "SELECT lp.name, lp.avatar_url, lp.country_code,\n (SELECT count(*) FROM legacy_personal_bests l WHERE l.player_steamid64 = $1\n AND NOT EXISTS (SELECT 1 FROM records_bans b WHERE b.player_steamid64 = $1)) AS \"maps!\"\n FROM legacy_players lp WHERE lp.steamid64 = $1", "describe": { "columns": [ { @@ -45,7 +45,6 @@ ], "parameters": { "Left": [ - "Int8", "Int8" ] }, @@ -56,5 +55,5 @@ null ] }, - "hash": "76a76384fa08304b4375bc1f64c01dbb0b4513e061dbbd6db50a1d12f578e260" + "hash": "733f080103fb495fdbac6f2940a423806fc16af983802d2a6069f0e6e81c9b37" } diff --git a/api/.sqlx/query-74493942786aa14a6e95ebe6198c28ffd2d175e76f4408e7706e54aedb353845.json b/api/.sqlx/query-74493942786aa14a6e95ebe6198c28ffd2d175e76f4408e7706e54aedb353845.json new file mode 100644 index 0000000..2f81701 --- /dev/null +++ b/api/.sqlx/query-74493942786aa14a6e95ebe6198c28ffd2d175e76f4408e7706e54aedb353845.json @@ -0,0 +1,190 @@ +{ + "db_name": "PostgreSQL", + "query": "\n WITH listed AS (\n SELECT m.id, m.name, m.last_run_at, m.players, tier.tier,\n row_number() OVER (ORDER BY\n -- How well the name matches: exactly, by a word's start (\"candy\" in bhop_candy), or within.\n CASE WHEN $6::text IS NULL OR m.name = $6 THEN 0\n WHEN starts_with(m.name, $6) OR strpos(m.name, '_' || $6) > 0 THEN 1\n WHEN m.name ILIKE $1 ESCAPE '\\' THEN 2 ELSE 3 END,\n CASE WHEN m.name NOT ILIKE $1 ESCAPE '\\' THEN word_similarity($7, m.name) END DESC NULLS LAST,\n CASE WHEN $2 = 'recent' THEN m.last_run_at END DESC,\n CASE WHEN $2 = 'newest' THEN m.listed_at END DESC NULLS LAST,\n CASE $2 WHEN 'popular' THEN -m.players WHEN 'least_popular' THEN m.players END,\n CASE $2 WHEN 'hardest' THEN -tier.tier WHEN 'easiest' THEN tier.tier END NULLS LAST,\n CASE $2 WHEN 'longest_record' THEN -record.time WHEN 'shortest_record' THEN record.time END\n NULLS LAST,\n CASE WHEN $2 = 'newest_record' THEN record.finished_at END DESC NULLS LAST,\n CASE WHEN $2 = 'oldest_record' THEN record.finished_at END NULLS LAST,\n CASE WHEN $2 = 'name_desc' THEN m.name END DESC,\n m.name\n ) AS position\n FROM maps m\n LEFT JOIN LATERAL (SELECT tier FROM map_tiers WHERE map_id = m.id AND track = 0\n ORDER BY assigned_at DESC LIMIT 1) tier ON true\n -- Every map's record, read only for the sorts that need it: the page below reads the page's own.\n LEFT JOIN LATERAL (\n SELECT pb.time, r.finished_at\n FROM personal_bests pb JOIN runs r ON r.id = pb.run_id\n WHERE $2 IN ('longest_record', 'shortest_record', 'newest_record', 'oldest_record') AND pb.map_id = m.id AND pb.style = $5 AND pb.track = 0\n ORDER BY pb.time, r.finished_at\n LIMIT 1\n ) record ON true\n WHERE m.last_run_at IS NOT NULL AND ($1::text IS NULL OR m.name ILIKE $1 ESCAPE '\\' OR $7 <% m.name)\n AND ($8::bigint IS NULL OR $9 = EXISTS (\n SELECT 1 FROM personal_bests WHERE player_steamid64 = $8 AND map_id = m.id AND style = $5 AND track = 0))\n AND ($10::smallint IS NULL OR tier.tier = $10)\n ), page AS (\n SELECT * FROM listed WHERE position > $4 ORDER BY position LIMIT $3\n )\n SELECT page.name, ci.id AS \"image_id?\", ci.key AS \"image_key?\", ci.focus AS \"image_focus?\", page.last_run_at AS \"last_run_at!\", page.players::int8 AS \"players!\",\n counts.bonuses AS \"bonuses!\", page.tier AS \"tier?\",\n wr.time AS \"wr_time?\", wr.run_id AS \"wr_run_id?\", wr.finished_at AS \"wr_finished_at?\",\n p.steamid64::text AS \"wr_steamid64?\", p.name AS \"wr_name?\", p.avatar_url AS \"wr_avatar_url?\",\n p.country_code AS \"wr_country_code?\"\n FROM page\n LEFT JOIN map_images ci ON ci.map_id = page.id AND ci.position = 0\n CROSS JOIN LATERAL (\n SELECT count(DISTINCT track) FILTER (WHERE track > 0) AS bonuses\n FROM personal_bests WHERE map_id = page.id\n ) counts\n LEFT JOIN LATERAL (\n SELECT pb.player_steamid64, pb.time, pb.run_id, r.finished_at\n FROM personal_bests pb JOIN runs r ON r.id = pb.run_id\n WHERE pb.map_id = page.id AND pb.style = $5 AND pb.track = 0\n ORDER BY pb.time, r.finished_at\n LIMIT 1\n ) wr ON true\n LEFT JOIN players p ON p.steamid64 = wr.player_steamid64\n ORDER BY page.position\n ", + "describe": { + "columns": [ + { + "ordinal": 0, + "name": "name", + "type_info": "Text", + "origin": { + "Table": { + "table": "maps", + "name": "name" + } + } + }, + { + "ordinal": 1, + "name": "image_id?", + "type_info": "Int8", + "origin": { + "Table": { + "table": "map_images", + "name": "id" + } + } + }, + { + "ordinal": 2, + "name": "image_key?", + "type_info": "Text", + "origin": { + "Table": { + "table": "map_images", + "name": "key" + } + } + }, + { + "ordinal": 3, + "name": "image_focus?", + "type_info": "Int2", + "origin": { + "Table": { + "table": "map_images", + "name": "focus" + } + } + }, + { + "ordinal": 4, + "name": "last_run_at!", + "type_info": "Timestamptz", + "origin": { + "Table": { + "table": "maps", + "name": "last_run_at" + } + } + }, + { + "ordinal": 5, + "name": "players!", + "type_info": "Int8", + "origin": "Expression" + }, + { + "ordinal": 6, + "name": "bonuses!", + "type_info": "Int8", + "origin": "Expression" + }, + { + "ordinal": 7, + "name": "tier?", + "type_info": "Int2", + "origin": { + "Table": { + "table": "map_tiers", + "name": "tier" + } + } + }, + { + "ordinal": 8, + "name": "wr_time?", + "type_info": "Float8", + "origin": { + "Table": { + "table": "personal_bests", + "name": "time" + } + } + }, + { + "ordinal": 9, + "name": "wr_run_id?", + "type_info": "Int8", + "origin": { + "Table": { + "table": "personal_bests", + "name": "run_id" + } + } + }, + { + "ordinal": 10, + "name": "wr_finished_at?", + "type_info": "Timestamptz", + "origin": { + "Table": { + "table": "runs", + "name": "finished_at" + } + } + }, + { + "ordinal": 11, + "name": "wr_steamid64?", + "type_info": "Text", + "origin": "Expression" + }, + { + "ordinal": 12, + "name": "wr_name?", + "type_info": "Text", + "origin": { + "Table": { + "table": "players", + "name": "name" + } + } + }, + { + "ordinal": 13, + "name": "wr_avatar_url?", + "type_info": "Text", + "origin": { + "Table": { + "table": "players", + "name": "avatar_url" + } + } + }, + { + "ordinal": 14, + "name": "wr_country_code?", + "type_info": "Text", + "origin": { + "Table": { + "table": "players", + "name": "country_code" + } + } + } + ], + "parameters": { + "Left": [ + "Text", + "Text", + "Int8", + "Int8", + "Text", + "Text", + "Text", + "Int8", + "Bool", + "Int2" + ] + }, + "nullable": [ + false, + false, + false, + false, + true, + null, + null, + false, + true, + true, + true, + null, + true, + true, + true + ] + }, + "hash": "74493942786aa14a6e95ebe6198c28ffd2d175e76f4408e7706e54aedb353845" +} diff --git a/api/.sqlx/query-7c5f569b9effc4618396bd3e861a27b3cea2208e6d9b40ca91cb25e1fec50882.json b/api/.sqlx/query-7c5f569b9effc4618396bd3e861a27b3cea2208e6d9b40ca91cb25e1fec50882.json new file mode 100644 index 0000000..6288cdf --- /dev/null +++ b/api/.sqlx/query-7c5f569b9effc4618396bd3e861a27b3cea2208e6d9b40ca91cb25e1fec50882.json @@ -0,0 +1,46 @@ +{ + "db_name": "PostgreSQL", + "query": "SELECT\n (SELECT count(*) FROM legacy_personal_bests l\n WHERE l.map_id = lm.id\n AND NOT EXISTS (SELECT 1 FROM records_bans b WHERE b.player_steamid64 = l.player_steamid64)\n ) AS \"players!\",\n (SELECT count(*) FROM legacy_runs l\n WHERE l.map_id = lm.id\n AND NOT EXISTS (SELECT 1 FROM records_bans b WHERE b.player_steamid64 = l.player_steamid64)\n ) AS \"runs!\",\n (SELECT count(*) FROM (\n SELECT l.player_steamid64 FROM legacy_personal_bests l\n WHERE l.map_id = lm.id\n AND NOT EXISTS (SELECT 1 FROM records_bans b WHERE b.player_steamid64 = l.player_steamid64)\n UNION\n SELECT pb.player_steamid64 FROM personal_bests pb\n JOIN maps m ON m.id = pb.map_id\n WHERE m.name = $1 AND pb.style = $2 AND pb.track = $3\n ) both_kinds) AS \"all_players!\",\n (SELECT max(l.finished_at) FROM legacy_runs l\n WHERE l.map_id = lm.id\n AND NOT EXISTS (SELECT 1 FROM records_bans b WHERE b.player_steamid64 = l.player_steamid64)\n ) AS last_run_at\n FROM legacy_maps lm WHERE lm.name = $1", + "describe": { + "columns": [ + { + "ordinal": 0, + "name": "players!", + "type_info": "Int8", + "origin": "Expression" + }, + { + "ordinal": 1, + "name": "runs!", + "type_info": "Int8", + "origin": "Expression" + }, + { + "ordinal": 2, + "name": "all_players!", + "type_info": "Int8", + "origin": "Expression" + }, + { + "ordinal": 3, + "name": "last_run_at", + "type_info": "Timestamptz", + "origin": "Expression" + } + ], + "parameters": { + "Left": [ + "Text", + "Text", + "Int2" + ] + }, + "nullable": [ + null, + null, + null, + null + ] + }, + "hash": "7c5f569b9effc4618396bd3e861a27b3cea2208e6d9b40ca91cb25e1fec50882" +} diff --git a/api/.sqlx/query-81f3fe110d986fc32580e3a9596e31f0f3207752afd2555f8489a8c8e7d9d00d.json b/api/.sqlx/query-81f3fe110d986fc32580e3a9596e31f0f3207752afd2555f8489a8c8e7d9d00d.json new file mode 100644 index 0000000..5c8f6a4 --- /dev/null +++ b/api/.sqlx/query-81f3fe110d986fc32580e3a9596e31f0f3207752afd2555f8489a8c8e7d9d00d.json @@ -0,0 +1,29 @@ +{ + "db_name": "PostgreSQL", + "query": "UPDATE replays SET shavit_key = coalesce(shavit_key, $2) WHERE run_id = $1\n RETURNING shavit_key AS \"shavit_key!\"", + "describe": { + "columns": [ + { + "ordinal": 0, + "name": "shavit_key!", + "type_info": "Text", + "origin": { + "Table": { + "table": "replays", + "name": "shavit_key" + } + } + } + ], + "parameters": { + "Left": [ + "Int8", + "Text" + ] + }, + "nullable": [ + true + ] + }, + "hash": "81f3fe110d986fc32580e3a9596e31f0f3207752afd2555f8489a8c8e7d9d00d" +} diff --git a/api/.sqlx/query-831b5885f04941793790f4d42deb1407e383129a44015640086b05c25f110716.json b/api/.sqlx/query-831b5885f04941793790f4d42deb1407e383129a44015640086b05c25f110716.json deleted file mode 100644 index cfc0f60..0000000 --- a/api/.sqlx/query-831b5885f04941793790f4d42deb1407e383129a44015640086b05c25f110716.json +++ /dev/null @@ -1,14 +0,0 @@ -{ - "db_name": "PostgreSQL", - "query": "\n WITH boards AS (SELECT DISTINCT map_id, style, track FROM runs WHERE id = ANY($1)),\n earlier AS (\n SELECT r.id,\n r.invalidated_at IS NULL AND r.time < coalesce(\n min(r.time) FILTER (WHERE r.invalidated_at IS NULL) OVER (\n PARTITION BY r.map_id, r.style, r.track ORDER BY r.finished_at, r.id\n ROWS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING),\n 'infinity') AS was_record\n FROM runs r\n JOIN boards b ON b.map_id = r.map_id AND b.style = r.style AND b.track = r.track\n )\n UPDATE runs r SET was_record = earlier.was_record FROM earlier\n WHERE r.id = earlier.id AND r.was_record <> earlier.was_record\n ", - "describe": { - "columns": [], - "parameters": { - "Left": [ - "Int8Array" - ] - }, - "nullable": [] - }, - "hash": "831b5885f04941793790f4d42deb1407e383129a44015640086b05c25f110716" -} diff --git a/api/.sqlx/query-913fd5bf2a6ab79de8c2abbf072dee740fa0f76384c8ec6d21618ff2e53a4c21.json b/api/.sqlx/query-913fd5bf2a6ab79de8c2abbf072dee740fa0f76384c8ec6d21618ff2e53a4c21.json new file mode 100644 index 0000000..6ddb3eb --- /dev/null +++ b/api/.sqlx/query-913fd5bf2a6ab79de8c2abbf072dee740fa0f76384c8ec6d21618ff2e53a4c21.json @@ -0,0 +1,26 @@ +{ + "db_name": "PostgreSQL", + "query": "SELECT DISTINCT ON (map_id, style, track) id FROM runs ORDER BY map_id, style, track, finished_at, id", + "describe": { + "columns": [ + { + "ordinal": 0, + "name": "id", + "type_info": "Int8", + "origin": { + "Table": { + "table": "runs", + "name": "id" + } + } + } + ], + "parameters": { + "Left": [] + }, + "nullable": [ + false + ] + }, + "hash": "913fd5bf2a6ab79de8c2abbf072dee740fa0f76384c8ec6d21618ff2e53a4c21" +} diff --git a/api/.sqlx/query-91ca896b1fabdab3ab2ce49444d92624a598d344f05fc7f66e66389a50f13699.json b/api/.sqlx/query-91ca896b1fabdab3ab2ce49444d92624a598d344f05fc7f66e66389a50f13699.json new file mode 100644 index 0000000..770f2ff --- /dev/null +++ b/api/.sqlx/query-91ca896b1fabdab3ab2ce49444d92624a598d344f05fc7f66e66389a50f13699.json @@ -0,0 +1,14 @@ +{ + "db_name": "PostgreSQL", + "query": "\n WITH starts AS (\n SELECT DISTINCT ON (map_id, style, track) map_id, style, track, finished_at, id\n FROM runs WHERE id = ANY($1)\n ORDER BY map_id, style, track, finished_at, id\n ), bests AS (\n SELECT s.*, (SELECT r.time FROM runs r\n WHERE r.map_id = s.map_id AND r.style = s.style AND r.track = s.track AND r.was_record\n AND (r.finished_at, r.id) < (s.finished_at, s.id)\n ORDER BY r.finished_at DESC, r.id DESC LIMIT 1) AS best\n FROM starts s\n ), placed AS (\n SELECT r.id,\n -- LEAST skips NULLs: no record before the start, or no valid run between it and this one.\n r.invalidated_at IS NULL AND r.time < coalesce(LEAST(b.best,\n min(r.time) FILTER (WHERE r.invalidated_at IS NULL) OVER (\n PARTITION BY r.map_id, r.style, r.track ORDER BY r.finished_at, r.id\n ROWS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING)),\n 'infinity') AS was_record\n FROM runs r\n JOIN bests b ON b.map_id = r.map_id AND b.style = r.style AND b.track = r.track\n AND (r.finished_at, r.id) >= (b.finished_at, b.id)\n )\n UPDATE runs r SET was_record = placed.was_record FROM placed\n WHERE r.id = placed.id AND r.was_record <> placed.was_record\n ", + "describe": { + "columns": [], + "parameters": { + "Left": [ + "Int8Array" + ] + }, + "nullable": [] + }, + "hash": "91ca896b1fabdab3ab2ce49444d92624a598d344f05fc7f66e66389a50f13699" +} diff --git a/api/.sqlx/query-958c4c7842edc4e16c8303468cb2e01319c9a616aeca32840a8d75074af47d5c.json b/api/.sqlx/query-958c4c7842edc4e16c8303468cb2e01319c9a616aeca32840a8d75074af47d5c.json new file mode 100644 index 0000000..44b1466 --- /dev/null +++ b/api/.sqlx/query-958c4c7842edc4e16c8303468cb2e01319c9a616aeca32840a8d75074af47d5c.json @@ -0,0 +1,12 @@ +{ + "db_name": "PostgreSQL", + "query": "DELETE FROM daily_totals_stale", + "describe": { + "columns": [], + "parameters": { + "Left": [] + }, + "nullable": [] + }, + "hash": "958c4c7842edc4e16c8303468cb2e01319c9a616aeca32840a8d75074af47d5c" +} diff --git a/api/.sqlx/query-97cfd8732c670f94694df1766d0162d3ac861b93e513725b9859f867dcc2d0b7.json b/api/.sqlx/query-97cfd8732c670f94694df1766d0162d3ac861b93e513725b9859f867dcc2d0b7.json new file mode 100644 index 0000000..8d32b36 --- /dev/null +++ b/api/.sqlx/query-97cfd8732c670f94694df1766d0162d3ac861b93e513725b9859f867dcc2d0b7.json @@ -0,0 +1,14 @@ +{ + "db_name": "PostgreSQL", + "query": "UPDATE replays SET shavit_key = NULL WHERE run_id = $1", + "describe": { + "columns": [], + "parameters": { + "Left": [ + "Int8" + ] + }, + "nullable": [] + }, + "hash": "97cfd8732c670f94694df1766d0162d3ac861b93e513725b9859f867dcc2d0b7" +} diff --git a/api/.sqlx/query-986a7f2319039db06dcfad7f596c201ca96d8000c151516898e7b1181f50535c.json b/api/.sqlx/query-986a7f2319039db06dcfad7f596c201ca96d8000c151516898e7b1181f50535c.json new file mode 100644 index 0000000..d6605c7 --- /dev/null +++ b/api/.sqlx/query-986a7f2319039db06dcfad7f596c201ca96d8000c151516898e7b1181f50535c.json @@ -0,0 +1,53 @@ +{ + "db_name": "PostgreSQL", + "query": "SELECT map_id, style, track FROM standings_boards WHERE map_id = $1 AND track = $2", + "describe": { + "columns": [ + { + "ordinal": 0, + "name": "map_id", + "type_info": "Int8", + "origin": { + "Table": { + "table": "standings_boards", + "name": "map_id" + } + } + }, + { + "ordinal": 1, + "name": "style", + "type_info": "Text", + "origin": { + "Table": { + "table": "standings_boards", + "name": "style" + } + } + }, + { + "ordinal": 2, + "name": "track", + "type_info": "Int2", + "origin": { + "Table": { + "table": "standings_boards", + "name": "track" + } + } + } + ], + "parameters": { + "Left": [ + "Int8", + "Int2" + ] + }, + "nullable": [ + false, + false, + false + ] + }, + "hash": "986a7f2319039db06dcfad7f596c201ca96d8000c151516898e7b1181f50535c" +} diff --git a/api/.sqlx/query-9aa31968d86ac6a6580708751f36de0b739ee9437803adb7ddf01492bbbd92cc.json b/api/.sqlx/query-9aa31968d86ac6a6580708751f36de0b739ee9437803adb7ddf01492bbbd92cc.json deleted file mode 100644 index 3fc0869..0000000 --- a/api/.sqlx/query-9aa31968d86ac6a6580708751f36de0b739ee9437803adb7ddf01492bbbd92cc.json +++ /dev/null @@ -1,19 +0,0 @@ -{ - "db_name": "PostgreSQL", - "query": "\n WITH tiers AS (\n SELECT DISTINCT ON (map_id, track) map_id, track, tier FROM map_tiers\n WHERE $1 ORDER BY map_id, track, assigned_at DESC\n ), scored AS (\n SELECT pb.player_steamid64, pb.map_id, pb.style, pb.track,\n coalesce(points_for(t.tier, pb.time,\n min(pb.time) OVER (PARTITION BY pb.map_id, pb.style, pb.track),\n pb.track > 0, $2, $3, $4, $5, $6), 0) AS points\n FROM personal_bests pb\n LEFT JOIN tiers t ON t.map_id = pb.map_id AND t.track = pb.track\n )\n UPDATE personal_bests pb SET points = scored.points FROM scored\n WHERE pb.player_steamid64 = scored.player_steamid64 AND pb.map_id = scored.map_id\n AND pb.style = scored.style AND pb.track = scored.track AND pb.points <> scored.points\n ", - "describe": { - "columns": [], - "parameters": { - "Left": [ - "Bool", - "Float8", - "Float8", - "Float8", - "Float8", - "Float8" - ] - }, - "nullable": [] - }, - "hash": "9aa31968d86ac6a6580708751f36de0b739ee9437803adb7ddf01492bbbd92cc" -} diff --git a/api/.sqlx/query-cce60431bd589fc0b70ec4e1485ae6147d387901b995045115541cce8d42369d.json b/api/.sqlx/query-a061a39f302bfd7bfd5c82be9cbdbe2f3a197190623fb73c037a60fd6154af89.json similarity index 78% rename from api/.sqlx/query-cce60431bd589fc0b70ec4e1485ae6147d387901b995045115541cce8d42369d.json rename to api/.sqlx/query-a061a39f302bfd7bfd5c82be9cbdbe2f3a197190623fb73c037a60fd6154af89.json index 365d490..37302d4 100644 --- a/api/.sqlx/query-cce60431bd589fc0b70ec4e1485ae6147d387901b995045115541cce8d42369d.json +++ b/api/.sqlx/query-a061a39f302bfd7bfd5c82be9cbdbe2f3a197190623fb73c037a60fd6154af89.json @@ -1,6 +1,6 @@ { "db_name": "PostgreSQL", - "query": "\n SELECT r.id, r.time, r.finished_at AS \"finished_at!\",\n r.player_steamid64::text AS \"steamid64!\", coalesce(p.name, lp.name) AS \"name!\",\n coalesce(p.avatar_url, lp.avatar_url) AS avatar_url,\n coalesce(p.country_code, lp.country_code) AS country_code\n FROM (\n SELECT l.id, l.player_steamid64, l.time, l.finished_at,\n min(l.time) OVER (\n ORDER BY l.finished_at, l.id ROWS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING\n ) AS best_before\n FROM legacy_runs l\n JOIN legacy_maps lm ON lm.id = l.map_id\n WHERE lm.name = $1 AND l.finished_at IS NOT NULL AND NOT EXISTS (\n SELECT 1 FROM bans\n WHERE bans.player_steamid64 = l.player_steamid64 AND bans.restrictions & $2 <> 0\n AND bans.lifted_at IS NULL AND (bans.ends_at IS NULL OR bans.ends_at > now())\n )\n ) r\n JOIN legacy_players lp ON lp.steamid64 = r.player_steamid64\n LEFT JOIN players p ON p.steamid64 = r.player_steamid64\n WHERE r.best_before IS NULL OR r.time < r.best_before\n ORDER BY r.finished_at, r.id\n ", + "query": "\n SELECT r.id, r.time, r.finished_at AS \"finished_at!\",\n r.player_steamid64::text AS \"steamid64!\", coalesce(p.name, lp.name) AS \"name!\",\n coalesce(p.avatar_url, lp.avatar_url) AS avatar_url,\n coalesce(p.country_code, lp.country_code) AS country_code\n FROM (\n SELECT l.id, l.player_steamid64, l.time, l.finished_at,\n min(l.time) OVER (\n ORDER BY l.finished_at, l.id ROWS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING\n ) AS best_before\n FROM legacy_runs l\n JOIN legacy_maps lm ON lm.id = l.map_id\n WHERE lm.name = $1 AND l.finished_at IS NOT NULL AND NOT EXISTS (\n SELECT 1 FROM records_bans b WHERE b.player_steamid64 = l.player_steamid64\n )\n ) r\n JOIN legacy_players lp ON lp.steamid64 = r.player_steamid64\n LEFT JOIN players p ON p.steamid64 = r.player_steamid64\n WHERE r.best_before IS NULL OR r.time < r.best_before\n ORDER BY r.finished_at, r.id\n ", "describe": { "columns": [ { @@ -63,8 +63,7 @@ ], "parameters": { "Left": [ - "Text", - "Int8" + "Text" ] }, "nullable": [ @@ -77,5 +76,5 @@ null ] }, - "hash": "cce60431bd589fc0b70ec4e1485ae6147d387901b995045115541cce8d42369d" + "hash": "a061a39f302bfd7bfd5c82be9cbdbe2f3a197190623fb73c037a60fd6154af89" } diff --git a/api/.sqlx/query-88ac63722baaeae981392f605da2cc4f28d5a33c06b7beb2edadbfa0b5f1d7d9.json b/api/.sqlx/query-a4c8c677bb3816e72692dbc8111347615d299604337674511045ca62053be8b7.json similarity index 60% rename from api/.sqlx/query-88ac63722baaeae981392f605da2cc4f28d5a33c06b7beb2edadbfa0b5f1d7d9.json rename to api/.sqlx/query-a4c8c677bb3816e72692dbc8111347615d299604337674511045ca62053be8b7.json index 5e475a0..6fa9642 100644 --- a/api/.sqlx/query-88ac63722baaeae981392f605da2cc4f28d5a33c06b7beb2edadbfa0b5f1d7d9.json +++ b/api/.sqlx/query-a4c8c677bb3816e72692dbc8111347615d299604337674511045ca62053be8b7.json @@ -1,6 +1,6 @@ { "db_name": "PostgreSQL", - "query": "\n WITH shown AS (\n SELECT s.player_steamid64, s.points, s.completed, p.name, p.avatar_url, p.country_code\n FROM legacy_standings s JOIN legacy_players p ON p.steamid64 = s.player_steamid64\n WHERE (NOT $1 OR s.points > 0) AND NOT EXISTS (\n SELECT 1 FROM bans\n WHERE bans.player_steamid64 = s.player_steamid64 AND bans.restrictions & $2 <> 0\n AND bans.lifted_at IS NULL AND (bans.ends_at IS NULL OR bans.ends_at > now())\n )\n ), ranked AS (\n SELECT shown.*,\n CASE WHEN $1 THEN rank() OVER (ORDER BY points DESC)\n ELSE rank() OVER (ORDER BY completed DESC) END AS rank,\n row_number() OVER (ORDER BY CASE WHEN $1 THEN points ELSE completed END DESC,\n lower(name)) AS position\n FROM shown\n ), page AS (\n SELECT * FROM ranked WHERE position > $3 ORDER BY position LIMIT $4\n )\n SELECT (SELECT count(*) FROM ranked) AS \"total!\", page.rank AS \"rank?\",\n round(page.points)::int8 AS \"points?\", page.completed::int8 AS \"completed?\",\n page.player_steamid64::text AS \"steamid64?\", page.name AS \"name?\",\n page.avatar_url AS \"avatar_url?\", page.country_code AS \"country_code?\"\n FROM (SELECT 1) one LEFT JOIN page ON true\n ORDER BY page.position\n ", + "query": "\n WITH shown AS (\n SELECT s.player_steamid64, s.points, s.completed, p.name, p.avatar_url, p.country_code\n FROM legacy_standings s JOIN legacy_players p ON p.steamid64 = s.player_steamid64\n WHERE (NOT $1 OR s.points > 0) AND NOT EXISTS (\n SELECT 1 FROM records_bans b WHERE b.player_steamid64 = s.player_steamid64\n )\n ), ranked AS (\n SELECT shown.*,\n CASE WHEN $1 THEN rank() OVER (ORDER BY points DESC)\n ELSE rank() OVER (ORDER BY completed DESC) END AS rank,\n row_number() OVER (ORDER BY CASE WHEN $1 THEN points ELSE completed END DESC,\n lower(name), player_steamid64) AS position\n FROM shown\n ), page AS (\n SELECT * FROM ranked WHERE position > $2 ORDER BY position LIMIT $3\n )\n SELECT (SELECT count(*) FROM ranked) AS \"total!\", page.rank AS \"rank?\",\n round(page.points)::int8 AS \"points?\", page.completed::int8 AS \"completed?\",\n page.player_steamid64::text AS \"steamid64?\", page.name AS \"name?\",\n page.avatar_url AS \"avatar_url?\", page.country_code AS \"country_code?\"\n FROM (SELECT 1) one LEFT JOIN page ON true\n ORDER BY page.position\n ", "describe": { "columns": [ { @@ -71,7 +71,6 @@ "Left": [ "Bool", "Int8", - "Int8", "Int8" ] }, @@ -86,5 +85,5 @@ true ] }, - "hash": "88ac63722baaeae981392f605da2cc4f28d5a33c06b7beb2edadbfa0b5f1d7d9" + "hash": "a4c8c677bb3816e72692dbc8111347615d299604337674511045ca62053be8b7" } diff --git a/api/.sqlx/query-a4ff9bcbe365dc4c9dc4a7a3ea774ee7eba59b26e92defb2aa2f749d2c6af482.json b/api/.sqlx/query-a4ff9bcbe365dc4c9dc4a7a3ea774ee7eba59b26e92defb2aa2f749d2c6af482.json new file mode 100644 index 0000000..cc80565 --- /dev/null +++ b/api/.sqlx/query-a4ff9bcbe365dc4c9dc4a7a3ea774ee7eba59b26e92defb2aa2f749d2c6af482.json @@ -0,0 +1,14 @@ +{ + "db_name": "PostgreSQL", + "query": "\n UPDATE maps m SET players = (SELECT count(DISTINCT player_steamid64) FROM personal_bests WHERE map_id = m.id)\n WHERE m.id IN (SELECT map_id FROM runs WHERE id = ANY($1))\n ", + "describe": { + "columns": [], + "parameters": { + "Left": [ + "Int8Array" + ] + }, + "nullable": [] + }, + "hash": "a4ff9bcbe365dc4c9dc4a7a3ea774ee7eba59b26e92defb2aa2f749d2c6af482" +} diff --git a/api/.sqlx/query-bb9da91e5dd31ed43150f400c10c670dc4b504cea6a119b2f98bb5bfb3dea670.json b/api/.sqlx/query-bb9da91e5dd31ed43150f400c10c670dc4b504cea6a119b2f98bb5bfb3dea670.json new file mode 100644 index 0000000..ecdd558 --- /dev/null +++ b/api/.sqlx/query-bb9da91e5dd31ed43150f400c10c670dc4b504cea6a119b2f98bb5bfb3dea670.json @@ -0,0 +1,23 @@ +{ + "db_name": "PostgreSQL", + "query": "\n WITH now AS MATERIALIZED (\n SELECT b.map_id, b.style, b.track,\n (SELECT min(pb.time) FROM personal_bests pb\n WHERE pb.map_id = b.map_id AND pb.style = b.style AND pb.track = b.track) AS best,\n CASE WHEN $5 THEN (SELECT t.tier FROM map_tiers t WHERE t.map_id = b.map_id AND t.track = b.track\n ORDER BY t.assigned_at DESC LIMIT 1) END AS tier\n FROM unnest($1::int8[], $2::text[], $3::int2[]) AS b (map_id, style, track)\n ), runners AS MATERIALIZED (\n SELECT DISTINCT player_steamid64, style, track > 0 AS bonus FROM runs WHERE id = ANY($4)\n ), moved AS (\n SELECT now.*, stored.best AS stored_best, stored.tier AS stored_tier\n FROM now JOIN standings_boards stored USING (map_id, style, track)\n WHERE now.best IS NOT NULL AND (now.best, now.tier) IS DISTINCT FROM (stored.best, stored.tier)\n ), changes AS (\n SELECT pb.style, pb.track > 0 AS bonus, pb.player_steamid64,\n sum(coalesce(points_for(m.tier, pb.time, m.best, pb.track > 0, $6, $7, $8, $9, $10), 0)\n - coalesce(points_for(m.stored_tier, pb.time, m.stored_best, pb.track > 0,\n $6, $7, $8, $9, $10), 0)) AS change\n FROM moved m\n JOIN personal_bests pb ON pb.map_id = m.map_id AND pb.style = m.style AND pb.track = m.track\n -- The runs' players are summed again below.\n WHERE NOT EXISTS (\n SELECT 1 FROM runners r\n WHERE r.player_steamid64 = pb.player_steamid64 AND r.style = pb.style AND r.bonus = (pb.track > 0)\n )\n GROUP BY 1, 2, 3\n ), moved_totals AS (\n UPDATE standings s SET points = s.points + c.change FROM changes c\n WHERE s.style = c.style AND s.bonus = c.bonus AND s.player_steamid64 = c.player_steamid64\n AND c.change <> 0\n ), emptied AS (\n DELETE FROM standings_boards stored USING now\n WHERE stored.map_id = now.map_id AND stored.style = now.style AND stored.track = now.track\n AND now.best IS NULL\n )\n INSERT INTO standings_boards (map_id, style, track, best, tier)\n SELECT map_id, style, track, best, tier FROM now WHERE best IS NOT NULL\n ON CONFLICT (map_id, style, track) DO UPDATE SET best = excluded.best, tier = excluded.tier\n ", + "describe": { + "columns": [], + "parameters": { + "Left": [ + "Int8Array", + "TextArray", + "Int2Array", + "Int8Array", + "Bool", + "Float8", + "Float8", + "Float8", + "Float8", + "Float8" + ] + }, + "nullable": [] + }, + "hash": "bb9da91e5dd31ed43150f400c10c670dc4b504cea6a119b2f98bb5bfb3dea670" +} diff --git a/api/.sqlx/query-c403d508a312a2a20268ccb565541a1bd94891e898c2e0f4c6551bd5f24dd315.json b/api/.sqlx/query-c403d508a312a2a20268ccb565541a1bd94891e898c2e0f4c6551bd5f24dd315.json new file mode 100644 index 0000000..0586349 --- /dev/null +++ b/api/.sqlx/query-c403d508a312a2a20268ccb565541a1bd94891e898c2e0f4c6551bd5f24dd315.json @@ -0,0 +1,23 @@ +{ + "db_name": "PostgreSQL", + "query": "SELECT EXISTS (\n SELECT 1 FROM bans b\n WHERE b.restrictions & $1 <> 0\n AND NOT EXISTS (SELECT 1 FROM records_bans r WHERE r.player_steamid64 = b.player_steamid64)\n AND NOT EXISTS (SELECT 1 FROM legacy_standings s WHERE s.player_steamid64 = b.player_steamid64)\n AND EXISTS (SELECT 1 FROM legacy_runs l WHERE l.player_steamid64 = b.player_steamid64)\n ) AS \"stale!\"", + "describe": { + "columns": [ + { + "ordinal": 0, + "name": "stale!", + "type_info": "Bool", + "origin": "Expression" + } + ], + "parameters": { + "Left": [ + "Int8" + ] + }, + "nullable": [ + null + ] + }, + "hash": "c403d508a312a2a20268ccb565541a1bd94891e898c2e0f4c6551bd5f24dd315" +} diff --git a/api/.sqlx/query-2824aafb7de8b00c7d7a6e28a7c216258c0e5fa650379bbb6dbf9a9e5b980e45.json b/api/.sqlx/query-c71d9da452ec06f9d29dcd4b1743ecb597e20f1c122d0f238d7de014e469c722.json similarity index 58% rename from api/.sqlx/query-2824aafb7de8b00c7d7a6e28a7c216258c0e5fa650379bbb6dbf9a9e5b980e45.json rename to api/.sqlx/query-c71d9da452ec06f9d29dcd4b1743ecb597e20f1c122d0f238d7de014e469c722.json index b726da6..2dd59fd 100644 --- a/api/.sqlx/query-2824aafb7de8b00c7d7a6e28a7c216258c0e5fa650379bbb6dbf9a9e5b980e45.json +++ b/api/.sqlx/query-c71d9da452ec06f9d29dcd4b1743ecb597e20f1c122d0f238d7de014e469c722.json @@ -1,6 +1,6 @@ { "db_name": "PostgreSQL", - "query": "\n INSERT INTO legacy_standings (player_steamid64, points, completed)\n SELECT player_steamid64, sum(points), count(*) FROM (\n SELECT l.player_steamid64,\n coalesce(points_for(m.tier, l.time, min(l.time) OVER (PARTITION BY l.map_id), false,\n $1, $2, $3, $4, $5), 0) AS points\n FROM legacy_personal_bests l\n JOIN legacy_maps m ON m.id = l.map_id\n WHERE NOT EXISTS (\n SELECT 1 FROM bans\n WHERE bans.player_steamid64 = l.player_steamid64 AND bans.restrictions & $6 <> 0\n AND bans.lifted_at IS NULL AND (bans.ends_at IS NULL OR bans.ends_at > now())\n )\n ) scored\n GROUP BY player_steamid64\n ", + "query": "\n INSERT INTO legacy_standings (player_steamid64, points, completed)\n SELECT player_steamid64, sum(points), count(*) FROM (\n SELECT l.player_steamid64,\n coalesce(points_for(m.tier, l.time, min(l.time) OVER (PARTITION BY l.map_id), false,\n $1, $2, $3, $4, $5), 0) AS points\n FROM legacy_personal_bests l\n JOIN legacy_maps m ON m.id = l.map_id\n WHERE NOT EXISTS (SELECT 1 FROM records_bans b WHERE b.player_steamid64 = l.player_steamid64)\n ) scored\n GROUP BY player_steamid64\n ", "describe": { "columns": [], "parameters": { @@ -9,11 +9,10 @@ "Float8", "Float8", "Float8", - "Float8", - "Int8" + "Float8" ] }, "nullable": [] }, - "hash": "2824aafb7de8b00c7d7a6e28a7c216258c0e5fa650379bbb6dbf9a9e5b980e45" + "hash": "c71d9da452ec06f9d29dcd4b1743ecb597e20f1c122d0f238d7de014e469c722" } diff --git a/api/.sqlx/query-d74f42ecb9ce6311f0c3204217ec772d8565e6740d97af6cab2ed6797b8d48f5.json b/api/.sqlx/query-d74f42ecb9ce6311f0c3204217ec772d8565e6740d97af6cab2ed6797b8d48f5.json deleted file mode 100644 index bad4405..0000000 --- a/api/.sqlx/query-d74f42ecb9ce6311f0c3204217ec772d8565e6740d97af6cab2ed6797b8d48f5.json +++ /dev/null @@ -1,29 +0,0 @@ -{ - "db_name": "PostgreSQL", - "query": "SELECT log_id FROM bans\n WHERE player_steamid64 = $1 AND restrictions & $2 <> 0\n AND lifted_at IS NULL AND (ends_at IS NULL OR ends_at > now())\n ORDER BY banned_at DESC, id DESC LIMIT 1", - "describe": { - "columns": [ - { - "ordinal": 0, - "name": "log_id", - "type_info": "Int8", - "origin": { - "Table": { - "table": "bans", - "name": "log_id" - } - } - } - ], - "parameters": { - "Left": [ - "Int8", - "Int8" - ] - }, - "nullable": [ - false - ] - }, - "hash": "d74f42ecb9ce6311f0c3204217ec772d8565e6740d97af6cab2ed6797b8d48f5" -} diff --git a/api/.sqlx/query-d92aa01c3efd4af53b9993f6a2e63eeb405a6cd02fcf2750947806502fdf4909.json b/api/.sqlx/query-d92aa01c3efd4af53b9993f6a2e63eeb405a6cd02fcf2750947806502fdf4909.json deleted file mode 100644 index 367b8ae..0000000 --- a/api/.sqlx/query-d92aa01c3efd4af53b9993f6a2e63eeb405a6cd02fcf2750947806502fdf4909.json +++ /dev/null @@ -1,16 +0,0 @@ -{ - "db_name": "PostgreSQL", - "query": "\n WITH tracks AS (\n SELECT map_id, track FROM runs WHERE id = ANY($1)\n UNION SELECT * FROM unnest($2::int8[], $3::int2[])\n ), affected AS (\n SELECT style, track > 0 AS bonus, player_steamid64 FROM personal_bests\n WHERE (map_id, track) IN (SELECT map_id, track FROM tracks)\n UNION SELECT style, track > 0, player_steamid64 FROM runs WHERE id = ANY($1)\n )\n INSERT INTO standings (style, bonus, player_steamid64, points, completed)\n SELECT pb.style, pb.track > 0, pb.player_steamid64, sum(pb.points), count(*)\n FROM personal_bests pb\n JOIN affected a ON a.style = pb.style AND a.bonus = (pb.track > 0)\n AND a.player_steamid64 = pb.player_steamid64\n GROUP BY pb.style, pb.track > 0, pb.player_steamid64\n ", - "describe": { - "columns": [], - "parameters": { - "Left": [ - "Int8Array", - "Int8Array", - "Int2Array" - ] - }, - "nullable": [] - }, - "hash": "d92aa01c3efd4af53b9993f6a2e63eeb405a6cd02fcf2750947806502fdf4909" -} diff --git a/api/.sqlx/query-d9e2097305726b1ec991641e4fad60fcc2249c4af23fd9b733063641f8633b83.json b/api/.sqlx/query-d9e2097305726b1ec991641e4fad60fcc2249c4af23fd9b733063641f8633b83.json new file mode 100644 index 0000000..d9d458b --- /dev/null +++ b/api/.sqlx/query-d9e2097305726b1ec991641e4fad60fcc2249c4af23fd9b733063641f8633b83.json @@ -0,0 +1,23 @@ +{ + "db_name": "PostgreSQL", + "query": "SELECT (SELECT count(*) FROM legacy_personal_bests l\n WHERE l.map_id = lm.id AND NOT EXISTS (\n SELECT 1 FROM records_bans b WHERE b.player_steamid64 = l.player_steamid64\n )) AS \"players!\"\n FROM legacy_maps lm WHERE lm.name = $1", + "describe": { + "columns": [ + { + "ordinal": 0, + "name": "players!", + "type_info": "Int8", + "origin": "Expression" + } + ], + "parameters": { + "Left": [ + "Text" + ] + }, + "nullable": [ + null + ] + }, + "hash": "d9e2097305726b1ec991641e4fad60fcc2249c4af23fd9b733063641f8633b83" +} diff --git a/api/.sqlx/query-dc57d5403dd5cba0fc3740698013aa5ee191704cb59d98aebc7c0e9708297759.json b/api/.sqlx/query-dc57d5403dd5cba0fc3740698013aa5ee191704cb59d98aebc7c0e9708297759.json deleted file mode 100644 index d29429b..0000000 --- a/api/.sqlx/query-dc57d5403dd5cba0fc3740698013aa5ee191704cb59d98aebc7c0e9708297759.json +++ /dev/null @@ -1,24 +0,0 @@ -{ - "db_name": "PostgreSQL", - "query": "SELECT (SELECT count(*) FROM legacy_personal_bests l\n WHERE l.map_id = lm.id AND NOT EXISTS (\n SELECT 1 FROM bans\n WHERE bans.player_steamid64 = l.player_steamid64 AND bans.restrictions & $2 <> 0\n AND bans.lifted_at IS NULL AND (bans.ends_at IS NULL OR bans.ends_at > now())\n )) AS \"players!\"\n FROM legacy_maps lm WHERE lm.name = $1", - "describe": { - "columns": [ - { - "ordinal": 0, - "name": "players!", - "type_info": "Int8", - "origin": "Expression" - } - ], - "parameters": { - "Left": [ - "Text", - "Int8" - ] - }, - "nullable": [ - null - ] - }, - "hash": "dc57d5403dd5cba0fc3740698013aa5ee191704cb59d98aebc7c0e9708297759" -} diff --git a/api/.sqlx/query-2857bfe5e47ae7124cf1f12ba4bdd507aeb72e8333c6975ae96f5cf956eff6f3.json b/api/.sqlx/query-e05df32d510205648ab98a20e572af70d244d4aad387d96ac85fdc944609659d.json similarity index 60% rename from api/.sqlx/query-2857bfe5e47ae7124cf1f12ba4bdd507aeb72e8333c6975ae96f5cf956eff6f3.json rename to api/.sqlx/query-e05df32d510205648ab98a20e572af70d244d4aad387d96ac85fdc944609659d.json index e55faab..2c9f755 100644 --- a/api/.sqlx/query-2857bfe5e47ae7124cf1f12ba4bdd507aeb72e8333c6975ae96f5cf956eff6f3.json +++ b/api/.sqlx/query-e05df32d510205648ab98a20e572af70d244d4aad387d96ac85fdc944609659d.json @@ -1,6 +1,6 @@ { "db_name": "PostgreSQL", - "query": "\n WITH shown AS (\n SELECT s.player_steamid64, s.points, s.completed, p.name, p.avatar_url, p.country_code\n FROM legacy_standings s JOIN legacy_players p ON p.steamid64 = s.player_steamid64\n WHERE NOT EXISTS (\n SELECT 1 FROM bans\n WHERE bans.player_steamid64 = s.player_steamid64 AND bans.restrictions & $2 <> 0\n AND bans.lifted_at IS NULL AND (bans.ends_at IS NULL OR bans.ends_at > now())\n )\n )\n (SELECT true AS \"by_points!\", rank() OVER (ORDER BY points DESC) AS \"rank!\",\n round(points)::int8 AS \"points!\", completed::int8 AS \"completed!\",\n player_steamid64::text AS \"steamid64!\", name AS \"name!\", avatar_url, country_code\n FROM shown WHERE points > 0 ORDER BY points DESC, lower(name) LIMIT $1)\n UNION ALL\n (SELECT false, rank() OVER (ORDER BY completed DESC), round(points)::int8, completed::int8,\n player_steamid64::text, name, avatar_url, country_code\n FROM shown ORDER BY completed DESC, lower(name) LIMIT $1)\n ", + "query": "\n WITH shown AS (\n SELECT s.player_steamid64, s.points, s.completed, p.name, p.avatar_url, p.country_code\n FROM legacy_standings s JOIN legacy_players p ON p.steamid64 = s.player_steamid64\n WHERE NOT EXISTS (SELECT 1 FROM records_bans b WHERE b.player_steamid64 = s.player_steamid64)\n )\n (SELECT true AS \"by_points!\", rank() OVER (ORDER BY points DESC) AS \"rank!\",\n round(points)::int8 AS \"points!\", completed::int8 AS \"completed!\",\n player_steamid64::text AS \"steamid64!\", name AS \"name!\", avatar_url, country_code\n FROM shown WHERE points > 0 ORDER BY points DESC, lower(name), player_steamid64 LIMIT $1)\n UNION ALL\n (SELECT false, rank() OVER (ORDER BY completed DESC), round(points)::int8, completed::int8,\n player_steamid64::text, name, avatar_url, country_code\n FROM shown ORDER BY completed DESC, lower(name), player_steamid64 LIMIT $1)\n ", "describe": { "columns": [ { @@ -54,7 +54,6 @@ ], "parameters": { "Left": [ - "Int8", "Int8" ] }, @@ -69,5 +68,5 @@ null ] }, - "hash": "2857bfe5e47ae7124cf1f12ba4bdd507aeb72e8333c6975ae96f5cf956eff6f3" + "hash": "e05df32d510205648ab98a20e572af70d244d4aad387d96ac85fdc944609659d" } diff --git a/api/.sqlx/query-0caa5f3eb76fa6e874067551d394fac519e3fe1239b7d47ec353c7bb0c69257d.json b/api/.sqlx/query-e2850506a353d2cdef1d30d072f1426014d114ff1129d05a25b7a0ed19935df8.json similarity index 65% rename from api/.sqlx/query-0caa5f3eb76fa6e874067551d394fac519e3fe1239b7d47ec353c7bb0c69257d.json rename to api/.sqlx/query-e2850506a353d2cdef1d30d072f1426014d114ff1129d05a25b7a0ed19935df8.json index afff9fb..75716ed 100644 --- a/api/.sqlx/query-0caa5f3eb76fa6e874067551d394fac519e3fe1239b7d47ec353c7bb0c69257d.json +++ b/api/.sqlx/query-e2850506a353d2cdef1d30d072f1426014d114ff1129d05a25b7a0ed19935df8.json @@ -1,6 +1,6 @@ { "db_name": "PostgreSQL", - "query": "SELECT coalesce(bit_or(restrictions), 0) AS \"bits!\" FROM bans\n WHERE player_steamid64 = $1 AND lifted_at IS NULL AND (ends_at IS NULL OR ends_at > now())", + "query": "SELECT coalesce(bit_or(restrictions), 0) AS \"bits!\" FROM active_bans WHERE player_steamid64 = $1", "describe": { "columns": [ { @@ -19,5 +19,5 @@ null ] }, - "hash": "0caa5f3eb76fa6e874067551d394fac519e3fe1239b7d47ec353c7bb0c69257d" + "hash": "e2850506a353d2cdef1d30d072f1426014d114ff1129d05a25b7a0ed19935df8" } diff --git a/api/.sqlx/query-eca2b4db86885e0aaa277aa51a91004f7c674d6947028edca0e75c704dfbed5f.json b/api/.sqlx/query-e41c78228d558089041cffc5849468b7cfbfd5660bb63f72a33b401139d9db76.json similarity index 75% rename from api/.sqlx/query-eca2b4db86885e0aaa277aa51a91004f7c674d6947028edca0e75c704dfbed5f.json rename to api/.sqlx/query-e41c78228d558089041cffc5849468b7cfbfd5660bb63f72a33b401139d9db76.json index 03dd6d7..e486882 100644 --- a/api/.sqlx/query-eca2b4db86885e0aaa277aa51a91004f7c674d6947028edca0e75c704dfbed5f.json +++ b/api/.sqlx/query-e41c78228d558089041cffc5849468b7cfbfd5660bb63f72a33b401139d9db76.json @@ -1,6 +1,6 @@ { "db_name": "PostgreSQL", - "query": "\n (SELECT true AS \"by_points!\", rank() OVER (ORDER BY s.points DESC) AS \"rank!\",\n round(s.points)::int8 AS \"points!\", s.completed::int8 AS \"completed!\",\n p.steamid64::text AS \"steamid64!\", p.name AS \"name!\", p.avatar_url, p.country_code\n FROM standings s JOIN players p ON p.steamid64 = s.player_steamid64\n WHERE s.style = $1 AND s.bonus = $2 AND s.points > 0\n ORDER BY s.points DESC, lower(p.name) LIMIT $3)\n UNION ALL\n (SELECT false, rank() OVER (ORDER BY s.completed DESC), round(s.points)::int8, s.completed::int8,\n p.steamid64::text, p.name, p.avatar_url, p.country_code\n FROM standings s JOIN players p ON p.steamid64 = s.player_steamid64\n WHERE s.style = $1 AND s.bonus = $2\n ORDER BY s.completed DESC, lower(p.name) LIMIT $3)\n ", + "query": "\n (SELECT true AS \"by_points!\", rank() OVER (ORDER BY s.points DESC) AS \"rank!\",\n round(s.points)::int8 AS \"points!\", s.completed::int8 AS \"completed!\",\n p.steamid64::text AS \"steamid64!\", p.name AS \"name!\", p.avatar_url, p.country_code\n FROM standings s JOIN players p ON p.steamid64 = s.player_steamid64\n WHERE s.style = $1 AND s.bonus = $2 AND s.points > 0\n ORDER BY s.points DESC, lower(p.name), s.player_steamid64 LIMIT $3)\n UNION ALL\n (SELECT false, rank() OVER (ORDER BY s.completed DESC), round(s.points)::int8, s.completed::int8,\n p.steamid64::text, p.name, p.avatar_url, p.country_code\n FROM standings s JOIN players p ON p.steamid64 = s.player_steamid64\n WHERE s.style = $1 AND s.bonus = $2\n ORDER BY s.completed DESC, lower(p.name), s.player_steamid64 LIMIT $3)\n ", "describe": { "columns": [ { @@ -70,5 +70,5 @@ null ] }, - "hash": "eca2b4db86885e0aaa277aa51a91004f7c674d6947028edca0e75c704dfbed5f" + "hash": "e41c78228d558089041cffc5849468b7cfbfd5660bb63f72a33b401139d9db76" } diff --git a/api/.sqlx/query-eacbaee4dbcceb3a2d515ae41cfb31e5c63a8d099abb5e5200820efc9a486dd6.json b/api/.sqlx/query-eacbaee4dbcceb3a2d515ae41cfb31e5c63a8d099abb5e5200820efc9a486dd6.json new file mode 100644 index 0000000..63ed277 --- /dev/null +++ b/api/.sqlx/query-eacbaee4dbcceb3a2d515ae41cfb31e5c63a8d099abb5e5200820efc9a486dd6.json @@ -0,0 +1,14 @@ +{ + "db_name": "PostgreSQL", + "query": "\n DELETE FROM standings s USING runs r\n WHERE r.id = ANY($1)\n AND s.style = r.style AND s.bonus = (r.track > 0) AND s.player_steamid64 = r.player_steamid64\n ", + "describe": { + "columns": [], + "parameters": { + "Left": [ + "Int8Array" + ] + }, + "nullable": [] + }, + "hash": "eacbaee4dbcceb3a2d515ae41cfb31e5c63a8d099abb5e5200820efc9a486dd6" +} diff --git a/api/.sqlx/query-ef66a14304a97bfc38d245c20061976dcc43355cc70d5c8c8f1f556ad681d78e.json b/api/.sqlx/query-ef66a14304a97bfc38d245c20061976dcc43355cc70d5c8c8f1f556ad681d78e.json new file mode 100644 index 0000000..1dd2e24 --- /dev/null +++ b/api/.sqlx/query-ef66a14304a97bfc38d245c20061976dcc43355cc70d5c8c8f1f556ad681d78e.json @@ -0,0 +1,38 @@ +{ + "db_name": "PostgreSQL", + "query": "SELECT run_id, shavit_key AS \"shavit_key!\" FROM replays\n WHERE shavit_key = regexp_replace(stored_key, '\\.sjr$', '.replay')", + "describe": { + "columns": [ + { + "ordinal": 0, + "name": "run_id", + "type_info": "Int8", + "origin": { + "Table": { + "table": "replays", + "name": "run_id" + } + } + }, + { + "ordinal": 1, + "name": "shavit_key!", + "type_info": "Text", + "origin": { + "Table": { + "table": "replays", + "name": "shavit_key" + } + } + } + ], + "parameters": { + "Left": [] + }, + "nullable": [ + false, + true + ] + }, + "hash": "ef66a14304a97bfc38d245c20061976dcc43355cc70d5c8c8f1f556ad681d78e" +} diff --git a/api/.sqlx/query-fb89d26d48097368c1db01803b5366e50406604dc3090125c5d9f6a0b68e25a5.json b/api/.sqlx/query-fb89d26d48097368c1db01803b5366e50406604dc3090125c5d9f6a0b68e25a5.json new file mode 100644 index 0000000..ba066d1 --- /dev/null +++ b/api/.sqlx/query-fb89d26d48097368c1db01803b5366e50406604dc3090125c5d9f6a0b68e25a5.json @@ -0,0 +1,18 @@ +{ + "db_name": "PostgreSQL", + "query": "\n INSERT INTO standings (style, bonus, player_steamid64, points, completed)\n SELECT pb.style, pb.track > 0, pb.player_steamid64,\n sum(coalesce(points_for(b.tier, pb.time, b.best, pb.track > 0, $1, $2, $3, $4, $5), 0)), count(*)\n FROM personal_bests pb\n JOIN standings_boards b ON b.map_id = pb.map_id AND b.style = pb.style AND b.track = pb.track\n GROUP BY 1, 2, 3\n ", + "describe": { + "columns": [], + "parameters": { + "Left": [ + "Float8", + "Float8", + "Float8", + "Float8", + "Float8" + ] + }, + "nullable": [] + }, + "hash": "fb89d26d48097368c1db01803b5366e50406604dc3090125c5d9f6a0b68e25a5" +} diff --git a/api/.sqlx/query-fc86ee27b250fe502f8df63881441bf4a8b7e1b3f2f8c494479214a931fca865.json b/api/.sqlx/query-fc86ee27b250fe502f8df63881441bf4a8b7e1b3f2f8c494479214a931fca865.json deleted file mode 100644 index 520a4b5..0000000 --- a/api/.sqlx/query-fc86ee27b250fe502f8df63881441bf4a8b7e1b3f2f8c494479214a931fca865.json +++ /dev/null @@ -1,47 +0,0 @@ -{ - "db_name": "PostgreSQL", - "query": "WITH banned AS (\n SELECT player_steamid64 FROM bans\n WHERE restrictions & $2 <> 0 AND lifted_at IS NULL AND (ends_at IS NULL OR ends_at > now())\n )\n SELECT\n (SELECT count(*) FROM legacy_personal_bests l\n WHERE l.map_id = lm.id\n AND NOT EXISTS (SELECT 1 FROM banned b WHERE b.player_steamid64 = l.player_steamid64)\n ) AS \"players!\",\n (SELECT count(*) FROM legacy_runs l\n WHERE l.map_id = lm.id\n AND NOT EXISTS (SELECT 1 FROM banned b WHERE b.player_steamid64 = l.player_steamid64)\n ) AS \"runs!\",\n (SELECT count(*) FROM (\n SELECT l.player_steamid64 FROM legacy_personal_bests l\n WHERE l.map_id = lm.id\n AND NOT EXISTS (SELECT 1 FROM banned b WHERE b.player_steamid64 = l.player_steamid64)\n UNION\n SELECT pb.player_steamid64 FROM personal_bests pb\n JOIN maps m ON m.id = pb.map_id\n WHERE m.name = $1 AND pb.style = $3 AND pb.track = $4\n ) both_kinds) AS \"all_players!\",\n (SELECT max(l.finished_at) FROM legacy_runs l\n WHERE l.map_id = lm.id\n AND NOT EXISTS (SELECT 1 FROM banned b WHERE b.player_steamid64 = l.player_steamid64)\n ) AS last_run_at\n FROM legacy_maps lm WHERE lm.name = $1", - "describe": { - "columns": [ - { - "ordinal": 0, - "name": "players!", - "type_info": "Int8", - "origin": "Expression" - }, - { - "ordinal": 1, - "name": "runs!", - "type_info": "Int8", - "origin": "Expression" - }, - { - "ordinal": 2, - "name": "all_players!", - "type_info": "Int8", - "origin": "Expression" - }, - { - "ordinal": 3, - "name": "last_run_at", - "type_info": "Timestamptz", - "origin": "Expression" - } - ], - "parameters": { - "Left": [ - "Text", - "Int8", - "Text", - "Int2" - ] - }, - "nullable": [ - null, - null, - null, - null - ] - }, - "hash": "fc86ee27b250fe502f8df63881441bf4a8b7e1b3f2f8c494479214a931fca865" -} diff --git a/api/crates/api/migrations/0047_active_bans.sql b/api/crates/api/migrations/0047_active_bans.sql new file mode 100644 index 0000000..66d2af2 --- /dev/null +++ b/api/crates/api/migrations/0047_active_bans.sql @@ -0,0 +1,12 @@ +-- Which bans are in force, spelled out once, so every query that leaves banned players out reads the same rule +-- (external audit, 2026-11-06). Views rather than functions: the planner folds a view into the query, so +-- `NOT EXISTS (SELECT 1 FROM records_bans ...)` over a whole board still runs as one anti-join, where a +-- function holding the subquery would be called once per row. + +-- A ban is active until it's lifted or its end passes. +CREATE VIEW active_bans AS + SELECT * FROM bans WHERE lifted_at IS NULL AND (ends_at IS NULL OR ends_at > now()); + +-- Active bans that keep the player off the boards: bit 0 is `no_records` (restrictions.rs). +CREATE VIEW records_bans AS + SELECT * FROM active_bans WHERE restrictions & 1 <> 0; diff --git a/api/crates/api/migrations/0048_daily_totals_stale.sql b/api/crates/api/migrations/0048_daily_totals_stale.sql new file mode 100644 index 0000000..cca65cc --- /dev/null +++ b/api/crates/api/migrations/0048_daily_totals_stale.sql @@ -0,0 +1,37 @@ +-- Days before yesterday whose daily totals changed since they were counted: a run that arrived late, or one +-- invalidated or restored (external audit, 2026-11-06). The job's own pass recounts yesterday and today; it +-- recounts from the earliest day here too, and empties the table. A trigger fills it, so no writer of runs can +-- forget to. Yesterday and today are left out: the pass counts them anyway, and leaving them out keeps the +-- ingest from queueing on one shared row per day. +CREATE TABLE daily_totals_stale ( + day date PRIMARY KEY +); + +CREATE FUNCTION runs_mark_daily_totals_stale() RETURNS trigger +LANGUAGE plpgsql AS $$ +BEGIN + -- The same day a run counts on in `daily_run_totals`. + IF NEW.finished_at < ((now() AT TIME ZONE 'UTC')::date - 1)::timestamp AT TIME ZONE 'UTC' THEN + INSERT INTO daily_totals_stale (day) VALUES ((NEW.finished_at AT TIME ZONE 'UTC')::date) + ON CONFLICT DO NOTHING; + END IF; + RETURN NULL; +END; +$$; + +-- `was_record` too: a run's day can change who was a record on later days, which those days count. +CREATE TRIGGER runs_daily_totals_stale + AFTER INSERT OR UPDATE OF invalidated_at, was_record ON runs + FOR EACH ROW EXECUTE FUNCTION runs_mark_daily_totals_stale(); + +-- A day's world records are its runs stored as records when set (`leaderboards/world_records.rs`), rather +-- than worked out again here. +CREATE OR REPLACE FUNCTION daily_run_totals(since timestamptz) +RETURNS TABLE (day date, runs integer, world_records integer) +LANGUAGE sql STABLE AS $$ + SELECT (LEAST(finished_at, now()) AT TIME ZONE 'UTC')::date, count(*)::integer, + (count(*) FILTER (WHERE was_record))::integer + FROM runs + WHERE invalidated_at IS NULL AND finished_at >= since + GROUP BY 1 +$$; diff --git a/api/crates/api/migrations/0049_runs_board_records.sql b/api/crates/api/migrations/0049_runs_board_records.sql new file mode 100644 index 0000000..0020710 --- /dev/null +++ b/api/crates/api/migrations/0049_runs_board_records.sql @@ -0,0 +1,4 @@ +-- Each board's records in WR history's order, so recounting record-when-set finds the board's best before a +-- run with one lookup instead of reading every earlier run (`leaderboards/world_records.rs`). Records are a +-- small share of runs, so the index stays small. +CREATE INDEX runs_board_records ON runs (map_id, style, track, finished_at, id) WHERE was_record; diff --git a/api/crates/api/migrations/0050_map_players.sql b/api/crates/api/migrations/0050_map_players.sql new file mode 100644 index 0000000..816ab75 --- /dev/null +++ b/api/crates/api/migrations/0050_map_players.sql @@ -0,0 +1,8 @@ +-- Players with a personal best on the map, in any style or track. The map list's default sort ranks every map +-- by it, which counted 179k personal bests on each view at the legacy site's size, so it's stored. Recounted +-- wherever personal bests change (`standings::refresh_runs`). +ALTER TABLE maps ADD COLUMN players integer NOT NULL DEFAULT 0 CHECK (players >= 0); + +UPDATE maps m SET players = counted.players +FROM (SELECT map_id, count(DISTINCT player_steamid64) AS players FROM personal_bests GROUP BY map_id) counted +WHERE counted.map_id = m.id; diff --git a/api/crates/api/migrations/0051_standings_boards.sql b/api/crates/api/migrations/0051_standings_boards.sql new file mode 100644 index 0000000..f01ca34 --- /dev/null +++ b/api/crates/api/migrations/0051_standings_boards.sql @@ -0,0 +1,46 @@ +-- Points are no longer stored per personal best. A board's points all follow from two values, its best time +-- and its tier, so those are stored per board instead, as the standings were counted with them. A new world +-- record or tier then moves the stored totals by the change in each player's points, and a full recount writes +-- the totals alone, where it used to rewrite every personal best: 4 of its 4½ minutes at 10× the legacy data +-- (2026-11-06). `leaderboards/standings.rs` keeps the table current; pages work a personal best's points out +-- with `personal_best_points`. + +-- Per board (map, style, track) with a personal best: what its points were last counted with. No foreign key +-- to maps: checking one locks the map's row, which the ingest holds while it waits for a recount to finish. +CREATE TABLE standings_boards ( + map_id bigint NOT NULL, + style text NOT NULL, + track smallint NOT NULL, + best double precision NOT NULL, + -- NULL when the board earns no points: no tier, or tiers not in force. + tier smallint, + PRIMARY KEY (map_id, style, track) +); + +-- What the current totals were counted with. Before the first count there are no totals to match. +INSERT INTO standings_boards (map_id, style, track, best, tier) +SELECT pb.map_id, pb.style, pb.track, min(pb.time), + CASE WHEN coalesce((SELECT (formula->>'tiers')::boolean FROM standings_formula), false) THEN + (SELECT tier FROM map_tiers t WHERE t.map_id = pb.map_id AND t.track = pb.track + ORDER BY assigned_at DESC LIMIT 1) + END +FROM personal_bests pb +GROUP BY pb.map_id, pb.style, pb.track; + +-- A personal best's points, as the standings count them: from its board's counted best and tier, and the +-- counted formula (`standings_formula`). 0 before the first count. +CREATE FUNCTION personal_best_points(pb_map_id bigint, pb_style text, pb_track smallint, pb_time double precision) +RETURNS double precision +LANGUAGE sql STABLE PARALLEL SAFE +RETURN coalesce(( + SELECT points_for(b.tier, pb_time, b.best, pb_track > 0, + (f.formula->>'base')::float8, (f.formula->>'growth_percent')::float8, + (f.formula->>'finish_percent')::float8, (f.formula->>'speed_exponent')::float8, + (f.formula->>'bonus_percent')::float8) + FROM standings_boards b CROSS JOIN standings_formula f + WHERE b.map_id = pb_map_id AND b.style = pb_style AND b.track = pb_track +), 0); + +-- Not additive: a binary from before this migration reads this column, so rolling back past it means +-- restoring the backup taken before it, rather than deploying the older build. +ALTER TABLE personal_bests DROP COLUMN points; diff --git a/api/crates/api/src/auth.rs b/api/crates/api/src/auth.rs index 7dd22f3..cda6ca2 100644 --- a/api/crates/api/src/auth.rs +++ b/api/crates/api/src/auth.rs @@ -1,7 +1,8 @@ //! Signing in with Steam and out again, and `GET /v1/me` for the signed-in player. //! //! These endpoints are for browsers: they answer with redirects and cookies, not JSON. The website links to -//! `/v1/auth/steam`, Steam sends the player back to the callback, and the callback sets the session cookie. +//! `/v1/auth/steam`, which sets the sign-in cookie that binds the sign-in to this browser; Steam sends the +//! player back to the callback, and the callback sets the session cookie. pub mod openid; pub mod sessions; @@ -12,7 +13,7 @@ use axum::{ http::HeaderMap, response::Redirect, }; -use axum_extra::extract::cookie::CookieJar; +use axum_extra::extract::cookie::{Cookie, CookieJar, SameSite}; use chrono::Utc; use serde::{Deserialize, Serialize}; use utoipa::{IntoParams, ToSchema}; @@ -29,6 +30,27 @@ use sessions::SignedIn; /// The website's page for a sign-in that didn't complete. const FAILED_PAGE: &str = "/sign-in/failed"; +/// Holds the value the callback URL must carry, from the start of a sign-in to its callback. It lasts until +/// the browser closes, or the callback uses it up; it needs no shorter life, since only a Steam answer at +/// most five minutes old is accepted with it. +const SIGN_IN_COOKIE: &str = "sj_sign_in"; + +/// Sent only to the callback. Lax, not Strict: Steam's redirect back is a cross-site navigation. +fn sign_in_cookie(value: String) -> Cookie<'static> { + Cookie::build((SIGN_IN_COOKIE, value)) + .http_only(true) + .secure(true) + .same_site(SameSite::Lax) + .path(openid::CALLBACK_PATH) + .build() +} + +fn sign_in_removal_cookie() -> Cookie<'static> { + Cookie::build((SIGN_IN_COOKIE, "")) + .path(openid::CALLBACK_PATH) + .build() +} + #[derive(Debug, Deserialize, IntoParams)] pub struct ReturnQuery { /// The page to come back to: a path on this site, such as `/maps`. Anything else means `/`. @@ -64,9 +86,19 @@ pub fn routes() -> OpenApiRouter { params(ReturnQuery), responses((status = SEE_OTHER, description = "To Steam's sign-in page")), )] -async fn start(State(state): State, Query(query): Query) -> Redirect { +async fn start( + State(state): State, + jar: CookieJar, + Query(query): Query, +) -> (CookieJar, Redirect) { let return_to = ReturnTo::new(query.return_to.as_deref()); - Redirect::to(openid::login_url(&state.steam, &state.public_url, &return_to).as_str()) + // A sign-in started in another tab since replaces this value, so only the latest one completes. + let browser = sessions::generate(); + let login_url = openid::login_url(&state.steam, &state.public_url, &return_to, &browser); + ( + jar.add(sign_in_cookie(browser)), + Redirect::to(login_url.as_str()), + ) } /// Where Steam sends the browser back. Starts a session if Steam confirms the sign-in. @@ -84,11 +116,17 @@ async fn callback( jar: CookieJar, Query(params): Query>, ) -> Result<(CookieJar, Redirect), ApiError> { + let browser = jar + .get(SIGN_IN_COOKIE) + .map(|cookie| cookie.value().to_owned()); + // Used up either way: a sign-in that failed starts again from the beginning. + let jar = jar.remove(sign_in_removal_cookie()); let verified = match openid::verify( &state.db, &state.steam, &state.public_url, ¶ms, + browser.as_deref(), Utc::now(), ) .await @@ -218,19 +256,38 @@ mod tests { } } - async fn sign_in(state: &AppState) -> TestResponse { + /// Steam's answer to the callback, from a browser whose sign-in cookie holds `browser`. + fn callback(state: &AppState, browser: Option<&str>) -> Request { let params = openid::tests::assertion(&state.steam, Utc::now()); let query = url::form_urlencoded::Serializer::new(String::new()) .extend_pairs(¶ms) .finish(); - let request = Request::get(format!("/v1/auth/steam/callback?{query}")) - .body(Body::empty()) - .unwrap(); + let request = Request::get(format!("/v1/auth/steam/callback?{query}")); + match browser { + Some(value) => request.header(header::COOKIE, format!("sj_sign_in={value}")), + None => request, + } + .body(Body::empty()) + .unwrap() + } + + async fn sign_in(state: &AppState) -> TestResponse { + let request = callback(state, Some(openid::tests::BROWSER)); test_client::send_to(state.clone(), request).await } + /// The `Set-Cookie` header for `name`, if the response has one. + fn set_cookie<'a>(response: &'a TestResponse, name: &str) -> Option<&'a str> { + response + .headers + .get_all(header::SET_COOKIE) + .iter() + .map(|value| value.to_str().unwrap()) + .find(|value| value.starts_with(&format!("{name}="))) + } + fn session_from(response: &TestResponse) -> String { - let set_cookie = response.headers[header::SET_COOKIE].to_str().unwrap(); + let set_cookie = set_cookie(response, "sj_session").expect("a session cookie"); assert!(set_cookie.contains("HttpOnly") && set_cookie.contains("Secure")); assert!(set_cookie.contains("SameSite=Lax")); set_cookie @@ -258,6 +315,9 @@ mod tests { assert_eq!(response.status, 303); assert_eq!(response.headers[header::LOCATION], "/maps/bhop_forest"); + let used_up = set_cookie(&response, "sj_sign_in").expect("the sign-in cookie is removed"); + assert!(used_up.starts_with("sj_sign_in=;"), "{used_up}"); + let me = me_with(&state, &session_from(&response)).await; assert_eq!(me.status, 200); let me: Me = me.json(); @@ -276,6 +336,10 @@ mod tests { test_client::send_to( state.clone(), Request::get(format!("/v1/auth/steam/callback?{query}")) + .header( + header::COOKIE, + format!("sj_sign_in={}", openid::tests::BROWSER), + ) .body(Body::empty()) .unwrap(), ) @@ -284,7 +348,18 @@ mod tests { assert_eq!(send().await.headers[header::LOCATION], "/maps/bhop_forest"); let replay = send().await; assert_eq!(replay.headers[header::LOCATION], FAILED_PAGE); - assert!(replay.headers.get(header::SET_COOKIE).is_none()); + assert!(set_cookie(&replay, "sj_session").is_none()); + } + + #[sqlx::test] + async fn a_sign_in_brought_back_by_another_browser_is_refused(db: PgPool) { + // Someone starts a sign-in to their own account and sends the link Steam gives back to someone else. + let state = state(&db, fake_steam(true).await); + for browser in [None, Some("fedcba9876543210")] { + let response = test_client::send_to(state.clone(), callback(&state, browser)).await; + assert_eq!(response.headers[header::LOCATION], FAILED_PAGE); + assert!(set_cookie(&response, "sj_session").is_none(), "{browser:?}"); + } } #[sqlx::test] @@ -293,7 +368,7 @@ mod tests { let response = sign_in(&state).await; assert_eq!(response.headers[header::LOCATION], FAILED_PAGE); - assert!(response.headers.get(header::SET_COOKIE).is_none()); + assert!(set_cookie(&response, "sj_session").is_none()); } #[sqlx::test] @@ -339,8 +414,25 @@ mod tests { location.starts_with("http://127.0.0.1:9/openid/login?"), "{location}" ); + let browser = set_cookie(&response, "sj_sign_in").expect("a sign-in cookie"); + assert!( + browser.contains("HttpOnly") + && browser.contains("Secure") + && browser.contains("SameSite=Lax") + && browser.contains("Path=/v1/auth/steam/callback"), + "{browser}" + ); + let value = browser + .split(';') + .next() + .unwrap() + .strip_prefix("sj_sign_in=") + .unwrap(); + assert_eq!(value.len(), 64); assert!( - location.contains("callback%3Freturn_to%3D%252F&"), + location.contains(&format!( + "callback%3Freturn_to%3D%252F%26browser%3D{value}&" + )), "{location}" ); } diff --git a/api/crates/api/src/auth/openid.rs b/api/crates/api/src/auth/openid.rs index d7f3e21..67cb00d 100644 --- a/api/crates/api/src/auth/openid.rs +++ b/api/crates/api/src/auth/openid.rs @@ -2,7 +2,10 @@ //! //! Sensitive path, fails closed. The browser carries Steam's answer to us, so anyone can forge one. Nothing in //! it is trusted until Steam itself confirms it (`check_authentication`), and only fields Steam signed are -//! read. A confirmed answer is accepted once: its nonce is recorded, so replaying it fails. +//! read. A confirmed answer is accepted once: its nonce is recorded, so replaying it fails. It's also bound +//! to the browser that started it: the callback URL carries a random value that browser holds in a cookie, +//! so someone who starts a sign-in can't send the link Steam gives back to another browser and sign it in +//! as them. use chrono::{DateTime, Duration, Utc}; use sqlx::PgPool; @@ -51,16 +54,18 @@ impl ReturnTo { } } -/// Our callback, with the page to return to riding along. Steam echoes it back as `openid.return_to`. -fn callback_url(public_url: &Url, return_to: &ReturnTo) -> Url { +/// Our callback, with the page to return to and the browser's value riding along. Steam echoes it back as +/// `openid.return_to`. +fn callback_url(public_url: &Url, return_to: &ReturnTo, browser: &str) -> Url { let mut url = public_url.join(CALLBACK_PATH).expect("a valid path"); url.query_pairs_mut() - .append_pair("return_to", return_to.as_str()); + .append_pair("return_to", return_to.as_str()) + .append_pair("browser", browser); url } -/// The Steam page that asks the player to sign in. -pub fn login_url(steam: &Steam, public_url: &Url, return_to: &ReturnTo) -> Url { +/// The Steam page that asks the player to sign in. `browser` is the value in the sign-in cookie. +pub fn login_url(steam: &Steam, public_url: &Url, return_to: &ReturnTo, browser: &str) -> Url { let mut url = steam.openid_endpoint.clone(); let realm = public_url.origin().ascii_serialization(); url.query_pairs_mut() @@ -68,7 +73,7 @@ pub fn login_url(steam: &Steam, public_url: &Url, return_to: &ReturnTo) -> Url { .append_pair("openid.mode", "checkid_setup") .append_pair( "openid.return_to", - callback_url(public_url, return_to).as_str(), + callback_url(public_url, return_to, browser).as_str(), ) .append_pair("openid.realm", &realm) .append_pair("openid.identity", IDENTIFIER_SELECT) @@ -82,6 +87,8 @@ pub enum Refusal { /// The player turned back on Steam's page. Not an error. Cancelled, Malformed(&'static str), + /// The browser bringing the answer back isn't the one that started the sign-in. + OtherBrowser, /// Steam said the assertion isn't one it made, or couldn't be asked. NotConfirmed, Replayed, @@ -94,15 +101,17 @@ pub struct Verified { pub return_to: ReturnTo, } -/// Checks Steam's answer, given as the callback's query parameters. +/// Checks Steam's answer, given as the callback's query parameters, and `browser`, the sign-in cookie's +/// value if the browser sent one. pub async fn verify( db: &PgPool, steam: &Steam, public_url: &Url, params: &[(String, String)], + browser: Option<&str>, now: DateTime, ) -> Result { - let (steamid64, return_to, nonce) = check_fields(steam, public_url, params, now)?; + let (steamid64, return_to, nonce) = check_fields(steam, public_url, params, browser, now)?; confirm_with_steam(steam, params).await?; record_nonce(db, &nonce, now).await?; Ok(Verified { @@ -123,6 +132,7 @@ fn check_fields( steam: &Steam, public_url: &Url, params: &[(String, String)], + browser: Option<&str>, now: DateTime, ) -> Result<(SteamId64, ReturnTo, String), Refusal> { let field = @@ -165,19 +175,24 @@ fn check_fields( .and_then(|id| SteamId64::try_from(id.to_owned()).ok()) .ok_or(Refusal::Malformed("not a Steam player ID"))?; - // The return URL must be our callback exactly; the page to go back to is read from it, since it's signed. + // The return URL must be our callback exactly; the page to go back to and the browser's value are read + // from it, since it's signed. let signed_return = Url::parse(field("return_to")?).map_err(|_| Refusal::Malformed("return_to isn't a URL"))?; - let return_to = ReturnTo::new( + let signed_query = |name: &str| { signed_return .query_pairs() - .find(|(key, _)| key == "return_to") - .map(|(_, value)| value) - .as_deref(), - ); - if signed_return != callback_url(public_url, &return_to) { + .find(|(key, _)| key == name) + .map(|(_, value)| value.into_owned()) + }; + let return_to = ReturnTo::new(signed_query("return_to").as_deref()); + let signed_browser = signed_query("browser").unwrap_or_default(); + if signed_return != callback_url(public_url, &return_to, &signed_browser) { return Err(Refusal::Malformed("return_to isn't our callback")); } + if signed_browser.is_empty() || browser != Some(signed_browser.as_str()) { + return Err(Refusal::OtherBrowser); + } let nonce = field("response_nonce")?; let issued_at = nonce @@ -260,6 +275,8 @@ pub mod tests { use super::*; pub const PLAYER: &str = "76561197960287930"; + /// The sign-in cookie's value in the browser `assertion` belongs to. + pub const BROWSER: &str = "0123456789abcdef"; pub fn public_url() -> Url { Url::parse("https://sourcejump.test/").unwrap() @@ -269,7 +286,11 @@ pub mod tests { pub fn assertion(steam: &Steam, issued_at: DateTime) -> Vec<(String, String)> { let claimed = format!("{CLAIMED_ID_PREFIX}{PLAYER}"); let nonce = format!("{}abc123", issued_at.format("%Y-%m-%dT%H:%M:%SZ")); - let return_to = callback_url(&public_url(), &ReturnTo::new(Some("/maps/bhop_forest"))); + let return_to = callback_url( + &public_url(), + &ReturnTo::new(Some("/maps/bhop_forest")), + BROWSER, + ); [ ("return_to", "/maps/bhop_forest"), ("openid.ns", NS), @@ -298,7 +319,13 @@ pub mod tests { } fn check(params: &[(String, String)]) -> Result<(SteamId64, ReturnTo, String), Refusal> { - check_fields(&Steam::new(), &public_url(), params, Utc::now()) + check_fields( + &Steam::new(), + &public_url(), + params, + Some(BROWSER), + Utc::now(), + ) } #[test] @@ -320,7 +347,12 @@ pub mod tests { #[test] fn the_login_url_goes_to_steam_and_back_to_us() { - let url = login_url(&Steam::new(), &public_url(), &ReturnTo::new(Some("/maps"))); + let url = login_url( + &Steam::new(), + &public_url(), + &ReturnTo::new(Some("/maps")), + BROWSER, + ); let query: Vec<(String, String)> = url.query_pairs().into_owned().collect(); assert!( @@ -329,7 +361,9 @@ pub mod tests { ); assert_eq!( openid(&query, "return_to"), - Some("https://sourcejump.test/v1/auth/steam/callback?return_to=%2Fmaps") + Some( + "https://sourcejump.test/v1/auth/steam/callback?return_to=%2Fmaps&browser=0123456789abcdef" + ) ); assert_eq!(openid(&query, "realm"), Some("https://sourcejump.test")); } @@ -378,6 +412,28 @@ pub mod tests { } } + #[test] + fn refuses_an_answer_brought_back_by_another_browser() { + let good = assertion(&Steam::new(), Utc::now()); + for browser in [None, Some("fedcba9876543210")] { + assert_eq!( + check_fields(&Steam::new(), &public_url(), &good, browser, Utc::now()), + Err(Refusal::OtherBrowser), + "{browser:?}" + ); + } + // An answer to a callback URL without the value is refused even when the browser has none either. + let unbound = with( + good, + "openid.return_to", + "https://sourcejump.test/v1/auth/steam/callback?return_to=%2Fmaps%2Fbhop_forest", + ); + assert_eq!( + check_fields(&Steam::new(), &public_url(), &unbound, None, Utc::now()), + Err(Refusal::Malformed("return_to isn't our callback")) + ); + } + #[test] fn refuses_a_repeated_field() { let other_player = format!("{CLAIMED_ID_PREFIX}76561197960287931"); diff --git a/api/crates/api/src/auth/sessions.rs b/api/crates/api/src/auth/sessions.rs index 3027757..f7e99f2 100644 --- a/api/crates/api/src/auth/sessions.rs +++ b/api/crates/api/src/auth/sessions.rs @@ -141,7 +141,8 @@ pub fn require_same_origin(headers: &HeaderMap, public_url: &Url) -> Result<(), } } -fn generate() -> String { +/// A random token, as hex. +pub(super) fn generate() -> String { let mut bytes = [0u8; TOKEN_BYTES]; getrandom::fill(&mut bytes).expect("the operating system's random source failed"); bytes diff --git a/api/crates/api/src/daily_totals.rs b/api/crates/api/src/daily_totals.rs index b835577..6ad0395 100644 --- a/api/crates/api/src/daily_totals.rs +++ b/api/crates/api/src/daily_totals.rs @@ -2,9 +2,10 @@ //! finished each day, for the charts on the Players and Activity pages. //! //! Counting these on every view would read every run, so a job keeps one row per day. Its first pass builds -//! the whole history from the runs; after that, each pass recounts yesterday and today. A day's player count -//! is the last one taken that day, so a player whose runs are all invalidated later still counts on the days -//! they had a time. A legacy import rebuilds the history, since it changes who had a time when. +//! the whole history from the runs; after that, each pass recounts yesterday and today, and from any earlier +//! day that a late, invalidated or restored run changed (`daily_totals_stale`, filled by a trigger on runs). A +//! day's player count is the last one taken that day, so a player whose runs are all invalidated later still +//! counts on the days they had a time. A legacy import rebuilds the history, since it changes who had a time when. use std::time::Duration; @@ -43,16 +44,27 @@ pub async fn run(db: PgPool) { } } -/// Builds the history if there is none, or else recounts from the day before the newest row to today. +/// Builds the history if there is none, or else recounts to today from the day before the newest row, or from +/// the earliest stale day if that's earlier. pub async fn refresh(db: &PgPool) -> Result<(), sqlx::Error> { - let mut conn = db.acquire().await?; + let mut tx = db.begin().await?; let newest = sqlx::query_scalar!("SELECT max(day) FROM daily_totals") - .fetch_one(&mut *conn) + .fetch_one(&mut *tx) .await?; let Some(newest) = newest else { - return rebuild(&mut conn).await; + rebuild(&mut tx).await?; + return tx.commit().await; }; + // Taken in the same transaction as the recount: a day marked after this is left for the next pass. + let stale = sqlx::query_scalar!( + "WITH taken AS (DELETE FROM daily_totals_stale RETURNING day) SELECT min(day) FROM taken" + ) + .fetch_one(&mut *tx) + .await?; // From the day before, so the last hour of a day that ended since the previous pass is counted too. + let from = stale.map_or(newest - TimeDelta::days(1), |stale| { + stale.min(newest - TimeDelta::days(1)) + }); sqlx::query!( r#" WITH players AS ( @@ -75,11 +87,11 @@ pub async fn refresh(db: &PgPool) -> Result<(), sqlx::Error> { players = CASE WHEN t.day = (now() AT TIME ZONE 'UTC')::date THEN excluded.players ELSE t.players END "#, - newest - TimeDelta::days(1) + from ) - .execute(&mut *conn) + .execute(&mut *tx) .await?; - Ok(()) + tx.commit().await } /// Replaces the history with one counted from every run: each day's players are those whose first recorded @@ -88,6 +100,9 @@ pub async fn rebuild(conn: &mut PgConnection) -> Result<(), sqlx::Error> { sqlx::query!("DELETE FROM daily_totals") .execute(&mut *conn) .await?; + sqlx::query!("DELETE FROM daily_totals_stale") + .execute(&mut *conn) + .await?; sqlx::query!( r#" WITH firsts AS ( @@ -315,7 +330,11 @@ mod tests { use super::{PlayerTotals, RunTotals, refresh}; use crate::{ - runs::tests::{run, setup}, + runs::{ + RunAccepted, + tests::{run, setup}, + }, + staff::runs::invalidate_run, test_client, }; @@ -383,4 +402,40 @@ mod tests { assert_eq!(players.points.last().map(|p| p.players), Some(4)); assert_eq!(players.points[28].players, 3); } + + #[sqlx::test] + async fn recounts_older_days_that_late_or_invalidated_runs_changed(db: PgPool) { + let key = setup(&db).await; + let submit = async |body: Value| -> i64 { + let accepted: RunAccepted = test_client::post(&db, "/v1/runs", Some(&key), &body) + .await + .json(); + accepted.id + }; + let record = submit(run_by("a", 76_561_197_960_287_930, "bhop_badges", 30.0, 3)).await; + submit(run_by("b", 76_561_197_960_287_931, "bhop_badges", 33.0, 3)).await; + refresh(&db).await.unwrap(); + // Points run oldest first, yesterday last: [25] is five days ago, [27] three. + let days = async || -> Vec<(i32, i32)> { + let runs: RunTotals = test_client::get(&db, "/v1/daily-totals/runs").await.json(); + runs.points[25..28] + .iter() + .map(|p| (p.runs, p.world_records)) + .collect() + }; + assert_eq!(days().await, [(0, 0), (0, 0), (2, 1)]); + + // Arrives late, finished five days ago; then the 30.0 is invalidated, so the 33.0 is no record either. + submit(run_by("c", 76_561_197_960_287_932, "bhop_badges", 31.0, 5)).await; + invalidate_run(&db, None, record, Some("test")) + .await + .unwrap(); + refresh(&db).await.unwrap(); + assert_eq!(days().await, [(1, 1), (0, 0), (1, 0)]); + let stale: i64 = sqlx::query_scalar("SELECT count(*) FROM daily_totals_stale") + .fetch_one(&db) + .await + .unwrap(); + assert_eq!(stale, 0, "taken by the pass"); + } } diff --git a/api/crates/api/src/leaderboards.rs b/api/crates/api/src/leaderboards.rs index 7a92848..0b0d5c9 100644 --- a/api/crates/api/src/leaderboards.rs +++ b/api/crates/api/src/leaderboards.rs @@ -18,7 +18,7 @@ use crate::{ app::AppState, error::{ApiError, ErrorBody}, maps::{default_style, played_count}, - players::{PlayerRef, restrictions::Restriction}, + players::PlayerRef, }; use standings::PointsFormula; @@ -157,9 +157,13 @@ async fn out_of( ) -> Result { match (view, track) { (LeaderboardView::Legacy, _) => { - sqlx::query_scalar!(r#"SELECT count(DISTINCT map_id) AS "count!" FROM legacy_runs"#) - .fetch_one(db) - .await + // One index probe per map beats a distinct over every legacy run. + sqlx::query_scalar!( + r#"SELECT count(*) AS "count!" FROM legacy_maps m + WHERE EXISTS (SELECT 1 FROM legacy_runs r WHERE r.map_id = m.id)"# + ) + .fetch_one(db) + .await } (LeaderboardView::Current, LeaderboardTrack::Main) => played_count(db).await, (LeaderboardView::Current, LeaderboardTrack::Bonuses) => bonus_count(db).await, @@ -189,13 +193,13 @@ async fn current( p.steamid64::text AS "steamid64!", p.name AS "name!", p.avatar_url, p.country_code FROM standings s JOIN players p ON p.steamid64 = s.player_steamid64 WHERE s.style = $1 AND s.bonus = $2 AND s.points > 0 - ORDER BY s.points DESC, lower(p.name) LIMIT $3) + ORDER BY s.points DESC, lower(p.name), s.player_steamid64 LIMIT $3) UNION ALL (SELECT false, rank() OVER (ORDER BY s.completed DESC), round(s.points)::int8, s.completed::int8, p.steamid64::text, p.name, p.avatar_url, p.country_code FROM standings s JOIN players p ON p.steamid64 = s.player_steamid64 WHERE s.style = $1 AND s.bonus = $2 - ORDER BY s.completed DESC, lower(p.name) LIMIT $3) + ORDER BY s.completed DESC, lower(p.name), s.player_steamid64 LIMIT $3) "#, style, track == LeaderboardTrack::Bonuses, @@ -230,23 +234,18 @@ async fn legacy(db: &sqlx::PgPool, limit: i64) -> Result, sqlx::Error> WITH shown AS ( SELECT s.player_steamid64, s.points, s.completed, p.name, p.avatar_url, p.country_code FROM legacy_standings s JOIN legacy_players p ON p.steamid64 = s.player_steamid64 - WHERE NOT EXISTS ( - SELECT 1 FROM bans - WHERE bans.player_steamid64 = s.player_steamid64 AND bans.restrictions & $2 <> 0 - AND bans.lifted_at IS NULL AND (bans.ends_at IS NULL OR bans.ends_at > now()) - ) + WHERE NOT EXISTS (SELECT 1 FROM records_bans b WHERE b.player_steamid64 = s.player_steamid64) ) (SELECT true AS "by_points!", rank() OVER (ORDER BY points DESC) AS "rank!", round(points)::int8 AS "points!", completed::int8 AS "completed!", player_steamid64::text AS "steamid64!", name AS "name!", avatar_url, country_code - FROM shown WHERE points > 0 ORDER BY points DESC, lower(name) LIMIT $1) + FROM shown WHERE points > 0 ORDER BY points DESC, lower(name), player_steamid64 LIMIT $1) UNION ALL (SELECT false, rank() OVER (ORDER BY completed DESC), round(points)::int8, completed::int8, player_steamid64::text, name, avatar_url, country_code - FROM shown ORDER BY completed DESC, lower(name) LIMIT $1) + FROM shown ORDER BY completed DESC, lower(name), player_steamid64 LIMIT $1) "#, limit, - Restriction::NoRecords.bit() ) .fetch_all(db) .await?; @@ -359,6 +358,7 @@ mod tests { // plus arcane T1 at the WR, 50. Bonuses count apart: arcane's T2 bonus is worth 25. let (top, _) = lists(&db, "").await; assert_eq!(top, [row("ricochet", 1, 218, 2), row("gabe", 2, 200, 1)]); + standings::tests::assert_totals_current(&db).await; let (top, completed) = lists(&db, "track=bonuses").await; assert_eq!(top, [row("ricochet", 1, 25, 1)]); assert_eq!(completed, top); @@ -367,6 +367,7 @@ mod tests { submit(&db, &key, "e", "bhop_arcane", 0, 18.0, false).await; let (top, _) = lists(&db, "").await; assert_eq!(top, [row("gabe", 1, 250, 2), row("ricochet", 2, 210, 2)]); + standings::tests::assert_totals_current(&db).await; // Taking that run off the boards gives the points back. let run_id: i64 = sqlx::query_scalar("SELECT id FROM runs WHERE server_run_id = 'e'") @@ -379,11 +380,12 @@ mod tests { let (top, completed) = lists(&db, "").await; assert_eq!(top, [row("ricochet", 1, 218, 2), row("gabe", 2, 200, 1)]); assert_eq!(completed[1], row("gabe", 2, 200, 1)); + standings::tests::assert_totals_current(&db).await; // A recount from scratch agrees with the recounts as they happened. - standings::refresh_all(&mut db.acquire().await.unwrap()) - .await - .unwrap(); + let mut tx = db.begin().await.unwrap(); + standings::refresh_all(&mut tx).await.unwrap(); + tx.commit().await.unwrap(); assert_eq!(lists(&db, "").await.0, top); assert_eq!( test_client::get(&db, "/v1/leaderboards?limit=0") @@ -399,10 +401,12 @@ mod tests { ); } - #[sqlx::test] - async fn the_legacy_view_leaves_banned_players_out(db: PgPool) { - setup(&db).await; - sqlx::raw_sql( + /// Two legacy maps, one tiered, and three players: gabe, alice, and a cheat whose records ban ends at + /// `cheat_ban_ends` (SQL). + async fn legacy_board_with_a_cheat(db: &PgPool, cheat_ban_ends: &str) { + setup(db).await; + // Only ever this test file's own SQL in `cheat_ban_ends`. + sqlx::raw_sql(sqlx::AssertSqlSafe(format!( "INSERT INTO legacy_maps (id, name, tier, points) VALUES (1, 'bhop_badges', 2, 0), (2, 'bhop_old', NULL, 0); INSERT INTO legacy_players (steamid64, name) VALUES (76561197960287930, 'gabe'), (76561197960287940, 'alice'), (76561197960287941, 'cheat'); @@ -418,12 +422,17 @@ mod tests { INSERT INTO staff_log (action, target, reason) VALUES ('ban_player', '76561197960287941', 'x') RETURNING id ) - INSERT INTO bans (player_steamid64, restrictions, reason, log_id) - SELECT 76561197960287941, 1, 'x', id FROM entry;", - ) - .execute(&db) + INSERT INTO bans (player_steamid64, restrictions, reason, ends_at, log_id) + SELECT 76561197960287941, 1, 'x', {cheat_ban_ends}, id FROM entry;", + ))) + .execute(db) .await .unwrap(); + } + + #[sqlx::test] + async fn the_legacy_view_leaves_banned_players_out(db: PgPool) { + legacy_board_with_a_cheat(&db, "NULL").await; standings::refresh_legacy(&mut db.acquire().await.unwrap()) .await .unwrap(); @@ -434,4 +443,31 @@ mod tests { assert_eq!(top, [row("alice", 1, 100, 2), row("gabe", 2, 69, 1)]); assert_eq!(completed, top); } + + #[sqlx::test] + async fn a_timed_ban_that_ends_puts_the_player_back_on_the_legacy_view(db: PgPool) { + legacy_board_with_a_cheat(&db, "now() + interval '1 second'").await; + standings::refresh_legacy(&mut db.acquire().await.unwrap()) + .await + .unwrap(); + assert!(!standings::refresh_legacy_if_bans_ended(&db).await.unwrap()); + assert_eq!(lists(&db, "view=legacy").await.0.len(), 2); + + tokio::time::sleep(std::time::Duration::from_millis(1100)).await; + assert!(standings::refresh_legacy_if_bans_ended(&db).await.unwrap()); + // Their 20 s is the record again: all of 100, and alice's 25 s drops to 50 + 50 × (20/25)^4 = 70.5. + let (top, _) = lists(&db, "view=legacy").await; + assert_eq!( + top, + [ + row("cheat", 1, 100, 1), + row("alice", 2, 70, 2), + row("gabe", 3, 58, 1) + ] + ); + assert!( + !standings::refresh_legacy_if_bans_ended(&db).await.unwrap(), + "counted once" + ); + } } diff --git a/api/crates/api/src/leaderboards/list.rs b/api/crates/api/src/leaderboards/list.rs index 368c613..531c4d1 100644 --- a/api/crates/api/src/leaderboards/list.rs +++ b/api/crates/api/src/leaderboards/list.rs @@ -13,7 +13,7 @@ use crate::{ app::AppState, error::{ApiError, ErrorBody}, maps::{check_page, default_style}, - players::{PlayerRef, restrictions::Restriction}, + players::PlayerRef, }; const DEFAULT_LIMIT: i64 = 50; @@ -150,8 +150,9 @@ async fn current( SELECT s.player_steamid64, s.points, s.completed, CASE WHEN $3 THEN rank() OVER (ORDER BY s.points DESC) ELSE rank() OVER (ORDER BY s.completed DESC) END AS rank, + -- Down to the SteamID, so equal points and names keep one order from page to page. row_number() OVER (ORDER BY CASE WHEN $3 THEN s.points ELSE s.completed END DESC, - lower(p.name)) AS position + lower(p.name), s.player_steamid64) AS position FROM standings s JOIN players p ON p.steamid64 = s.player_steamid64 WHERE s.style = $1 AND s.bonus = $2 AND (NOT $3 OR s.points > 0) ), page AS ( @@ -209,19 +210,17 @@ async fn legacy(db: &sqlx::PgPool, page: Page) -> Result<(i64, Vec 0) AND NOT EXISTS ( - SELECT 1 FROM bans - WHERE bans.player_steamid64 = s.player_steamid64 AND bans.restrictions & $2 <> 0 - AND bans.lifted_at IS NULL AND (bans.ends_at IS NULL OR bans.ends_at > now()) + SELECT 1 FROM records_bans b WHERE b.player_steamid64 = s.player_steamid64 ) ), ranked AS ( SELECT shown.*, CASE WHEN $1 THEN rank() OVER (ORDER BY points DESC) ELSE rank() OVER (ORDER BY completed DESC) END AS rank, row_number() OVER (ORDER BY CASE WHEN $1 THEN points ELSE completed END DESC, - lower(name)) AS position + lower(name), player_steamid64) AS position FROM shown ), page AS ( - SELECT * FROM ranked WHERE position > $3 ORDER BY position LIMIT $4 + SELECT * FROM ranked WHERE position > $2 ORDER BY position LIMIT $3 ) SELECT (SELECT count(*) FROM ranked) AS "total!", page.rank AS "rank?", round(page.points)::int8 AS "points?", page.completed::int8 AS "completed?", @@ -231,7 +230,6 @@ async fn legacy(db: &sqlx::PgPool, page: Page) -> Result<(i64, Vec Result Result, sqlx::Error> { + let counted = sqlx::query_scalar!("SELECT formula FROM standings_formula") + .fetch_optional(&mut *conn) + .await?; + Ok(counted.and_then(|counted| serde_json::from_value(counted).ok())) +} + +/// After personal bests changed on these runs' boards: recounts their maps' player counts, then those boards. +/// +/// The player counts are recounted only here: a tier or policy change moves points, not who has a personal +/// best. They're recounted before this module's lock is taken: the callers' map row locks are what keep them +/// right, and every ingest waits on this lock. pub async fn refresh_runs(conn: &mut PgConnection, run_ids: &[i64]) -> Result<(), sqlx::Error> { - refresh(conn, run_ids, &[], &[]).await + crate::maps::count_players(conn, run_ids).await?; + let boards = sqlx::query_as!( + Board, + "SELECT DISTINCT map_id, style, track FROM runs WHERE id = ANY($1)", + run_ids + ) + .fetch_all(&mut *conn) + .await?; + refresh(conn, &boards, run_ids).await } -/// After a track's tier changed: recounts it, in every style. +/// After a track's tier changed: recounts its board in every style. pub async fn refresh_track( conn: &mut PgConnection, map_id: i64, track: i16, ) -> Result<(), sqlx::Error> { - refresh(conn, &[], &[map_id], &[track]).await + // Under the lock, so the list matches the stored boards the recount compares with. + lock(conn).await?; + let boards = sqlx::query_as!( + Board, + "SELECT map_id, style, track FROM standings_boards WHERE map_id = $1 AND track = $2", + map_id, + track + ) + .fetch_all(&mut *conn) + .await?; + refresh(conn, &boards, &[]).await +} + +/// One leaderboard: a map's track in one style. +struct Board { + map_id: i64, + style: String, + track: i16, } -/// Recounts the tracks of `run_ids` and those of `map_ids` × `tracks` (pairs, by position): their personal -/// bests' points, then the totals of every player on them, and of the runs' players, who may have just lost -/// their personal best there. +/// Recounts these boards, and the totals they move: those of everyone on a board whose best time or tier +/// changed, and those of `run_ids`' players, who may have a new personal best there or have just lost one. +/// +/// Everyone else on a changed board kept their time, so their total moves by their points with the new best +/// and tier less those with the stored ones: one read of the board, not of all their personal bests. The runs' +/// players are summed again, as their own time and completed count may have changed and they may have no row +/// yet. Totals moved by changes can differ from a fresh sum in the last digits; `refresh_all` sums them again. async fn refresh( conn: &mut PgConnection, + boards: &[Board], run_ids: &[i64], - map_ids: &[i64], - tracks: &[i16], ) -> Result<(), sqlx::Error> { lock(conn).await?; - let formula = formula(conn).await?; + let Some(formula) = counted_formula(conn).await? else { + // Nothing counted yet, so there's nothing to move: count everything. + return refresh_all(conn).await; + }; let p = formula.policy; + let map_ids: Vec = boards.iter().map(|board| board.map_id).collect(); + let styles: Vec = boards.iter().map(|board| board.style.clone()).collect(); + let tracks: Vec = boards.iter().map(|board| board.track).collect(); + + // One statement, so every part reads the stored boards as they were before it: the changes are measured + // from the old best and tier while the new ones are stored. The first two parts are materialized so each + // is worked out once: inlined, a prepared statement's generic plan redid them for every personal best. sqlx::query!( r#" - WITH tracks AS ( - SELECT map_id, track FROM runs WHERE id = ANY($1) - UNION SELECT * FROM unnest($2::int8[], $3::int2[]) - ), tiers AS ( - SELECT DISTINCT ON (map_id, track) map_id, track, tier FROM map_tiers - WHERE $4 AND (map_id, track) IN (SELECT map_id, track FROM tracks) - ORDER BY map_id, track, assigned_at DESC - ), scored AS ( - SELECT pb.player_steamid64, pb.map_id, pb.style, pb.track, - coalesce(points_for(t.tier, pb.time, - min(pb.time) OVER (PARTITION BY pb.map_id, pb.style, pb.track), - pb.track > 0, $5, $6, $7, $8, $9), 0) AS points - FROM personal_bests pb - LEFT JOIN tiers t ON t.map_id = pb.map_id AND t.track = pb.track - WHERE (pb.map_id, pb.track) IN (SELECT map_id, track FROM tracks) + WITH now AS MATERIALIZED ( + SELECT b.map_id, b.style, b.track, + (SELECT min(pb.time) FROM personal_bests pb + WHERE pb.map_id = b.map_id AND pb.style = b.style AND pb.track = b.track) AS best, + CASE WHEN $5 THEN (SELECT t.tier FROM map_tiers t WHERE t.map_id = b.map_id AND t.track = b.track + ORDER BY t.assigned_at DESC LIMIT 1) END AS tier + FROM unnest($1::int8[], $2::text[], $3::int2[]) AS b (map_id, style, track) + ), runners AS MATERIALIZED ( + SELECT DISTINCT player_steamid64, style, track > 0 AS bonus FROM runs WHERE id = ANY($4) + ), moved AS ( + SELECT now.*, stored.best AS stored_best, stored.tier AS stored_tier + FROM now JOIN standings_boards stored USING (map_id, style, track) + WHERE now.best IS NOT NULL AND (now.best, now.tier) IS DISTINCT FROM (stored.best, stored.tier) + ), changes AS ( + SELECT pb.style, pb.track > 0 AS bonus, pb.player_steamid64, + sum(coalesce(points_for(m.tier, pb.time, m.best, pb.track > 0, $6, $7, $8, $9, $10), 0) + - coalesce(points_for(m.stored_tier, pb.time, m.stored_best, pb.track > 0, + $6, $7, $8, $9, $10), 0)) AS change + FROM moved m + JOIN personal_bests pb ON pb.map_id = m.map_id AND pb.style = m.style AND pb.track = m.track + -- The runs' players are summed again below. + WHERE NOT EXISTS ( + SELECT 1 FROM runners r + WHERE r.player_steamid64 = pb.player_steamid64 AND r.style = pb.style AND r.bonus = (pb.track > 0) + ) + GROUP BY 1, 2, 3 + ), moved_totals AS ( + UPDATE standings s SET points = s.points + c.change FROM changes c + WHERE s.style = c.style AND s.bonus = c.bonus AND s.player_steamid64 = c.player_steamid64 + AND c.change <> 0 + ), emptied AS ( + DELETE FROM standings_boards stored USING now + WHERE stored.map_id = now.map_id AND stored.style = now.style AND stored.track = now.track + AND now.best IS NULL ) - UPDATE personal_bests pb SET points = scored.points FROM scored - WHERE pb.player_steamid64 = scored.player_steamid64 AND pb.map_id = scored.map_id - AND pb.style = scored.style AND pb.track = scored.track AND pb.points <> scored.points + INSERT INTO standings_boards (map_id, style, track, best, tier) + SELECT map_id, style, track, best, tier FROM now WHERE best IS NOT NULL + ON CONFLICT (map_id, style, track) DO UPDATE SET best = excluded.best, tier = excluded.tier "#, + &map_ids, + &styles, + &tracks, run_ids, - map_ids, - tracks, formula.tiers, f64::from(p.base), f64::from(p.growth_percent), @@ -115,46 +199,50 @@ async fn refresh( .execute(&mut *conn) .await?; - // Delete-then-insert under the lock: a player who no longer has a personal best there loses their row. + sum_runners_again(conn, run_ids, &formula.policy).await +} + +/// The runs' players' totals, summed again from the boards as now stored. Delete-then-insert under the lock: a +/// player who no longer has a personal best there loses their row. +async fn sum_runners_again( + conn: &mut PgConnection, + run_ids: &[i64], + p: &PointsPolicy, +) -> Result<(), sqlx::Error> { sqlx::query!( r#" - WITH tracks AS ( - SELECT map_id, track FROM runs WHERE id = ANY($1) - UNION SELECT * FROM unnest($2::int8[], $3::int2[]) - ), affected AS ( - SELECT style, track > 0 AS bonus, player_steamid64 FROM personal_bests - WHERE (map_id, track) IN (SELECT map_id, track FROM tracks) - UNION SELECT style, track > 0, player_steamid64 FROM runs WHERE id = ANY($1) - ) - DELETE FROM standings s USING affected a - WHERE s.style = a.style AND s.bonus = a.bonus AND s.player_steamid64 = a.player_steamid64 + DELETE FROM standings s USING runs r + WHERE r.id = ANY($1) + AND s.style = r.style AND s.bonus = (r.track > 0) AND s.player_steamid64 = r.player_steamid64 "#, - run_ids, - map_ids, - tracks + run_ids ) .execute(&mut *conn) .await?; sqlx::query!( r#" - WITH tracks AS ( - SELECT map_id, track FROM runs WHERE id = ANY($1) - UNION SELECT * FROM unnest($2::int8[], $3::int2[]) - ), affected AS ( - SELECT style, track > 0 AS bonus, player_steamid64 FROM personal_bests - WHERE (map_id, track) IN (SELECT map_id, track FROM tracks) - UNION SELECT style, track > 0, player_steamid64 FROM runs WHERE id = ANY($1) - ) + WITH affected AS (SELECT DISTINCT style, track > 0 AS bonus, player_steamid64 FROM runs WHERE id = ANY($1)) INSERT INTO standings (style, bonus, player_steamid64, points, completed) - SELECT pb.style, pb.track > 0, pb.player_steamid64, sum(pb.points), count(*) - FROM personal_bests pb - JOIN affected a ON a.style = pb.style AND a.bonus = (pb.track > 0) - AND a.player_steamid64 = pb.player_steamid64 - GROUP BY pb.style, pb.track > 0, pb.player_steamid64 + SELECT a.style, a.bonus, a.player_steamid64, totals.points, totals.completed + FROM affected a + -- Each player's own personal bests, through the primary key: joined on the list instead, the planner + -- can't tell how short it is and reads every personal best. + CROSS JOIN LATERAL ( + SELECT sum(coalesce(points_for(b.tier, pb.time, b.best, pb.track > 0, $2, $3, $4, $5, $6), 0)) + AS points, + count(*) AS completed + FROM personal_bests pb + LEFT JOIN standings_boards b ON b.map_id = pb.map_id AND b.style = pb.style AND b.track = pb.track + WHERE pb.player_steamid64 = a.player_steamid64 AND pb.style = a.style AND (pb.track > 0) = a.bonus + ) totals + WHERE totals.completed > 0 "#, run_ids, - map_ids, - tracks + f64::from(p.base), + f64::from(p.growth_percent), + f64::from(p.finish_percent), + f64::from(p.speed_exponent), + f64::from(p.bonus_percent) ) .execute(&mut *conn) .await?; @@ -162,33 +250,28 @@ async fn refresh( } /// Recounts everything, 2.0 and legacy, with the formula in force, and records that formula. +/// +/// It writes the boards and totals alone, so personal bests stay free for the ingest; runs stored meanwhile +/// wait on the lock to recount, then do so from what this stored. pub async fn refresh_all(conn: &mut PgConnection) -> Result<(), sqlx::Error> { lock(conn).await?; let formula = formula(conn).await?; let p = formula.policy; + sqlx::query!("DELETE FROM standings_boards") + .execute(&mut *conn) + .await?; sqlx::query!( r#" WITH tiers AS ( SELECT DISTINCT ON (map_id, track) map_id, track, tier FROM map_tiers WHERE $1 ORDER BY map_id, track, assigned_at DESC - ), scored AS ( - SELECT pb.player_steamid64, pb.map_id, pb.style, pb.track, - coalesce(points_for(t.tier, pb.time, - min(pb.time) OVER (PARTITION BY pb.map_id, pb.style, pb.track), - pb.track > 0, $2, $3, $4, $5, $6), 0) AS points - FROM personal_bests pb - LEFT JOIN tiers t ON t.map_id = pb.map_id AND t.track = pb.track ) - UPDATE personal_bests pb SET points = scored.points FROM scored - WHERE pb.player_steamid64 = scored.player_steamid64 AND pb.map_id = scored.map_id - AND pb.style = scored.style AND pb.track = scored.track AND pb.points <> scored.points + INSERT INTO standings_boards (map_id, style, track, best, tier) + SELECT b.map_id, b.style, b.track, b.best, t.tier + FROM (SELECT map_id, style, track, min(time) AS best FROM personal_bests GROUP BY 1, 2, 3) b + LEFT JOIN tiers t ON t.map_id = b.map_id AND t.track = b.track "#, - formula.tiers, - f64::from(p.base), - f64::from(p.growth_percent), - f64::from(p.finish_percent), - f64::from(p.speed_exponent), - f64::from(p.bonus_percent) + formula.tiers ) .execute(&mut *conn) .await?; @@ -198,9 +281,17 @@ pub async fn refresh_all(conn: &mut PgConnection) -> Result<(), sqlx::Error> { sqlx::query!( r#" INSERT INTO standings (style, bonus, player_steamid64, points, completed) - SELECT style, track > 0, player_steamid64, sum(points), count(*) - FROM personal_bests GROUP BY style, track > 0, player_steamid64 - "# + SELECT pb.style, pb.track > 0, pb.player_steamid64, + sum(coalesce(points_for(b.tier, pb.time, b.best, pb.track > 0, $1, $2, $3, $4, $5), 0)), count(*) + FROM personal_bests pb + JOIN standings_boards b ON b.map_id = pb.map_id AND b.style = pb.style AND b.track = pb.track + GROUP BY 1, 2, 3 + "#, + f64::from(p.base), + f64::from(p.growth_percent), + f64::from(p.finish_percent), + f64::from(p.speed_exponent), + f64::from(p.bonus_percent) ) .execute(&mut *conn) .await?; @@ -224,18 +315,66 @@ pub async fn refresh_legacy(conn: &mut PgConnection) -> Result<(), sqlx::Error> count_legacy(conn, &formula).await } -/// At startup: recounts everything if the formula in force isn't the one the stored points were counted with, -/// as after this table's migration or a new draft in code. +/// Runs for as long as the API does, starting with a look at startup, which also finds bans that ended while +/// it was down. +pub async fn watch_ended_bans(db: PgPool) { + let mut ticks = tokio::time::interval(ENDED_BANS_INTERVAL); + ticks.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Delay); + loop { + ticks.tick().await; + match refresh_legacy_if_bans_ended(&db).await { + Ok(true) => tracing::info!("recounted legacy standings after a records ban ended"), + Ok(false) => {} + Err(error) => { + tracing::error!(%error, "looking for ended bans failed; trying again later"); + } + } + } +} + +/// Recounts the legacy standings if they're missing a player with legacy runs whose records ban is over: one +/// that ended on its date since they were counted. Lifting a ban recounts by itself, so this finds only those. +pub async fn refresh_legacy_if_bans_ended(db: &PgPool) -> Result { + // Looked for outside the lock, which ingest waits on; the recount reads what's committed under it. + let stale = sqlx::query_scalar!( + r#"SELECT EXISTS ( + SELECT 1 FROM bans b + WHERE b.restrictions & $1 <> 0 + AND NOT EXISTS (SELECT 1 FROM records_bans r WHERE r.player_steamid64 = b.player_steamid64) + AND NOT EXISTS (SELECT 1 FROM legacy_standings s WHERE s.player_steamid64 = b.player_steamid64) + AND EXISTS (SELECT 1 FROM legacy_runs l WHERE l.player_steamid64 = b.player_steamid64) + ) AS "stale!""#, + Restriction::NoRecords.bit() + ) + .fetch_one(db) + .await?; + if stale { + let mut tx = db.begin().await?; + refresh_legacy(&mut tx).await?; + tx.commit().await?; + } + Ok(stale) +} + +/// Runs once after startup, beside requests: recounts everything if the formula in force isn't the one the +/// stored totals were counted with, as after a new draft or formula in code. Until it's done, pages show the +/// totals counted before, with the points they were counted with, and runs stored meanwhile wait to recount. +pub async fn recount_if_stale(db: PgPool) { + match refresh_if_stale(&db).await { + Ok(true) => tracing::info!("recounted points and standings with the formula in force"), + Ok(false) => {} + Err(error) => { + tracing::error!(%error, "recounting the standings failed; restart to try again"); + } + } +} + +/// Recounts everything if the formula in force isn't the one the stored totals were counted with. Returns +/// whether it did. pub async fn refresh_if_stale(db: &PgPool) -> Result { let mut tx = db.begin().await?; - let counted = sqlx::query_scalar!("SELECT formula FROM standings_formula") - .fetch_optional(&mut *tx) - .await?; let in_force = formula(&mut tx).await?; - let current = counted - .and_then(|counted| serde_json::from_value::(counted).ok()) - .is_some_and(|counted| counted == in_force); - if current { + if counted_formula(&mut tx).await?.as_ref() == Some(&in_force) { return Ok(false); } refresh_all(&mut tx).await?; @@ -258,11 +397,7 @@ async fn count_legacy(conn: &mut PgConnection, formula: &PointsFormula) -> Resul $1, $2, $3, $4, $5), 0) AS points FROM legacy_personal_bests l JOIN legacy_maps m ON m.id = l.map_id - WHERE NOT EXISTS ( - SELECT 1 FROM bans - WHERE bans.player_steamid64 = l.player_steamid64 AND bans.restrictions & $6 <> 0 - AND bans.lifted_at IS NULL AND (bans.ends_at IS NULL OR bans.ends_at > now()) - ) + WHERE NOT EXISTS (SELECT 1 FROM records_bans b WHERE b.player_steamid64 = l.player_steamid64) ) scored GROUP BY player_steamid64 "#, @@ -270,8 +405,7 @@ async fn count_legacy(conn: &mut PgConnection, formula: &PointsFormula) -> Resul f64::from(p.growth_percent), f64::from(p.finish_percent), f64::from(p.speed_exponent), - f64::from(p.bonus_percent), - Restriction::NoRecords.bit() + f64::from(p.bonus_percent) ) .execute(&mut *conn) .await?; @@ -286,3 +420,138 @@ async fn lock(conn: &mut PgConnection) -> Result<(), sqlx::Error> { .await?; Ok(()) } + +#[cfg(test)] +pub(crate) mod tests { + use std::time::Duration; + + use sqlx::PgPool; + + use crate::{ + runs::tests::{run, setup}, + test_client, + }; + + /// Fails if the stored totals or boards differ from a full recount, beyond the last digits that moving + /// totals by changes can leave. The recount is rolled back. + pub(crate) async fn assert_totals_current(db: &PgPool) { + async fn read( + conn: &mut sqlx::PgConnection, + ) -> (Vec<(String, bool, i64, f64, i32)>, Vec) { + let totals = sqlx::query_as( + "SELECT style, bonus, player_steamid64, points, completed FROM standings ORDER BY 1, 2, 3", + ) + .fetch_all(&mut *conn) + .await + .unwrap(); + let boards = sqlx::query_scalar( + "SELECT concat_ws(' ', map_id, style, track, best, tier) FROM standings_boards ORDER BY 1", + ) + .fetch_all(&mut *conn) + .await + .unwrap(); + (totals, boards) + } + // What pages show adds up to the stored totals. + let unequal: i64 = sqlx::query_scalar( + "SELECT count(*) FROM standings s + JOIN (SELECT style, track > 0 AS bonus, player_steamid64, + sum(personal_best_points(map_id, style, track, time)) AS points + FROM personal_bests GROUP BY 1, 2, 3) shown USING (style, bonus, player_steamid64) + WHERE abs(s.points - shown.points) > 1e-6", + ) + .fetch_one(db) + .await + .unwrap(); + assert_eq!( + unequal, 0, + "points shown on pages don't add up to the stored totals" + ); + let mut tx = db.begin().await.unwrap(); + let (stored, stored_boards) = read(&mut tx).await; + super::refresh_all(&mut tx).await.unwrap(); + let (fresh, fresh_boards) = read(&mut tx).await; + tx.rollback().await.unwrap(); + assert_eq!(stored_boards, fresh_boards, "stored boards drifted"); + let same = stored.len() == fresh.len() + && stored.iter().zip(&fresh).all(|(s, f)| { + (&s.0, s.1, s.2, s.4) == (&f.0, f.1, f.2, f.4) && (s.3 - f.3).abs() < 1e-6 + }); + assert!(same, "stored totals drifted:\n{stored:?}\n{fresh:?}"); + } + + /// The ingest writes a personal best, then recounts. A full recount started in between counts without it, + /// and the ingest's own recount then counts it: no deadlock, and nothing missed. + #[sqlx::test] + async fn a_full_recount_waits_for_a_run_being_stored(db: PgPool) { + let key = setup(&db).await; + test_client::post(&db, "/v1/runs", Some(&key), &run("a", 30.0)).await; + let run_id: i64 = sqlx::query_scalar("SELECT run_id FROM personal_bests") + .fetch_one(&db) + .await + .unwrap(); + + // As the ingest does: the map's row and the personal best's first, the recount after. + let mut ingest = db.begin().await.unwrap(); + sqlx::query( + "INSERT INTO maps (name) VALUES ('bhop_badges') ON CONFLICT (name) DO UPDATE SET name = excluded.name", + ) + .execute(&mut *ingest) + .await + .unwrap(); + sqlx::query("UPDATE personal_bests SET time = 29.0") + .execute(&mut *ingest) + .await + .unwrap(); + let full = tokio::spawn({ + let db = db.clone(); + async move { + let mut tx = db.begin().await?; + super::refresh_all(&mut tx).await?; + tx.commit().await + } + }); + tokio::time::sleep(Duration::from_millis(300)).await; + super::refresh_runs(&mut ingest, &[run_id]).await.unwrap(); + ingest.commit().await.unwrap(); + full.await.unwrap().unwrap(); + assert_totals_current(&db).await; + } + + /// A tier change waiting for the standings lock, then a full recount behind it. They once deadlocked, when + /// recounts wrote personal bests. + #[sqlx::test] + async fn a_full_recount_waits_for_a_tier_change(db: PgPool) { + let key = setup(&db).await; + test_client::post(&db, "/v1/runs", Some(&key), &run("a", 30.0)).await; + let map_id: i64 = sqlx::query_scalar("SELECT id FROM maps") + .fetch_one(&db) + .await + .unwrap(); + + // Another recount holds the lock, as a legacy recount would, so both queue behind it in this order. + let mut holder = db.begin().await.unwrap(); + super::lock(&mut holder).await.unwrap(); + let tier = tokio::spawn({ + let db = db.clone(); + async move { + let mut tx = db.begin().await?; + super::refresh_track(&mut tx, map_id, 0).await?; + tx.commit().await + } + }); + tokio::time::sleep(Duration::from_millis(300)).await; + let full = tokio::spawn({ + let db = db.clone(); + async move { + let mut tx = db.begin().await?; + super::refresh_all(&mut tx).await?; + tx.commit().await + } + }); + tokio::time::sleep(Duration::from_millis(300)).await; + holder.commit().await.unwrap(); + tier.await.unwrap().unwrap(); + full.await.unwrap().unwrap(); + } +} diff --git a/api/crates/api/src/leaderboards/world_records.rs b/api/crates/api/src/leaderboards/world_records.rs index 4be469f..d4b9e34 100644 --- a/api/crates/api/src/leaderboards/world_records.rs +++ b/api/crates/api/src/leaderboards/world_records.rs @@ -1,11 +1,12 @@ //! Stored world records: `world_record_holders` (who holds each board's record now) and `runs.was_record` -//! (whether a run was a record when it was set), kept current so profiles, the points leaderboards and -//! Activity read them instead of comparing every personal best or run with its board. +//! (whether a run was a record when it was set), kept current so profiles, the points leaderboards, Activity, +//! WR history and the Discord feeds read them instead of comparing every personal best or run with its board. +//! The pre-2.0 record history (`legacy.rs`) works its own out, as legacy runs have no stored flag. //! //! Both change only when a board's runs or personal bests do, so the two places that change those call //! `refresh` for the boards they touched: the ingest, for every valid run it stores, and run invalidation //! and restoring. A run arriving late, with an earlier finish time, can take the record-when-set from runs -//! after it, so the whole board is redone, not just the new run. +//! after it, so its board is redone from that run on, not just the run itself. //! //! Concurrency: each refresh reads the board from what's committed, under the board's map row lock. The ingest //! already holds it (`maps::record_run` updates the row); invalidation takes it with `lock_maps` before @@ -49,16 +50,18 @@ pub async fn refresh(conn: &mut PgConnection, run_ids: &[i64]) -> Result Result= (b.finished_at, b.id) ) - UPDATE runs r SET was_record = earlier.was_record FROM earlier - WHERE r.id = earlier.id AND r.was_record <> earlier.was_record + UPDATE runs r SET was_record = placed.was_record FROM placed + WHERE r.id = placed.id AND r.was_record <> placed.was_record "#, run_ids ) @@ -95,9 +112,10 @@ pub async fn refresh(conn: &mut PgConnection, run_ids: &[i64]) -> Result Result { let mut tx = db.begin().await?; - // One run per board names it. - let run_ids = - sqlx::query_scalar!(r#"SELECT min(id) AS "id!" FROM runs GROUP BY map_id, style, track"#) + // Each board's earliest run names it, so every run on it is redone. + let run_ids = sqlx::query_scalar!( + r#"SELECT DISTINCT ON (map_id, style, track) id FROM runs ORDER BY map_id, style, track, finished_at, id"# + ) .fetch_all(&mut *tx) .await?; let changed = refresh(&mut tx, &run_ids).await?; @@ -168,6 +186,13 @@ pub(crate) mod tests { send("c1", 28.0, "76561197960287940", "alice", 400).await; assert_eq!(records_when_set(&db).await, ["b1", "c1"]); assert_eq!(holders(&db).await, ["gabe", "ricochet"]); + + // One that lands in the middle: the best before it is c1's 28, the latest record before it. + send("d1", 27.0, "76561197960287941", "bob", 250).await; + assert_eq!(records_when_set(&db).await, ["b1", "c1", "d1"]); + // Slower than c1 and in the middle too: not a record, and nothing after it moves. + send("e1", 29.0, "76561197960287942", "eve", 350).await; + assert_eq!(records_when_set(&db).await, ["b1", "c1", "d1"]); assert_current(&db).await; } } diff --git a/api/crates/api/src/legacy.rs b/api/crates/api/src/legacy.rs index 0bd15b8..a37842f 100644 --- a/api/crates/api/src/legacy.rs +++ b/api/crates/api/src/legacy.rs @@ -10,11 +10,7 @@ use sqlx::PgPool; use utoipa::ToSchema; use utoipa_axum::{router::OpenApiRouter, routes}; -use crate::{ - app::AppState, - maps::wr_history::WrHistoryEntry, - players::{PlayerRef, restrictions::Restriction}, -}; +use crate::{app::AppState, maps::wr_history::WrHistoryEntry, players::PlayerRef}; mod dump; pub mod import; @@ -62,13 +58,10 @@ pub async fn board_players(db: &PgPool, map: &str) -> Result, sqlx:: sqlx::query_scalar!( r#"SELECT (SELECT count(*) FROM legacy_personal_bests l WHERE l.map_id = lm.id AND NOT EXISTS ( - SELECT 1 FROM bans - WHERE bans.player_steamid64 = l.player_steamid64 AND bans.restrictions & $2 <> 0 - AND bans.lifted_at IS NULL AND (bans.ends_at IS NULL OR bans.ends_at > now()) + SELECT 1 FROM records_bans b WHERE b.player_steamid64 = l.player_steamid64 )) AS "players!" FROM legacy_maps lm WHERE lm.name = $1"#, map, - Restriction::NoRecords.bit() ) .fetch_optional(db) .await @@ -90,35 +83,30 @@ pub struct BoardTotals { pub async fn board_totals(db: &PgPool, map: &str) -> Result, sqlx::Error> { sqlx::query_as!( BoardTotals, - r#"WITH banned AS ( - SELECT player_steamid64 FROM bans - WHERE restrictions & $2 <> 0 AND lifted_at IS NULL AND (ends_at IS NULL OR ends_at > now()) - ) - SELECT + r#"SELECT (SELECT count(*) FROM legacy_personal_bests l WHERE l.map_id = lm.id - AND NOT EXISTS (SELECT 1 FROM banned b WHERE b.player_steamid64 = l.player_steamid64) + AND NOT EXISTS (SELECT 1 FROM records_bans b WHERE b.player_steamid64 = l.player_steamid64) ) AS "players!", (SELECT count(*) FROM legacy_runs l WHERE l.map_id = lm.id - AND NOT EXISTS (SELECT 1 FROM banned b WHERE b.player_steamid64 = l.player_steamid64) + AND NOT EXISTS (SELECT 1 FROM records_bans b WHERE b.player_steamid64 = l.player_steamid64) ) AS "runs!", (SELECT count(*) FROM ( SELECT l.player_steamid64 FROM legacy_personal_bests l WHERE l.map_id = lm.id - AND NOT EXISTS (SELECT 1 FROM banned b WHERE b.player_steamid64 = l.player_steamid64) + AND NOT EXISTS (SELECT 1 FROM records_bans b WHERE b.player_steamid64 = l.player_steamid64) UNION SELECT pb.player_steamid64 FROM personal_bests pb JOIN maps m ON m.id = pb.map_id - WHERE m.name = $1 AND pb.style = $3 AND pb.track = $4 + WHERE m.name = $1 AND pb.style = $2 AND pb.track = $3 ) both_kinds) AS "all_players!", (SELECT max(l.finished_at) FROM legacy_runs l WHERE l.map_id = lm.id - AND NOT EXISTS (SELECT 1 FROM banned b WHERE b.player_steamid64 = l.player_steamid64) + AND NOT EXISTS (SELECT 1 FROM records_bans b WHERE b.player_steamid64 = l.player_steamid64) ) AS last_run_at FROM legacy_maps lm WHERE lm.name = $1"#, map, - Restriction::NoRecords.bit(), STYLE, TRACK ) @@ -145,9 +133,7 @@ pub async fn record_history(db: &PgPool, map: &str) -> Result 0 - AND bans.lifted_at IS NULL AND (bans.ends_at IS NULL OR bans.ends_at > now()) + SELECT 1 FROM records_bans b WHERE b.player_steamid64 = l.player_steamid64 ) ) r JOIN legacy_players lp ON lp.steamid64 = r.player_steamid64 @@ -156,7 +142,6 @@ pub async fn record_history(db: &PgPool, map: &str) -> Result 0 AND lifted_at IS NULL AND (ends_at IS NULL OR ends_at > now()) - ) SELECT l.id, lm.name AS map, l.player_name, l.time, l.jumps, l.strafes, l.sync, l.finished_at, l.server_hostname, l.player_steamid64::text AS "steamid64!", coalesce(p.name, lp.name) AS "name!", @@ -68,17 +64,16 @@ pub async fn legacy_run_detail( coalesce(p.country_code, lp.country_code) AS country_code, (SELECT min(o.time) FROM legacy_runs o WHERE o.map_id = l.map_id - AND NOT EXISTS (SELECT 1 FROM banned b WHERE b.player_steamid64 = o.player_steamid64) + AND NOT EXISTS (SELECT 1 FROM records_bans b WHERE b.player_steamid64 = o.player_steamid64) ) AS "record_time!" FROM legacy_runs l JOIN legacy_maps lm ON lm.id = l.map_id JOIN legacy_players lp ON lp.steamid64 = l.player_steamid64 LEFT JOIN players p ON p.steamid64 = l.player_steamid64 WHERE l.id = $1 - AND NOT EXISTS (SELECT 1 FROM banned b WHERE b.player_steamid64 = l.player_steamid64) + AND NOT EXISTS (SELECT 1 FROM records_bans b WHERE b.player_steamid64 = l.player_steamid64) "#, id, - Restriction::NoRecords.bit() ) .fetch_optional(&state.db) .await? diff --git a/api/crates/api/src/main.rs b/api/crates/api/src/main.rs index d5a886d..0d66454 100644 --- a/api/crates/api/src/main.rs +++ b/api/crates/api/src/main.rs @@ -391,11 +391,21 @@ async fn serve() -> Result<(), Box> { let staging = storage::Staging::from_env()?; let discord_feeds = record_events::discord::feeds_from_env()?; - if leaderboards::standings::refresh_if_stale(&db).await? { - tracing::info!("recounted points and standings with the formula in force"); - } - tokio::spawn(replays::processing::run(db.clone(), storage.clone())); + // In the background: a full recount takes minutes on a lot of data, and pages can show the totals counted + // before meanwhile. + tokio::spawn(leaderboards::standings::recount_if_stale(db.clone())); + // In the background: storage being down shouldn't keep the API from starting. + tokio::spawn(replays::download::forget_derived_shavit_files_at_start( + db.clone(), + storage.clone(), + )); + tokio::spawn(replays::processing::run( + db.clone(), + storage.clone(), + staging.clone(), + )); tokio::spawn(daily_totals::run(db.clone())); + tokio::spawn(leaderboards::standings::watch_ended_bans(db.clone())); tokio::spawn(record_events::run(db.clone())); tokio::spawn(servers::heartbeat::run(db.clone())); let (wake_streams, record_events) = tokio::sync::watch::channel(()); diff --git a/api/crates/api/src/maps.rs b/api/crates/api/src/maps.rs index ecab8e1..6e15a9e 100644 --- a/api/crates/api/src/maps.rs +++ b/api/crates/api/src/maps.rs @@ -61,8 +61,8 @@ pub(crate) fn check_page(limit: i64, offset: i64, max: i64) -> Result<(), String Ok(()) } -/// A map name as shavit reports it: lowercase letters, digits, `_`, `-` and `.`, at most 64 characters. -/// Input is lowercased first, so `bhop_Badges` and `bhop_badges` are the same map. +/// A map name as shavit reports it: lowercase letters, digits, `_`, `-` and `.`, at most 64 characters, and +/// not only dots. Input is lowercased first, so `bhop_Badges` and `bhop_badges` are the same map. #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] #[serde(try_from = "String", into = "String")] pub struct MapName(String); @@ -79,7 +79,9 @@ impl TryFrom for MapName { fn try_from(text: String) -> Result { let name = text.to_lowercase(); let allowed = |c: char| c.is_ascii_lowercase() || c.is_ascii_digit() || "_-.".contains(c); - if name.is_empty() || name.len() > 64 || !name.chars().all(allowed) { + // Names go into storage keys and URL paths, where `.` and `..` mean this folder and the one above. + let only_dots = name.chars().all(|c| c == '.'); + if name.is_empty() || name.len() > 64 || !name.chars().all(allowed) || only_dots { return Err(format!("{text:?} is not a valid map name")); } Ok(Self(name)) @@ -121,6 +123,22 @@ pub async fn record_run( Ok(()) } +/// Recounts `maps.players` for these runs' maps, after their personal bests changed. Callers hold those maps' +/// rows, so a personal best written there meanwhile either waits for this transaction or has committed and is +/// counted. +pub async fn count_players(conn: &mut PgConnection, run_ids: &[i64]) -> Result<(), sqlx::Error> { + sqlx::query!( + r#" + UPDATE maps m SET players = (SELECT count(DISTINCT player_steamid64) FROM personal_bests WHERE map_id = m.id) + WHERE m.id IN (SELECT map_id FROM runs WHERE id = ANY($1)) + "#, + run_ids + ) + .execute(conn) + .await?; + Ok(()) +} + #[cfg(test)] mod tests { use super::MapName; @@ -134,5 +152,12 @@ mod tests { assert!(parse("../etc/passwd").is_err()); assert!(parse("bhop badges").is_err()); assert!(parse("").is_err()); + for dots in [".", "..", "..."] { + assert!(parse(dots).is_err(), "{dots}"); + } + assert!( + parse(".bhop.").is_ok(), + "dots among other characters are fine" + ); } } diff --git a/api/crates/api/src/maps/leaderboard.rs b/api/crates/api/src/maps/leaderboard.rs index abe7dde..cacc0e5 100644 --- a/api/crates/api/src/maps/leaderboard.rs +++ b/api/crates/api/src/maps/leaderboard.rs @@ -17,7 +17,7 @@ use crate::{ app::AppState, error::{ApiError, ErrorBody}, legacy::{self, View}, - players::{PlayerRef, SteamId64, restrictions::Restriction}, + players::{PlayerRef, SteamId64}, replays::current_policy, }; @@ -170,7 +170,7 @@ pub(super) async fn entries( UNION ALL SELECT style, track FROM every_board WHERE $2::text IS NULL ), page AS ( - SELECT pb.style, pb.track, pb.player_steamid64, pb.time, pb.finished_at, pb.run_id, pb.points, + SELECT pb.style, pb.track, pb.player_steamid64, pb.time, pb.finished_at, pb.run_id, rank() OVER board AS page_rank, min(pb.time) OVER board AS first_time FROM boards b CROSS JOIN LATERAL ( @@ -197,7 +197,8 @@ pub(super) async fn entries( SELECT p.style, p.track, p.time, p.run_id, p.finished_at, CASE WHEN p.time = p.first_time THEN a.faster_than_first + 1 ELSE a.before_page + p.page_rank END AS "rank!", - r.jumps, r.strafes, r.sync, p.points, rp.stored_key IS NOT NULL AS "stored!", + r.jumps, r.strafes, r.sync, personal_best_points($1, p.style, p.track, p.time) AS "points!", + rp.stored_key IS NOT NULL AS "stored!", pl.steamid64::text AS "steamid64!", pl.name, pl.avatar_url, pl.country_code FROM page p JOIN ahead a ON a.style = p.style AND a.track = p.track @@ -450,19 +451,19 @@ async fn view_page( WITH board AS ( SELECT DISTINCT ON (player) * FROM ( SELECT false AS legacy, pb.player_steamid64 AS player, pb.time, r.finished_at, - pb.run_id AS id, r.jumps, r.strafes, r.sync, pb.points, rp.stored_key IS NOT NULL AS stored + pb.run_id AS id, r.jumps, r.strafes, r.sync, + personal_best_points(pb.map_id, pb.style, pb.track, pb.time) AS points, + rp.stored_key IS NOT NULL AS stored FROM personal_bests pb JOIN runs r ON r.id = pb.run_id LEFT JOIN replays rp ON rp.run_id = pb.run_id - WHERE $3 AND pb.map_id = $1 AND pb.style = $10 AND pb.track = $11 + WHERE $3 AND pb.map_id = $1 AND pb.style = $9 AND pb.track = $10 UNION ALL SELECT true, l.player_steamid64, l.time, l.finished_at, l.id::bigint, l.jumps, l.strafes, l.sync, 0, false FROM legacy_personal_bests l -- By ID, not a join on name, so the view picks bests on this map alone, not across all runs. WHERE l.map_id = (SELECT id FROM legacy_maps WHERE name = $2) AND NOT EXISTS ( - SELECT 1 FROM bans - WHERE bans.player_steamid64 = l.player_steamid64 AND bans.restrictions & $4 <> 0 - AND bans.lifted_at IS NULL AND (bans.ends_at IS NULL OR bans.ends_at > now()) + SELECT 1 FROM records_bans b WHERE b.player_steamid64 = l.player_steamid64 ) ) both_kinds -- Each player's fastest run of either kind; on equal times, their 2.0 run. @@ -482,16 +483,15 @@ async fn view_page( FROM ranked LEFT JOIN players p ON p.steamid64 = ranked.player LEFT JOIN legacy_players lp ON lp.steamid64 = ranked.player - WHERE $5::float8 IS NULL + WHERE $4::float8 IS NULL OR (ranked.time, ranked.sort_at, ranked.legacy, ranked.id) - > ($5, coalesce($6::timestamptz, 'infinity'), $7::bool, $8::bigint) + > ($4, coalesce($5::timestamptz, 'infinity'), $6::bool, $7::bigint) ORDER BY ranked.time, ranked.sort_at, ranked.legacy, ranked.id - LIMIT $9 + LIMIT $8 "#, map_id, map.as_str(), view == View::All, - Restriction::NoRecords.bit(), after.map(|(time, _, _, _)| time), after.and_then(|(_, finished_at, _, _)| finished_at), after.map(|(_, _, legacy, _)| legacy), diff --git a/api/crates/api/src/maps/list.rs b/api/crates/api/src/maps/list.rs index d2e2b18..ad52592 100644 --- a/api/crates/api/src/maps/list.rs +++ b/api/crates/api/src/maps/list.rs @@ -376,7 +376,7 @@ async fn listed_page( ListedMap, r#" WITH listed AS ( - SELECT m.id, m.name, m.last_run_at, counts.players, counts.bonuses, tier.tier, + SELECT m.id, m.name, m.last_run_at, m.players, tier.tier, row_number() OVER (ORDER BY -- How well the name matches: exactly, by a word's start ("candy" in bhop_candy), or within. CASE WHEN $6::text IS NULL OR m.name = $6 THEN 0 @@ -385,7 +385,7 @@ async fn listed_page( CASE WHEN m.name NOT ILIKE $1 ESCAPE '\' THEN word_similarity($7, m.name) END DESC NULLS LAST, CASE WHEN $2 = 'recent' THEN m.last_run_at END DESC, CASE WHEN $2 = 'newest' THEN m.listed_at END DESC NULLS LAST, - CASE $2 WHEN 'popular' THEN -counts.players WHEN 'least_popular' THEN counts.players END, + CASE $2 WHEN 'popular' THEN -m.players WHEN 'least_popular' THEN m.players END, CASE $2 WHEN 'hardest' THEN -tier.tier WHEN 'easiest' THEN tier.tier END NULLS LAST, CASE $2 WHEN 'longest_record' THEN -record.time WHEN 'shortest_record' THEN record.time END NULLS LAST, @@ -405,11 +405,6 @@ async fn listed_page( ORDER BY pb.time, r.finished_at LIMIT 1 ) record ON true - CROSS JOIN LATERAL ( - SELECT count(DISTINCT player_steamid64) AS players, - count(DISTINCT track) FILTER (WHERE track > 0) AS bonuses - FROM personal_bests WHERE map_id = m.id - ) counts WHERE m.last_run_at IS NOT NULL AND ($1::text IS NULL OR m.name ILIKE $1 ESCAPE '\' OR $7 <% m.name) AND ($8::bigint IS NULL OR $9 = EXISTS ( SELECT 1 FROM personal_bests WHERE player_steamid64 = $8 AND map_id = m.id AND style = $5 AND track = 0)) @@ -417,13 +412,17 @@ async fn listed_page( ), page AS ( SELECT * FROM listed WHERE position > $4 ORDER BY position LIMIT $3 ) - SELECT page.name, ci.id AS "image_id?", ci.key AS "image_key?", ci.focus AS "image_focus?", page.last_run_at AS "last_run_at!", page.players AS "players!", - page.bonuses AS "bonuses!", page.tier AS "tier?", + SELECT page.name, ci.id AS "image_id?", ci.key AS "image_key?", ci.focus AS "image_focus?", page.last_run_at AS "last_run_at!", page.players::int8 AS "players!", + counts.bonuses AS "bonuses!", page.tier AS "tier?", wr.time AS "wr_time?", wr.run_id AS "wr_run_id?", wr.finished_at AS "wr_finished_at?", p.steamid64::text AS "wr_steamid64?", p.name AS "wr_name?", p.avatar_url AS "wr_avatar_url?", p.country_code AS "wr_country_code?" FROM page LEFT JOIN map_images ci ON ci.map_id = page.id AND ci.position = 0 + CROSS JOIN LATERAL ( + SELECT count(DISTINCT track) FILTER (WHERE track > 0) AS bonuses + FROM personal_bests WHERE map_id = page.id + ) counts LEFT JOIN LATERAL ( SELECT pb.player_steamid64, pb.time, pb.run_id, r.finished_at FROM personal_bests pb JOIN runs r ON r.id = pb.run_id @@ -554,7 +553,10 @@ mod tests { use super::MapList; use crate::{ - runs::tests::{run, setup}, + runs::{ + invalidation, + tests::{run, setup}, + }, test_client, }; @@ -626,6 +628,57 @@ mod tests { ); } + /// `bhop_badges`' stored player count, and the count its personal bests give. + async fn badges_players(db: &PgPool) -> (i32, i64) { + sqlx::query_as( + "SELECT m.players, (SELECT count(DISTINCT player_steamid64) FROM personal_bests WHERE map_id = m.id) + FROM maps m WHERE name = 'bhop_badges'", + ) + .fetch_one(db) + .await + .unwrap() + } + + #[sqlx::test] + async fn keeps_each_maps_player_count_as_personal_bests_change(db: PgPool) { + let key = setup(&db).await; + let mut bonus = run("b", 40.0); + bonus["track"] = json!(1); + let mut second = run("c", 25.0); + second["player"] = json!({ "steamid64": "76561197960287931", "name": "second" }); + // A player's second track on a map is still one player. + for (body, players) in [(&run("a", 30.0), 1), (&bonus, 1), (&second, 2)] { + let status = test_client::post(&db, "/v1/runs", Some(&key), body) + .await + .status; + assert_eq!(status, 201); + assert_eq!(badges_players(&db).await, (players, i64::from(players))); + } + + let second_run: i64 = sqlx::query_scalar("SELECT id FROM runs WHERE server_run_id = 'c'") + .fetch_one(&db) + .await + .unwrap(); + let log_id: i64 = sqlx::query_scalar( + "INSERT INTO staff_log (action, target, reason) VALUES ('invalidate_run', 'c', 'x') RETURNING id", + ) + .fetch_one(&db) + .await + .unwrap(); + let mut conn = db.acquire().await.unwrap(); + invalidation::invalidate(&mut conn, &[second_run], log_id) + .await + .unwrap(); + assert_eq!(badges_players(&db).await, (1, 1)); + invalidation::restore(&mut conn, &[second_run]) + .await + .unwrap(); + assert_eq!(badges_players(&db).await, (2, 2)); + + let popular: MapList = test_client::get(&db, "/v1/maps?sort=popular").await.json(); + assert_eq!(popular.maps[0].players, 2); + } + #[sqlx::test] async fn counts_the_weeks_players_per_map(db: PgPool) { let key = setup(&db).await; diff --git a/api/crates/api/src/maps/wr_history.rs b/api/crates/api/src/maps/wr_history.rs index 7be3ff4..d32313a 100644 --- a/api/crates/api/src/maps/wr_history.rs +++ b/api/crates/api/src/maps/wr_history.rs @@ -101,25 +101,17 @@ pub(super) async fn records( map_id: i64, board: Option<(&str, i16)>, ) -> Result, sqlx::Error> { - // A running minimum over every earlier valid run on the same leaderboard: a run is a record if it beats - // all of them. Invalidated runs are left out, so the runs they had kept from being records become ones. - // This reads all of the map's runs, which the `runs_map` index serves in order. + // The runs that were records when set, as `leaderboards/world_records.rs` stores them: faster than every + // earlier valid run on the board. Invalidating a run redoes its board, so the runs it had kept from being + // records become ones. let rows = sqlx::query!( r#" SELECT r.style, r.track, r.id, r.time, r.finished_at, p.steamid64::text AS "steamid64!", p.name, p.avatar_url, p.country_code - FROM ( - SELECT id, style, track, player_steamid64, time, finished_at, - min(time) OVER ( - PARTITION BY style, track ORDER BY finished_at, id - ROWS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING - ) AS best_before - FROM runs - WHERE map_id = $1 AND invalidated_at IS NULL - AND ($2::text IS NULL OR (style = $2 AND track = $3::smallint)) - ) r + FROM runs r JOIN players p ON p.steamid64 = r.player_steamid64 - WHERE r.best_before IS NULL OR r.time < r.best_before + WHERE r.map_id = $1 AND r.was_record + AND ($2::text IS NULL OR (r.style = $2 AND r.track = $3::smallint)) ORDER BY r.style, r.track, r.finished_at, r.id "#, map_id, diff --git a/api/crates/api/src/players/personal_bests.rs b/api/crates/api/src/players/personal_bests.rs index 59ab8a8..5fc38aa 100644 --- a/api/crates/api/src/players/personal_bests.rs +++ b/api/crates/api/src/players/personal_bests.rs @@ -11,7 +11,7 @@ use serde::{Deserialize, Serialize}; use sqlx::PgPool; use utoipa::{IntoParams, ToSchema}; -use super::{SteamId64, restrictions::Restriction}; +use super::SteamId64; use crate::{ app::AppState, error::{ApiError, ErrorBody}, @@ -202,15 +202,11 @@ async fn view_personal_bests( SELECT lm.name, NULL::bigint, $4::smallint, l.time, true, l.id::bigint, l.finished_at FROM legacy_personal_bests l JOIN legacy_maps lm ON lm.id = l.map_id - WHERE l.player_steamid64 = $1 AND NOT EXISTS ( - SELECT 1 FROM bans - WHERE bans.player_steamid64 = $1 AND bans.restrictions & $5 <> 0 - AND bans.lifted_at IS NULL AND (bans.ends_at IS NULL OR bans.ends_at > now()) - ) + WHERE l.player_steamid64 = $1 AND NOT EXISTS (SELECT 1 FROM records_bans b WHERE b.player_steamid64 = $1) ) both_kinds - WHERE $6::text IS NULL OR (both_kinds.map, both_kinds.track) > ($6, $7::smallint) + WHERE $5::text IS NULL OR (both_kinds.map, both_kinds.track) > ($5, $6::smallint) ORDER BY both_kinds.map, both_kinds.track, both_kinds.time, both_kinds.legacy - LIMIT $8 + LIMIT $7 ) shown ORDER BY shown.map, shown.track "#, @@ -218,7 +214,6 @@ async fn view_personal_bests( legacy::STYLE, view == View::All, legacy::TRACK, - Restriction::NoRecords.bit(), after.map(|(map, _)| map), after.map(|(_, track)| track), limit diff --git a/api/crates/api/src/players/profile.rs b/api/crates/api/src/players/profile.rs index 1d7b758..902af0f 100644 --- a/api/crates/api/src/players/profile.rs +++ b/api/crates/api/src/players/profile.rs @@ -14,7 +14,6 @@ use super::{ SteamId64, details::{self, PlayerDetails}, personal_bests::{self, DEFAULT_LIMIT, PersonalBest, Selection, Tracks}, - restrictions::Restriction, steam_profile, }; use crate::{ @@ -125,14 +124,9 @@ pub async fn player_profile( let legacy = sqlx::query!( r#"SELECT lp.name, lp.avatar_url, lp.country_code, (SELECT count(*) FROM legacy_personal_bests l WHERE l.player_steamid64 = $1 - AND NOT EXISTS ( - SELECT 1 FROM bans - WHERE bans.player_steamid64 = $1 AND bans.restrictions & $2 <> 0 - AND bans.lifted_at IS NULL AND (bans.ends_at IS NULL OR bans.ends_at > now()) - )) AS "maps!" + AND NOT EXISTS (SELECT 1 FROM records_bans b WHERE b.player_steamid64 = $1)) AS "maps!" FROM legacy_players lp WHERE lp.steamid64 = $1"#, steamid64.get(), - Restriction::NoRecords.bit() ) .fetch_optional(&state.db) .await?; diff --git a/api/crates/api/src/players/restrictions.rs b/api/crates/api/src/players/restrictions.rs index 95cbcd2..00b5b1a 100644 --- a/api/crates/api/src/players/restrictions.rs +++ b/api/crates/api/src/players/restrictions.rs @@ -67,15 +67,14 @@ impl Restrictions { } } -/// What the player's active bans restrict, together. A ban is active until it's lifted or its end passes; -/// every query for active bans in the API spells out that same condition. +/// What the player's active bans restrict, together. Which bans are active is the `active_bans` view's rule, +/// and the queries that leave banned players off the boards read `records_bans` (migration 0047). pub async fn active( conn: &mut PgConnection, player: SteamId64, ) -> Result { let bits = sqlx::query_scalar!( - r#"SELECT coalesce(bit_or(restrictions), 0) AS "bits!" FROM bans - WHERE player_steamid64 = $1 AND lifted_at IS NULL AND (ends_at IS NULL OR ends_at > now())"#, + r#"SELECT coalesce(bit_or(restrictions), 0) AS "bits!" FROM active_bans WHERE player_steamid64 = $1"#, player.get() ) .fetch_one(conn) @@ -100,12 +99,9 @@ pub async fn records_ban( .fetch_optional(&mut *conn) .await?; sqlx::query_scalar!( - r#"SELECT log_id FROM bans - WHERE player_steamid64 = $1 AND restrictions & $2 <> 0 - AND lifted_at IS NULL AND (ends_at IS NULL OR ends_at > now()) - ORDER BY banned_at DESC, id DESC LIMIT 1"#, - player.get(), - Restriction::NoRecords.bit() + r#"SELECT log_id AS "log_id!" FROM records_bans + WHERE player_steamid64 = $1 ORDER BY banned_at DESC, id DESC LIMIT 1"#, + player.get() ) .fetch_optional(conn) .await @@ -121,4 +117,45 @@ mod tests { assert_eq!(read.list(), [Restriction::NoRecords]); assert_eq!(Restrictions::of(&Restriction::ALL).bits(), 0b111); } + + /// `records_bans` spells out `no_records`'s bit itself (migration 0047), so it has to agree with this file. + #[sqlx::test] + async fn records_bans_are_the_active_no_records_ones(db: sqlx::PgPool) { + // By player: in force; another restriction; ended yesterday; lifted. Ends are from now. + let bans = [ + (1, Restriction::NoRecords, Some("1 day"), false), + (2, Restriction::NoPosting, None, false), + (3, Restriction::NoRecords, Some("-1 day"), false), + (4, Restriction::NoRecords, None, true), + ]; + for (player, restriction, ends_in, lifted) in bans { + sqlx::query( + " + WITH player AS ( + INSERT INTO players (steamid64, name, name_seen_at) VALUES ($1, 'p', '-infinity') + ), entry AS ( + INSERT INTO staff_log (action, target, reason) VALUES ('ban_player', $1::text, 'x') RETURNING id + ) + INSERT INTO bans (player_steamid64, restrictions, reason, banned_at, ends_at, log_id, + lifted_at, lift_reason) + SELECT $1, $2, 'x', now() - interval '2 days', now() + $3::interval, id, + CASE WHEN $4 THEN now() END, CASE WHEN $4 THEN 'x' END + FROM entry + ", + ) + .bind(player) + .bind(restriction.bit()) + .bind(ends_in) + .bind(lifted) + .execute(&db) + .await + .unwrap(); + } + let banned: Vec = + sqlx::query_scalar("SELECT player_steamid64 FROM records_bans ORDER BY 1") + .fetch_all(&db) + .await + .unwrap(); + assert_eq!(banned, [1]); + } } diff --git a/api/crates/api/src/record_events/discord.rs b/api/crates/api/src/record_events/discord.rs index acf7f35..138ab76 100644 --- a/api/crates/api/src/record_events/discord.rs +++ b/api/crates/api/src/record_events/discord.rs @@ -410,6 +410,8 @@ mod tests { submit(&db, &key, &run("a", 30.0)).await; // Second place on the board: not a world record. submit(&db, &key, &by("second", "76561197960287931", "b", 32.0)).await; + // Ties the record and shares rank 1, but isn't faster: not a world record either. + submit(&db, &key, &by("tie", "76561197960287932", "c", 30.0)).await; feed_once(&db, &feeds).await; feed_once(&db, &feeds).await; diff --git a/api/crates/api/src/record_events/discord/post.rs b/api/crates/api/src/record_events/discord/post.rs index bdab74f..9220aad 100644 --- a/api/crates/api/src/record_events/discord/post.rs +++ b/api/crates/api/src/record_events/discord/post.rs @@ -38,6 +38,9 @@ struct Record { previous_time: Option, /// The run's place on its board when the feed read it. rank: Option, + /// A world record when set: faster than every earlier run on its board (`leaderboards/world_records.rs`). + /// A tie isn't. + fastest_yet: bool, sync: f32, strafes: i32, jumps: i32, @@ -65,7 +68,12 @@ pub(super) async fn post( .fetch_all(&mut *conn) .await?; let mut records = records(conn, feed.records, &run_ids).await?; - records.retain(|r| !posted.contains(&r.run_id) && feed.takes_tier(r.tier)); + // Rank 1 alone would take a run that ties the record, which the site doesn't count as one. + records.retain(|r| { + !posted.contains(&r.run_id) + && feed.takes_tier(r.tier) + && (feed.records == Records::PersonalBests || r.fastest_yet) + }); // The same run can be reported twice in a page, by a restore. records.dedup_by_key(|r| r.run_id); for record in &mut records { @@ -108,18 +116,18 @@ async fn records( let tiers_published = tier_count(&mut *conn).await?.is_some(); let rows = sqlx::query!( r#" - SELECT r.id, m.name AS map, r.style, r.track, r.time, r.sync, r.strafes, r.jumps, r.finished_at, + SELECT r.id, m.name AS map, r.style, r.track, r.time, r.sync, r.strafes, r.jumps, r.finished_at, r.was_record, r.player_steamid64::text AS "steamid64!", p.name AS player, s.name AS server_name, (SELECT st->>'name' FROM rulesets, jsonb_array_elements(document->'styles') st WHERE st->>'key' = r.style ORDER BY version DESC LIMIT 1) AS style_name, (SELECT min(o.time) FROM runs o WHERE o.map_id = r.map_id AND o.style = r.style AND o.track = r.track - AND o.invalidated_at IS NULL AND o.id <> r.id AND o.finished_at <= r.finished_at) + AND o.invalidated_at IS NULL AND (o.finished_at, o.id) < (r.finished_at, r.id)) AS previous_record, (SELECT min(o.time) FROM runs o WHERE o.map_id = r.map_id AND o.style = r.style AND o.track = r.track AND o.player_steamid64 = r.player_steamid64 - AND o.invalidated_at IS NULL AND o.id <> r.id AND o.finished_at <= r.finished_at) + AND o.invalidated_at IS NULL AND (o.finished_at, o.id) < (r.finished_at, r.id)) AS previous_personal_best, (SELECT tier FROM map_tiers WHERE map_id = r.map_id AND track = r.track ORDER BY assigned_at DESC LIMIT 1) AS tier, @@ -154,6 +162,7 @@ async fn records( Records::PersonalBests => row.previous_personal_best, }, rank: None, + fastest_yet: row.was_record, sync: row.sync, strafes: row.strafes, jumps: row.jumps, diff --git a/api/crates/api/src/records.rs b/api/crates/api/src/records.rs index b38dca3..166f0bb 100644 --- a/api/crates/api/src/records.rs +++ b/api/crates/api/src/records.rs @@ -92,7 +92,9 @@ async fn map_records( name AS "name!", time AS "time!", run_id AS "run_id!", finished_at AS "finished_at!", stored AS "has_replay!", points AS "points!" FROM ( - SELECT pb.style, pb.track, pb.time, pb.run_id, pb.points, p.steamid64, p.name, r.finished_at, + SELECT pb.style, pb.track, pb.time, pb.run_id, + personal_best_points(pb.map_id, pb.style, pb.track, pb.time) AS points, + p.steamid64, p.name, r.finished_at, rp.stored_key IS NOT NULL AS stored, rank() OVER (PARTITION BY pb.style, pb.track ORDER BY pb.time) AS rank FROM personal_bests pb diff --git a/api/crates/api/src/replays.rs b/api/crates/api/src/replays.rs index 1c6732b..6ea2019 100644 --- a/api/crates/api/src/replays.rs +++ b/api/crates/api/src/replays.rs @@ -24,7 +24,7 @@ use utoipa_axum::{router::OpenApiRouter, routes}; use crate::{ app::AppState, - storage::{Storage, random_key_part}, + storage::{Staging, random_key_part}, }; // Plugins upload between runs, usually within minutes. A server that misses the window resends the run @@ -162,9 +162,9 @@ pub async fn requested( })) } -pub fn upload_link(storage: &Storage, requested: Requested, now: DateTime) -> ReplayUpload { +pub fn upload_link(staging: &Staging, requested: Requested, now: DateTime) -> ReplayUpload { ReplayUpload { - upload_url: storage.presign_put(&requested.upload_key, UPLOAD_LINK_LIFETIME), + upload_url: staging.presign_put(&requested.upload_key, UPLOAD_LINK_LIFETIME), expires_at: now + TimeDelta::from_std(UPLOAD_LINK_LIFETIME).expect("one hour fits in a TimeDelta"), last_refusal: requested.last_refusal, diff --git a/api/crates/api/src/replays/download.rs b/api/crates/api/src/replays/download.rs index 1ea648a..dc5ddaa 100644 --- a/api/crates/api/src/replays/download.rs +++ b/api/crates/api/src/replays/download.rs @@ -1,7 +1,9 @@ //! `GET /v1/replays/{run_id}`: the run's replay as the shavit file it was uploaded as, for players and for //! servers that play replays in-game. Replays are kept as SJR, a fifth of the size; the first download //! rebuilds the shavit file and keeps it too, and every download after passes it on from storage as it -//! arrives, so a popular replay costs neither memory nor a rebuild per download. +//! arrives, so a popular replay costs neither memory nor a rebuild per download. The kept file has its own +//! random key: the SJR's is public, and a key derived from it would let anyone fetch the shavit file from +//! storage directly, invalidated runs' included. use std::sync::Arc; @@ -12,7 +14,7 @@ use axum::{ response::{IntoResponse, Response}, }; use serde::{Deserialize, Serialize}; -use sqlx::{PgConnection, types::Json as JsonColumn}; +use sqlx::{PgConnection, PgPool, types::Json as JsonColumn}; use tokio::sync::Semaphore; use utoipa::ToSchema; @@ -20,6 +22,7 @@ use super::{current_policy, sjr}; use crate::{ app::AppState, error::{ApiError, ErrorBody}, + storage::{Storage, random_key_part}, }; /// How many first downloads rebuild their shavit file at once. Each holds the whole file in memory, up to @@ -177,22 +180,68 @@ async fn keep_shavit_file( .get(stored_key) .await .map_err(|error| ApiError::Internal(error.into()))?; - let file = sjr::decode(&stored).map_err(|error| { - ApiError::Internal(format!("stored replay {stored_key} is damaged: {error}").into()) - })?; - // Named after the SJR, so two first downloads at once write the same file to the same key. - let key = format!("{}.replay", stored_key.trim_end_matches(".sjr")); + // Decompressing up to 128 MB takes a moment of CPU: off the async workers. + let file = tokio::task::spawn_blocking(move || sjr::decode(&stored)) + .await + .map_err(|error| ApiError::Internal(error.into()))? + .map_err(|error| { + ApiError::Internal(format!("stored replay {stored_key} is damaged: {error}").into()) + })?; + let key = format!("replays/{run_id}-{}.replay", random_key_part()); state .storage .put_immutable(&key, "application/octet-stream", None, file) .await .map_err(|error| ApiError::Internal(error.into()))?; - sqlx::query!( - "UPDATE replays SET shavit_key = $2 WHERE run_id = $1", + // Two first downloads at once each write their own copy; the first to record it wins. + let kept = sqlx::query_scalar!( + r#"UPDATE replays SET shavit_key = coalesce(shavit_key, $2) WHERE run_id = $1 + RETURNING shavit_key AS "shavit_key!""#, run_id, key ) - .execute(&state.db) + .fetch_one(&state.db) + .await?; + if kept != key + && let Err(error) = state.storage.delete(&key).await + { + tracing::warn!(key, %error, "spare shavit file not deleted"); + } + Ok(kept) +} + +/// Deletes shavit files kept under the key derived from their SJR's, as they were before they got their own +/// random part. The next download rebuilds each under a random key. Once none are left this finds nothing. +pub async fn forget_derived_shavit_files( + db: &PgPool, + storage: &Storage, +) -> Result> { + let derived = sqlx::query!( + r#"SELECT run_id, shavit_key AS "shavit_key!" FROM replays + WHERE shavit_key = regexp_replace(stored_key, '\.sjr$', '.replay')"# + ) + .fetch_all(db) .await?; - Ok(key) + for row in &derived { + // Deleted first: if that fails the row still names the file, so the next start tries again. + storage.delete(&row.shavit_key).await?; + sqlx::query!( + "UPDATE replays SET shavit_key = NULL WHERE run_id = $1", + row.run_id + ) + .execute(db) + .await?; + } + Ok(derived.len()) +} + +/// `forget_derived_shavit_files` as the API starts, logging what came of it. +pub async fn forget_derived_shavit_files_at_start(db: PgPool, storage: Storage) { + match forget_derived_shavit_files(&db, &storage).await { + Ok(0) => {} + Ok(count) => tracing::info!(count, "deleted shavit files kept under guessable keys"), + Err(error) => { + tracing::error!(%error, "couldn't delete shavit files kept under guessable keys; trying at the next start"); + } + } } diff --git a/api/crates/api/src/replays/processing.rs b/api/crates/api/src/replays/processing.rs index bc2e843..9c822e4 100644 --- a/api/crates/api/src/replays/processing.rs +++ b/api/crates/api/src/replays/processing.rs @@ -1,9 +1,11 @@ //! The replay job: finds uploaded replays, checks each against its run, and stores the good ones as SJR. //! -//! Servers upload straight to storage and tell the API nothing, so the job lists the upload folder instead. -//! One listing covers every pending upload however many runs are waiting, and a complete upload is all that -//! appears there: S3 only shows an object once its upload has finished. A bad upload is deleted and its -//! reason kept; the server may upload again with a fresh link. A stored replay is final. +//! Servers upload straight to the private staging bucket and tell the API nothing, so the job lists the +//! upload folder instead. One listing covers every pending upload however many runs are waiting, and a +//! complete upload is all that appears there: S3 only shows an object once its upload has finished. Only +//! the SJR the job built from a checked upload reaches the public bucket, so nothing a server uploads is +//! ever served as it sent it. A bad upload is deleted and its reason kept; the server may upload again with +//! a fresh link. A stored replay is final. use std::time::Duration; @@ -15,7 +17,7 @@ use super::{MAX_REPLAY_BYTES, UPLOAD_PREFIX, header, route, sjr}; use crate::{ players::INDIVIDUAL_BASE, record_events, - storage::{Storage, StorageError, StoredObject, random_key_part}, + storage::{Staged, Staging, Storage, StoredObject, random_key_part}, }; /// How often the job looks for uploads. Replays aren't needed the moment a run ends; this keeps the @@ -23,13 +25,13 @@ use crate::{ const INTERVAL: Duration = Duration::from_secs(30); /// Runs for as long as the API does. The API runs as one process, so only one job lists at a time. -pub async fn run(db: PgPool, storage: Storage) { +pub async fn run(db: PgPool, storage: Storage, staging: Staging) { let mut ticks = tokio::time::interval(INTERVAL); ticks.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Delay); loop { ticks.tick().await; // Only listing the uploads fails the whole tick; each upload's own errors are logged as it goes. - if let Err(error) = process_uploads(&db, &storage).await { + if let Err(error) = process_uploads(&db, &storage, &staging).await { tracing::error!(%error, "replay job couldn't list uploads; trying again later"); } } @@ -48,12 +50,13 @@ pub struct Processed { pub async fn process_uploads( db: &PgPool, storage: &Storage, + staging: &Staging, ) -> Result> { let mut processed = Processed::default(); - for upload in storage.list(UPLOAD_PREFIX).await? { + for upload in staging.list(UPLOAD_PREFIX).await? { // One upload failing (storage refusing that object, a database error) is skipped and tried again // next tick. Ending the tick instead would stop every upload listed after it, on every tick. - let outcome = match process(db, storage, &upload).await { + let outcome = match process(db, storage, staging, &upload).await { Ok(outcome) => outcome, Err(error) => { tracing::error!(key = upload.key, %error, "replay upload failed; trying it again later"); @@ -71,7 +74,7 @@ pub async fn process_uploads( } // Deleted only after the database has the outcome, so a failure in between means the upload is // looked at again, never lost. - if let Err(error) = storage.delete(&upload.key).await { + if let Err(error) = staging.delete(&upload.key).await { tracing::error!(key = upload.key, %error, "replay upload not deleted; it is looked at again"); } } @@ -85,6 +88,7 @@ enum Outcome { } /// What the upload is checked against. +#[derive(Clone)] struct Expected { run_id: i64, already_stored: bool, @@ -98,6 +102,7 @@ struct Expected { async fn process( db: &PgPool, storage: &Storage, + staging: &Staging, upload: &StoredObject, ) -> Result> { let expected = sqlx::query_as!( @@ -116,25 +121,36 @@ async fn process( return Ok(Outcome::Discarded); }; - let checked = if usize::try_from(upload.size).map_or(true, |size| size > MAX_REPLAY_BYTES) { + // The listed size is only what was there then: the server holds the link for an hour and may replace + // the file before it's read, so the read itself stops past the limit. + let too_large = || { Err(format!( - "the file is {} bytes; replays over {MAX_REPLAY_BYTES} bytes aren't accepted", - upload.size + "the file is over {MAX_REPLAY_BYTES} bytes; larger replays aren't accepted" )) + }; + let checked = if usize::try_from(upload.size).map_or(true, |size| size > MAX_REPLAY_BYTES) { + too_large() } else { - let file = match storage.get(&upload.key).await { - Ok(file) => file, + match staging.read(&upload.key, MAX_REPLAY_BYTES).await? { // Deleted between the listing and now, by an earlier run of the job that failed later on. - Err(StorageError::Status(404)) => return Ok(Outcome::Discarded), - Err(error) => return Err(error.into()), - }; - check(&file, &expected).map(|header| { - ( - sjr::encode(&file, &header), - route::route(&file, &header), - file.len(), - ) - }) + Staged::Missing => return Ok(Outcome::Discarded), + Staged::TooLarge => too_large(), + Staged::Bytes(file) => { + // Parsing, compressing at gzip's highest level and tracing the route of a file up to 128 MB + // take seconds of CPU: off the async workers, so requests aren't held up meanwhile. + let run = expected.clone(); + tokio::task::spawn_blocking(move || { + check(&file, &run).map(|header| { + ( + sjr::encode(&file, &header), + route::route(&file, &header), + file.len(), + ) + }) + }) + .await? + } + } }; match checked { @@ -196,7 +212,8 @@ fn check(file: &[u8], run: &Expected) -> Result { if steamid64 != run.steamid64 { return Err("the replay is of another player".to_owned()); } - if (header.tickrate - run.tickrate).abs() > 0.01 { + // A NaN in the file would pass a plain `>`: every comparison with NaN is false. + if !header.tickrate.is_finite() || (header.tickrate - run.tickrate).abs() > 0.01 { return Err(format!( "the replay was recorded at {} tick, the run at {}", header.tickrate, run.tickrate @@ -205,7 +222,7 @@ fn check(file: &[u8], run: &Expected) -> Result { // The header stores the time as a 32-bit float, so allow for its rounding: half a tick is far beyond // that and still tells runs apart. let half_tick = 0.5 / f64::from(run.tickrate); - if (f64::from(header.time) - run.time).abs() > half_tick { + if !header.time.is_finite() || (f64::from(header.time) - run.time).abs() > half_tick { return Err(format!( "the replay's time is {:.3} s, the run's {:.3} s", header.time, run.time @@ -220,6 +237,7 @@ mod tests { use sqlx::PgPool; use super::{Processed, process_uploads}; + use crate::replays::download::forget_derived_shavit_files; use crate::{ replays::{ UPLOAD_PREFIX, @@ -227,7 +245,7 @@ mod tests { sjr, }, runs::{RunAccepted, tests::setup}, - storage::Storage, + storage::{Staging, Storage}, test_client, }; @@ -248,11 +266,19 @@ mod tests { .json(); let url = accepted.replay.expect("the replay is wanted").upload_url; let path = url::Url::parse(&url).unwrap().path().to_owned(); + assert!( + path.starts_with("/test-staging/"), + "uploads go to the private bucket: {url}" + ); let at = path.find(UPLOAD_PREFIX).expect("an upload key"); path[at..].to_owned() } - fn upload(storage: &Storage, key: &str, file: &[u8]) { + fn upload(staging: &Staging, key: &str, file: &[u8]) { + staging.objects().insert(key.to_owned(), file.to_vec()); + } + + fn upload_to(storage: &Storage, key: &str, file: &[u8]) { storage .objects .lock() @@ -269,11 +295,11 @@ mod tests { #[sqlx::test] async fn a_good_upload_is_stored_as_sjr_and_the_upload_removed(db: PgPool) { let key = setup(&db).await; - let storage = Storage::for_tests(); + let (storage, staging) = (Storage::for_tests(), Staging::for_tests()); let upload_key = submit(&db, &key, &maze_run("a")).await; - upload(&storage, &upload_key, MAZE); + upload(&staging, &upload_key, MAZE); - let processed = process_uploads(&db, &storage).await.unwrap(); + let processed = process_uploads(&db, &storage, &staging).await.unwrap(); assert_eq!( processed, @@ -288,6 +314,7 @@ mod tests { .await .unwrap(); assert_eq!(replay_events, 1); + assert!(staging.objects().is_empty(), "the upload is removed"); let stored = keys(&storage); assert_eq!(stored.len(), 1); assert!( @@ -315,12 +342,12 @@ mod tests { #[sqlx::test] async fn a_replay_of_a_map_named_with_capitals_matches_its_lowercased_run(db: PgPool) { let key = setup(&db).await; - let storage = Storage::for_tests(); + let (storage, staging) = (Storage::for_tests(), Staging::for_tests()); let upload_key = submit(&db, &key, &maze_run("a")).await; let file = replay_of_map(MAZE, "bhop_maze", "bhop_Maze"); - upload(&storage, &upload_key, &file); + upload(&staging, &upload_key, &file); - let processed = process_uploads(&db, &storage).await.unwrap(); + let processed = process_uploads(&db, &storage, &staging).await.unwrap(); assert_eq!(processed.stored, 1, "{processed:?}"); let stored = keys(&storage); @@ -345,8 +372,10 @@ mod tests { let key = setup(&db).await; let state = crate::app::AppState::for_tests(db.clone()); let upload_key = submit(&db, &key, &maze_run("a")).await; - upload(&state.storage, &upload_key, MAZE); - process_uploads(&db, &state.storage).await.unwrap(); + upload(&state.staging, &upload_key, MAZE); + process_uploads(&db, &state.storage, &state.staging) + .await + .unwrap(); let run_id: i64 = sqlx::query_scalar("SELECT id FROM runs") .fetch_one(&db) .await @@ -362,31 +391,79 @@ mod tests { assert_eq!(first.status, 200); assert_eq!(&first.body[..], MAZE, "rebuilt byte for byte"); assert_eq!(first.headers["content-length"], MAZE.len().to_string()); - let kept: String = sqlx::query_scalar("SELECT shavit_key FROM replays") - .fetch_one(&db) - .await - .unwrap(); + let (sjr_key, kept): (String, String) = + sqlx::query_as("SELECT stored_key, shavit_key FROM replays") + .fetch_one(&db) + .await + .unwrap(); assert!( kept.starts_with("replays/") && kept.ends_with(".replay"), "{kept}" ); + assert_ne!( + kept.trim_end_matches(".replay"), + sjr_key.trim_end_matches(".sjr"), + "the SJR's public key doesn't give the kept file's away" + ); // Without the SJR, only the kept file can answer. - let sjr_key = kept.replace(".replay", ".sjr"); let sjr_file = state.storage.objects.lock().unwrap().remove(&sjr_key); - assert!(sjr_file.is_some(), "the kept file sits next to its SJR"); + assert!(sjr_file.is_some()); let second = download().await; assert_eq!(second.status, 200); assert_eq!(&second.body[..], MAZE); } + #[sqlx::test] + async fn shavit_files_kept_under_a_guessable_key_are_deleted_and_rebuilt(db: PgPool) { + let key = setup(&db).await; + let state = crate::app::AppState::for_tests(db.clone()); + let upload_key = submit(&db, &key, &maze_run("a")).await; + upload(&state.staging, &upload_key, MAZE); + process_uploads(&db, &state.storage, &state.staging) + .await + .unwrap(); + // As the first download kept it before kept files got their own random part. + let sjr_key: String = sqlx::query_scalar("SELECT stored_key FROM replays") + .fetch_one(&db) + .await + .unwrap(); + let derived = sjr_key.replace(".sjr", ".replay"); + upload_to(&state.storage, &derived, MAZE); + sqlx::query("UPDATE replays SET shavit_key = $1") + .bind(&derived) + .execute(&db) + .await + .unwrap(); + + let forget = || forget_derived_shavit_files(&db, &state.storage); + assert_eq!(forget().await.unwrap(), 1); + assert!(!keys(&state.storage).contains(&derived), "deleted"); + assert_eq!(forget().await.unwrap(), 0, "nothing left the next time"); + + let run_id: i64 = sqlx::query_scalar("SELECT id FROM runs") + .fetch_one(&db) + .await + .unwrap(); + let request = axum::http::Request::get(format!("/v1/replays/{run_id}")) + .body(axum::body::Body::empty()) + .unwrap(); + let rebuilt = test_client::send_to(state.clone(), request).await; + assert_eq!(&rebuilt.body[..], MAZE); + let kept: String = sqlx::query_scalar("SELECT shavit_key FROM replays") + .fetch_one(&db) + .await + .unwrap(); + assert_ne!(kept, derived); + } + #[sqlx::test] async fn an_invalidated_runs_replay_can_be_watched_but_not_downloaded(db: PgPool) { let key = setup(&db).await; - let storage = Storage::for_tests(); + let (storage, staging) = (Storage::for_tests(), Staging::for_tests()); let upload_key = submit(&db, &key, &maze_run("a")).await; - upload(&storage, &upload_key, MAZE); - process_uploads(&db, &storage).await.unwrap(); + upload(&staging, &upload_key, MAZE); + process_uploads(&db, &storage, &staging).await.unwrap(); let run_id: i64 = sqlx::query_scalar("SELECT id FROM runs") .fetch_one(&db) .await @@ -416,16 +493,20 @@ mod tests { #[sqlx::test] async fn an_upload_that_isnt_the_runs_replay_is_refused_and_may_be_sent_again(db: PgPool) { let key = setup(&db).await; - let storage = Storage::for_tests(); + let (storage, staging) = (Storage::for_tests(), Staging::for_tests()); let mut slower = maze_run("a"); slower["time"] = json!(slower["time"].as_f64().unwrap() + 1.0); let upload_key = submit(&db, &key, &slower).await; - upload(&storage, &upload_key, MAZE); + upload(&staging, &upload_key, MAZE); - let processed = process_uploads(&db, &storage).await.unwrap(); + let processed = process_uploads(&db, &storage, &staging).await.unwrap(); assert_eq!(processed.refused, 1); - assert!(keys(&storage).is_empty(), "the bad upload is deleted"); + assert!(staging.objects().is_empty(), "the bad upload is deleted"); + assert!( + keys(&storage).is_empty(), + "nothing reaches the public bucket" + ); let reason: String = sqlx::query_scalar("SELECT refused_reason FROM replays") .fetch_one(&db) .await @@ -440,8 +521,8 @@ mod tests { .expect("the resend says why the upload was refused"); assert_eq!(refusal.reason, reason); - upload(&storage, &upload_key, b"not a replay"); - process_uploads(&db, &storage).await.unwrap(); + upload(&staging, &upload_key, b"not a replay"); + process_uploads(&db, &storage, &staging).await.unwrap(); let reason: String = sqlx::query_scalar("SELECT refused_reason FROM replays") .fetch_one(&db) .await @@ -452,10 +533,35 @@ mod tests { ); } + #[sqlx::test] + async fn a_replay_whose_time_is_nan_is_refused(db: PgPool) { + let key = setup(&db).await; + let (storage, staging) = (Storage::for_tests(), Staging::for_tests()); + let upload_key = submit(&db, &key, &maze_run("a")).await; + let time = header::parse(MAZE).unwrap().time.to_le_bytes(); + let at = MAZE + .windows(time.len()) + .position(|window| window == time) + .expect("the header holds the time"); + let mut file = MAZE.to_vec(); + file[at..at + time.len()].copy_from_slice(&f32::NAN.to_le_bytes()); + assert!(header::parse(&file).unwrap().time.is_nan()); + upload(&staging, &upload_key, &file); + + assert_eq!( + process_uploads(&db, &storage, &staging) + .await + .unwrap() + .refused, + 1 + ); + assert_eq!(keys(&storage), [""; 0], "nothing is stored"); + } + #[sqlx::test] async fn the_player_map_and_track_must_match(db: PgPool) { let key = setup(&db).await; - let storage = Storage::for_tests(); + let (storage, staging) = (Storage::for_tests(), Staging::for_tests()); let mut other_player = maze_run("a"); other_player["player"]["steamid64"] = json!("76561197960287930"); let mut other_map = maze_run("b"); @@ -470,8 +576,14 @@ mod tests { (bonus, "track"), ] { let upload_key = submit(&db, &key, &run).await; - upload(&storage, &upload_key, MAZE); - assert_eq!(process_uploads(&db, &storage).await.unwrap().refused, 1); + upload(&staging, &upload_key, MAZE); + assert_eq!( + process_uploads(&db, &storage, &staging) + .await + .unwrap() + .refused, + 1 + ); let reason: String = sqlx::query_scalar("SELECT refused_reason FROM replays WHERE upload_key = $1") .bind(&upload_key) @@ -488,15 +600,15 @@ mod tests { #[sqlx::test] async fn unrequested_files_and_repeats_after_storing_are_discarded(db: PgPool) { let key = setup(&db).await; - let storage = Storage::for_tests(); + let (storage, staging) = (Storage::for_tests(), Staging::for_tests()); let upload_key = submit(&db, &key, &maze_run("a")).await; - upload(&storage, &upload_key, MAZE); - process_uploads(&db, &storage).await.unwrap(); + upload(&staging, &upload_key, MAZE); + process_uploads(&db, &storage, &staging).await.unwrap(); let stored = keys(&storage); - upload(&storage, &upload_key, MAZE); - upload(&storage, &format!("{UPLOAD_PREFIX}999-guess.replay"), MAZE); - let processed = process_uploads(&db, &storage).await.unwrap(); + upload(&staging, &upload_key, MAZE); + upload(&staging, &format!("{UPLOAD_PREFIX}999-guess.replay"), MAZE); + let processed = process_uploads(&db, &storage, &staging).await.unwrap(); assert_eq!( processed, diff --git a/api/crates/api/src/runs.rs b/api/crates/api/src/runs.rs index e898c7e..dc2460e 100644 --- a/api/crates/api/src/runs.rs +++ b/api/crates/api/src/runs.rs @@ -137,7 +137,7 @@ async fn submit_run( }; let replay = stored .replay - .map(|requested| replays::upload_link(&state.storage, requested, Utc::now())); + .map(|requested| replays::upload_link(&state.staging, requested, Utc::now())); Ok(( status, Json(RunAccepted { @@ -231,7 +231,6 @@ async fn store( maps::record_run(conn, map_id, finished_at).await?; let new_personal_best = record_personal_best(conn, run, map_id, id, finished_at).await?; if new_personal_best { - crate::leaderboards::standings::refresh_runs(conn, &[id]).await?; record_events::record(conn, record_events::Kind::Run, &[id]).await?; } // Every valid run, not only personal bests: a run that arrives late may have been a record when it was set. @@ -247,6 +246,10 @@ async fn store( } else { None }; + // Last: the recount holds the standings lock, which every other recount waits on, until this commits. + if new_personal_best { + crate::leaderboards::standings::refresh_runs(conn, &[id]).await?; + } Ok(Stored { id, diff --git a/api/crates/api/src/runs/recent.rs b/api/crates/api/src/runs/recent.rs index 901ae56..9187d88 100644 --- a/api/crates/api/src/runs/recent.rs +++ b/api/crates/api/src/runs/recent.rs @@ -195,14 +195,16 @@ async fn page( tiers_in_force: bool, ) -> Result, sqlx::Error> { let track = track_key(query); - // "Before" a run means earlier on its board in WR history's order: finish time, then ID. Records only - // reads the stored `was_record` through its own index (`leaderboards/world_records.rs`). `prior`, the + // "Before" a run means earlier on its board in WR history's order: finish time, then ID. Whether a run + // was a record is the stored `was_record` (`leaderboards/world_records.rs`), which the records filter reads + // through its own index. `prior`, the // board's best before the run, and placements are worked out for the page's runs alone, reading the // board's earlier runs through the `runs_map` index. let rows = sqlx::query!( r#" WITH listed AS ( - SELECT r.id, r.map_id, r.style, r.track, r.player_steamid64, r.time, r.finished_at, tier.tier + SELECT r.id, r.map_id, r.style, r.track, r.player_steamid64, r.time, r.finished_at, r.was_record, + tier.tier FROM runs r LEFT JOIN LATERAL (SELECT tier FROM map_tiers WHERE map_id = r.map_id AND track = r.track ORDER BY assigned_at DESC LIMIT 1) tier ON $7 @@ -215,7 +217,7 @@ async fn page( ORDER BY r.finished_at DESC, r.id DESC LIMIT $8 OFFSET $9 ) - SELECT l.id, l.style, l.track, l.time, l.finished_at, l.tier AS "tier?", prior.best AS "best?", + SELECT l.id, l.style, l.track, l.time, l.finished_at, l.was_record, l.tier AS "tier?", prior.best AS "best?", m.name AS map, place.placement AS "placement?", p.steamid64::text AS "steamid64!", p.name, p.avatar_url, p.country_code FROM listed l @@ -259,7 +261,6 @@ async fn page( Ok(rows .into_iter() .map(|row| { - let world_record = row.best.is_none_or(|best| row.time < best); RecentRun { run_id: row.id, player: PlayerRef { @@ -275,7 +276,7 @@ async fn page( time: row.time, finished_at: row.finished_at, placement: row.placement, - world_record, + world_record: row.was_record, // Rounded to the millisecond, as in WR history. gap: row .best diff --git a/api/crates/api/src/servers/heartbeat.rs b/api/crates/api/src/servers/heartbeat.rs index 910a7ab..1d9cd6b 100644 --- a/api/crates/api/src/servers/heartbeat.rs +++ b/api/crates/api/src/servers/heartbeat.rs @@ -124,7 +124,7 @@ async fn heartbeat( State(state): State, ClientAddress(ip): ClientAddress, ClientCountry(country_code): ClientCountry, - OptionalKey(lookup): OptionalKey, + PresentedKey(presented): PresentedKey, JsonBody(mut report): JsonBody, ) -> Result, ApiError> { if !state.heartbeat_limit.allow(ip, Instant::now()) { @@ -136,6 +136,11 @@ async fn heartbeat( return Err(ApiError::Invalid("port can't be 0".to_owned())); } report.hostname = clean_hostname(&report.hostname); + // Looked up only now: anyone can send heartbeats, so the limit is checked before any database work. + let lookup = match presented { + None => None, + Some(presented) => Some(keys::look_up(&state.db, &presented).await?), + }; let (key, server_id) = match lookup { None => (KeyStatus::None, None), Some(KeyLookup::Unknown) => (KeyStatus::Unknown, None), @@ -239,20 +244,17 @@ async fn prune(db: &PgPool) -> Result<(), sqlx::Error> { Ok(()) } -/// The key lookup when the request sent a key, or `None` when it sent none. Never rejects. -struct OptionalKey(Option); +/// The key the request sent, not yet looked up, or `None` when it sent none. Never rejects. +struct PresentedKey(Option); -impl FromRequestParts for OptionalKey { +impl FromRequestParts for PresentedKey { type Rejection = ApiError; - async fn from_request_parts( + fn from_request_parts( parts: &mut Parts, - state: &AppState, - ) -> Result { - match keys::bearer(parts) { - None => Ok(Self(None)), - Some(presented) => Ok(Self(Some(keys::look_up(&state.db, presented).await?))), - } + _state: &AppState, + ) -> impl Future> { + std::future::ready(Ok(Self(keys::bearer(parts).map(str::to_owned)))) } } @@ -339,6 +341,22 @@ pub(crate) mod tests { assert_eq!(send("203.0.113.8").await, 200); } + #[sqlx::test] + async fn the_limit_is_checked_before_the_key_is_looked_up(db: PgPool) { + let state = crate::app::AppState::for_tests(db.clone()); + let ip = "203.0.113.7".parse().unwrap(); + while state.heartbeat_limit.allow(ip, std::time::Instant::now()) {} + // With the database gone, any query would answer 500: a 429 shows none was made. + db.close().await; + let request = Request::post("/v1/servers/heartbeat") + .header("content-type", "application/json") + .header("x-forwarded-for", "203.0.113.7") + .header("authorization", "Bearer sj_not_a_real_key") + .body(Body::from(report(27015).to_string())) + .unwrap(); + assert_eq!(test_client::send_to(state, request).await.status, 429); + } + #[sqlx::test] async fn reports_the_key_status(db: PgPool) { let issued = keys::issue(&db, "Test server").await.unwrap(); diff --git a/api/crates/api/src/staff/maps.rs b/api/crates/api/src/staff/maps.rs index 8d4f1a3..d8beadf 100644 --- a/api/crates/api/src/staff/maps.rs +++ b/api/crates/api/src/staff/maps.rs @@ -942,6 +942,48 @@ pub(crate) mod tests { ); } + #[sqlx::test] + async fn staff_set_and_clear_a_maps_description(db: PgPool) { + let token = session(&db, STAFF, &[Privilege::ManageMaps, Privilege::ManageZones]).await; + let path = "/v1/staff/maps/bhop_fresh/description"; + let put = |description: Value| { + send( + &db, + "PUT", + path, + &token, + Some(json!({ "description": description })), + ) + }; + assert_eq!(put(json!("Short and fast")).await.status, 404); + let zones = json!({ "based_on": null, "zones": zone_set(1000.0), "note": "First" }); + send( + &db, + "POST", + "/v1/staff/maps/bhop_fresh/zones", + &token, + Some(zones), + ) + .await; + + assert_eq!(put(json!(" Short and fast ")).await.status, 204); + assert_eq!(put(json!("Short and fast")).await.status, 422, "unchanged"); + assert_eq!(put(json!("a".repeat(1001))).await.status, 422, "too long"); + let public: Value = test_client::get(&db, "/v1/maps/bhop_fresh").await.json(); + assert_eq!(public["description"], "Short and fast", "trimmed"); + + assert_eq!(put(json!(" ")).await.status, 204, "blank clears it"); + let public: Value = test_client::get(&db, "/v1/maps/bhop_fresh").await.json(); + assert_eq!(public["description"], Value::Null); + let logged = sqlx::query_scalar!( + r#"SELECT count(*) AS "count!" FROM staff_log WHERE action = 'set_map_description'"# + ) + .fetch_one(&db) + .await + .unwrap(); + assert_eq!(logged, 2); + } + #[sqlx::test] async fn staff_set_when_a_map_was_released_and_who_made_it(db: PgPool) { let token = session(&db, STAFF, &[Privilege::ManageMaps, Privilege::ManageZones]).await; @@ -1126,6 +1168,23 @@ pub(crate) mod tests { .status, 403 ); + // The description and the release are map details, like tiers: `manage_maps`, not `manage_zones`. + for (path, body) in [ + ( + "/v1/staff/maps/bhop_a/description", + json!({ "description": "Hijacked" }), + ), + ( + "/v1/staff/maps/bhop_a/release", + json!({ "released": { "year": 2012 }, "mapper": null }), + ), + ] { + assert_eq!( + send(&db, "PUT", path, &zones_only, Some(body)).await.status, + 403, + "{path}" + ); + } let maps_only = session(&db, STAFF, &[Privilege::ManageMaps]).await; let body = json!({ "based_on": null, "zones": zone_set(1000.0), "note": "Sneaky" }); diff --git a/api/crates/api/src/staff/players.rs b/api/crates/api/src/staff/players.rs index c5f4524..26d24f5 100644 --- a/api/crates/api/src/staff/players.rs +++ b/api/crates/api/src/staff/players.rs @@ -217,7 +217,7 @@ async fn read_bans(db: &PgPool, which: Which) -> Result, sqlx::Error> { r#" SELECT * FROM ( SELECT b.id, b.restrictions, b.reason, b.banned_at, b.ends_at, b.lifted_at, b.lift_reason, - b.lifted_at IS NULL AND (b.ends_at IS NULL OR b.ends_at > now()) AS "active!", + EXISTS (SELECT 1 FROM active_bans a WHERE a.id = b.id) AS "active!", p.steamid64::text AS "steamid64!", p.name, p.avatar_url, p.country_code, s.steamid64::text AS "by_steamid64?", s.name AS "by_name?", s.avatar_url AS by_avatar_url, s.country_code AS by_country_code, @@ -434,7 +434,7 @@ pub struct BanRequest { pub reason: String, /// When it ends by itself; null or left out: until lifted. pub ends_at: Option>, - /// Also invalidate every valid run they have. Needs `no_records`. + /// Also invalidate every valid run they have. Needs `no_records`, and the `invalidate_runs` privilege. #[serde(default)] pub invalidate_runs: bool, } @@ -449,7 +449,7 @@ pub struct BanRequest { responses( (status = CREATED, body = Banned), (status = UNAUTHORIZED, description = "Not signed in", body = ErrorBody), - (status = FORBIDDEN, description = "Needs manage_bans, from SourceJump's pages", body = ErrorBody), + (status = FORBIDDEN, description = "Needs manage_bans (and invalidate_runs to invalidate), from SourceJump's pages", body = ErrorBody), (status = UNPROCESSABLE_ENTITY, description = "Not a SteamID, no restriction or reason, an end in the past, or yourself", body = ErrorBody), ), )] @@ -460,6 +460,9 @@ pub async fn post_ban( JsonBody(request): JsonBody, ) -> Result<(StatusCode, Json), ApiError> { staff.require(Privilege::ManageBans)?; + if request.invalidate_runs { + staff.require(Privilege::InvalidateRuns)?; + } let player = parse_player(&player)?; let reason = check_reason(&request.reason)?; let banned = ban( @@ -671,7 +674,12 @@ mod tests { .execute(&db) .await .unwrap(); - let token = session(&db, STAFF, &[Privilege::ManageBans]).await; + let token = session( + &db, + STAFF, + &[Privilege::ManageBans, Privilege::InvalidateRuns], + ) + .await; submit(&db, &key, "before", 30.0).await; let body = json!({ @@ -830,6 +838,27 @@ mod tests { ); } + #[sqlx::test] + async fn a_ban_takes_runs_only_from_staff_who_may_invalidate_them(db: PgPool) { + let key = setup(&db).await; + let token = session(&db, STAFF, &[Privilege::ManageBans]).await; + submit(&db, &key, "r1", 30.0).await; + + let body = json!({ "restrictions": ["no_records"], "reason": "Cheating", "invalidate_runs": true }); + assert_eq!(post_ban(&db, &token, body).await.status, 403); + // Refused whole: no ban either, and the run stays on the board. + let bans = sqlx::query_scalar!("SELECT count(*) FROM bans") + .fetch_one(&db) + .await + .unwrap(); + assert_eq!(bans, Some(0)); + assert_eq!(board(&db).await, [30.0]); + + let body = json!({ "restrictions": ["no_records"], "reason": "Cheating" }); + let banned: Banned = post_ban(&db, &token, body).await.json(); + assert_eq!(banned.invalidated_runs, 0); + } + #[sqlx::test] async fn a_ban_from_posting_stops_post_writes(db: PgPool) { let staff = session(&db, STAFF, &[Privilege::ManageBans]).await; @@ -871,7 +900,12 @@ mod tests { #[sqlx::test] async fn bans_are_checked(db: PgPool) { - let token = session(&db, STAFF, &[Privilege::ManageBans]).await; + let token = session( + &db, + STAFF, + &[Privilege::ManageBans, Privilege::InvalidateRuns], + ) + .await; for body in [ json!({ "restrictions": [], "reason": "Nothing" }), json!({ "restrictions": ["no_records"], "reason": " " }), diff --git a/api/crates/api/src/storage.rs b/api/crates/api/src/storage.rs index 9d5ba68..c3e871f 100644 --- a/api/crates/api/src/storage.rs +++ b/api/crates/api/src/storage.rs @@ -25,6 +25,17 @@ const REQUEST_TIMEOUT: Duration = Duration::from_secs(30); #[cfg(not(test))] const STAGED_READ_TIMEOUT: Duration = Duration::from_mins(5); +/// Storing a public file: viewer files run to about 256 MB, which at a slow 1 MB/s from the API's host to +/// storage takes four minutes and a bit. Small files finish long before, so the bound costs them nothing. +#[cfg(not(test))] +const PUT_TIMEOUT: Duration = Duration::from_mins(10); + +/// Passing a file on (`get_stream`): the body is read from storage only as fast as the client downloads it, +/// so this bounds the slowest download allowed. Half an hour lets a 128 MB replay through at 75 KB/s; a +/// download slower than that is cut off, and the client can start it again. +#[cfg(not(test))] +const STREAM_TIMEOUT: Duration = Duration::from_mins(30); + fn env_var(name: &str) -> Result { std::env::var(name).map_err(|_| format!("{name} must be set")) } @@ -149,6 +160,7 @@ impl Storage { let mut request = self .http .put(url) + .timeout(PUT_TIMEOUT) .header("content-type", content_type) .header("cache-control", "public, max-age=31536000, immutable"); if let Some(encoding) = content_encoding { @@ -259,7 +271,7 @@ impl Storage { .get_object(Some(&self.credentials), object_key) .sign(REQUEST_LIFETIME) .into(); - let response = self.http.get(url).send().await?; + let response = self.http.get(url).timeout(STREAM_TIMEOUT).send().await?; check(response.status())?; let size = response.content_length(); // Ends after the first error, which the receiver sees as a cut-off body. @@ -391,6 +403,11 @@ impl Staging { } } + /// The uploads under `prefix` that have finished, with the sizes they had when listed. + pub async fn list(&self, prefix: &str) -> Result, StorageError> { + self.0.list(prefix).await + } + /// Removes an upload. Removing one that isn't there succeeds. pub async fn delete(&self, object_key: &str) -> Result<(), StorageError> { self.0.delete(object_key).await diff --git a/api/openapi.json b/api/openapi.json index 659546f..fb89b9b 100644 --- a/api/openapi.json +++ b/api/openapi.json @@ -4727,7 +4727,7 @@ } }, "403": { - "description": "Needs manage_bans, from SourceJump's pages", + "description": "Needs manage_bans (and invalidate_runs to invalidate), from SourceJump's pages", "content": { "application/json": { "schema": { @@ -6080,7 +6080,7 @@ }, "invalidate_runs": { "type": "boolean", - "description": "Also invalidate every valid run they have. Needs `no_records`." + "description": "Also invalidate every valid run they have. Needs `no_records`, and the `invalidate_runs` privilege." }, "reason": { "type": "string", diff --git a/api/seed/dev.sql b/api/seed/dev.sql index 482f115..ade0ee8 100644 --- a/api/seed/dev.sql +++ b/api/seed/dev.sql @@ -288,6 +288,9 @@ FROM runs WHERE invalidated_at IS NULL ORDER BY player_steamid64, map_id, style, track, time, id ON CONFLICT DO NOTHING; +-- Written above without the API, so each map's player count is recounted here (`maps::count_players`). +UPDATE maps m SET players = (SELECT count(DISTINCT player_steamid64) FROM personal_bests WHERE map_id = m.id); + -- Sample bans, added once: sample_42 (whose runs were invalidated above) from records and posting until -- lifted, and zephyr's lifted ban from posting. Each ban's log entry is written with it, as the API does. WITH logged AS ( diff --git a/docs/api.md b/docs/api.md index c49fa9d..4153656 100644 --- a/docs/api.md +++ b/docs/api.md @@ -24,10 +24,10 @@ How the `/v1/` API behaves for game servers, and rules every client can rely on. ## Rules every client can rely on - **Errors** always have the body `{"error": "..."}`. The message is for people; don't match on it. -- **Retrying:** `2xx` means stored. Network errors, `5xx` and `429` (too many at once) are temporary: keep the item queued and resend it later. Any other `4xx` is final: the request breaks a rule and resending it will never work, so the plugin drops it. +- **Retrying:** `2xx` means stored. Network errors, `5xx` and `429` (too many) are temporary: keep the item queued and resend it later. Any other `4xx` is final: the request breaks a rule and resending never works, so the plugin drops it. - **Idempotency:** runs and detections carry an ID from the game server (`server_run_id`, `server_detection_id`), unique per server. A resend gets `200` with the first answer and changes nothing; the first send gets `201`. - **SteamID64s** are JSON strings: too big for a JavaScript number, and SourcePawn has no 64-bit integers. -- **Map names** are lowercased; other characters than `a-z 0-9 _ - .` are rejected. +- **Map names** are lowercased; other characters than `a-z 0-9 _ - .`, or only dots, are rejected. - **Server times** (`finished_at`, `detected_at`) are Unix seconds by the game server's clock. The API also records when it received each item. Times over 5 minutes ahead of the API's clock get `422`. ## Keys @@ -37,10 +37,10 @@ How the `/v1/` API behaves for game servers, and rules every client can rely on. - Staff issue, replace and revoke keys on `/staff/keys` (or `sourcejump-api keys issue ""`, `keys revoke `). A server holds one active key at a time (a unique index); a new key revokes its old one in the same step. How servers request keys is still open (`domain.md`). ## Server heartbeats -- Every server with the plugin, keyed or not, sends one on map start and then every `next_heartbeat_seconds` (600 now): hostname, port, map, players and slots, versions (`shavit_version` null without shavit), plugins, compliance. +- Every plugin server, keyed or not, sends one on map start and then every `next_heartbeat_seconds` (600 now): hostname, port, map, players and slots, versions (`shavit_version` null without shavit), plugins, compliance. - A server is its address: the IP the heartbeat came from plus its reported port. Behind Caddy the IP is the last `X-Forwarded-For` entry, `country_code` Cloudflare's `CF-IPCountry` (`XX`, `T1`: none), trusted only from this machine (`client_address.rs`). The reported `ip` is for staff, never trusted. - Only the latest heartbeat per address is kept (`server_heartbeats`), at most 32 ports per IP; an hourly job drops unlisted ones silent 7 days. A valid key links the address to its `servers` row. -- Over 64 KB gets `413`. Hostnames lose control characters, cut to 64 characters. An IP (IPv6: /64) may send 120 at once, then 30 a minute; more get `429` (`heartbeat_limit.rs`). +- Over 64 KB gets `413`. Hostnames lose control characters, cut to 64. An IP (IPv6: /64) may send 120 at once, then 30 a minute; more get `429`, before any key check (`heartbeat_limit.rs`). ## Runs - Accepted only if the run's ruleset version exists, its style is ranked in that version, its tickrate matches the ruleset's `tickrate` if set, and the plugin reports the server as compliant. Field ranges are checked once, at the boundary. @@ -49,9 +49,9 @@ How the `/v1/` API behaves for game servers, and rules every client can rely on. ## Replays - The `replays` policy decides which replays are wanted (`request`: `new_personal_best`, the lead's call on 2026-10-19; `every_run` or `nothing`) and which styles' replays are public (`public_styles`: every ranked style, lead 2026-11-03). A missing policy asks for nothing and makes nothing public. -- A wanted replay gets a presigned `PUT` link, valid for an hour, to `uploads/replays/-.replay`. The server uploads there directly. A resend of the run returns a fresh link, with `last_refusal` (reason, time) if the last upload was refused, or no link once the replay is stored (delete the local copy). -- Every 30 s a job (`replays/processing.rs`) lists the uploads and checks each against its run: shavit format 12 only, frames fill the file exactly, same map (any case), track, player and tickrate, and the time within half a tick. Files over 128 MB are refused unread. A good file is stored as SJR (`replays/sjr.rs`, a fifth the size) at `replays/-.sjr`; the upload is deleted. A bad one is deleted, its reason kept on the `replays` row, and the server may upload again. A stored replay never changes; an erroring upload is retried later. -- The bucket is public; random keys mean only the API reveals where a file is. `GET /v1/runs/{id}` gives the SJR link (`replay.sjr_url`; invalidated runs too, lead 2026-11-03), its route from above (`replay.route`, `replay-viewer.md`), and `GET /v1/replays/{run_id}` the shavit file, rebuilt byte for byte at its first download and kept, only while the run is valid and its style is public. +- A wanted replay gets a presigned `PUT` link, valid for an hour, to `uploads/replays/-.replay` in a private bucket. The server uploads there directly. A resend of the run returns a fresh link, with `last_refusal` (reason, time) if the last upload was refused, or no link once the replay is stored (delete the local copy). +- Every 30 s a job (`replays/processing.rs`) lists the uploads and checks each against its run: shavit format 12 only, frames fill the file exactly, same map (any case), track, player and tickrate, and the time within half a tick. Over 128 MB is refused. A good file is stored as SJR (`replays/sjr.rs`, a fifth the size) at `replays/-.sjr`; the upload is deleted. A bad one is deleted, its reason kept on the `replays` row, and the server may upload again. A stored replay never changes; a failing upload is retried. +- The bucket is public; random keys mean only the API reveals files. `GET /v1/runs/{id}` gives the SJR link (`replay.sjr_url`; invalidated runs too, lead 2026-11-03), its route from above (`replay.route`, `replay-viewer.md`), and `GET /v1/replays/{run_id}` the shavit file, rebuilt byte for byte at its first download and kept (own random key), only while the run is valid and its style is public. - `GET /v1/maps/{map}/records` marks each record `has_replay` by the same rule, so a server only asks for files it will get. ## Map records for `/wr` diff --git a/docs/deploy.md b/docs/deploy.md index a85d8e3..ac0af89 100644 --- a/docs/deploy.md +++ b/docs/deploy.md @@ -22,7 +22,7 @@ Each environment has a file `deploy/environments/.env` with `SSH_TARGET` (a - Logs: `journalctl -u sourcejump-api -u sourcejump-web` on the server. - Issue or revoke a server's API key: `ssh 'set -a; . /etc/sourcejump/api.env; /opt/sourcejump/api/sourcejump-api keys issue ""'` (or `keys revoke `). The key is printed once. - Discord record feeds (`record_events/discord.rs`): put the feeds JSON at `/etc/sourcejump/discord-feeds.json` (owned by `sourcejump`, mode 600: its webhook URLs are secrets) and set `SJ_DISCORD_FEEDS` to that path in `api.env`. Each entry is `{"name", "records"?: "world_records" | "personal_bests", "style"?, "min_tier"?, "max_tier"?, "untiered"?, "bonuses"?, "webhook"}`; the lead keeps the current file. Without it nothing is posted. A feed's first start posts only records from then on. The tier icons next to map names are `web/public/tiers/` (`scripts/tier-icons.py`). -- Rollback: check out the previous commit and deploy it. Migrations are additive, so the older binary still runs on the newer schema; binaries from 2026-11-05 on start despite migrations they don't know. Before that, they refused: restore the backup instead. +- Rollback: check out the previous commit and deploy it. Migrations are additive, so the older binary still runs on the newer schema; binaries from 2026-11-05 on start despite migrations they don't know. Before that, they refused: restore the backup instead. Migration 0051 dropped a column that older binaries read, so rolling back past it means restoring the backup taken before it. - Sample data on staging: `ssh 'runuser -u sourcejump -- psql sourcejump' < api/seed/dev.sql`. Never on production. ## Gotchas @@ -32,12 +32,12 @@ Each environment has a file `deploy/environments/.env` with `SSH_TARGET` (a - `/opt` is read-only to the services except the web app's `.next/cache`. ## Object storage: Cloudflare R2 (ADR 0007) -Two buckets from the beta on. `sourcejump` is public by design: keys carry a random part, so only the API says where a file is (`docs/api.md`). `sourcejump-staging` is private: staff uploads wait there until the API checks them, so it never gets a custom domain or r2.dev access. Set up once, in the Cloudflare dashboard: +Two buckets from the beta on. `sourcejump` is public by design: keys carry a random part, so only the API says where a file is (`docs/api.md`). `sourcejump-staging` is private: staff and game server uploads (viewer files, replays) wait there until the API checks them, so it never gets a custom domain or r2.dev access. Set up once, in the Cloudflare dashboard: 1. R2 > Create buckets `sourcejump` and `sourcejump-staging`. Then Manage API tokens > Create: permission Object Read & Write, applied to those two buckets only. Note the access key ID and secret; they're shown once. 2. Bucket > Settings > Custom Domains > Add `files.sourcejump.net`. Not r2.dev, which is rate-limited and uncached. 3. Bucket > Settings > CORS Policy > JSON, for the replay viewer (`docs/replay-viewer.md`); add sourcejump.net at the switch: `[{"AllowedOrigins": ["https://beta.sourcejump.net"], "AllowedMethods": ["GET", "HEAD"], "AllowedHeaders": ["*"], "ExposeHeaders": ["Content-Range", "Content-Length"], "MaxAgeSeconds": 86400}]` - For `sourcejump-staging`, staff pages upload straight to it: + For `sourcejump-staging`, staff pages upload straight to it (game servers upload replays there too, without CORS): `[{"AllowedOrigins": ["https://beta.sourcejump.net"], "AllowedMethods": ["PUT"], "AllowedHeaders": ["*"], "MaxAgeSeconds": 86400}]` and under Object lifecycle rules, delete objects 1 day after upload, so abandoned uploads go. 4. Caching > Cache Rules: for hostname `files.sourcejump.net`, eligible for cache. By default Cloudflare caches only some file types, not `.sjr`, `.bsp` or `.vpk`; the files' own `Cache-Control` sets how long. diff --git a/docs/site-api.md b/docs/site-api.md index 6a0bec7..33d8dd4 100644 --- a/docs/site-api.md +++ b/docs/site-api.md @@ -2,10 +2,10 @@ What the endpoints behind the website do: sign-in, players, maps, the server list and community posts; the staff tools are in `staff-api.md`. Rules for every client (errors, retries, SteamIDs) and game servers' endpoints: `api.md`. ## Sign-in and sessions -Players sign in with Steam (OpenID 2.0). These endpoints answer browsers with redirects and cookies, via the site's origin (Caddy in production, a Next.js rewrite in development). -- Steam's answer is trusted only after Steam confirms it (`check_authentication`). Only signed fields are read, the return URL must be our callback, the nonce must be under 5 minutes old, and each nonce works once (`openid_nonces`). A refused sign-in goes to `/sign-in/failed`; cancelling on Steam goes back where the player was. +Players sign in with Steam (OpenID 2.0); the endpoints answer browsers (redirects, cookies) via the site's origin (Caddy in production, Next.js locally). +- Steam must confirm its answer (`check_authentication`). Only signed fields are read, the return URL must be our callback, a nonce works once, under 5 minutes old (`openid_nonces`). The callback URL must carry the value `/v1/auth/steam` put in the `sj_sign_in` cookie, tying sign-in to its browser. Refused: `/sign-in/failed`; cancelled: back where they were. - `return_to` must be a path on our site; anything else becomes `/`. -- A session is 32 random bytes in the `sj_session` cookie (`HttpOnly; Secure; SameSite=Lax`). Only the SHA-256 is stored. It lasts 30 days from last use; the expiry moves at most once a day. +- A session is 32 random bytes in the `sj_session` cookie (`HttpOnly; Secure; SameSite=Lax`). Only the SHA-256 is stored. It lasts 30 days from last use (expiry moves at most daily). - Sign-out also requires our `Origin` header. A missing session answers `401` without `WWW-Authenticate`. - `SJ_PUBLIC_URL` is where players reach the site; redirects and the origin check use it. @@ -42,7 +42,7 @@ Players sign in with Steam (OpenID 2.0). These endpoints answer browsers with re ## Server list - `GET /v1/servers` shows only addresses in `server_listings`, kept by staff (lead, 2026-10-20), heard from within the `servers` policy's `listed_hours` (24); `online` means within `online_minutes` (25). Without the policy the list is empty. *Connected*: the latest heartbeat had a still-valid key. Compliance and versions aren't public. `whitelist` (staff-set: only approved players join) puts it under the Whitelist tab. - Listed servers' heartbeats also go to `server_heartbeat_log` (players, 30 days) and `server_activity_daily` (per-server UTC-day totals, 400 days). `…/activity?range=day|week|month` buckets the log (20 min, 2 h, 8 h); `year` reads the daily totals. Per bucket: servers heard from, and their average players summed. -- Staff with `manage_servers` see every server on `/servers`: unlisted ones dimmed, toggles to list or unlist and to mark whitelist, and a dialog with its last report (versions, plugins, compliance, reported IP, key), loaded in the browser so the page stays cacheable. The CLI's `servers seen`, `servers list ` and `servers unlist ` do the same. +- Staff with `manage_servers` see every server on `/servers`: unlisted ones dimmed, toggles to list or unlist and to mark whitelist, and a dialog with its last report (versions, plugins, compliance, reported IP, key), loaded in the browser, never in the server render. The CLI's `servers seen`, `servers list ` and `servers unlist ` do the same. ## Community posts - Articles (Markdown) and YouTube videos (a link with an optional note; the site links out and embeds nothing, lead 2026-10-20). Players with `post_community` post and change or remove their own; `manage_posts` edits anyone's, with a `reason` and logged (`edit_post`, fields changed) when it's someone else's and changes something. Every write needs a session and our `Origin`. diff --git a/docs/staff-api.md b/docs/staff-api.md index 3da83e9..ba29a0d 100644 --- a/docs/staff-api.md +++ b/docs/staff-api.md @@ -6,7 +6,7 @@ What the endpoints behind the staff tools do. Staff act on the pages the tools a - `GET /v1/staff/maps?q=` lists every map, including ones with zones and no runs (`no_runs=true` for only those, the Maps page's staff tab); `GET /v1/staff/maps/{map}` answers for any valid name, so zones can be set up before a map's first run; it carries the map's `viewer_file` for the zone editor. `PUT …/tiers/{track}`, `PUT …/description`, `PUT …/release` (`released`: a year, month or day, not in the future; `mapper` as any SteamID form) and the pictures (`staff/map_images.rs`: `POST …/images` with the picture as the body, `DELETE …/images/{id}`, `PUT …/images/order` naming every picture once, `PUT …/images/{id}/focus`) need `manage_maps`; a map's picture changes lock its row, so two uploads can't both take the 10th place; the CLI's `maps import-pictures ` gives maps without pictures their first ones from one folder per map (`/` or `/screenshots/`), encoding on the machine running it so only the WebP files are uploaded, the owner's `_owner_*` shot first as the cover, too-small shots left out rather than scaled up, one log entry per map (`--dry-run` reports only); `POST …/zones` (whole set, `based_on`, note) needs `manage_zones`. The viewer file (`staff/map_viewer_file.rs`) takes either privilege, since zoning needs the file, and uploads in two steps, because Cloudflare refuses request bodies over 100 MB on the way to the API: `POST …/viewer-file/uploads` gives a 15-minute presigned link into the private staging bucket (`storage::Staging`, never served), the page uploads the packed map there (brotli, up to 256 MB), then `PUT …/viewer-file` names the upload; `GET /v1/staff/viewer-files` lists every stored file's map, hash and size, for the Maps page's bulk upload. The API reads it (stopping past 256 MB), checks it by stripping it again, which must change nothing, and stores the bytes it checked; it adds a map SourceJump hasn't seen and replaces the old file. Packing happens in the page because brotli at quality 11 takes minutes of CPU on a large map (3–4 s per MB in WebAssembly); the packer is `api/crates/viewer-pack-web`, built into `web/public/viewer-pack.wasm` by `just wasm` and committed. For many maps at once staff run `sj-pack`, the same packer natively, several maps in parallel; it and the packer live in the public `sourcejump-packer` repo, whose GitHub releases build it for Linux and Windows. Tiers and zone sets are append-only; the Manage view shows tier history in the map's log. - Zone sets are published on the map's Manage view, `/maps/{map}/manage`, from a file or drawn in its 3D editor (or `sourcejump-api zones import `; `zones import-folder` seeds every map in a folder of such files that has no zones yet); both check the zone types, that tracks are shavit's (main and bonuses 1–8) with at most 128 zones, and that the main track has a start and an end, and both are logged. - Run invalidation needs `invalidate_runs`: `POST /v1/staff/runs/{id}/invalidate` and `…/restore` for one run (from its page), `POST /v1/staff/invalidations` for every valid run of a `player` or `server` (across its keys), `GET …/invalidations/preview` to count them first, `GET /v1/staff/invalidations` (`?player=`, `?server=`) to list them and `POST …/{id}/restore` to undo one. Each takes a reason. `runs.invalidated_by` names the log entry that took a run, so an undo restores exactly that. Revoking a key with `invalidate_runs` also needs the privilege. `runs/invalidation.rs` recomputes the affected personal bests in the same transaction (ties keep the earlier run); every public read skips invalidated runs. Nobody invalidates or restores their own runs; restoring a server's invalidation leaves the restorer's own for a colleague. -- Bans need `manage_bans`: `GET /v1/staff/bans` (active, and the 50 latest ended), `POST /v1/staff/players/{player}/bans` (restrictions, reason, optional `ends_at`, `invalidate_runs` with `no_records`), `POST …/bans/{id}/lift` (reason). `GET /v1/staff/players/{player}` answers any SteamID for staff with `manage_bans`, `invalidate_runs`, `manage_privileges` or `review_anticheat`, with bans only for the first. Restriction bits are in `players/restrictions.rs`; a player's restrictions are their active bans' (not lifted, not past `ends_at`). `bans` rows change only by one lift (trigger). Nobody bans themselves or lifts their own ban. +- Bans need `manage_bans`: `GET /v1/staff/bans` (active, and the 50 latest ended), `POST /v1/staff/players/{player}/bans` (restrictions, reason, optional `ends_at`, `invalidate_runs` with `no_records`, which also needs that privilege), `POST …/bans/{id}/lift` (reason). `GET /v1/staff/players/{player}` answers any SteamID for staff with `manage_bans`, `invalidate_runs`, `manage_privileges` or `review_anticheat`, with bans only for the first. Restriction bits are in `players/restrictions.rs`; a player's restrictions are their active bans' (not lifted, not past `ends_at`: the `active_bans` view; `records_bans` are those with `no_records`). `bans` rows change only by one lift (trigger). Nobody bans themselves or lifts their own ban. - `POST /v1/staff/players/{player}/privileges` (`grant`, `revoke`, reason) needs `manage_privileges`; the player page then lists what they hold. Only the changing half is logged, and a request that changes nothing is refused. Nobody changes their own, and `manage_privileges` itself changes only on the CLI. - `GET /v1/staff/players/{player}/detections` needs `review_anticheat`: one player's anti-cheat detections (`staff/anticheat.rs`) for their Manage view, 50 a page, newest first by ID (a server's clock can be wrong), older via `?before=` (the page's `older`). Detections are read-only for now; what staff do with them is still open (`domain.md`). Reviewers may also read `GET /v1/staff/players/{player}`. - Posts need `manage_posts`: `POST /v1/staff/posts/{id}/hide` removes someone else's post (reason; `posts.hidden_log_id` links the entry), `GET /v1/staff/posts` pages posts like `GET /v1/posts` with removed ones in place (with `removal`: who, when, why), `GET /v1/staff/posts/{id}` reads one even when removed, `POST …/{id}/restore` (reason) brings back one staff removed. An author's own removal can't be restored; nobody restores their own post. Community pages load removed posts, New post, Edit and Remove in the browser, so they render alike for all. diff --git a/scripts/dev-s3.sh b/scripts/dev-s3.sh index 0459451..f6e9077 100755 --- a/scripts/dev-s3.sh +++ b/scripts/dev-s3.sh @@ -9,7 +9,7 @@ cd "$(dirname "$0")/.." dir="$PWD/.garage" config="$dir/garage.toml" bucket=sourcejump-dev -# Private, no website: staff uploads wait here until the API checks them. +# Private, no website: staff and replay uploads wait here until the API checks them. staging=sourcejump-dev-staging key_id=GK000000000000000000000001 key_secret=0000000000000000000000000000000000000000000000000000000000000001 diff --git a/web/src/api/schema.ts b/web/src/api/schema.ts index c0bc7c7..87344f9 100644 --- a/web/src/api/schema.ts +++ b/web/src/api/schema.ts @@ -1496,7 +1496,7 @@ export interface components { * @description When it ends by itself; null or left out: until lifted. */ ends_at?: string | null; - /** @description Also invalidate every valid run they have. Needs `no_records`. */ + /** @description Also invalidate every valid run they have. Needs `no_records`, and the `invalidate_runs` privilege. */ invalidate_runs?: boolean; /** @description Why, for staff only. 1 to 500 characters. */ reason: string; @@ -6734,7 +6734,7 @@ export interface operations { "application/json": components["schemas"]["ErrorBody"]; }; }; - /** @description Needs manage_bans, from SourceJump's pages */ + /** @description Needs manage_bans (and invalidate_runs to invalidate), from SourceJump's pages */ 403: { headers: { [name: string]: unknown; diff --git a/web/src/features/auth/useSignedInPlayer.ts b/web/src/features/auth/useSignedInPlayer.ts index c092a1e..4f3ef21 100644 --- a/web/src/features/auth/useSignedInPlayer.ts +++ b/web/src/features/auth/useSignedInPlayer.ts @@ -1,6 +1,7 @@ "use client"; // The signed-in player, asked from the browser. Public pages show per-player buttons and staff tools through -// this, so their server render is the same for everyone and can be cached. The API checks every change again. +// this, so their server render is the same for everyone (nothing caches it yet; it is built per request). The API +// checks every change again. import { useEffect, useState } from "react"; import { api, type Schemas } from "@/api/client"; diff --git a/web/src/features/leaderboards/StandingsTable.tsx b/web/src/features/leaderboards/StandingsTable.tsx index 714c36b..8b6a083 100644 --- a/web/src/features/leaderboards/StandingsTable.tsx +++ b/web/src/features/leaderboards/StandingsTable.tsx @@ -33,8 +33,13 @@ export function StandingsTable({ completedLabel: string; }) { const [standings, setStandings] = useState(first.standings); + // Rows read so far, counting repeats, so a page of players already shown still moves the next one on. + const [offset, setOffset] = useState(first.standings.length); const [loading, setLoading] = useState(false); const [failed, setFailed] = useState(false); + // Set when a page comes back empty: players left the board since the first page, so the total overstates it. + const [ended, setEnded] = useState(false); + const complete = ended || standings.length >= first.total; const byPoints = first.ranking === "points"; const legacy = first.view === "legacy"; @@ -54,15 +59,29 @@ export function StandingsTable({ view: "current", }), limit: PAGE_SIZE, - offset: standings.length, + offset, }, }, }) .then((result) => result.data) .catch(() => undefined); setLoading(false); - if (loaded) setStandings((shown) => [...shown, ...loaded.standings]); - else setFailed(true); + if (!loaded) { + setFailed(true); + return; + } + if (loaded.standings.length === 0) setEnded(true); + setOffset((read) => read + loaded.standings.length); + // Standings move while someone reads them, so a player already shown can come again on a later page. + setStandings((shown) => { + const seen = new Set(shown.map((standing) => standing.player.steamid64)); + return [ + ...shown, + ...loaded.standings.filter( + (standing) => !seen.has(standing.player.steamid64), + ), + ]; + }); } // The ranked value comes first after the player, the other one after it. @@ -159,8 +178,8 @@ export function StandingsTable({
- {standings.length >= first.total - ? `All ${numbers.format(first.total)} players` + {complete + ? `All ${numbers.format(ended ? standings.length : first.total)} players` : `Top ${numbers.format(standings.length)} of ${numbers.format(first.total)} players`} {failed && ( @@ -168,7 +187,7 @@ export function StandingsTable({ More couldn't be loaded. Try again. )} - {standings.length < first.total && ( + {!complete && ( diff --git a/web/src/features/ruleset/RulesetView.tsx b/web/src/features/ruleset/RulesetView.tsx index 87a5dbc..52ab576 100644 --- a/web/src/features/ruleset/RulesetView.tsx +++ b/web/src/features/ruleset/RulesetView.tsx @@ -1,7 +1,7 @@ -// The current ruleset, fetched from `GET /v1/ruleset` on every request. -import { connection } from "next/server"; +// The current ruleset, as `GET /v1/ruleset` serves it. import type { ReactNode } from "react"; -import { api, type Schemas } from "@/api/client"; +import type { Schemas } from "@/api/client"; +import { currentRuleset } from "./currentRuleset"; const publishedDate = new Intl.DateTimeFormat("en", { dateStyle: "medium", @@ -9,20 +9,18 @@ const publishedDate = new Intl.DateTimeFormat("en", { }); export async function RulesetView() { - // Rendered at request time: the API isn't reachable while the site is built, and rulesets change. - await connection(); - const result = await api.GET("/v1/ruleset").catch(() => undefined); + const ruleset = await currentRuleset().catch(() => undefined); - if (!result?.data) { + if (!ruleset) { return (

- {result?.response.status === 404 + {ruleset === null ? "No ruleset has been published yet." : "The ruleset couldn't be loaded. Try again in a moment."}

); } - return ; + return ; } function RulesetDetails({ ruleset }: { ruleset: Schemas["Ruleset"] }) { diff --git a/web/src/features/ruleset/currentRuleset.ts b/web/src/features/ruleset/currentRuleset.ts new file mode 100644 index 0000000..245de65 --- /dev/null +++ b/web/src/features/ruleset/currentRuleset.ts @@ -0,0 +1,26 @@ +// The current ruleset, read from the API once per web server process. +import { cacheLife } from "next/cache"; +import { connection } from "next/server"; +import { api, type Schemas } from "@/api/client"; + +/** + * The current ruleset, or null if none is published. Throws when the API can't be reached, and a failed read + * isn't cached, so the next request asks again. + */ +export async function currentRuleset(): Promise { + // Read at request time, never while the site is built: the API isn't reachable then, and a build-time read + // would be kept in the static shell. + await connection(); + return cachedRuleset(); +} + +// A ruleset is published only by a migration (`docs/domain.md`), which ships with a deploy, and a deploy +// restarts the web app, which empties this in-memory cache. So the read can be kept for the process's life. +async function cachedRuleset(): Promise { + "use cache"; + cacheLife("max"); + const result = await api.GET("/v1/ruleset"); + if (result.data) return result.data; + if (result.response.status === 404) return null; + throw new Error(`GET /v1/ruleset answered ${result.response.status}`); +} diff --git a/web/src/features/ruleset/styles.ts b/web/src/features/ruleset/styles.ts index 5e4e873..0e5400d 100644 --- a/web/src/features/ruleset/styles.ts +++ b/web/src/features/ruleset/styles.ts @@ -1,6 +1,5 @@ // The ranked styles' display names and order, from the current ruleset. -import { cache } from "react"; -import { api } from "@/api/client"; +import { currentRuleset } from "./currentRuleset"; export interface Styles { /** Style keys in the ruleset's order. */ @@ -9,13 +8,13 @@ export interface Styles { name: (key: string) => string; } -/** Fetched once per request, however many components ask. Without a ruleset, keys stand in for names. */ -export const currentStyles = cache(async (): Promise => { - const result = await api.GET("/v1/ruleset").catch(() => undefined); - const styles = result?.data?.styles ?? []; +/** Without a ruleset, or while the API can't be reached, keys stand in for names. */ +export async function currentStyles(): Promise { + const ruleset = await currentRuleset().catch(() => null); + const styles = ruleset?.styles ?? []; const names = new Map(styles.map((style) => [style.key, style.name])); return { keys: styles.map((style) => style.key), name: (key) => names.get(key) ?? key, }; -}); +} diff --git a/web/src/features/servers/ServerRows.tsx b/web/src/features/servers/ServerRows.tsx index bf7ca1c..5221bbe 100644 --- a/web/src/features/servers/ServerRows.tsx +++ b/web/src/features/servers/ServerRows.tsx @@ -1,7 +1,8 @@ "use client"; // The Servers page's list. Everyone gets the listed servers rendered on the server; staff who hold // manage_servers then get every server from the browser, unlisted ones included, with their tools. A -// Public / Whitelist switch shows whitelist servers apart, filtered here so the page stays cacheable. +// Public / Whitelist switch shows whitelist servers apart, filtered here so the page's +// server render is the same whichever is picked. Nothing caches that render yet; it is built per request. import { useSearchParams } from "next/navigation"; import type { CSSProperties, ReactNode } from "react"; import type { Schemas } from "@/api/client"; diff --git a/web/src/features/staff/players/BanPlayer.tsx b/web/src/features/staff/players/BanPlayer.tsx index 11e40de..64fb124 100644 --- a/web/src/features/staff/players/BanPlayer.tsx +++ b/web/src/features/staff/players/BanPlayer.tsx @@ -18,7 +18,7 @@ export function BanPlayer({ validRuns, }: { player: Schemas["PlayerRef"]; - /** Their valid runs, for the option to invalidate them. */ + /** Their valid runs, for the option to invalidate them; 0 when the staff member can't invalidate runs. */ validRuns: number; }) { const [open, setOpen] = useState(false); diff --git a/web/src/features/staff/players/ManagePlayer.tsx b/web/src/features/staff/players/ManagePlayer.tsx index 1c4a700..5d912e0 100644 --- a/web/src/features/staff/players/ManagePlayer.tsx +++ b/web/src/features/staff/players/ManagePlayer.tsx @@ -67,7 +67,14 @@ export async function ManagePlayer({ steamid }: { steamid: string }) {
} + action={ + !own && ( + + ) + } > {bans.length === 0 ? (

Never banned.

diff --git a/web/src/features/staff/servers/useStaffServers.ts b/web/src/features/staff/servers/useStaffServers.ts index 48ec19b..9be1ac3 100644 --- a/web/src/features/staff/servers/useStaffServers.ts +++ b/web/src/features/staff/servers/useStaffServers.ts @@ -1,5 +1,6 @@ "use client"; -// Every server, for staff who hold manage_servers, read from the browser so the Servers page stays cacheable. +// Every server, for staff who hold manage_servers, read from the browser so the Servers page's +// server render is the same for everyone. Nothing caches that render yet; it is built per request. import { useCallback, useEffect, useState } from "react"; import { api, type Schemas } from "@/api/client"; import { useHeldPrivilege } from "../useHeldPrivilege";