From 24697f5ffceaf4e4d286263bcd07f3034ffec2d3 Mon Sep 17 00:00:00 2001 From: Philipp Hofer Date: Sat, 2 Aug 2025 19:49:35 +0200 Subject: [PATCH] add prototype of highscore list --- src/game.rs | 22 ++++++++++++++-------- src/model/highscore.rs | 28 ++++++++++++++++++++++++++++ src/model/mod.rs | 1 + 3 files changed, 43 insertions(+), 8 deletions(-) create mode 100644 src/model/highscore.rs diff --git a/src/game.rs b/src/game.rs index 4a35018..4117484 100644 --- a/src/game.rs +++ b/src/game.rs @@ -15,6 +15,7 @@ async fn index(State(backend): State>, cookies: CookieJar) -> Respo let sightings = backend.sightings_for_client(&client).await; let amount_total_cameras = backend.amount_total_cameras().await; + let highscore = backend.highscore().await; let markup = new(html! { hgroup { @@ -29,15 +30,20 @@ async fn index(State(backend): State>, cookies: CookieJar) -> Respo p { mark { "TODO: Show optional REGISTER-NAME message" } } + p { "You have found " (sightings.len()) "/" (amount_total_cameras) " cameras." } p { - "You have found " - (sightings.len()) - "/" - (amount_total_cameras) - " cameras." - } - p { - mark { "TODO: High score" } + h2 { "Highscore" } + ul { + @for rank in highscore { + li { + @if rank.uuid == client.uuid { (PreEscaped("")) } + @if let Some(name) = rank.name { (name) } @else { "Anonymer Bär" } + @if rank.uuid == client.uuid { (PreEscaped("")) } + (PreEscaped("→")) + (rank.amount) + } + } + } } }); diff --git a/src/model/highscore.rs b/src/model/highscore.rs new file mode 100644 index 0000000..87b1af5 --- /dev/null +++ b/src/model/highscore.rs @@ -0,0 +1,28 @@ +use crate::Backend; + +pub(crate) struct Rank { + pub(crate) name: Option, + pub(crate) uuid: String, + pub(crate) amount: i64, +} + +impl Backend { + pub(crate) async fn highscore(&self) -> Vec { + match self { + Backend::Sqlite(db) => sqlx::query_as!( + Rank, + "SELECT + c.name, + c.uuid, + COUNT(s.client_uuid) as amount + FROM client c + LEFT JOIN sightings s ON c.uuid = s.client_uuid + GROUP BY c.uuid, c.name + ORDER BY amount DESC" + ) + .fetch_all(db) + .await + .unwrap_or_default(), + } + } +} diff --git a/src/model/mod.rs b/src/model/mod.rs index 399109a..b76e201 100644 --- a/src/model/mod.rs +++ b/src/model/mod.rs @@ -1,3 +1,4 @@ pub(crate) mod camera; pub(crate) mod client; +pub(crate) mod highscore; pub(crate) mod sighting;