diff --git a/api/.sqlx/query-05845fdb39cec480cfa34441c591594827f4b38fa1cc1b81d2974fc8141fa7b5.json b/api/.sqlx/query-05845fdb39cec480cfa34441c591594827f4b38fa1cc1b81d2974fc8141fa7b5.json deleted file mode 100644 index 5127cbf..0000000 --- a/api/.sqlx/query-05845fdb39cec480cfa34441c591594827f4b38fa1cc1b81d2974fc8141fa7b5.json +++ /dev/null @@ -1,71 +0,0 @@ -{ - "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 (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": [ - { - "ordinal": 0, - "name": "map!", - "type_info": "Text", - "origin": "Expression" - }, - { - "ordinal": 1, - "name": "track!", - "type_info": "Int2", - "origin": "Expression" - }, - { - "ordinal": 2, - "name": "time!", - "type_info": "Float8", - "origin": "Expression" - }, - { - "ordinal": 3, - "name": "rank", - "type_info": "Int8", - "origin": "Expression" - }, - { - "ordinal": 4, - "name": "legacy!", - "type_info": "Bool", - "origin": "Expression" - }, - { - "ordinal": 5, - "name": "id!", - "type_info": "Int8", - "origin": "Expression" - }, - { - "ordinal": 6, - "name": "finished_at", - "type_info": "Timestamptz", - "origin": "Expression" - } - ], - "parameters": { - "Left": [ - "Int8", - "Text", - "Bool", - "Int2", - "Text", - "Int2", - "Int8" - ] - }, - "nullable": [ - null, - null, - null, - null, - null, - null, - null - ] - }, - "hash": "05845fdb39cec480cfa34441c591594827f4b38fa1cc1b81d2974fc8141fa7b5" -} diff --git a/api/.sqlx/query-79f8a533b9ffccdb99dad6b682bac3b451a29743fab3da841bd22e572864ff19.json b/api/.sqlx/query-0e5fd428486c3ff6a9210aa1813d243795a041590b0a94267c0f6ed61a0f7746.json similarity index 73% rename from api/.sqlx/query-79f8a533b9ffccdb99dad6b682bac3b451a29743fab3da841bd22e572864ff19.json rename to api/.sqlx/query-0e5fd428486c3ff6a9210aa1813d243795a041590b0a94267c0f6ed61a0f7746.json index 186b6c9..c7e603b 100644 --- a/api/.sqlx/query-79f8a533b9ffccdb99dad6b682bac3b451a29743fab3da841bd22e572864ff19.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, 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\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 ", + "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": [ { @@ -152,6 +152,7 @@ "Timestamptz", "Int8", "Bool", + "Int8", "Int8" ] }, @@ -172,5 +173,5 @@ true ] }, - "hash": "79f8a533b9ffccdb99dad6b682bac3b451a29743fab3da841bd22e572864ff19" + "hash": "0e5fd428486c3ff6a9210aa1813d243795a041590b0a94267c0f6ed61a0f7746" } diff --git a/api/.sqlx/query-4d15f7b63cc1357d0f7bd08a497b0201d51891b09b5a2e8451c269958c283cb1.json b/api/.sqlx/query-4d15f7b63cc1357d0f7bd08a497b0201d51891b09b5a2e8451c269958c283cb1.json new file mode 100644 index 0000000..281c45c --- /dev/null +++ b/api/.sqlx/query-4d15f7b63cc1357d0f7bd08a497b0201d51891b09b5a2e8451c269958c283cb1.json @@ -0,0 +1,15 @@ +{ + "db_name": "PostgreSQL", + "query": "\n UPDATE players SET\n country_chosen = $2::text IS NOT NULL,\n country_from_server = country_from_server AND $2::text IS NULL,\n country_code = CASE WHEN $2::text IS NOT NULL THEN $2\n WHEN country_chosen THEN NULL ELSE country_code END,\n steam_profile_fetched_at = CASE WHEN $2::text IS NULL AND country_chosen THEN NULL\n ELSE steam_profile_fetched_at END\n WHERE steamid64 = $1\n ", + "describe": { + "columns": [], + "parameters": { + "Left": [ + "Int8", + "Text" + ] + }, + "nullable": [] + }, + "hash": "4d15f7b63cc1357d0f7bd08a497b0201d51891b09b5a2e8451c269958c283cb1" +} diff --git a/api/.sqlx/query-5b6cb1e375d768e19f444b9f407d80ab686ec4309e28f33e98af9653e0510e43.json b/api/.sqlx/query-5b6cb1e375d768e19f444b9f407d80ab686ec4309e28f33e98af9653e0510e43.json deleted file mode 100644 index 777adfb..0000000 --- a/api/.sqlx/query-5b6cb1e375d768e19f444b9f407d80ab686ec4309e28f33e98af9653e0510e43.json +++ /dev/null @@ -1,38 +0,0 @@ -{ - "db_name": "PostgreSQL", - "query": "\n WITH bounds AS (\n SELECT make_interval(mins => $1) AS bucket,\n date_bin(make_interval(mins => $1), now(), TIMESTAMPTZ '2000-01-01') AS current\n ), buckets AS (\n SELECT generate_series(current - $2 * bucket, current - bucket, bucket) AS at FROM bounds\n ), per_server AS (\n SELECT date_bin(bounds.bucket, h.received_at, TIMESTAMPTZ '2000-01-01') AS at,\n h.ip, h.port, avg(h.players) AS players\n FROM server_heartbeat_log h\n CROSS JOIN bounds\n JOIN server_listings l ON l.ip = h.ip AND l.port = h.port\n WHERE h.received_at >= bounds.current - $2 * bounds.bucket AND h.received_at < bounds.current\n GROUP BY 1, 2, 3\n )\n SELECT b.at AS \"at!\", count(p.ip)::int AS \"servers!\", coalesce(round(sum(p.players)), 0)::int AS \"players!\"\n FROM buckets b\n LEFT JOIN per_server p ON p.at = b.at\n GROUP BY b.at\n ORDER BY b.at\n ", - "describe": { - "columns": [ - { - "ordinal": 0, - "name": "at!", - "type_info": "Timestamptz", - "origin": "Expression" - }, - { - "ordinal": 1, - "name": "servers!", - "type_info": "Int4", - "origin": "Expression" - }, - { - "ordinal": 2, - "name": "players!", - "type_info": "Int4", - "origin": "Expression" - } - ], - "parameters": { - "Left": [ - "Int4", - "Float8" - ] - }, - "nullable": [ - null, - null, - null - ] - }, - "hash": "5b6cb1e375d768e19f444b9f407d80ab686ec4309e28f33e98af9653e0510e43" -} diff --git a/api/.sqlx/query-7fc6e231d7984b3b86529449e28e100d4f3e2c846b85cb200ac020301aa634db.json b/api/.sqlx/query-7fc6e231d7984b3b86529449e28e100d4f3e2c846b85cb200ac020301aa634db.json new file mode 100644 index 0000000..8f7ceab --- /dev/null +++ b/api/.sqlx/query-7fc6e231d7984b3b86529449e28e100d4f3e2c846b85cb200ac020301aa634db.json @@ -0,0 +1,27 @@ +{ + "db_name": "PostgreSQL", + "query": "\n SELECT count(*) AS \"total!\"\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 $5\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 ", + "describe": { + "columns": [ + { + "ordinal": 0, + "name": "total!", + "type_info": "Int8", + "origin": "Expression" + } + ], + "parameters": { + "Left": [ + "Text", + "Text", + "Int2", + "Bool", + "Bool" + ] + }, + "nullable": [ + null + ] + }, + "hash": "7fc6e231d7984b3b86529449e28e100d4f3e2c846b85cb200ac020301aa634db" +} diff --git a/api/.sqlx/query-93eea59cfcf180c2a844d37d091ea617589d12ed2e0a83bdd21fadded3c3d003.json b/api/.sqlx/query-93eea59cfcf180c2a844d37d091ea617589d12ed2e0a83bdd21fadded3c3d003.json new file mode 100644 index 0000000..90db3ff --- /dev/null +++ b/api/.sqlx/query-93eea59cfcf180c2a844d37d091ea617589d12ed2e0a83bdd21fadded3c3d003.json @@ -0,0 +1,23 @@ +{ + "db_name": "PostgreSQL", + "query": "SELECT coalesce(max(players), 0) AS \"players!\" FROM server_activity_peaks\n WHERE day >= (now() AT TIME ZONE 'UTC')::date - $1::int", + "describe": { + "columns": [ + { + "ordinal": 0, + "name": "players!", + "type_info": "Int4", + "origin": "Expression" + } + ], + "parameters": { + "Left": [ + "Int4" + ] + }, + "nullable": [ + null + ] + }, + "hash": "93eea59cfcf180c2a844d37d091ea617589d12ed2e0a83bdd21fadded3c3d003" +} diff --git a/api/.sqlx/query-a973e3651cccc5eda8cb8d30757435a668d6e76642ce55d5040addaaddf5c017.json b/api/.sqlx/query-a973e3651cccc5eda8cb8d30757435a668d6e76642ce55d5040addaaddf5c017.json new file mode 100644 index 0000000..e45a363 --- /dev/null +++ b/api/.sqlx/query-a973e3651cccc5eda8cb8d30757435a668d6e76642ce55d5040addaaddf5c017.json @@ -0,0 +1,12 @@ +{ + "db_name": "PostgreSQL", + "query": "DELETE FROM server_activity_peaks WHERE day < current_date - 400", + "describe": { + "columns": [], + "parameters": { + "Left": [] + }, + "nullable": [] + }, + "hash": "a973e3651cccc5eda8cb8d30757435a668d6e76642ce55d5040addaaddf5c017" +} diff --git a/api/.sqlx/query-a9c1668c23595148dae099063553e60c7876573fdcdb78c49deb9fe758dcbba5.json b/api/.sqlx/query-a9c1668c23595148dae099063553e60c7876573fdcdb78c49deb9fe758dcbba5.json deleted file mode 100644 index 803009b..0000000 --- a/api/.sqlx/query-a9c1668c23595148dae099063553e60c7876573fdcdb78c49deb9fe758dcbba5.json +++ /dev/null @@ -1,15 +0,0 @@ -{ - "db_name": "PostgreSQL", - "query": "\n UPDATE players SET\n country_chosen = $2::text IS NOT NULL,\n country_code = CASE WHEN $2::text IS NOT NULL THEN $2\n WHEN country_chosen THEN NULL ELSE country_code END,\n steam_profile_fetched_at = CASE WHEN $2::text IS NULL AND country_chosen THEN NULL\n ELSE steam_profile_fetched_at END\n WHERE steamid64 = $1\n ", - "describe": { - "columns": [], - "parameters": { - "Left": [ - "Int8", - "Text" - ] - }, - "nullable": [] - }, - "hash": "a9c1668c23595148dae099063553e60c7876573fdcdb78c49deb9fe758dcbba5" -} diff --git a/api/.sqlx/query-c105f82668f6dffc122fd260c3000f3b398402da30de05d54339d0b2a1d852c3.json b/api/.sqlx/query-c105f82668f6dffc122fd260c3000f3b398402da30de05d54339d0b2a1d852c3.json new file mode 100644 index 0000000..33b215e --- /dev/null +++ b/api/.sqlx/query-c105f82668f6dffc122fd260c3000f3b398402da30de05d54339d0b2a1d852c3.json @@ -0,0 +1,54 @@ +{ + "db_name": "PostgreSQL", + "query": "\n WITH bounds AS (\n SELECT make_interval(mins => $1) AS bucket,\n date_bin(make_interval(mins => $1), now(), TIMESTAMPTZ '2000-01-01') AS current,\n date_bin(make_interval(mins => $3), now(), TIMESTAMPTZ '2000-01-01') AS latest\n ), buckets AS (\n SELECT generate_series(current - $2 * bucket, current - bucket, bucket) AS at FROM bounds\n ), per_server AS (\n SELECT date_bin(bounds.bucket, h.received_at, TIMESTAMPTZ '2000-01-01') AS at,\n h.ip, h.port, avg(h.players) AS players\n FROM server_heartbeat_log h\n CROSS JOIN bounds\n JOIN server_listings l ON l.ip = h.ip AND l.port = h.port\n WHERE h.received_at >= bounds.current - $2 * bounds.bucket AND h.received_at < bounds.current\n GROUP BY 1, 2, 3\n ), online AS (\n SELECT m.at, sum(latest.players) AS players\n FROM bounds\n CROSS JOIN generate_series(\n bounds.current - $2 * bounds.bucket,\n bounds.latest,\n make_interval(mins => $3)\n ) AS m(at)\n CROSS JOIN LATERAL (\n SELECT DISTINCT ON (h.ip, h.port) h.players\n FROM server_heartbeat_log h\n JOIN server_listings l ON l.ip = h.ip AND l.port = h.port\n WHERE h.received_at > m.at - make_interval(mins => $4) AND h.received_at <= m.at\n ORDER BY h.ip, h.port, h.received_at DESC\n ) latest\n GROUP BY m.at\n ), peaks AS (\n SELECT date_bin(bounds.bucket, online.at, TIMESTAMPTZ '2000-01-01') AS at, max(online.players) AS players\n FROM online CROSS JOIN bounds\n GROUP BY 1\n ), whole AS (\n SELECT coalesce(max(online.players), 0)::int AS players\n FROM online CROSS JOIN bounds\n WHERE online.at >= bounds.current - $2 * bounds.bucket\n )\n SELECT b.at AS \"at!\", count(p.ip)::int AS \"servers!\", coalesce(round(sum(p.players)), 0)::int AS \"players!\",\n coalesce(pk.players, 0)::int AS \"peak_players!\", whole.players AS \"range_peak!\"\n FROM buckets b\n CROSS JOIN whole\n LEFT JOIN per_server p ON p.at = b.at\n LEFT JOIN peaks pk ON pk.at = b.at\n GROUP BY b.at, pk.players, whole.players\n ORDER BY b.at\n ", + "describe": { + "columns": [ + { + "ordinal": 0, + "name": "at!", + "type_info": "Timestamptz", + "origin": "Expression" + }, + { + "ordinal": 1, + "name": "servers!", + "type_info": "Int4", + "origin": "Expression" + }, + { + "ordinal": 2, + "name": "players!", + "type_info": "Int4", + "origin": "Expression" + }, + { + "ordinal": 3, + "name": "peak_players!", + "type_info": "Int4", + "origin": "Expression" + }, + { + "ordinal": 4, + "name": "range_peak!", + "type_info": "Int4", + "origin": "Expression" + } + ], + "parameters": { + "Left": [ + "Int4", + "Float8", + "Int4", + "Int4" + ] + }, + "nullable": [ + null, + null, + null, + null, + null + ] + }, + "hash": "c105f82668f6dffc122fd260c3000f3b398402da30de05d54339d0b2a1d852c3" +} diff --git a/api/.sqlx/query-5d5552b38e89f4ba65a4739c79d3902426a3541c08ea94eb96efc73cfdd91d28.json b/api/.sqlx/query-d229738260622b500ce9606ced26758e381bc932126584a48c813acb6e272509.json similarity index 50% rename from api/.sqlx/query-5d5552b38e89f4ba65a4739c79d3902426a3541c08ea94eb96efc73cfdd91d28.json rename to api/.sqlx/query-d229738260622b500ce9606ced26758e381bc932126584a48c813acb6e272509.json index fe7f77d..2c5680d 100644 --- a/api/.sqlx/query-5d5552b38e89f4ba65a4739c79d3902426a3541c08ea94eb96efc73cfdd91d28.json +++ b/api/.sqlx/query-d229738260622b500ce9606ced26758e381bc932126584a48c813acb6e272509.json @@ -1,6 +1,6 @@ { "db_name": "PostgreSQL", - "query": "\n SELECT own.style AS \"style!\", own.map AS \"map!\", own.track AS \"track!\", own.time AS \"time!\",\n 1 + (SELECT count(*) FROM personal_bests faster\n WHERE faster.map_id = own.map_id AND faster.style = own.style\n AND faster.track = own.track AND faster.time < own.time) AS \"rank!\",\n own.run_id AS \"run_id!\", own.finished_at AS \"finished_at!\"\n FROM (\n SELECT pb.style, pb.map_id, m.name AS map, pb.track, pb.time, pb.run_id, pb.finished_at\n FROM personal_bests pb\n JOIN maps m ON m.id = pb.map_id\n WHERE pb.player_steamid64 = $1\n AND ($2::text IS NULL OR pb.style = $2)\n AND ($3::text IS NULL OR (m.name, pb.track) > ($3, $4::smallint))\n AND ($6::bool IS NULL OR (pb.track > 0) = $6)\n AND (NOT $7 OR EXISTS (\n SELECT 1 FROM world_record_holders h\n WHERE h.player_steamid64 = $1 AND h.map_id = pb.map_id AND h.style = pb.style\n AND h.track = pb.track))\n AND ($9::timestamptz IS NULL OR (pb.finished_at, pb.run_id) < ($9, $10::int8))\n ORDER BY CASE WHEN $8 THEN pb.finished_at END DESC, CASE WHEN $8 THEN pb.run_id END DESC,\n pb.style, m.name, pb.track\n LIMIT $5\n ) own\n ORDER BY CASE WHEN $8 THEN own.finished_at END DESC, CASE WHEN $8 THEN own.run_id END DESC,\n own.style, own.map, own.track\n ", + "query": "\n SELECT own.style AS \"style!\", own.map AS \"map!\", own.track AS \"track!\", own.time AS \"time!\",\n 1 + (SELECT count(*) FROM personal_bests faster\n WHERE faster.map_id = own.map_id AND faster.style = own.style\n AND faster.track = own.track AND faster.time < own.time) AS \"rank!\",\n (SELECT min(best.time) FROM personal_bests best\n WHERE best.map_id = own.map_id AND best.style = own.style\n AND best.track = own.track) AS \"world_record_time!\",\n own.run_id AS \"run_id!\", own.finished_at AS \"finished_at!\", tier.tier AS \"tier?\"\n FROM (\n SELECT pb.style, pb.map_id, m.name AS map, pb.track, pb.time, pb.run_id, pb.finished_at\n FROM personal_bests pb\n JOIN maps m ON m.id = pb.map_id\n WHERE pb.player_steamid64 = $1\n AND ($2::text IS NULL OR pb.style = $2)\n AND ($3::text IS NULL OR (m.name, pb.track) > ($3, $4::smallint))\n AND ($6::bool IS NULL OR (pb.track > 0) = $6)\n AND (NOT $7 OR EXISTS (\n SELECT 1 FROM world_record_holders h\n WHERE h.player_steamid64 = $1 AND h.map_id = pb.map_id AND h.style = pb.style\n AND h.track = pb.track))\n AND ($9::timestamptz IS NULL OR (pb.finished_at, pb.run_id) < ($9, $10::int8))\n ORDER BY CASE WHEN $8 THEN pb.finished_at END DESC, CASE WHEN $8 THEN pb.run_id END DESC,\n pb.style, m.name, pb.track\n LIMIT $5\n ) own\n LEFT JOIN LATERAL (SELECT tier FROM map_tiers WHERE map_id = own.map_id AND track = own.track\n ORDER BY assigned_at DESC LIMIT 1) tier ON $11\n ORDER BY CASE WHEN $8 THEN own.finished_at END DESC, CASE WHEN $8 THEN own.run_id END DESC,\n own.style, own.map, own.track\n ", "describe": { "columns": [ { @@ -55,6 +55,12 @@ }, { "ordinal": 5, + "name": "world_record_time!", + "type_info": "Float8", + "origin": "Expression" + }, + { + "ordinal": 6, "name": "run_id!", "type_info": "Int8", "origin": { @@ -65,7 +71,7 @@ } }, { - "ordinal": 6, + "ordinal": 7, "name": "finished_at!", "type_info": "Timestamptz", "origin": { @@ -74,6 +80,17 @@ "name": "finished_at" } } + }, + { + "ordinal": 8, + "name": "tier?", + "type_info": "Int2", + "origin": { + "Table": { + "table": "map_tiers", + "name": "tier" + } + } } ], "parameters": { @@ -87,7 +104,8 @@ "Bool", "Bool", "Timestamptz", - "Int8" + "Int8", + "Bool" ] }, "nullable": [ @@ -96,9 +114,11 @@ false, false, null, + null, + false, false, false ] }, - "hash": "5d5552b38e89f4ba65a4739c79d3902426a3541c08ea94eb96efc73cfdd91d28" + "hash": "d229738260622b500ce9606ced26758e381bc932126584a48c813acb6e272509" } diff --git a/api/.sqlx/query-e8461c94cdc9a070ad92f8775d49ddddfd4e6a2d3d2e60667fc8bd7bf1720dc1.json b/api/.sqlx/query-e8461c94cdc9a070ad92f8775d49ddddfd4e6a2d3d2e60667fc8bd7bf1720dc1.json deleted file mode 100644 index 5f39239..0000000 --- a/api/.sqlx/query-e8461c94cdc9a070ad92f8775d49ddddfd4e6a2d3d2e60667fc8bd7bf1720dc1.json +++ /dev/null @@ -1,17 +0,0 @@ -{ - "db_name": "PostgreSQL", - "query": "\n UPDATE players SET\n avatar_url = $2,\n -- A country the player picked on their profile stays.\n country_code = CASE WHEN country_chosen THEN country_code ELSE $3 END,\n steam_profile_fetched_at = now(),\n name = CASE WHEN name_seen_at = '-infinity' AND $4::text IS NOT NULL THEN $4 ELSE name END\n WHERE steamid64 = $1\n ", - "describe": { - "columns": [], - "parameters": { - "Left": [ - "Int8", - "Text", - "Text", - "Text" - ] - }, - "nullable": [] - }, - "hash": "e8461c94cdc9a070ad92f8775d49ddddfd4e6a2d3d2e60667fc8bd7bf1720dc1" -} diff --git a/api/.sqlx/query-e9012bc9223b6610b7c31d60ea297dabf0f4666650de0b0bb02053016bbae597.json b/api/.sqlx/query-e9012bc9223b6610b7c31d60ea297dabf0f4666650de0b0bb02053016bbae597.json new file mode 100644 index 0000000..56c3130 --- /dev/null +++ b/api/.sqlx/query-e9012bc9223b6610b7c31d60ea297dabf0f4666650de0b0bb02053016bbae597.json @@ -0,0 +1,14 @@ +{ + "db_name": "PostgreSQL", + "query": "\n INSERT INTO server_activity_peaks AS p (day, players)\n SELECT (now() AT TIME ZONE 'UTC')::date, coalesce(sum(latest.players), 0)\n FROM (\n SELECT DISTINCT ON (h.ip, h.port) h.players\n FROM server_heartbeat_log h\n JOIN server_listings l ON l.ip = h.ip AND l.port = h.port\n WHERE h.received_at > now() - make_interval(mins => $1)\n ORDER BY h.ip, h.port, h.received_at DESC\n ) latest\n ON CONFLICT (day) DO UPDATE SET players = greatest(p.players, excluded.players)\n ", + "describe": { + "columns": [], + "parameters": { + "Left": [ + "Int4" + ] + }, + "nullable": [] + }, + "hash": "e9012bc9223b6610b7c31d60ea297dabf0f4666650de0b0bb02053016bbae597" +} diff --git a/api/.sqlx/query-edba6cf047a4c41a5fa80bf5e5c6de783bb61e2e78949dd939cb2c7979de7869.json b/api/.sqlx/query-edba6cf047a4c41a5fa80bf5e5c6de783bb61e2e78949dd939cb2c7979de7869.json new file mode 100644 index 0000000..00c8f4a --- /dev/null +++ b/api/.sqlx/query-edba6cf047a4c41a5fa80bf5e5c6de783bb61e2e78949dd939cb2c7979de7869.json @@ -0,0 +1,16 @@ +{ + "db_name": "PostgreSQL", + "query": "\n UPDATE players SET country_code = $2, country_from_server = true, country_chosen = false\n WHERE steamid64 = $1 AND name_seen_at <= $3\n AND (country_code IS DISTINCT FROM $2 OR NOT country_from_server)\n ", + "describe": { + "columns": [], + "parameters": { + "Left": [ + "Int8", + "Text", + "Timestamptz" + ] + }, + "nullable": [] + }, + "hash": "edba6cf047a4c41a5fa80bf5e5c6de783bb61e2e78949dd939cb2c7979de7869" +} diff --git a/api/.sqlx/query-fb61c057eeaad93846cb18dc80de808b8f42a0540f9eee7761f8845c487934f8.json b/api/.sqlx/query-fb61c057eeaad93846cb18dc80de808b8f42a0540f9eee7761f8845c487934f8.json new file mode 100644 index 0000000..88c2c6b --- /dev/null +++ b/api/.sqlx/query-fb61c057eeaad93846cb18dc80de808b8f42a0540f9eee7761f8845c487934f8.json @@ -0,0 +1,17 @@ +{ + "db_name": "PostgreSQL", + "query": "\n UPDATE players SET\n avatar_url = $2,\n -- Steam's country comes last: one a game server found or the player picked stays.\n country_code = CASE WHEN country_chosen OR country_from_server THEN country_code ELSE $3 END,\n steam_profile_fetched_at = now(),\n name = CASE WHEN name_seen_at = '-infinity' AND $4::text IS NOT NULL THEN $4 ELSE name END\n WHERE steamid64 = $1\n ", + "describe": { + "columns": [], + "parameters": { + "Left": [ + "Int8", + "Text", + "Text", + "Text" + ] + }, + "nullable": [] + }, + "hash": "fb61c057eeaad93846cb18dc80de808b8f42a0540f9eee7761f8845c487934f8" +} diff --git a/api/.sqlx/query-acb722e75d97bd75a675a6e0153070345c0ef081f30d9e5efd1307f4b74008b4.json b/api/.sqlx/query-fd5b8736cae460ef4bcd20932b9e4543feced1f02ad9e893892c36eed2e9a69d.json similarity index 62% rename from api/.sqlx/query-acb722e75d97bd75a675a6e0153070345c0ef081f30d9e5efd1307f4b74008b4.json rename to api/.sqlx/query-fd5b8736cae460ef4bcd20932b9e4543feced1f02ad9e893892c36eed2e9a69d.json index 3fabd8b..8bf4ecc 100644 --- a/api/.sqlx/query-acb722e75d97bd75a675a6e0153070345c0ef081f30d9e5efd1307f4b74008b4.json +++ b/api/.sqlx/query-fd5b8736cae460ef4bcd20932b9e4543feced1f02ad9e893892c36eed2e9a69d.json @@ -1,6 +1,6 @@ { "db_name": "PostgreSQL", - "query": "\n WITH days AS (\n SELECT generate_series((today - $1::int)::timestamp, (today - 1)::timestamp, interval '1 day')::date AS day\n FROM (SELECT (now() AT TIME ZONE 'UTC')::date AS today) t\n )\n SELECT days.day::timestamp AT TIME ZONE 'UTC' AS \"at!\",\n count(d.ip)::int AS \"servers!\",\n coalesce(round(sum(d.players_sum::float8 / d.heartbeats)), 0)::int AS \"players!\"\n FROM days\n LEFT JOIN server_activity_daily d ON d.day = days.day\n AND EXISTS (SELECT 1 FROM server_listings l WHERE l.ip = d.ip AND l.port = d.port)\n GROUP BY days.day\n ORDER BY days.day\n ", + "query": "\n WITH days AS (\n SELECT generate_series((today - $1::int)::timestamp, (today - 1)::timestamp, interval '1 day')::date AS day\n FROM (SELECT (now() AT TIME ZONE 'UTC')::date AS today) t\n )\n SELECT days.day::timestamp AT TIME ZONE 'UTC' AS \"at!\",\n count(d.ip)::int AS \"servers!\",\n coalesce(round(sum(d.players_sum::float8 / d.heartbeats)), 0)::int AS \"players!\",\n coalesce(pk.players, 0) AS \"peak_players!\"\n FROM days\n LEFT JOIN server_activity_daily d ON d.day = days.day\n AND EXISTS (SELECT 1 FROM server_listings l WHERE l.ip = d.ip AND l.port = d.port)\n LEFT JOIN server_activity_peaks pk ON pk.day = days.day\n GROUP BY days.day, pk.players\n ORDER BY days.day\n ", "describe": { "columns": [ { @@ -20,6 +20,12 @@ "name": "players!", "type_info": "Int4", "origin": "Expression" + }, + { + "ordinal": 3, + "name": "peak_players!", + "type_info": "Int4", + "origin": "Expression" } ], "parameters": { @@ -28,10 +34,11 @@ ] }, "nullable": [ + null, null, null, null ] }, - "hash": "acb722e75d97bd75a675a6e0153070345c0ef081f30d9e5efd1307f4b74008b4" + "hash": "fd5b8736cae460ef4bcd20932b9e4543feced1f02ad9e893892c36eed2e9a69d" } diff --git a/api/.sqlx/query-fe47d13f296be60170a39f1f7bfd568993ed49e0f088a4e391206d71d84e8e4e.json b/api/.sqlx/query-fe47d13f296be60170a39f1f7bfd568993ed49e0f088a4e391206d71d84e8e4e.json new file mode 100644 index 0000000..7d1d2fc --- /dev/null +++ b/api/.sqlx/query-fe47d13f296be60170a39f1f7bfd568993ed49e0f088a4e391206d71d84e8e4e.json @@ -0,0 +1,91 @@ +{ + "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 CASE WHEN NOT shown.legacy THEN (\n SELECT min(best.time) FROM personal_bests best\n WHERE best.map_id = shown.map_id AND best.style = $2 AND best.track = shown.track)\n END AS world_record_time,\n shown.legacy AS \"legacy!\", shown.id AS \"id!\", shown.finished_at, tier.tier AS \"tier?\"\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 -- A legacy row's map_id is NULL, so it finds no tier.\n LEFT JOIN LATERAL (SELECT tier FROM map_tiers WHERE map_id = shown.map_id AND track = shown.track\n ORDER BY assigned_at DESC LIMIT 1) tier ON $8\n ORDER BY shown.map, shown.track\n ", + "describe": { + "columns": [ + { + "ordinal": 0, + "name": "map!", + "type_info": "Text", + "origin": "Expression" + }, + { + "ordinal": 1, + "name": "track!", + "type_info": "Int2", + "origin": "Expression" + }, + { + "ordinal": 2, + "name": "time!", + "type_info": "Float8", + "origin": "Expression" + }, + { + "ordinal": 3, + "name": "rank", + "type_info": "Int8", + "origin": "Expression" + }, + { + "ordinal": 4, + "name": "world_record_time", + "type_info": "Float8", + "origin": "Expression" + }, + { + "ordinal": 5, + "name": "legacy!", + "type_info": "Bool", + "origin": "Expression" + }, + { + "ordinal": 6, + "name": "id!", + "type_info": "Int8", + "origin": "Expression" + }, + { + "ordinal": 7, + "name": "finished_at", + "type_info": "Timestamptz", + "origin": "Expression" + }, + { + "ordinal": 8, + "name": "tier?", + "type_info": "Int2", + "origin": { + "Table": { + "table": "map_tiers", + "name": "tier" + } + } + } + ], + "parameters": { + "Left": [ + "Int8", + "Text", + "Bool", + "Int2", + "Text", + "Int2", + "Int8", + "Bool" + ] + }, + "nullable": [ + null, + null, + null, + null, + null, + null, + null, + null, + true + ] + }, + "hash": "fe47d13f296be60170a39f1f7bfd568993ed49e0f088a4e391206d71d84e8e4e" +} diff --git a/api/contract/runs.json b/api/contract/runs.json index 8e5b7e3..1253420 100644 --- a/api/contract/runs.json +++ b/api/contract/runs.json @@ -6,7 +6,8 @@ "server_run_id": "9b2f6a1e-4c1d-4e7a-9a51-3f0f6f1c2d10", "player": { "steamid64": "76561197960287930", - "name": "gabe" + "name": "gabe", + "country_code": "SE" }, "map": "bhop_badges", "style": "normal", diff --git a/api/crates/api/migrations/0052_players_country_from_server.sql b/api/crates/api/migrations/0052_players_country_from_server.sql new file mode 100644 index 0000000..9faf57b --- /dev/null +++ b/api/crates/api/migrations/0052_players_country_from_server.sql @@ -0,0 +1,6 @@ +-- Whether `players.country_code` came from a game server: its GeoIP lookup of the player's address, sent with a +-- run. A player's country has one source at a time, so this and `country_chosen` are never both true. Which +-- source wins is in `docs/site-api.md`. +ALTER TABLE players + ADD COLUMN country_from_server boolean NOT NULL DEFAULT false, + ADD CONSTRAINT players_one_country_source CHECK (NOT (country_chosen AND country_from_server)); diff --git a/api/crates/api/migrations/0053_server_activity_peaks.sql b/api/crates/api/migrations/0053_server_activity_peaks.sql new file mode 100644 index 0000000..44ee529 --- /dev/null +++ b/api/crates/api/migrations/0053_server_activity_peaks.sql @@ -0,0 +1,29 @@ +-- The most players online at once across listed servers per UTC day, for the activity chart's year view. +-- Per-server daily averages can't give a cross-server "at once" peak, so heartbeats keep this (see +-- `servers/heartbeat.rs`), and it's kept for 400 days like `server_activity_daily`. +CREATE TABLE server_activity_peaks ( + day date PRIMARY KEY, + players integer NOT NULL CHECK (players >= 0) +); + +-- Back-filled from the 30-day log by the rule `servers/activity.rs` uses up to a month: every 5 minutes, each +-- listed server's latest heartbeat within the last 15 minutes, summed; the day's highest sum. +INSERT INTO server_activity_peaks (day, players) +SELECT (online.at AT TIME ZONE 'UTC')::date, max(online.players) +FROM ( + SELECT m.at, sum(latest.players) AS players + FROM generate_series( + date_bin(interval '5 minutes', (SELECT min(received_at) FROM server_heartbeat_log), TIMESTAMPTZ '2000-01-01'), + now(), + interval '5 minutes' + ) AS m(at) + CROSS JOIN LATERAL ( + SELECT DISTINCT ON (h.ip, h.port) h.players + FROM server_heartbeat_log h + JOIN server_listings l ON l.ip = h.ip AND l.port = h.port + WHERE h.received_at > m.at - interval '15 minutes' AND h.received_at <= m.at + ORDER BY h.ip, h.port, h.received_at DESC + ) latest + GROUP BY m.at +) online +GROUP BY 1; diff --git a/api/crates/api/src/legacy.rs b/api/crates/api/src/legacy.rs index a37842f..4eca276 100644 --- a/api/crates/api/src/legacy.rs +++ b/api/crates/api/src/legacy.rs @@ -176,8 +176,10 @@ mod tests { use sqlx::PgPool; use crate::{ + maps::MapName, + policies::{Policy, TiersPolicy}, runs::tests::{run, setup}, - test_client, + staff, test_client, }; const GABE: &str = "76561197960287930"; @@ -362,6 +364,19 @@ mod tests { 0 ); + staff::policies::publish_policy( + &db, + None, + None, + &Policy::Tiers(TiersPolicy { count: 5 }), + None, + ) + .await + .unwrap(); + let badges = MapName::try_from("bhop_badges".to_owned()).unwrap(); + staff::maps::set_tier(&db, None, &badges, 0, 3) + .await + .unwrap(); let pbs = get( &db, &format!("/v1/players/{ALICE}/personal-bests?style=normal&view=legacy"), @@ -374,6 +389,14 @@ mod tests { .map(|pb| (pb["map"].as_str().unwrap(), pb["time"].as_f64().unwrap())) .collect(); assert_eq!(maps, [("bhop_badges", 25.0), ("bhop_oldonly", 40.0)]); + // Legacy runs have no tier or record time, even on a map whose board has both. + assert!( + pbs["personal_bests"] + .as_array() + .unwrap() + .iter() + .all(|pb| pb["tier"].is_null() && pb["world_record_time"].is_null()) + ); let gabe = get( &db, @@ -386,6 +409,8 @@ mod tests { (Some(30.0), Some(false)) ); assert_eq!(pb["rank"], 1); + assert_eq!(pb["tier"], 3); + assert_eq!(pb["world_record_time"], 30.0); let found = get(&db, "/v1/players?q=alic").await; assert_eq!(found["players"][0]["steamid64"], ALICE); diff --git a/api/crates/api/src/players.rs b/api/crates/api/src/players.rs index 90af67c..fc1f7be 100644 --- a/api/crates/api/src/players.rs +++ b/api/crates/api/src/players.rs @@ -162,6 +162,31 @@ pub async fn record_name( Ok(()) } +/// Makes `code`, found by a game server from the player's address, their country, replacing one they picked or +/// Steam's (the order is in `docs/site-api.md`). Only from their latest run, as for names: `record_name` has +/// already moved `name_seen_at` to the newest time a server saw them, so a run sent late from a queue can't roll +/// the country back. +pub async fn record_server_country( + conn: &mut PgConnection, + steamid64: SteamId64, + code: &str, + finished_at: DateTime, +) -> Result<(), sqlx::Error> { + sqlx::query!( + r#" + UPDATE players SET country_code = $2, country_from_server = true, country_chosen = false + WHERE steamid64 = $1 AND name_seen_at <= $3 + AND (country_code IS DISTINCT FROM $2 OR NOT country_from_server) + "#, + steamid64.get(), + code, + finished_at + ) + .execute(conn) + .await?; + Ok(()) +} + #[cfg(test)] mod tests { use super::SteamId64; diff --git a/api/crates/api/src/players/details.rs b/api/crates/api/src/players/details.rs index 56deff8..a793d37 100644 --- a/api/crates/api/src/players/details.rs +++ b/api/crates/api/src/players/details.rs @@ -1,5 +1,5 @@ //! What players say about themselves on their profile, `PUT /v1/players/{player}/details`: their setup (mouse, -//! DPI, sensitivity, mouse pad, keyboard, monitor, aspect ratio) and a country to show instead of Steam's. +//! DPI, sensitivity, mouse pad, keyboard, monitor, aspect ratio) and a country to show (order: `docs/site-api.md`). //! //! Anyone can type anything here, so every field is checked once, at this boundary: short single lines of //! text with no links or invisible characters, numbers in a sensible range, a ratio from a fixed list and a @@ -216,7 +216,8 @@ pub struct DetailsInput { pub monitor: Option, #[serde(default)] pub aspect: Option, - /// An ISO 3166-1 alpha-2 code to show instead of the Steam profile's country; null to show Steam's. + /// An ISO 3166-1 alpha-2 code to show instead of Steam's or a game server's, until a run's server finds one; + /// null to stop showing the player's pick. #[serde(default)] pub country_code: Option, /// Required when staff change someone else's details: why, for the staff log. @@ -346,12 +347,13 @@ async fn store( ) .execute(&mut *tx) .await?; - // Going back to Steam's country clears ours and marks the Steam profile stale, so the next view of the - // profile fetches it again. + // A pick replaces a game server's country. Dropping a pick clears it and marks the Steam profile stale, so + // the next view of the profile fetches Steam's again; dropping a pick the player doesn't have changes nothing. sqlx::query!( r#" UPDATE players SET country_chosen = $2::text IS NOT NULL, + country_from_server = country_from_server AND $2::text IS NULL, country_code = CASE WHEN $2::text IS NOT NULL THEN $2 WHEN country_chosen THEN NULL ELSE country_code END, steam_profile_fetched_at = CASE WHEN $2::text IS NULL AND country_chosen THEN NULL @@ -539,6 +541,37 @@ mod tests { ); } + #[sqlx::test] + async fn a_pick_replaces_a_servers_country_and_saving_without_one_keeps_it(db: PgPool) { + let token = player(&db, GABE, &[]).await; + sqlx::query("UPDATE players SET country_code = 'SE', country_from_server = true") + .execute(&db) + .await + .unwrap(); + let country = || async { + sqlx::query_as::<_, (Option, bool, bool)>( + "SELECT country_code, country_from_server, country_chosen FROM players", + ) + .fetch_one(&db) + .await + .unwrap() + }; + + assert_eq!( + save(&db, &token, GABE, json!({ "dpi": 800 })).await.status, + 204 + ); + assert_eq!(country().await, (Some("SE".into()), true, false)); + + sqlx::query("UPDATE player_details SET updated_at = now() - interval '1 minute'") + .execute(&db) + .await + .unwrap(); + let body = json!({ "country_code": "fi" }); + assert_eq!(save(&db, &token, GABE, body).await.status, 204); + assert_eq!(country().await, (Some("FI".into()), false, true)); + } + #[sqlx::test] async fn bad_values_are_refused(db: PgPool) { let token = player(&db, GABE, &[]).await; diff --git a/api/crates/api/src/players/personal_bests.rs b/api/crates/api/src/players/personal_bests.rs index 5fc38aa..8238094 100644 --- a/api/crates/api/src/players/personal_bests.rs +++ b/api/crates/api/src/players/personal_bests.rs @@ -16,6 +16,7 @@ use crate::{ app::AppState, error::{ApiError, ErrorBody}, legacy::{self, View}, + maps::tiers, }; pub const DEFAULT_LIMIT: i64 = 50; @@ -66,8 +67,14 @@ pub struct PersonalBest { pub map: String, /// 0 is the main track, 1 and up are bonuses. pub track: i16, + /// The board's tier. None while tiers aren't in force or staff haven't set this board's, and for a legacy + /// run. + pub tier: Option, /// Seconds. pub time: f64, + /// The board's 2.0 record time, as on its map leaderboard, so a client can show the gap to it. None for a + /// legacy run. + pub world_record_time: Option, /// Placement on the map's 2.0 leaderboard for this style and track. Equal times share a placement. None /// for a legacy run, which has no 2.0 placement. pub rank: Option, @@ -99,13 +106,14 @@ pub(super) struct Selection<'a> { } /// The player's personal bests with placements, as `selection` asks; `after` continues after a position, and -/// `limit` caps the count (None for all of them). +/// `limit` caps the count (None for all of them). `tiers_in_force` is whether a tier count is published. pub(super) async fn personal_bests( db: &PgPool, steamid64: SteamId64, selection: Selection<'_>, after: Option<&Position>, limit: Option, + tiers_in_force: bool, ) -> Result, sqlx::Error> { let (after_map, after_time) = match after { Some(Position::ByMap(map, track)) => (Some((map.as_str(), *track)), None), @@ -114,14 +122,18 @@ pub(super) async fn personal_bests( }; // The player's own rows are picked and paged first; each one's placement is then one more than the // personal bests faster than it on its board, an index range on `personal_bests_board`. Ranking whole - // boards and keeping the player's rows read every board they're on. + // boards and keeping the player's rows read every board they're on. Record times and tiers too are + // looked up for the page's rows alone. let rows = sqlx::query!( r#" SELECT own.style AS "style!", own.map AS "map!", own.track AS "track!", own.time AS "time!", 1 + (SELECT count(*) FROM personal_bests faster WHERE faster.map_id = own.map_id AND faster.style = own.style AND faster.track = own.track AND faster.time < own.time) AS "rank!", - own.run_id AS "run_id!", own.finished_at AS "finished_at!" + (SELECT min(best.time) FROM personal_bests best + WHERE best.map_id = own.map_id AND best.style = own.style + AND best.track = own.track) AS "world_record_time!", + own.run_id AS "run_id!", own.finished_at AS "finished_at!", tier.tier AS "tier?" FROM ( SELECT pb.style, pb.map_id, m.name AS map, pb.track, pb.time, pb.run_id, pb.finished_at FROM personal_bests pb @@ -139,6 +151,8 @@ pub(super) async fn personal_bests( pb.style, m.name, pb.track LIMIT $5 ) own + LEFT JOIN LATERAL (SELECT tier FROM map_tiers WHERE map_id = own.map_id AND track = own.track + ORDER BY assigned_at DESC LIMIT 1) tier ON $11 ORDER BY CASE WHEN $8 THEN own.finished_at END DESC, CASE WHEN $8 THEN own.run_id END DESC, own.style, own.map, own.track "#, @@ -151,7 +165,8 @@ pub(super) async fn personal_bests( selection.records_only, selection.recent_first, after_time.map(|(finished_at, _)| finished_at), - after_time.map(|(_, run_id)| run_id) + after_time.map(|(_, run_id)| run_id), + tiers_in_force ) .fetch_all(db) .await?; @@ -162,7 +177,9 @@ pub(super) async fn personal_bests( style: row.style, map: row.map, track: row.track, + tier: row.tier, time: row.time, + world_record_time: Some(row.world_record_time), rank: Some(row.rank), legacy: false, run_id: row.run_id, @@ -174,13 +191,14 @@ pub(super) async fn personal_bests( /// The player's main-track personal bests in the legacy board's style for the "2.0 + pre-2.0" or "Pre-2.0" /// view, sorted by map. With both, each map shows the faster of their 2.0 and legacy runs (the 2.0 one on /// equal times). A player banned from records shows no legacy runs, as on the -/// old site. +/// old site. Only the 2.0 runs can have a tier and a record time. async fn view_personal_bests( db: &PgPool, steamid64: SteamId64, view: View, after: Option<(&str, i16)>, limit: i64, + tiers_in_force: bool, ) -> Result, sqlx::Error> { let rows = sqlx::query!( r#" @@ -189,7 +207,11 @@ async fn view_personal_bests( SELECT count(*) FROM personal_bests faster WHERE faster.map_id = shown.map_id AND faster.style = $2 AND faster.track = shown.track AND faster.time < shown.time) END AS rank, - shown.legacy AS "legacy!", shown.id AS "id!", shown.finished_at + CASE WHEN NOT shown.legacy THEN ( + SELECT min(best.time) FROM personal_bests best + WHERE best.map_id = shown.map_id AND best.style = $2 AND best.track = shown.track) + END AS world_record_time, + shown.legacy AS "legacy!", shown.id AS "id!", shown.finished_at, tier.tier AS "tier?" FROM ( SELECT DISTINCT ON (both_kinds.map, both_kinds.track) both_kinds.* FROM ( @@ -208,6 +230,9 @@ async fn view_personal_bests( ORDER BY both_kinds.map, both_kinds.track, both_kinds.time, both_kinds.legacy LIMIT $7 ) shown + -- A legacy row's map_id is NULL, so it finds no tier. + LEFT JOIN LATERAL (SELECT tier FROM map_tiers WHERE map_id = shown.map_id AND track = shown.track + ORDER BY assigned_at DESC LIMIT 1) tier ON $8 ORDER BY shown.map, shown.track "#, steamid64.get(), @@ -216,7 +241,8 @@ async fn view_personal_bests( legacy::TRACK, after.map(|(map, _)| map), after.map(|(_, track)| track), - limit + limit, + tiers_in_force ) .fetch_all(db) .await?; @@ -227,7 +253,9 @@ async fn view_personal_bests( style: legacy::STYLE.to_owned(), map: row.map, track: row.track, + tier: row.tier, time: row.time, + world_record_time: row.world_record_time, rank: row.rank, legacy: row.legacy, run_id: row.id, @@ -333,6 +361,7 @@ pub async fn player_personal_bests( return Err(ApiError::NotFound("SourceJump has no such player")); } + let tiers_in_force = tiers::tier_count(&state.db).await?.is_some(); let rows = if view == View::Current { let selection = Selection { style: query.style.as_deref(), @@ -346,6 +375,7 @@ pub async fn player_personal_bests( selection, after.as_ref(), Some(limit + 1), + tiers_in_force, ) .await? } else { @@ -358,7 +388,7 @@ pub async fn player_personal_bests( } None => None, }; - view_personal_bests(&state.db, steamid64, view, after, limit + 1).await? + view_personal_bests(&state.db, steamid64, view, after, limit + 1, tiers_in_force).await? }; let (personal_bests, next_cursor) = split_page( rows, diff --git a/api/crates/api/src/players/profile.rs b/api/crates/api/src/players/profile.rs index 3dd23fa..ea97b13 100644 --- a/api/crates/api/src/players/profile.rs +++ b/api/crates/api/src/players/profile.rs @@ -20,6 +20,7 @@ use crate::{ app::AppState, error::{ApiError, ErrorBody}, leaderboards::standings, + maps::tiers, }; #[derive(Debug, Serialize, Deserialize, ToSchema)] @@ -29,9 +30,10 @@ pub struct PlayerProfile { /// The name from their latest run, or from Steam if they haven't played yet. pub name: String, pub avatar_url: Option, - /// ISO 3166-1 alpha-2: the one the player picked, or else their Steam profile's. None if neither has one. + /// ISO 3166-1 alpha-2: the latest one a game server found for them, the player's pick if they made it + /// since, or else their Steam profile's. None if none has one. pub country_code: Option, - /// Whether the player picked `country_code` themselves, rather than it coming from Steam. + /// Whether the player picked `country_code` themselves, rather than it coming from a game server or Steam. pub country_chosen: bool, /// Their setup, as they describe it. None when they've said nothing. pub details: Option, @@ -52,6 +54,9 @@ pub struct PlayerProfile { /// Maps they have a legacy run on, none for a player banned from records. When there are any, the /// normal style's personal bests have the "2.0 + pre-2.0" and "Pre-2.0" views. pub legacy_maps: i64, + /// Number of tiers in force; None while the community hasn't set tiers, and personal bests then have no + /// tier. + pub tier_count: Option, } #[derive(Debug, Default, Serialize, Deserialize, ToSchema)] @@ -133,6 +138,7 @@ pub async fn player_profile( let map_count = crate::maps::played_count(&state.db).await?; let legacy_maps = legacy.as_ref().map_or(0, |legacy| legacy.maps); let shown = details::shown(&mut *state.db.acquire().await?, steamid64).await?; + let tier_count = tiers::tier_count(&state.db).await?; let Some(player) = player else { // Known only from the old site: its name, avatar and country as it last had them. @@ -154,6 +160,7 @@ pub async fn player_profile( .await? .draft, legacy_maps, + tier_count, })); }; @@ -165,7 +172,7 @@ pub async fn player_profile( ); let all_styles = totals(&state.db, steamid64).await?; - let mut styles = styles_of(&state.db, steamid64).await?; + let mut styles = styles_of(&state.db, steamid64, tier_count.is_some()).await?; for standing in standings_of(&state.db, steamid64).await? { if let Some(style) = styles.iter_mut().find(|s| s.style == standing.style) { let place = Some(StyleStanding { @@ -198,6 +205,7 @@ pub async fn player_profile( all_styles, points_draft, legacy_maps, + tier_count, })) } @@ -251,6 +259,7 @@ async fn totals(db: &sqlx::PgPool, steamid64: SteamId64) -> Result Result, sqlx::Error> { let counts = sqlx::query!( r#" @@ -275,9 +284,15 @@ async fn styles_of( tracks: Some(Tracks::Main), ..Selection::default() }; - let first_page = - personal_bests::personal_bests(db, steamid64, selection, None, Some(DEFAULT_LIMIT + 1)) - .await?; + let first_page = personal_bests::personal_bests( + db, + steamid64, + selection, + None, + Some(DEFAULT_LIMIT + 1), + tiers_in_force, + ) + .await?; let (personal_bests, next_cursor) = personal_bests::split_page(first_page, page_size, false); styles.push(PlayerStyle { @@ -416,6 +431,8 @@ mod tests { .await .json(); assert_eq!(main_standing(&profile), None, "no points without tiers"); + assert_eq!(profile.tier_count, None); + assert_eq!(profile.styles[0].personal_bests[0].tier, None); staff::policies::publish_policy( &db, @@ -434,6 +451,13 @@ mod tests { .await .json(); assert_eq!(main_standing(&profile), Some(rank), "{player}"); + assert_eq!(profile.tier_count, Some(5)); + assert_eq!(profile.styles[0].personal_bests[0].tier, Some(1)); + // The record holder's own time, the faster time for the player behind. + assert_eq!( + profile.styles[0].personal_bests[0].world_record_time, + Some(20.0) + ); assert!(profile.styles[0].bonus_standing.is_none()); assert!(profile.points_draft); } diff --git a/api/crates/api/src/players/steam_profile.rs b/api/crates/api/src/players/steam_profile.rs index e4a1b35..4079863 100644 --- a/api/crates/api/src/players/steam_profile.rs +++ b/api/crates/api/src/players/steam_profile.rs @@ -132,8 +132,8 @@ pub async fn refresh(db: &PgPool, steam: &Steam, steamid64: SteamId64) -> Result r#" UPDATE players SET avatar_url = $2, - -- A country the player picked on their profile stays. - country_code = CASE WHEN country_chosen THEN country_code ELSE $3 END, + -- Steam's country comes last: one a game server found or the player picked stays. + country_code = CASE WHEN country_chosen OR country_from_server THEN country_code ELSE $3 END, steam_profile_fetched_at = now(), name = CASE WHEN name_seen_at = '-infinity' AND $4::text IS NOT NULL THEN $4 ELSE name END WHERE steamid64 = $1 @@ -220,6 +220,51 @@ mod tests { assert!(queue(&steam, first), "a finished refresh frees its place"); } + /// A stand-in for Steam that answers every profile with `PUBLIC`. + async fn fake_steam() -> Steam { + let app = + axum::Router::new().route("/profiles/{id}/", axum::routing::get(|| async { PUBLIC })); + let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap(); + let address = listener.local_addr().unwrap(); + tokio::spawn(async move { axum::serve(listener, app).await.unwrap() }); + Steam::with_base(&url::Url::parse(&format!("http://{address}/")).unwrap()) + } + + #[sqlx::test] + async fn steams_country_comes_after_a_servers_and_the_players_own(db: PgPool) { + // Steam's own (none yet), a game server's, and the player's pick. + let players = [ + ("[U:1:1]", None, false, false, Some("NO")), + ("[U:1:2]", Some("SE"), true, false, Some("SE")), + ("[U:1:3]", Some("FI"), false, true, Some("FI")), + ]; + for (id, code, from_server, chosen, expected) in players { + let steamid64 = SteamId64::parse_any(id).unwrap(); + sqlx::query( + "INSERT INTO players (steamid64, name, name_seen_at, country_code, country_from_server, country_chosen) + VALUES ($1, 'nova', now(), $2, $3, $4)", + ) + .bind(steamid64.get()) + .bind(code) + .bind(from_server) + .bind(chosen) + .execute(&db) + .await + .unwrap(); + // A Steam each, so no refresh waits its turn behind another. + refresh(&db, &fake_steam().await, steamid64).await.unwrap(); + + let (country, avatar): (Option, Option) = + sqlx::query_as("SELECT country_code, avatar_url FROM players WHERE steamid64 = $1") + .bind(steamid64.get()) + .fetch_one(&db) + .await + .unwrap(); + assert!(avatar.is_some(), "{id}: the profile was stored"); + assert_eq!(country.as_deref(), expected, "{id}"); + } + } + #[sqlx::test] async fn a_failed_fetch_waits_before_trying_again(db: PgPool) { let steamid64 = SteamId64::parse_any("76561197960287930").unwrap(); diff --git a/api/crates/api/src/runs.rs b/api/crates/api/src/runs.rs index dc2460e..93103f3 100644 --- a/api/crates/api/src/runs.rs +++ b/api/crates/api/src/runs.rs @@ -78,6 +78,13 @@ pub struct RunPlayer { #[schema(value_type = String, example = "76561197960287930")] pub steamid64: SteamId64, pub name: String, + /// The player's country, as the game server found it from their address with SourceMod's `GeoipCode2`: an ISO + /// 3166-1 alpha-2 code. Leave it out when the lookup finds nothing; send only the code, never the address. It + /// becomes the player's flag. A code SourceJump doesn't know as a country is ignored and the run is stored + /// anyway. Runs only: detections ignore it. + #[serde(default, skip_serializing_if = "Option::is_none")] + #[schema(example = "SE")] + pub country_code: Option, } /// SourceJump's answer to a run. A resend gets the same answer, with a fresh upload link if one applies. @@ -130,6 +137,24 @@ async fn submit_run( let stored = store(&mut tx, key, &run).await?; tx.commit().await?; + // A player is otherwise fetched from Steam only when they sign in or their profile is opened, so one who just + // plays would show initials everywhere they're listed. Their runs keep the copy fresh instead; the queue's + // spacing and cap keep this to Steam's pace however busy the servers are. + if stored.created { + let fetched_at = sqlx::query_scalar!( + "SELECT steam_profile_fetched_at FROM players WHERE steamid64 = $1", + run.player.steamid64.get() + ) + .fetch_one(&state.db) + .await?; + players::steam_profile::refresh_if_stale( + state.db.clone(), + state.steam.clone(), + run.player.steamid64, + fetched_at, + ); + } + let status = if stored.created { StatusCode::CREATED } else { @@ -206,6 +231,15 @@ async fn store( let finished_at = DateTime::from_timestamp(run.finished_at, 0).expect("checked before storing"); players::record_name(conn, run.player.steamid64, &run.player.name, finished_at).await?; + // An unknown code costs the player a flag, never their run. + if let Some(code) = run + .player + .country_code + .as_deref() + .and_then(players::countries::known_code) + { + players::record_server_country(conn, run.player.steamid64, code, finished_at).await?; + } let ban = restrictions::records_ban(conn, run.player.steamid64).await?; let map_id = maps::id_for(conn, &run.map).await?; @@ -592,6 +626,82 @@ pub(crate) mod tests { assert_eq!(submit(&db, &key, &any).await.status, 201); } + /// The run player's `country_code`, `country_from_server` and `country_chosen`. + async fn country(db: &PgPool) -> (Option, bool, bool) { + sqlx::query_as( + "SELECT country_code, country_from_server, country_chosen FROM players WHERE steamid64 = 76561197960287930", + ) + .fetch_one(db) + .await + .unwrap() + } + + fn with_country(server_run_id: &str, code: &str) -> Value { + let mut body = run(server_run_id, 30.0); + body["player"]["country_code"] = json!(code); + body + } + + #[sqlx::test] + async fn the_country_a_server_found_replaces_a_pick_and_steams(db: PgPool) { + let key = setup(&db).await; + + assert_eq!( + submit(&db, &key, &with_country("a", "se")).await.status, + 201 + ); + assert_eq!(country(&db).await, (Some("SE".into()), true, false)); + + // The player picks their own; a run without a country leaves it, the next one with a country replaces it. + sqlx::query("UPDATE players SET country_code = 'NO', country_chosen = true, country_from_server = false") + .execute(&db) + .await + .unwrap(); + assert_eq!(submit(&db, &key, &run("b", 31.0)).await.status, 201); + assert_eq!(country(&db).await, (Some("NO".into()), false, true)); + assert_eq!( + submit(&db, &key, &with_country("c", "SE")).await.status, + 201 + ); + assert_eq!(country(&db).await, (Some("SE".into()), true, false)); + + // A resend changes nothing, whatever country it carries. + assert_eq!( + submit(&db, &key, &with_country("a", "fi")).await.status, + 200 + ); + assert_eq!(country(&db).await, (Some("SE".into()), true, false)); + } + + #[sqlx::test] + async fn a_country_sourcejump_doesnt_know_is_ignored_and_the_run_stored(db: PgPool) { + let key = setup(&db).await; + + for (id, code) in [("a", "XX"), ("b", "A1"), ("c", "EU"), ("d", "Sweden")] { + assert_eq!( + submit(&db, &key, &with_country(id, code)).await.status, + 201, + "{code}" + ); + } + + assert_eq!(count(&db, "runs").await, 4); + assert_eq!(country(&db).await, (None, false, false)); + } + + #[sqlx::test] + async fn a_run_sent_late_cant_roll_the_country_back(db: PgPool) { + let key = setup(&db).await; + let mut newer = with_country("a", "SE"); + newer["finished_at"] = json!(1_792_400_100); + + submit(&db, &key, &newer).await; + submit(&db, &key, &with_country("b", "FI")).await; + + assert_eq!(count(&db, "runs").await, 2); + assert_eq!(country(&db).await, (Some("SE".into()), true, false)); + } + #[sqlx::test] async fn runs_need_a_valid_key(db: PgPool) { setup(&db).await; diff --git a/api/crates/api/src/runs/recent.rs b/api/crates/api/src/runs/recent.rs index 00d0416..9187d88 100644 --- a/api/crates/api/src/runs/recent.rs +++ b/api/crates/api/src/runs/recent.rs @@ -33,6 +33,8 @@ pub struct RecentRunsQuery { records: Option, /// 1 to 100. Default 50. limit: Option, + /// Runs to skip, for going straight to a page. Default 0. Counted after `cursor` when both are given. + offset: Option, /// Continue after this point: a `next_cursor` from an earlier page with the same filters. cursor: Option, } @@ -52,6 +54,8 @@ pub struct RecentRuns { pub runs: Vec, /// Pass as `cursor` for the runs before these. None at the end. pub next_cursor: Option, + /// How many runs match the filters, across all pages. + pub total: i64, /// Number of tiers in force; None while the community hasn't set tiers, and runs then have no tier. pub tier_count: Option, } @@ -88,7 +92,7 @@ pub struct RecentRun { params(RecentRunsQuery), responses( (status = OK, body = RecentRuns), - (status = UNPROCESSABLE_ENTITY, description = "Not a valid tier, limit or cursor", body = ErrorBody), + (status = UNPROCESSABLE_ENTITY, description = "Not a valid tier, limit, offset or cursor", body = ErrorBody), ), )] pub async fn recent_runs( @@ -96,7 +100,8 @@ pub async fn recent_runs( Query(query): Query, ) -> Result, ApiError> { let limit = query.limit.unwrap_or(DEFAULT_LIMIT); - check_page(limit, 0, MAX_LIMIT).map_err(ApiError::Invalid)?; + let offset = query.offset.unwrap_or(0); + check_page(limit, offset, MAX_LIMIT).map_err(ApiError::Invalid)?; if query.min_tier.is_some_and(|tier| tier < 1) { return Err(ApiError::Invalid("min_tier must be 1 or more".to_owned())); } @@ -110,11 +115,16 @@ pub async fn recent_runs( return Ok(Json(RecentRuns { runs: Vec::new(), next_cursor: None, + total: 0, tier_count, })); } - let mut runs = page(&state.db, &query, after, limit + 1, tier_count.is_some()).await?; + let tiers_in_force = tier_count.is_some(); + let (mut runs, total) = tokio::try_join!( + page(&state.db, &query, after, limit + 1, offset, tiers_in_force), + count(&state.db, &query, tiers_in_force), + )?; let limit = usize::try_from(limit).expect("checked to be positive"); let next_cursor = (runs.len() > limit).then(|| { runs.truncate(limit); @@ -123,6 +133,7 @@ pub async fn recent_runs( Ok(Json(RecentRuns { runs, next_cursor: next_cursor.flatten(), + total, tier_count, })) } @@ -140,17 +151,50 @@ async fn position(db: &PgPool, cursor: &str) -> Result { Ok((finished_at, run_id)) } +fn track_key(query: &RecentRunsQuery) -> Option<&'static str> { + query.track.map(|track| match track { + TrackKind::Main => "main", + TrackKind::Bonus => "bonus", + }) +} + +/// Every run the filters match, ignoring `cursor`, `offset` and `limit`. The same conditions as `page`'s. +async fn count( + db: &PgPool, + query: &RecentRunsQuery, + tiers_in_force: bool, +) -> Result { + sqlx::query_scalar!( + r#" + SELECT count(*) AS "total!" + 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 $5 + WHERE r.invalidated_at IS NULL + AND (NOT $4 OR r.was_record) + AND ($1::text IS NULL OR r.style = $1) + AND ($2::text IS NULL OR ($2 = 'main') = (r.track = 0)) + AND ($3::smallint IS NULL OR tier.tier >= $3) + "#, + query.style, + track_key(query), + query.min_tier, + query.records.unwrap_or(false), + tiers_in_force, + ) + .fetch_one(db) + .await +} + async fn page( db: &PgPool, query: &RecentRunsQuery, after: Option, limit: i64, + offset: i64, tiers_in_force: bool, ) -> Result, sqlx::Error> { - let track = query.track.map(|track| match track { - TrackKind::Main => "main", - TrackKind::Bonus => "bonus", - }); + let track = track_key(query); // "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 @@ -171,7 +215,7 @@ async fn page( AND ($3::smallint IS NULL OR tier.tier >= $3) AND ($5::timestamptz IS NULL OR (r.finished_at, r.id) < ($5, $6::bigint)) ORDER BY r.finished_at DESC, r.id DESC - LIMIT $8 + LIMIT $8 OFFSET $9 ) 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?", @@ -208,7 +252,8 @@ async fn page( after.map(|(finished_at, _)| finished_at), after.map(|(_, run_id)| run_id), tiers_in_force, - limit + limit, + offset ) .fetch_all(db) .await?; @@ -361,6 +406,29 @@ mod tests { assert_eq!(test_client::get(&db, "/v1/runs?cursor=x").await.status, 422); assert_eq!(test_client::get(&db, "/v1/runs?limit=0").await.status, 422); + // An offset goes straight to a page, and the total counts the whole filtered list, not the page. + let third = list(&db, "?limit=2&offset=2").await; + assert_eq!( + third, + [ + row("ricochet", "bhop_badges", 0, 28.0, Some(1), true), + row("ricochet", "bhop_badges", 0, 33.0, Some(2), false), + ] + ); + let last: RecentRuns = test_client::get(&db, "/v1/runs?limit=2&offset=4") + .await + .json(); + assert_eq!((last.runs.len(), last.total), (1, 5)); + assert_eq!(last.next_cursor, None); + let main: RecentRuns = test_client::get(&db, "/v1/runs?track=main&limit=1") + .await + .json(); + assert_eq!(main.total, 4); + assert_eq!( + test_client::get(&db, "/v1/runs?offset=-1").await.status, + 422 + ); + // An invalidated record leaves the list, and the record it had beaten is the board's best again. let run_id: i64 = sqlx::query_scalar("SELECT id FROM runs WHERE server_run_id = 'c'") .fetch_one(&db) @@ -388,7 +456,8 @@ mod tests { ] { test_client::post(&db, "/v1/runs", Some(&key), &body).await; } - assert_eq!(list(&db, "?min_tier=1").await.len(), 0); + let none: RecentRuns = test_client::get(&db, "/v1/runs?min_tier=1").await.json(); + assert_eq!((none.runs.len(), none.total), (0, 0)); assert_eq!( test_client::get(&db, "/v1/runs?min_tier=0").await.status, 422 diff --git a/api/crates/api/src/servers/activity.rs b/api/crates/api/src/servers/activity.rs index 6095e0b..3967d4c 100644 --- a/api/crates/api/src/servers/activity.rs +++ b/api/crates/api/src/servers/activity.rs @@ -1,6 +1,6 @@ //! `GET /v1/servers/activity`: players and servers online over time, across listed servers, for the chart on //! the Servers page. Up to a month is built from `server_heartbeat_log`, which keeps 30 days; a year from -//! `server_activity_daily`. +//! `server_activity_daily` and `server_activity_peaks`. use axum::{ Json, @@ -14,6 +14,16 @@ use sqlx::PgPool; use crate::{app::AppState, error::ApiError}; +/// A listed server counts as online at a moment if it beat within this long before it: the heartbeat interval +/// plus five minutes of grace, so a late or missed beat doesn't drop it from the count. Also used where +/// heartbeats keep `server_activity_peaks`, and written out in its migration (0053). +pub const ONLINE_WINDOW_MINUTES: i32 = 15; +/// How often "online at once" is sampled for a peak up to a month. Half the heartbeat interval, so every +/// regular beat is seen at least once; month (30 days) is 8640 samples, each one short index range scan. The +/// grid is anchored like the buckets, so a shorter range's samples are a subset of a longer range's, and its +/// `peak_players` can never exceed a longer one's. +const SAMPLE_MINUTES: i32 = 5; + #[derive(Debug, Clone, Copy, Deserialize, Serialize, ToSchema)] #[serde(rename_all = "snake_case")] pub enum ActivityRange { @@ -46,6 +56,10 @@ pub struct ActivityQuery { pub struct ServerActivity { pub range: ActivityRange, pub bucket_minutes: i32, + /// The most players online at once across listed servers over the whole range, from the start of its + /// first point up to now, the bucket in progress included: no point reads higher, and a shorter range never + /// reads higher than a longer one. + pub peak_players: i32, /// Oldest first, one per bucket, empty buckets included. The bucket still in progress is left out, so the /// last point isn't a partial count. pub points: Vec, @@ -59,6 +73,10 @@ pub struct ActivityPoint { pub servers: i32, /// The average player count of each of those servers during the bucket, summed. pub players: i32, + /// The most players online at once during the bucket, across listed servers: each server's latest + /// heartbeat count, summed at the same moment. Up to a month, sampled every 5 minutes; for a year, the + /// day's highest total at any heartbeat. + pub peak_players: i32, } /// Players and servers online over the last day, week, month or year. @@ -75,30 +93,37 @@ pub async fn server_activity( ) -> Result, ApiError> { let range = query.range.unwrap_or(ActivityRange::Day); let (bucket_minutes, count) = range.buckets(); - let points = if let ActivityRange::Year = range { - by_day(&state.db, count).await? + let (points, peak_players) = if let ActivityRange::Year = range { + let (points, peak) = by_day(&state.db, count).await?; + // The daily peaks are kept at every beat and the month at 5-minute samples, so either can read higher + // (a spike between samples, or a server since unlisted); taking both keeps the year at least the month. + let (month_minutes, month_count) = ActivityRange::Month.buckets(); + let (_, month_peak) = by_bucket(&state.db, month_minutes, month_count).await?; + (points, peak.max(month_peak)) } else { by_bucket(&state.db, bucket_minutes, count).await? }; Ok(Json(ServerActivity { range, bucket_minutes, + peak_players, points, })) } -/// Buckets of `minutes` from the heartbeat log. +/// Buckets of `minutes` from the heartbeat log, and the range's peak: samples run on through the bucket in +/// progress to the latest whole sample moment, and the peak takes those from `count` buckets' length ago. async fn by_bucket( db: &PgPool, minutes: i32, count: i32, -) -> Result, sqlx::Error> { - sqlx::query_as!( - ActivityPoint, +) -> Result<(Vec, i32), sqlx::Error> { + let rows = sqlx::query!( r#" WITH bounds AS ( SELECT make_interval(mins => $1) AS bucket, - date_bin(make_interval(mins => $1), now(), TIMESTAMPTZ '2000-01-01') AS current + date_bin(make_interval(mins => $1), now(), TIMESTAMPTZ '2000-01-01') AS current, + date_bin(make_interval(mins => $3), now(), TIMESTAMPTZ '2000-01-01') AS latest ), buckets AS ( SELECT generate_series(current - $2 * bucket, current - bucket, bucket) AS at FROM bounds ), per_server AS ( @@ -109,23 +134,72 @@ async fn by_bucket( JOIN server_listings l ON l.ip = h.ip AND l.port = h.port WHERE h.received_at >= bounds.current - $2 * bounds.bucket AND h.received_at < bounds.current GROUP BY 1, 2, 3 + ), online AS ( + SELECT m.at, sum(latest.players) AS players + FROM bounds + CROSS JOIN generate_series( + bounds.current - $2 * bounds.bucket, + bounds.latest, + make_interval(mins => $3) + ) AS m(at) + CROSS JOIN LATERAL ( + SELECT DISTINCT ON (h.ip, h.port) h.players + FROM server_heartbeat_log h + JOIN server_listings l ON l.ip = h.ip AND l.port = h.port + WHERE h.received_at > m.at - make_interval(mins => $4) AND h.received_at <= m.at + ORDER BY h.ip, h.port, h.received_at DESC + ) latest + GROUP BY m.at + ), peaks AS ( + SELECT date_bin(bounds.bucket, online.at, TIMESTAMPTZ '2000-01-01') AS at, max(online.players) AS players + FROM online CROSS JOIN bounds + GROUP BY 1 + ), whole AS ( + SELECT coalesce(max(online.players), 0)::int AS players + FROM online CROSS JOIN bounds + WHERE online.at >= bounds.current - $2 * bounds.bucket ) - SELECT b.at AS "at!", count(p.ip)::int AS "servers!", coalesce(round(sum(p.players)), 0)::int AS "players!" + SELECT b.at AS "at!", count(p.ip)::int AS "servers!", coalesce(round(sum(p.players)), 0)::int AS "players!", + coalesce(pk.players, 0)::int AS "peak_players!", whole.players AS "range_peak!" FROM buckets b + CROSS JOIN whole LEFT JOIN per_server p ON p.at = b.at - GROUP BY b.at + LEFT JOIN peaks pk ON pk.at = b.at + GROUP BY b.at, pk.players, whole.players ORDER BY b.at "#, minutes, - f64::from(count) + f64::from(count), + SAMPLE_MINUTES, + ONLINE_WINDOW_MINUTES ) .fetch_all(db) - .await + .await?; + let peak = rows.first().map_or(0, |r| r.range_peak); + let points = rows + .into_iter() + .map(|r| ActivityPoint { + at: r.at, + servers: r.servers, + players: r.players, + peak_players: r.peak_players, + }) + .collect(); + Ok((points, peak)) } -/// The last `count` whole UTC days from the daily totals; today is still in progress, so it's left out. -async fn by_day(db: &PgPool, count: i32) -> Result, sqlx::Error> { - sqlx::query_as!( +/// The last `count` whole UTC days from the daily totals and peaks; today is still in progress, so it's left +/// out. And the highest daily peak from the first of those days through today, so the range's peak is never +/// below a day the chart shows, nor misses today. +async fn by_day(db: &PgPool, count: i32) -> Result<(Vec, i32), sqlx::Error> { + let peak = sqlx::query_scalar!( + r#"SELECT coalesce(max(players), 0) AS "players!" FROM server_activity_peaks + WHERE day >= (now() AT TIME ZONE 'UTC')::date - $1::int"#, + count + ) + .fetch_one(db) + .await?; + let points = sqlx::query_as!( ActivityPoint, r#" WITH days AS ( @@ -134,17 +208,20 @@ async fn by_day(db: &PgPool, count: i32) -> Result, sqlx::Err ) SELECT days.day::timestamp AT TIME ZONE 'UTC' AS "at!", count(d.ip)::int AS "servers!", - coalesce(round(sum(d.players_sum::float8 / d.heartbeats)), 0)::int AS "players!" + coalesce(round(sum(d.players_sum::float8 / d.heartbeats)), 0)::int AS "players!", + coalesce(pk.players, 0) AS "peak_players!" FROM days LEFT JOIN server_activity_daily d ON d.day = days.day AND EXISTS (SELECT 1 FROM server_listings l WHERE l.ip = d.ip AND l.port = d.port) - GROUP BY days.day + LEFT JOIN server_activity_peaks pk ON pk.day = days.day + GROUP BY days.day, pk.players ORDER BY days.day "#, count ) .fetch_all(db) - .await + .await?; + Ok((points, peak)) } #[cfg(test)] @@ -179,6 +256,10 @@ mod tests { .execute(&db) .await .unwrap(); + sqlx::query("UPDATE server_heartbeat_log SET received_at = received_at + interval '1 minute' WHERE players = 20") + .execute(&db) + .await + .unwrap(); let activity: ServerActivity = test_client::get(&db, "/v1/servers/activity").await.json(); @@ -188,10 +269,179 @@ mod tests { .points .iter() .filter(|p| p.servers > 0) - .map(|p| (p.servers, p.players)) + .map(|p| (p.servers, p.players, p.peak_players)) .collect(); - // .1 averaged (12 + 20) / 2 = 16, .2 had 12; .3 isn't listed. - assert_eq!(busy, [(2, 28)]); + // .1 averaged (12 + 20) / 2 = 16, .2 had 12; .3 isn't listed. At once: .1's latest 20, and .2's 12. + assert_eq!(busy, [(2, 28, 32)]); + } + + /// Lists the servers, then has each send a beat of `players` at `minutes` before the start of the + /// current 8-hour bin: a finished bucket for the day, week and month, and within the last day. + async fn beats_at(db: &PgPool, beats: &[(&str, i64, i64)]) { + for (ip, players, minutes) in beats { + listing::list(db, format!("{ip}:27015").parse().unwrap(), None) + .await + .unwrap(); + let mut body = report(27015); + body["players"] = serde_json::json!(players); + beat(db, ip, None, &body).await; + sqlx::query( + "UPDATE server_heartbeat_log SET received_at = date_bin('8 hours', now(), '2000-01-01') - make_interval(mins => $1::int) + WHERE received_at > now() - interval '1 minute'", + ) + .bind(minutes) + .execute(db) + .await + .unwrap(); + } + } + + async fn activity(db: &PgPool, range: &str) -> ServerActivity { + test_client::get(db, &format!("/v1/servers/activity?range={range}")) + .await + .json() + } + + fn peak(activity: &ServerActivity) -> i32 { + activity + .points + .iter() + .map(|p| p.peak_players) + .max() + .unwrap() + } + + #[sqlx::test] + async fn the_peak_is_players_online_at_the_same_moment(db: PgPool) { + // .1 has 10, then 2; .2 joins with 6 five minutes after .1's first beat, while .1 still counts. + beats_at( + &db, + &[ + ("203.0.113.1", 10, 180), + ("203.0.113.2", 6, 175), + ("203.0.113.1", 2, 170), + ], + ) + .await; + + let day = activity(&db, "day").await; + let busy: Vec<_> = day.points.iter().filter(|p| p.servers > 0).collect(); + // One 20-minute bucket: averages (10 + 2) / 2 + 6 = 12, but 10 + 6 were online at once. + assert_eq!(busy.len(), 1); + assert_eq!((busy[0].players, busy[0].peak_players), (12, 16)); + } + + #[sqlx::test] + async fn a_spike_the_average_flattens_still_peaks_and_longer_ranges_keep_it(db: PgPool) { + // One 2-hour bucket of beats every 10 minutes with 2 players, and one with 30 between them. + let quiet: Vec<_> = (0..11).map(|i| ("203.0.113.1", 2, 119 - i * 10)).collect(); + beats_at(&db, &quiet).await; + beats_at(&db, &[("203.0.113.1", 30, 64)]).await; + + let (day, week, month) = ( + activity(&db, "day").await, + activity(&db, "week").await, + activity(&db, "month").await, + ); + assert_eq!(peak(&day), 30); + // The 2-hour bucket's average is (11 × 2 + 30) / 12 = 4, but the spike is its peak. + let bucket = week.points.iter().find(|p| p.servers > 0).unwrap(); + assert_eq!((bucket.players, bucket.peak_players), (4, 30)); + // Longer ranges sample the same moments and more, so they never peak lower. + assert!(peak(&week) >= peak(&day) && peak(&month) >= peak(&day)); + } + + /// Has a listed server beat with `players` at the latest 5-minute moment: within every range's bucket in + /// progress, and its last sample. + async fn beat_now(db: &PgPool, ip: &str, players: i64) { + listing::list(db, format!("{ip}:27015").parse().unwrap(), None) + .await + .unwrap(); + let mut body = report(27015); + body["players"] = serde_json::json!(players); + beat(db, ip, None, &body).await; + sqlx::query( + "UPDATE server_heartbeat_log SET received_at = date_bin('5 minutes', now(), '2000-01-01') + WHERE received_at > now() - interval '1 minute'", + ) + .execute(db) + .await + .unwrap(); + } + + #[sqlx::test] + async fn the_range_peak_includes_the_bucket_in_progress(db: PgPool) { + beats_at(&db, &[("203.0.113.1", 2, 60)]).await; + beat_now(&db, "203.0.113.1", 30).await; + + let mut left_out = false; + for range in ["day", "week", "month", "year"] { + let activity = activity(&db, range).await; + assert_eq!(activity.peak_players, 30, "{range}"); + left_out |= peak(&activity) < 30; + } + // The points leave the bucket in progress out, so they can't show the spike yet. + assert!(left_out); + } + + #[sqlx::test] + async fn a_longer_range_never_peaks_lower(db: PgPool) { + // 50 at once 20 days ago, 30 three days ago, 10 now; 70 on a day 100 days ago. + beats_at( + &db, + &[ + ("203.0.113.1", 25, 20 * 1440), + ("203.0.113.2", 25, 20 * 1440), + ("203.0.113.1", 30, 3 * 1440), + ], + ) + .await; + beat_now(&db, "203.0.113.2", 10).await; + // Beats kept today's peak as sent, all at once; clear it to test the windows alone. + sqlx::query("DELETE FROM server_activity_peaks") + .execute(&db) + .await + .unwrap(); + let peaks = || async { + let mut peaks = vec![]; + for range in ["day", "week", "month", "year"] { + peaks.push(activity(&db, range).await.peak_players); + } + peaks + }; + + // With no daily peaks, a year is still at least the month. + assert_eq!(peaks().await, [10, 30, 50, 50]); + sqlx::query( + "INSERT INTO server_activity_peaks (day, players) VALUES (current_date - 100, 70)", + ) + .execute(&db) + .await + .unwrap(); + assert_eq!(peaks().await, [10, 30, 50, 70]); + } + + #[sqlx::test] + async fn heartbeats_keep_the_days_peak(db: PgPool) { + listing::list(&db, "203.0.113.1:27015".parse().unwrap(), None) + .await + .unwrap(); + listing::list(&db, "203.0.113.2:27015".parse().unwrap(), None) + .await + .unwrap(); + let mut quiet = report(27015); + quiet["players"] = serde_json::json!(4); + beat(&db, "203.0.113.1", None, &report(27015)).await; + beat(&db, "203.0.113.2", None, &report(27015)).await; + beat(&db, "203.0.113.1", None, &quiet).await; + // An unlisted server doesn't count. + beat(&db, "203.0.113.3", None, &report(27015)).await; + let today: i32 = sqlx::query_scalar("SELECT players FROM server_activity_peaks") + .fetch_one(&db) + .await + .unwrap(); + // 12 + 12 at the second beat; .1 dropping to 4 doesn't lower the day's peak. + assert_eq!(today, 24); } #[sqlx::test] @@ -205,10 +455,12 @@ mod tests { beat(&db, "203.0.113.1", None, &quiet).await; beat(&db, "203.0.113.2", None, &report(27015)).await; // Today is left out of the year, so move today's totals to yesterday. - sqlx::query("UPDATE server_activity_daily SET day = day - 1") - .execute(&db) - .await - .unwrap(); + for sql in [ + "UPDATE server_activity_daily SET day = day - 1", + "UPDATE server_activity_peaks SET day = day - 1", + ] { + sqlx::query(sql).execute(&db).await.unwrap(); + } let year: ServerActivity = test_client::get(&db, "/v1/servers/activity?range=year") .await @@ -216,8 +468,15 @@ mod tests { assert_eq!((year.bucket_minutes, year.points.len()), (1440, 365)); let last = year.points.last().unwrap(); - // (12 + 4) / 2 players on average, on the one listed server. - assert_eq!((last.servers, last.players), (1, 8)); + // (12 + 4) / 2 players on average, on the one listed server; at most 12 at once. + assert_eq!((last.servers, last.players, last.peak_players), (1, 8, 12)); + assert!( + year.points + .iter() + .rev() + .skip(1) + .all(|p| p.peak_players == 0) + ); } #[sqlx::test] @@ -226,7 +485,11 @@ mod tests { .await .json(); assert_eq!((week.bucket_minutes, week.points.len()), (120, 84)); - assert!(week.points.iter().all(|p| p.servers == 0 && p.players == 0)); + assert!( + week.points + .iter() + .all(|p| p.servers == 0 && p.players == 0 && p.peak_players == 0) + ); let status = test_client::get(&db, "/v1/servers/activity?range=decade") .await .status; diff --git a/api/crates/api/src/servers/heartbeat.rs b/api/crates/api/src/servers/heartbeat.rs index 1d9cd6b..c570811 100644 --- a/api/crates/api/src/servers/heartbeat.rs +++ b/api/crates/api/src/servers/heartbeat.rs @@ -18,7 +18,10 @@ use sqlx::{PgPool, types::Json as JsonColumn}; use utoipa::ToSchema; use utoipa_axum::{router::OpenApiRouter, routes}; -use super::keys::{self, KeyLookup}; +use super::{ + activity, + keys::{self, KeyLookup}, +}; use crate::{ app::AppState, client_address::{ClientAddress, ClientCountry}, @@ -174,7 +177,7 @@ async fn heartbeat( // Only listed servers feed the activity chart, which is all it shows; it also keeps unlisted // senders, which anyone can be, from growing the log. The log serves up to a month, the daily totals // a year. - sqlx::query!( + let logged = sqlx::query!( r#" WITH listed AS ( SELECT ip, port FROM server_listings WHERE ip = $1::inet AND port = $2 @@ -192,6 +195,27 @@ async fn heartbeat( ) .execute(&state.db) .await?; + // A beat is the only time the total online can rise, so the day's peak is kept here. Its own statement: + // within the one above, the log row it just wrote wouldn't be visible yet. + if logged.rows_affected() > 0 { + sqlx::query!( + r#" + INSERT INTO server_activity_peaks AS p (day, players) + SELECT (now() AT TIME ZONE 'UTC')::date, coalesce(sum(latest.players), 0) + FROM ( + SELECT DISTINCT ON (h.ip, h.port) h.players + FROM server_heartbeat_log h + JOIN server_listings l ON l.ip = h.ip AND l.port = h.port + WHERE h.received_at > now() - make_interval(mins => $1) + ORDER BY h.ip, h.port, h.received_at DESC + ) latest + ON CONFLICT (day) DO UPDATE SET players = greatest(p.players, excluded.players) + "#, + activity::ONLINE_WINDOW_MINUTES + ) + .execute(&state.db) + .await?; + } Ok(Json(HeartbeatResponse { key, @@ -211,7 +235,7 @@ fn clean_hostname(hostname: &str) -> String { } /// Prunes every hour, for as long as the API runs. Not on each heartbeat: anyone can send those, and each -/// would cost three deletes. +/// would cost four deletes. pub async fn run(db: PgPool) { let mut ticks = tokio::time::interval(PRUNE_INTERVAL); ticks.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Delay); @@ -223,8 +247,8 @@ pub async fn run(db: PgPool) { } } -/// Forgets unlisted servers silent for a week, logged heartbeats after 30 days and daily totals after 400 -/// days. +/// Forgets unlisted servers silent for a week, logged heartbeats after 30 days and daily totals and peaks +/// after 400 days. async fn prune(db: &PgPool) -> Result<(), sqlx::Error> { sqlx::query!("DELETE FROM server_heartbeat_log WHERE received_at < now() - interval '30 days'") .execute(db) @@ -232,6 +256,9 @@ async fn prune(db: &PgPool) -> Result<(), sqlx::Error> { sqlx::query!("DELETE FROM server_activity_daily WHERE day < current_date - 400") .execute(db) .await?; + sqlx::query!("DELETE FROM server_activity_peaks WHERE day < current_date - 400") + .execute(db) + .await?; sqlx::query!( r#" DELETE FROM server_heartbeats h diff --git a/api/openapi.json b/api/openapi.json index 91d277d..679725f 100644 --- a/api/openapi.json +++ b/api/openapi.json @@ -2039,6 +2039,19 @@ "format": "int64" } }, + { + "name": "offset", + "in": "query", + "description": "Runs to skip, for going straight to a page. Default 0. Counted after `cursor` when both are given.", + "required": false, + "schema": { + "type": [ + "integer", + "null" + ], + "format": "int64" + } + }, { "name": "cursor", "in": "query", @@ -2063,7 +2076,7 @@ } }, "422": { - "description": "Not a valid tier, limit or cursor", + "description": "Not a valid tier, limit, offset or cursor", "content": { "application/json": { "schema": { @@ -5917,7 +5930,8 @@ "required": [ "at", "servers", - "players" + "players", + "peak_players" ], "properties": { "at": { @@ -5925,6 +5939,11 @@ "format": "date-time", "description": "Where the bucket starts." }, + "peak_players": { + "type": "integer", + "format": "int32", + "description": "The most players online at once during the bucket, across listed servers: each server's latest\nheartbeat count, summed at the same moment. Up to a month, sampled every 5 minutes; for a year, the\nday's highest total at any heartbeat." + }, "players": { "type": "integer", "format": "int32", @@ -6191,7 +6210,7 @@ "string", "null" ], - "description": "An ISO 3166-1 alpha-2 code to show instead of the Steam profile's country; null to show Steam's." + "description": "An ISO 3166-1 alpha-2 code to show instead of Steam's or a game server's, until a run's server finds one;\nnull to stop showing the player's pick." }, "dpi": { "type": [ @@ -7945,6 +7964,14 @@ "style": { "type": "string" }, + "tier": { + "type": [ + "integer", + "null" + ], + "format": "int32", + "description": "The board's tier. None while tiers aren't in force or staff haven't set this board's, and for a legacy\nrun." + }, "time": { "type": "number", "format": "double", @@ -7954,6 +7981,14 @@ "type": "integer", "format": "int32", "description": "0 is the main track, 1 and up are bonuses." + }, + "world_record_time": { + "type": [ + "number", + "null" + ], + "format": "double", + "description": "The board's 2.0 record time, as on its map leaderboard, so a client can show the gap to it. None for a\nlegacy run." } } }, @@ -8135,14 +8170,14 @@ }, "country_chosen": { "type": "boolean", - "description": "Whether the player picked `country_code` themselves, rather than it coming from Steam." + "description": "Whether the player picked `country_code` themselves, rather than it coming from a game server or Steam." }, "country_code": { "type": [ "string", "null" ], - "description": "ISO 3166-1 alpha-2: the one the player picked, or else their Steam profile's. None if neither has one." + "description": "ISO 3166-1 alpha-2: the latest one a game server found for them, the player's pick if they made it\nsince, or else their Steam profile's. None if none has one." }, "details": { "oneOf": [ @@ -8202,6 +8237,14 @@ "$ref": "#/components/schemas/PlayerStyle" }, "description": "One entry per style they hold a personal best in, sorted by style key. Counts are 2.0 alone." + }, + "tier_count": { + "type": [ + "integer", + "null" + ], + "format": "int32", + "description": "Number of tiers in force; None while the community hasn't set tiers, and personal bests then have no\ntier." } } }, @@ -8858,7 +8901,8 @@ "RecentRuns": { "type": "object", "required": [ - "runs" + "runs", + "total" ], "properties": { "next_cursor": { @@ -8882,6 +8926,11 @@ ], "format": "int32", "description": "Number of tiers in force; None while the community hasn't set tiers, and runs then have no tier." + }, + "total": { + "type": "integer", + "format": "int64", + "description": "How many runs match the filters, across all pages." } } }, @@ -9677,6 +9726,14 @@ "name" ], "properties": { + "country_code": { + "type": [ + "string", + "null" + ], + "description": "The player's country, as the game server found it from their address with SourceMod's `GeoipCode2`: an ISO\n3166-1 alpha-2 code. Leave it out when the lookup finds nothing; send only the code, never the address. It\nbecomes the player's flag. A code SourceJump doesn't know as a country is ignored and the run is stored\nanyway. Runs only: detections ignore it.", + "example": "SE" + }, "name": { "type": "string" }, @@ -9899,6 +9956,7 @@ "required": [ "range", "bucket_minutes", + "peak_players", "points" ], "properties": { @@ -9906,6 +9964,11 @@ "type": "integer", "format": "int32" }, + "peak_players": { + "type": "integer", + "format": "int32", + "description": "The most players online at once across listed servers over the whole range, from the start of its\nfirst point up to now, the bucket in progress included: no point reads higher, and a shorter range never\nreads higher than a longer one." + }, "points": { "type": "array", "items": { diff --git a/docs/api.md b/docs/api.md index 4153656..092768b 100644 --- a/docs/api.md +++ b/docs/api.md @@ -1,5 +1,5 @@ # API -How the `/v1/` API behaves for game servers, and rules every client can rely on. The website's own endpoints (sign-in, players, maps, servers, posts): `site-api.md`. The exact schema is `api/openapi.json`. Examples, checked by `cargo test`: `api/contract/`. +How the `/v1/` API behaves for game servers, and rules for every client. The website's own endpoints (sign-in, players, maps, servers, posts): `site-api.md`. The exact schema is `api/openapi.json`. Examples, checked by `cargo test`: `api/contract/`. ## Endpoints | Endpoint | Key | Code | @@ -24,54 +24,55 @@ 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) 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. +- **Retrying:** `2xx` means stored. Network errors, `5xx` and `429` (too many) are temporary: keep the item queued and resend it. Any other `4xx` is final: the request breaks a rule, 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 _ - .`, 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`. +- **Server times** (`finished_at`, `detected_at`) are Unix seconds by the game server's clock. The API also records when each item arrived. Times over 5 minutes ahead of the API's clock get `422`. ## Keys - One key per server, sent as `Authorization: Bearer sj_…`. Only its SHA-256 is stored, so a lost key is replaced, never recovered. - A missing, unknown or revoked key gets `401` on keyed endpoints (`ServerKey` in `servers/keys.rs`). The heartbeat never rejects for its key; it answers `valid`, `revoked`, `unknown` or `none`. - Every run and detection records the key it came with, so revoking a key can invalidate every run its server sent, with any key (staff choose, `site-api.md`). -- 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`). +- 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 the old in the same step. How servers request keys is still open (`domain.md`). ## Server heartbeats -- 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. +- Every plugin server, keyed or not, sends one on map start, 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. 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. +- Accepted only if the run's ruleset version exists, its style is ranked there, 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. +- Optional `player.country_code`: the server's `GeoipCode2` for the player (never their IP) sets their flag; unknown codes are ignored. - `personal_bests` holds each player's fastest run per map, style and track, updated in the same transaction. A tie keeps the earlier run. -- Any keyed server can add a map by submitting a run on it. Which maps are ranked is a separate decision. +- Any keyed server adds a map by submitting a run on it; ranking it is a separate decision. ## 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` 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. +- 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 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; 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. +- `GET /v1/maps/{map}/records` marks each record `has_replay` by the same rule, so servers ask only for files they'll get. ## Map records for `/wr` - `GET /v1/maps/{map}/leaderboard?style=&track=` pages one board (`total`, `next_cursor`); `player=` (any SteamID) adds their best and rank as `player_entry`. Opaque cursors name where a page ended, so paging survives that player improving; old run-ID ones work while the run is a PB. Its rows and `…/records`' carry `points` and `has_replay`. - Both send an `ETag` (`etag.rs`, a body hash); `If-None-Match` with it gets a bodiless `304` until the answer changes. So do `/v1/ruleset`, `…/zones` and `/v1/plugin/latest`. ## Record events -- `GET /v1/records/events` lists what happened to records, for servers announcing them or playing them on their bot: `run` (a new personal best, or a restored run that is one again), `replay` (its replay stored) and `invalidated` (staff took a run off a board; read the map's records again). Events are written with their change and kept a week. `previous_time` is the player's best on the board before that run, as it stood then (null: none; migration 0043). The same events feed the Discord posts of records and personal bests (`record_events/discord.rs`). -- Servers poll every 10 to 30 s with `after`, the `next` of their last answer; without it they get only a `next` from now. Cursors are opaque. `max_rank` (default 1) keeps runs at that rank or better now; `invalidated` events always come. +- `GET /v1/records/events` lists what happened to records, for servers announcing them or playing them on their bot: `run` (a new personal best, or a restored run that is one again), `replay` (its replay stored) and `invalidated` (staff took a run off a board; read the map's records again). Events are written with their change, kept a week. `previous_time` is the player's best on the board before that run, as it stood then (null: none; migration 0043). The same events feed the Discord posts of records and personal bests (`record_events/discord.rs`). +- Servers poll every 10 to 30 s with `after`, the `next` of their last answer; without it, only a `next` from now. Cursors are opaque. `max_rank` (default 1) keeps runs at that rank or better now; `invalidated` events always come. - Pages hold only events from transactions older than every running one, so a late commit is delayed, never skipped. - `GET /v1/records/stream` pushes the same events as server-sent events (`event:` kind, `id:` cursor, `data:` the event), starting from `Last-Event-ID`, `after` or now, with a comment every 15 s. One reader, woken by `NOTIFY`, reads each event once for every stream (`stream.rs`); an IP may hold 40 streams, `429` past that. Streams end at shutdown; clients reconnect with `Last-Event-ID`. - The ruleset's `features.record_events` and `features.record_stream` tell the plugin to follow them (migration 0035). ## Policies and versioned data -Rulesets, zone sets and `policies` (community policy the plugin doesn't need: `servers`, `replays`, `tiers`, typed in `policies.rs`) are append-only: database triggers refuse updates and deletes. A change is a new version, and the newest version is in force. Runs record the ruleset version they were played under. A new version ships as a migration (lead, 2026-10-21); a test checks every ruleset they publish (`RulesetDocument::check`). The CLI's `tiers count` publishes the tier count, logged. +Rulesets, zone sets and `policies` (community policy the plugin doesn't need: `servers`, `replays`, `tiers`, typed in `policies.rs`) are append-only: database triggers refuse updates and deletes. A change is a new version; the newest is in force. Runs record the ruleset version they were played under. A new version ships as a migration (lead, 2026-10-21); a test checks every ruleset they publish (`RulesetDocument::check`). The CLI's `tiers count` publishes the tier count, logged. - Ranked styles carry shavit `settings` and `flags`; `RankedStyle` (`ruleset.rs`) says how they match. Provisional values: migrations 0041–0042 (lead, 2026-11-04). ## Zones -Served in exactly the JSON array `shavit-zones-json` reads, so servers point `shavit_zones_json_url` at `/v1/maps/{map}/zones`. A `404` means SourceJump has no zones for the map, and servers don't submit there. The `SourceJump-Zone-Set-Version` header (on `304` too) names the set served. Staff publish zone sets (`staff-api.md`); everyone sees the set in force at `/maps/{map}/zones`. Runs carry a zone fingerprint, stored as sent; checking it comes with run verification. +Served in exactly the JSON array `shavit-zones-json` reads, so servers point `shavit_zones_json_url` at `/v1/maps/{map}/zones`. A `404`: SourceJump has no zones for the map; servers don't submit there. The `SourceJump-Zone-Set-Version` header (on `304` too) names the set served. Staff publish zone sets (`staff-api.md`); everyone sees the set in force at `/maps/{map}/zones`. Runs carry a zone fingerprint, stored as sent; checking it comes with run verification. ## Not yet decided or built -- Field names can still change: no client uses them yet. Once plugin work starts, the lead approves the contract and it's copied to the plugin repo. -- Whether old plugin endpoints keep working, how zones are submitted and reviewed, and whether keyless servers' queued runs can be imported later. +- Field names can still change: no client uses them yet. Once plugin work starts, the lead approves the contract and it goes to the plugin repo. +- Whether old plugin endpoints keep working, how zones are submitted and reviewed, and whether keyless servers' queued runs can be imported. diff --git a/docs/site-api.md b/docs/site-api.md index 36ecdcd..2168dae 100644 --- a/docs/site-api.md +++ b/docs/site-api.md @@ -1,30 +1,31 @@ # Site API -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`. +The website's endpoints: sign-in, players, maps, server list, community posts; staff tools: `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); 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 (expiry moves at most daily). -- Sign-out also requires our `Origin` header. A missing session answers `401` without `WWW-Authenticate`. +- Sign-out also needs 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. ## Players and privileges -- Profiles take a SteamID64, SteamID2 or SteamID3 and answer with the SteamID64. Placements rank personal bests per map, style and track; equal times share a place. -- Per style, a profile counts completed maps (main tracks, out of the map list's count), completed bonuses and world records, and carries the first 50 main-track personal bests by map; `…/personal-bests?style=&tracks=main|bonuses&cursor=` pages on, and `records=true` keeps only world records (2.0 only). Without `style` it lists every style, newest first; `all_styles` counts across styles. Ranks: `main_standing`, `bonus_standing` (lead, 2026-11-04). -- `PUT /v1/players/{player}/details` (rules in `players/details.rs`): setup and a country (`GET /v1/countries`), a save per 5 s; others' need `manage_bans` and a logged reason. Old-site setup shows first. -- Avatar, country (unless the player picked one) and (until a run gives one) name come from their public Steam profile XML, fetched at sign-in and on viewing one over a day old (an hour after a failed one): one request per 2 s, 50 queued, one per player. Country names map to ISO codes in `players/countries.rs`. -- Staff privileges are a bitmap in `players.privileges` (`players/privileges.rs`). Bit positions never change; the API names privileges and never sends numbers. `players.restrictions` is unused (bans hold restrictions). The CLI's `privileges grant …`, `revoke`, `list` and the staff tools share `staff/privileges.rs`. +- Profiles take a SteamID64, SteamID2 or SteamID3 and answer with a SteamID64. +- Per style, a profile counts completed maps (main tracks, out of the map list's count), completed bonuses and world records, and carries the first 50 main-track personal bests by map; `…/personal-bests?style=&tracks=main|bonuses&cursor=` pages on; `records=true` keeps only 2.0 world records. Without `style`, every style, newest first; `all_styles` counts across styles. Ranks: `main_standing`, `bonus_standing` (lead, 2026-11-04). Personal bests have a placement (ties share one), `tier` and `world_record_time`; profiles `tier_count`. +- `PUT /v1/players/{player}/details` (`players/details.rs`): setup and a country (`GET /v1/countries`), a save per 5 s; others' need `manage_bans` and a logged reason. Old-site setup shows first. +- Country: a run's server GeoIP code, else the player's pick (shown until the next such run), else Steam's. +- Avatar, country and (until a run gives one) name come from their public Steam profile XML, fetched at sign-in, and when a day old after a run or a view (an hour after a failed one): one request per 2 s, 50 queued, one per player. Country names map to ISO codes in `players/countries.rs`. +- Staff privileges are a bitmap in `players.privileges` (`players/privileges.rs`). Bit positions never change; the API names privileges, never numbers. `players.restrictions` is unused (bans hold restrictions). The CLI's `privileges grant …`, `revoke`, `list` and the staff tools share `staff/privileges.rs`. ## Maps and leaderboards - The map list holds every map with a run (`maps.last_run_at`, set by the ingest); whether one is ranked is open. Sorts: name, players, last/first run (`listed_at`), tier, main-track record; `tier` keeps one tier. `player` (any SteamID form) with `completed=true|false` keeps maps whose main track they have or haven't finished in the list's style; `completed` counts them. `stats`: tiers, 5 newest, 5 most players this week. -- Leaderboards rank personal bests per map, style and track; equal times share a place, and the earlier run is listed first. The world record of a tie belongs to whoever set it first. -- `GET /v1/maps/{map}` carries every style and track's top 25, `next_cursor` and WR history, so a map page switches between them without another request. `…/leaderboard?cursor=` pages on from there; cursors are opaque. -- WR history is every run faster than all runs that finished before it on that leaderboard, by `finished_at`. Ties don't count. +- Leaderboards rank personal bests per map, style and track; equal times share a place, the earlier run listed first. A tie's world record belongs to whoever set it first. +- `GET /v1/maps/{map}` carries every style and track's top 25, `next_cursor` and WR history, so a map page switches between them locally. `…/leaderboard?cursor=` pages on from there; cursors are opaque. +- WR history: every run faster than all runs finished before it on that board, by `finished_at`. Ties don't count. - Tiers: the `tiers` policy holds the tier count, `map_tiers` the tier per map and track (append-only, newest in force). The API serves no tier until a count is published. -- Any run opens by ID with stats, server and place (while it's a personal best); compliance and keys stay private. +- Any run opens by ID with stats, server and place (while a personal best); compliance and keys stay private. - Pre-2.0 runs (`legacy_*` tables, `legacy.rs`) count toward no 2.0 standing: not WRs, WR histories, placements or `…/records`. The normal style's main track has two more views, `view=all` (each player's faster of 2.0 and legacy) and `view=legacy`, on `…/leaderboard` and `…/personal-bests`; entries carry `legacy`; a legacy `run_id` is the old site's, at `GET /v1/legacy-runs/{id}` (no run page). A map's `legacy_record_history` charts the Pre-2.0 view. Banned players' legacy runs are left out. Legacy-only maps and players get pages and show in search. -- The tier count is published with the CLI's `tiers count` (it can't drop below a tier a map holds). Staff rate tracks and set images and descriptions on the map's Manage view (`/maps/{map}/manage`), or with the CLI's `maps tier` and `maps image`, logged. +- The CLI's `tiers count` publishes the tier count (never below a tier a map holds). Staff rate tracks and set images and descriptions on the map's Manage view (`/maps/{map}/manage`), or with the CLI's `maps tier` and `maps image`, logged. - A map holds up to 10 pictures (`map_images`, in order; the first is the cover). The API reads PNG, JPEG or WebP up to 15 MB, crops to 16:9, centred (at least 1920×1080), and stores 1920×1080 and 480×270 WebP at quality 80 (`maps/image.rs`), named by content hash, cached for good, deleted with the picture. `GET /v1/maps/{map}` carries `images`, each with both URLs and `focus` (0–100, the strip a banner shows); lists carry the cover as `image`. Browsers read the files from `SJ_S3_PUBLIC_URL`; the API only writes them (ADR 0006). - Staff set `description` (≤1000 chars), `released` (year, month or day) and `mapper` (made if new) @@ -34,21 +35,21 @@ Players sign in with Steam (OpenID 2.0); the endpoints answer browsers (redirect - Stored, not computed per visit (lead, 2026-10-26): `personal_bests.points`, `standings` (per style, main and bonuses apart) and `legacy_standings`, recounted by `leaderboards/standings.rs` where they change: a track when a personal best or its tier changes there (a new WR moves all points); everything on a points or tiers policy, or at startup if the formula changed; legacy on bans, lifts, imports. One advisory lock serializes recounts. So are world records (`leaderboards/world_records.rs`): holders and `runs.was_record`, per board. ## Activity -- `GET /v1/runs?style=&track=main|bonus&min_tier=&records=&cursor=` (`runs/recent.rs`): valid runs newest first by `finished_at`, each with its board's tier, `placement` counted against the valid runs before it (null unless it beat the player's own best), `world_record` (as in WR history) and its `gap` to the record it faced. `min_tier` matches nothing until a tier count is published. +- `GET /v1/runs?style=&track=main|bonus&min_tier=&records=&cursor=&offset=` (`runs/recent.rs`, with `total`): valid runs newest first by `finished_at`, each with its board's tier, `placement` against the valid runs before it (null unless a personal best), `world_record` (as in WR history) and `gap` to the record it faced. `min_tier` matches nothing until a tier count is published. ## Daily totals - `GET /v1/daily-totals/players|runs?range=month|year|all` (`daily_totals.rs`, hourly): players with a time (incl. pre-2.0), 2.0 runs and WRs per UTC day. ## 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, never in the server render. The CLI's `servers seen`, `servers list ` and `servers unlist ` do the same. +- `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). No policy, no list. *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, per UTC day for 400 days, `server_activity_daily` (per-server totals) and `server_activity_peaks`. `…/activity?range=day|week|month` buckets the log (20 min, 2 h, 8 h), `year` the daily tables. Per bucket: servers heard from, their average players summed, and `peak_players`, most online at once; the top-level one, the whole range through now (never above a longer range's). +- Staff with `manage_servers` see every server on `/servers`: unlisted ones dimmed, toggles for listing and whitelist, and a dialog with its last report (versions, plugins, compliance, reported IP, key), browser-loaded, never server-rendered. The CLI's `servers seen` and `servers list|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`. +- 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`, logged (`edit_post`, fields changed) if someone else's and changed. Every write needs a session and our `Origin`. - `body_html` is rendered on each read by `posts/markdown.rs`, the only place deciding what HTML a post may contain: raw HTML becomes text, images their alt text, and links keep only web, mail and on-site addresses, opening `nofollow noopener`. -- A video link in any common form is stored as its YouTube ID (`posts/youtube.rs`), served back as a watch link. -- Pages link to `/community/{id}-{slug}`; only the ID counts, so a new title doesn't break links. Removing a post hides it and keeps the row. `DELETE /v1/posts/{post}` is for the author only. +- A video link in any common form is stored as its YouTube ID (`posts/youtube.rs`), served as a watch link. +- Pages link to `/community/{id}-{slug}`; only the ID counts, so retitling keeps links. Removal hides a post, keeping the row. `DELETE /v1/posts/{post}` is for the author only. ## Run comments - `/v1/runs/{id}/comments` (`runs/comments.rs`): players not banned from posting comment and reply one level deep (plain text, one per 30 s); the runner pins one; authors `DELETE` theirs (lead 2026-10-29). 25 threads a page, 3 replies each; more at `…/replies`. diff --git a/web/src/api/schema.ts b/web/src/api/schema.ts index 239e647..3444987 100644 --- a/web/src/api/schema.ts +++ b/web/src/api/schema.ts @@ -1446,6 +1446,13 @@ export interface components { * @description Where the bucket starts. */ at: string; + /** + * Format: int32 + * @description The most players online at once during the bucket, across listed servers: each server's latest + * heartbeat count, summed at the same moment. Up to a month, sampled every 5 minutes; for a year, the + * day's highest total at any heartbeat. + */ + peak_players: number; /** * Format: int32 * @description The average player count of each of those servers during the bucket, summed. @@ -1537,7 +1544,10 @@ export interface components { /** @description New details for a profile. Every field is replaced: leave one out or null to clear it. */ DetailsInput: { aspect?: components["schemas"]["Aspect"] | null; - /** @description An ISO 3166-1 alpha-2 code to show instead of the Steam profile's country; null to show Steam's. */ + /** + * @description An ISO 3166-1 alpha-2 code to show instead of Steam's or a game server's, until a run's server finds one; + * null to stop showing the player's pick. + */ country_code?: string | null; /** * Format: int32 @@ -2266,6 +2276,12 @@ export interface components { /** Format: int64 */ run_id: number; style: string; + /** + * Format: int32 + * @description The board's tier. None while tiers aren't in force or staff haven't set this board's, and for a legacy + * run. + */ + tier?: number | null; /** * Format: double * @description Seconds. @@ -2276,6 +2292,12 @@ export interface components { * @description 0 is the main track, 1 and up are bonuses. */ track: number; + /** + * Format: double + * @description The board's 2.0 record time, as on its map leaderboard, so a client can show the gap to it. None for a + * legacy run. + */ + world_record_time?: number | null; }; PersonalBestPage: { /** @description Pass as `cursor` for the personal bests after these. None at the end. */ @@ -2335,9 +2357,12 @@ export interface components { */ all_styles: components["schemas"]["StyleTotals"]; avatar_url?: string | null; - /** @description Whether the player picked `country_code` themselves, rather than it coming from Steam. */ + /** @description Whether the player picked `country_code` themselves, rather than it coming from a game server or Steam. */ country_chosen: boolean; - /** @description ISO 3166-1 alpha-2: the one the player picked, or else their Steam profile's. None if neither has one. */ + /** + * @description ISO 3166-1 alpha-2: the latest one a game server found for them, the player's pick if they made it + * since, or else their Steam profile's. None if none has one. + */ country_code?: string | null; details?: components["schemas"]["PlayerDetails"] | null; /** @description Whether `details` are what they had on the old site, shown until they save their own. */ @@ -2368,6 +2393,12 @@ export interface components { steamid64: string; /** @description One entry per style they hold a personal best in, sorted by style key. Counts are 2.0 alone. */ styles: components["schemas"]["PlayerStyle"][]; + /** + * Format: int32 + * @description Number of tiers in force; None while the community hasn't set tiers, and personal bests then have no + * tier. + */ + tier_count?: number | null; }; /** * @description A player as leaderboards, records and runs show them: enough to draw their name, avatar and flag, and to @@ -2657,6 +2688,11 @@ export interface components { * @description Number of tiers in force; None while the community hasn't set tiers, and runs then have no tier. */ tier_count?: number | null; + /** + * Format: int64 + * @description How many runs match the filters, across all pages. + */ + total: number; }; Record: { /** Format: date-time */ @@ -2992,6 +3028,14 @@ export interface components { world_record_time?: number | null; }; RunPlayer: { + /** + * @description The player's country, as the game server found it from their address with SourceMod's `GeoipCode2`: an ISO + * 3166-1 alpha-2 code. Leave it out when the lookup finds nothing; send only the code, never the address. It + * becomes the player's flag. A code SourceJump doesn't know as a country is ignored and the run is stored + * anyway. Runs only: detections ignore it. + * @example SE + */ + country_code?: string | null; name: string; /** @example 76561197960287930 */ steamid64: string; @@ -3110,6 +3154,13 @@ export interface components { ServerActivity: { /** Format: int32 */ bucket_minutes: number; + /** + * Format: int32 + * @description The most players online at once across listed servers over the whole range, from the start of its + * first point up to now, the bucket in progress included: no point reads higher, and a shorter range never + * reads higher than a longer one. + */ + peak_players: number; /** * @description Oldest first, one per bucket, empty buckets included. The bucket still in progress is left out, so the * last point isn't a partial count. @@ -4688,6 +4739,8 @@ export interface operations { records?: boolean | null; /** @description 1 to 100. Default 50. */ limit?: number | null; + /** @description Runs to skip, for going straight to a page. Default 0. Counted after `cursor` when both are given. */ + offset?: number | null; /** @description Continue after this point: a `next_cursor` from an earlier page with the same filters. */ cursor?: string | null; }; @@ -4705,7 +4758,7 @@ export interface operations { "application/json": components["schemas"]["RecentRuns"]; }; }; - /** @description Not a valid tier, limit or cursor */ + /** @description Not a valid tier, limit, offset or cursor */ 422: { headers: { [name: string]: unknown;