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 {
(rank.rank)"."
}
@if rank.uuid == client.uuid { (PreEscaped("<mark>")) }
(rank.name)
@if rank.uuid == client.uuid { (PreEscaped("</mark>")) }
@if rank.client == client { (PreEscaped("<mark>")) }
(rank.client.get_display_name())
@if rank.client == client{ (PreEscaped("</mark>")) }
}
span.font-headline.cam {
(rank.amount)(PreEscaped("&nbsp;"))"📸"

View File

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

View File

@@ -1,56 +1,42 @@
use crate::{random_names::get_name_by_uuid, Backend};
struct RankDb {
pub(crate) rank: i64,
pub(crate) name: Option<String>,
pub(crate) uuid: String,
pub(crate) amount: i64,
}
use crate::{model::client::Client, Backend};
pub(crate) struct Rank {
pub(crate) rank: i64,
pub(crate) name: String,
pub(crate) uuid: String,
pub(crate) client: Client,
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 {
pub(crate) async fn highscore(&self) -> Vec<Rank> {
match self {
Backend::Sqlite(db) => sqlx::query_as!(
RankDb,
"SELECT
RANK() OVER (ORDER BY COUNT(s.client_uuid) DESC) as rank,
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()
.into_iter()
.map(|r| r.into())
.collect(),
Backend::Sqlite(db) => {
let rows = sqlx::query!(
"SELECT
RANK() OVER (ORDER BY COUNT(s.client_uuid) DESC) as rank,
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();
rows.into_iter()
.map(|row| Rank {
rank: row.rank,
client: Client {
uuid: row.uuid,
name: row.name,
},
amount: row.amount,
})
.map(|r| r.into())
.collect()
}
}
}
}