remove cookie flash-messages; simply show uuid
This commit is contained in:
95
src/game.rs
95
src/game.rs
@@ -1,13 +1,16 @@
|
||||
use crate::{language::language, page::Page, Backend, NameUpdateError};
|
||||
use crate::{
|
||||
language::language,
|
||||
page::{MyMessage, Page},
|
||||
Backend, NameUpdateError,
|
||||
};
|
||||
use axum::{
|
||||
extract::{Path, State},
|
||||
http::HeaderMap,
|
||||
response::{IntoResponse, Redirect, Response},
|
||||
response::{IntoResponse, Response},
|
||||
routing::{get, post},
|
||||
Form, Router,
|
||||
};
|
||||
use axum_extra::extract::CookieJar;
|
||||
use axum_messages::Messages;
|
||||
use maud::{html, Markup, PreEscaped};
|
||||
use serde::Deserialize;
|
||||
use std::sync::Arc;
|
||||
@@ -16,13 +19,18 @@ use uuid::Uuid;
|
||||
async fn index(
|
||||
State(backend): State<Arc<Backend>>,
|
||||
cookies: CookieJar,
|
||||
messages: Messages,
|
||||
headers: HeaderMap,
|
||||
) -> Response {
|
||||
tracing::info!("in /index");
|
||||
retu(backend, cookies, headers, None).await
|
||||
}
|
||||
|
||||
async fn retu(
|
||||
backend: Arc<Backend>,
|
||||
cookies: CookieJar,
|
||||
headers: HeaderMap,
|
||||
message: Option<MyMessage>,
|
||||
) -> Response {
|
||||
let (cookies, req) = backend.client_full(cookies, &headers).await;
|
||||
tracing::info!("cookies = {cookies:#?}");
|
||||
tracing::info!("req = {req:#?}");
|
||||
let client = req.client;
|
||||
rust_i18n::set_locale(&req.lang.to_string());
|
||||
|
||||
@@ -31,7 +39,9 @@ async fn index(
|
||||
let highscore = backend.highscore().await;
|
||||
|
||||
let mut page = Page::new(req.lang);
|
||||
page.messages(messages);
|
||||
if let Some(message) = message {
|
||||
page.set_message(message);
|
||||
}
|
||||
let markup = page.content(html! {
|
||||
hgroup {
|
||||
h1 { (t!("game_title")) }
|
||||
@@ -98,33 +108,31 @@ async fn game(
|
||||
State(backend): State<Arc<Backend>>,
|
||||
cookies: CookieJar,
|
||||
headers: HeaderMap,
|
||||
messages: Messages,
|
||||
Path(uuid): Path<String>,
|
||||
) -> Result<Redirect, Response> {
|
||||
) -> Response {
|
||||
let (cookies, req) = backend.client_full(cookies, &headers).await;
|
||||
let client = req.client;
|
||||
rust_i18n::set_locale(req.lang.to_locale());
|
||||
|
||||
let Ok(uuid) = Uuid::parse_str(&uuid) else {
|
||||
return Err(not_found(cookies, headers).await.into_response());
|
||||
return not_found(cookies, headers).await.into_response();
|
||||
};
|
||||
|
||||
let Some(camera) = backend.get_camera(&uuid).await else {
|
||||
return Err(not_found(cookies, headers).await.into_response());
|
||||
return not_found(cookies, headers).await.into_response();
|
||||
};
|
||||
|
||||
if let Ok(number) = backend.client_found_camera(&client, &camera).await {
|
||||
messages.info(format!("found-cam|{}|{number}", camera.name));
|
||||
let message = if let Ok(number) = backend.client_found_camera(&client, &camera).await {
|
||||
MyMessage::FoundCam(camera.name, number)
|
||||
} else {
|
||||
messages.info(format!(
|
||||
"err|{}|{}|{}",
|
||||
t!("error_already_found_title"),
|
||||
t!("error_already_found_body"),
|
||||
t!("error_already_found_footer")
|
||||
));
|
||||
}
|
||||
MyMessage::Error(
|
||||
t!("error_already_found_title").into(),
|
||||
t!("error_already_found_body").into(),
|
||||
t!("error_already_found_footer").into(),
|
||||
)
|
||||
};
|
||||
|
||||
Ok(Redirect::to("/game"))
|
||||
retu(backend, cookies, headers, Some(message)).await
|
||||
}
|
||||
|
||||
async fn not_found(cookies: CookieJar, headers: HeaderMap) -> Markup {
|
||||
@@ -142,7 +150,6 @@ struct NameForm {
|
||||
async fn set_name(
|
||||
State(backend): State<Arc<Backend>>,
|
||||
cookies: CookieJar,
|
||||
messages: Messages,
|
||||
headers: HeaderMap,
|
||||
Form(form): Form<NameForm>,
|
||||
) -> Response {
|
||||
@@ -150,32 +157,26 @@ async fn set_name(
|
||||
let client = req.client;
|
||||
rust_i18n::set_locale(req.lang.to_locale());
|
||||
|
||||
match backend.set_client_name(&client, &form.name).await {
|
||||
Ok(()) => messages.info("set-name-succ"),
|
||||
Err(NameUpdateError::TooShort(expected, actual)) => messages.info(format!(
|
||||
"err|{}|{}|{}: {}",
|
||||
t!("error_name_too_short_title"),
|
||||
t!("error_name_too_short_body", expected = expected),
|
||||
t!("received_characters"),
|
||||
actual
|
||||
)),
|
||||
Err(NameUpdateError::TooLong(expected, actual)) => messages.info(format!(
|
||||
"err|{}|{}|{}: {}",
|
||||
t!("error_name_too_long_title"),
|
||||
t!("error_name_too_long_body", expected = expected),
|
||||
t!("received_characters"),
|
||||
actual
|
||||
)),
|
||||
Err(NameUpdateError::ContainsBadWord) => messages.info(format!(
|
||||
"err|{}|{}|{}",
|
||||
t!("error_bad_word_title"),
|
||||
t!("error_bad_word_body"),
|
||||
t!("error_bad_word_footer")
|
||||
)),
|
||||
let message = match backend.set_client_name(&client, &form.name).await {
|
||||
Ok(()) => MyMessage::NameChanged,
|
||||
Err(NameUpdateError::TooShort(expected, actual)) => MyMessage::Error(
|
||||
t!("error_name_too_short_title").into(),
|
||||
t!("error_name_too_short_body", expected = expected).into(),
|
||||
format!("{}: {actual}", t!("received_characters")),
|
||||
),
|
||||
Err(NameUpdateError::TooLong(expected, actual)) => MyMessage::Error(
|
||||
t!("error_name_too_long_title").into(),
|
||||
t!("error_name_too_long_body", expected = expected).into(),
|
||||
format!("{}: {actual}", t!("received_characters")),
|
||||
),
|
||||
Err(NameUpdateError::ContainsBadWord) => MyMessage::Error(
|
||||
t!("error_bad_word_title").into(),
|
||||
t!("error_bad_word_body").into(),
|
||||
t!("error_bad_word_footer").into(),
|
||||
),
|
||||
};
|
||||
|
||||
// Redirect back to the game page
|
||||
(cookies, Redirect::to("/game")).into_response()
|
||||
retu(backend, cookies, headers, Some(message)).await
|
||||
}
|
||||
|
||||
pub(super) fn routes() -> Router<Arc<Backend>> {
|
||||
|
Reference in New Issue
Block a user