show ranking for each camera on sighting
This commit is contained in:
@@ -102,8 +102,8 @@ async fn game(
|
||||
return Err(not_found(cookies, headers).await.into_response());
|
||||
};
|
||||
|
||||
if backend.client_found_camera(&client, &camera).await {
|
||||
messages.info(format!("found-cam|{}", camera.name));
|
||||
if let Ok(number) = backend.client_found_camera(&client, &camera).await {
|
||||
messages.info(format!("found-cam|{}|{number}", camera.name));
|
||||
} else {
|
||||
messages.info("err|Try to find a new camera!|You have already collected this camera.|");
|
||||
}
|
||||
|
@@ -58,19 +58,33 @@ impl Backend {
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) async fn client_found_camera(&self, client: &Client, camera: &Camera) -> bool {
|
||||
pub(crate) async fn client_found_camera(
|
||||
&self,
|
||||
client: &Client,
|
||||
camera: &Camera,
|
||||
) -> Result<i64, ()> {
|
||||
let client_uuid = client.uuid.to_string();
|
||||
let camera_uuid = camera.uuid.to_string();
|
||||
|
||||
match self {
|
||||
Backend::Sqlite(db) => sqlx::query!(
|
||||
"INSERT INTO sightings(client_uuid, camera_id) VALUES (?, ?) RETURNING client_uuid",
|
||||
client_uuid,
|
||||
camera_uuid
|
||||
)
|
||||
.fetch_one(db)
|
||||
.await
|
||||
.is_ok(),
|
||||
Backend::Sqlite(db) => {
|
||||
sqlx::query!(
|
||||
"INSERT INTO sightings(client_uuid, camera_id) VALUES (?, ?)",
|
||||
client_uuid,
|
||||
camera_uuid
|
||||
)
|
||||
.execute(db)
|
||||
.await
|
||||
.map_err(|_| ())?;
|
||||
|
||||
Ok(sqlx::query_scalar!(
|
||||
"SELECT COUNT(client_uuid) FROM sightings WHERE camera_id = ?",
|
||||
camera_uuid
|
||||
)
|
||||
.fetch_one(db)
|
||||
.await
|
||||
.map_err(|_| ())?)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
19
src/page.rs
19
src/page.rs
@@ -6,7 +6,7 @@ use maud::{html, Markup, DOCTYPE};
|
||||
|
||||
pub(crate) struct Page {
|
||||
lang: Language,
|
||||
found_camera: Option<String>,
|
||||
found_camera: Option<(String, i64)>,
|
||||
new_name: bool,
|
||||
err: Option<(String, String, String)>,
|
||||
}
|
||||
@@ -29,8 +29,13 @@ impl Page {
|
||||
self.new_name = true;
|
||||
}
|
||||
(_, msg) if msg.starts_with("found-cam|") => {
|
||||
let (_, name) = msg.split_once('|').expect("we just checked |");
|
||||
self.found_camera = Some(name.into());
|
||||
let mut parts = msg.splitn(3, '|');
|
||||
let _ = parts.next().expect("just checked |");
|
||||
if let (Some(name), Some(amount)) = (parts.next(), parts.next()) {
|
||||
if let Ok(amount) = amount.parse::<i64>() {
|
||||
self.found_camera = Some((name.into(), amount));
|
||||
}
|
||||
}
|
||||
}
|
||||
(_, msg) if msg.starts_with("err|") => {
|
||||
let mut parts = msg.splitn(4, '|');
|
||||
@@ -96,11 +101,9 @@ impl Page {
|
||||
main.container {
|
||||
@if let Some(found_camera) = &self.found_camera {
|
||||
article class="succ w-full" {
|
||||
header { (t!("found_camera_title", name = found_camera)) }
|
||||
(t!("found_camera_body"))
|
||||
" "
|
||||
a href="#ranking" { "See your ranking" }
|
||||
footer { (found_camera) }
|
||||
header { (t!("found_camera_title", name = found_camera.0)) }
|
||||
(t!("found_camera_body", amount = found_camera.1))
|
||||
footer { a href="#ranking" { "See your ranking" } }
|
||||
}
|
||||
}
|
||||
@if self.new_name {
|
||||
|
Reference in New Issue
Block a user