only one place to 'calculate' the name

This commit is contained in:
2025-08-02 21:15:50 +02:00
parent 63a10d55fc
commit 33008e6b32
3 changed files with 41 additions and 49 deletions

View File

@@ -45,9 +45,9 @@ async fn index(State(backend): State<Arc<Backend>>, cookies: CookieJar) -> Respo
span.font-headline.rank.text-muted { span.font-headline.rank.text-muted {
(rank.rank)"." (rank.rank)"."
} }
@if rank.uuid == client.uuid { (PreEscaped("<mark>")) } @if rank.client == client { (PreEscaped("<mark>")) }
(rank.name) (rank.client.get_display_name())
@if rank.uuid == client.uuid { (PreEscaped("</mark>")) } @if rank.client == client{ (PreEscaped("</mark>")) }
} }
span.font-headline.cam { span.font-headline.cam {
(rank.amount)(PreEscaped("&nbsp;"))"📸" (rank.amount)(PreEscaped("&nbsp;"))"📸"

View File

@@ -9,6 +9,12 @@ pub struct Client {
pub name: Option<String>, pub name: Option<String>,
} }
impl PartialEq for Client {
fn eq(&self, other: &Self) -> bool {
self.uuid == other.uuid
}
}
impl Client { impl Client {
pub(crate) fn get_display_name(&self) -> String { pub(crate) fn get_display_name(&self) -> String {
match &self.name { match &self.name {

View File

@@ -1,56 +1,42 @@
use crate::{random_names::get_name_by_uuid, Backend}; use crate::{model::client::Client, Backend};
struct RankDb {
pub(crate) rank: i64,
pub(crate) name: Option<String>,
pub(crate) uuid: String,
pub(crate) amount: i64,
}
pub(crate) struct Rank { pub(crate) struct Rank {
pub(crate) rank: i64, pub(crate) rank: i64,
pub(crate) name: String, pub(crate) client: Client,
pub(crate) uuid: String,
pub(crate) amount: i64, pub(crate) amount: i64,
} }
impl From<RankDb> for Rank {
fn from(value: RankDb) -> Self {
let name = match value.name {
Some(name) => name,
None => get_name_by_uuid(&value.uuid).to_string(),
};
Self {
rank: value.rank,
name,
uuid: value.uuid,
amount: value.amount,
}
}
}
impl Backend { impl Backend {
pub(crate) async fn highscore(&self) -> Vec<Rank> { pub(crate) async fn highscore(&self) -> Vec<Rank> {
match self { match self {
Backend::Sqlite(db) => sqlx::query_as!( Backend::Sqlite(db) => {
RankDb, let rows = sqlx::query!(
"SELECT "SELECT
RANK() OVER (ORDER BY COUNT(s.client_uuid) DESC) as rank, RANK() OVER (ORDER BY COUNT(s.client_uuid) DESC) as rank,
c.name, c.name,
c.uuid, c.uuid,
COUNT(s.client_uuid) as amount COUNT(s.client_uuid) as amount
FROM client c FROM client c
LEFT JOIN sightings s ON c.uuid = s.client_uuid LEFT JOIN sightings s ON c.uuid = s.client_uuid
GROUP BY c.uuid, c.name GROUP BY c.uuid, c.name
ORDER BY amount DESC" ORDER BY amount DESC"
) )
.fetch_all(db) .fetch_all(db)
.await .await
.unwrap_or_default() .unwrap_or_default();
.into_iter()
.map(|r| r.into()) rows.into_iter()
.collect(), .map(|row| Rank {
rank: row.rank,
client: Client {
uuid: row.uuid,
name: row.name,
},
amount: row.amount,
})
.map(|r| r.into())
.collect()
}
} }
} }
} }