move name'calculation' into backend

This commit is contained in:
2025-08-02 20:55:26 +02:00
parent 88b13e215c
commit 5356557f7b
3 changed files with 35 additions and 11 deletions

View File

@@ -1,4 +1,4 @@
use crate::{page::new, random_names::get_name_by_uuid, Backend};
use crate::{page::new, Backend};
use axum::{
extract::{Path, State},
response::{IntoResponse, Redirect, Response},
@@ -41,14 +41,12 @@ async fn index(State(backend): State<Arc<Backend>>, cookies: CookieJar) -> Respo
(rank.rank)
}
@if rank.uuid == client.uuid { (PreEscaped("<mark>")) }
@if let Some(name) = rank.name { (name) } @else {
"Anonymous "
(get_name_by_uuid(&rank.uuid))}
(rank.name)
@if rank.uuid == client.uuid { (PreEscaped("</mark>")) }
}
span {
(rank.amount)
}
}
}
}
}

View File

@@ -1,17 +1,40 @@
use crate::Backend;
use crate::{random_names::get_name_by_uuid, Backend};
pub(crate) struct Rank {
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) rank: i64,
pub(crate) name: String,
pub(crate) uuid: String,
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!(
Rank,
RankDb,
"SELECT
RANK() OVER (ORDER BY COUNT(s.client_uuid) DESC) as rank,
c.name,
@@ -24,7 +47,10 @@ impl Backend {
)
.fetch_all(db)
.await
.unwrap_or_default(),
.unwrap_or_default()
.into_iter()
.map(|r| r.into())
.collect(),
}
}
}

View File

@@ -470,11 +470,11 @@ const NAMES: [&str; 467] = [
"Yellow",
];
pub(crate) fn get_name_by_uuid(uuid: &str) -> &str {
pub(crate) fn get_name_by_uuid(uuid: &str) -> String {
let mut hasher = DefaultHasher::new();
uuid.hash(&mut hasher);
let hash = hasher.finish();
let index = (hash % NAMES.len() as u64) as usize;
NAMES[index]
format!("Anonymous {}", NAMES[index])
}