add error/succ messages
This commit is contained in:
133
src/game.rs
133
src/game.rs
@@ -1,4 +1,4 @@
|
||||
use crate::{language::language, page::new, Backend};
|
||||
use crate::{language::language, page::Page, Backend, NameUpdateError};
|
||||
use axum::{
|
||||
extract::{Path, State},
|
||||
http::HeaderMap,
|
||||
@@ -7,6 +7,7 @@ use axum::{
|
||||
Form, Router,
|
||||
};
|
||||
use axum_extra::extract::CookieJar;
|
||||
use axum_messages::Messages;
|
||||
use maud::{html, Markup, PreEscaped};
|
||||
use serde::Deserialize;
|
||||
use std::sync::Arc;
|
||||
@@ -15,6 +16,7 @@ use uuid::Uuid;
|
||||
async fn index(
|
||||
State(backend): State<Arc<Backend>>,
|
||||
cookies: CookieJar,
|
||||
messages: Messages,
|
||||
headers: HeaderMap,
|
||||
) -> Response {
|
||||
let (cookies, req) = backend.client_full(cookies, &headers).await;
|
||||
@@ -24,66 +26,65 @@ async fn index(
|
||||
let amount_total_cameras = backend.amount_total_cameras().await;
|
||||
let highscore = backend.highscore().await;
|
||||
|
||||
let markup = new(
|
||||
html! {
|
||||
hgroup {
|
||||
h1 { "Who finds the most cameras?" }
|
||||
}
|
||||
p {
|
||||
mark { "TODO: Explanation of AEF / digital shadows / search game" }
|
||||
}
|
||||
let mut page = Page::new(req.lang);
|
||||
page.messages(messages);
|
||||
let markup = page.content(html! {
|
||||
hgroup {
|
||||
h1 { "Who finds the most cameras?" }
|
||||
}
|
||||
p {
|
||||
mark { "TODO: Explanation of AEF / digital shadows / search game" }
|
||||
}
|
||||
|
||||
div.mb-sm {
|
||||
(client.get_display_name())
|
||||
", do you want to be named something different? No worries, change here 👇"
|
||||
}
|
||||
div.mb-sm {
|
||||
(client.get_display_name())
|
||||
", do you want to be named something different? No worries, change here 👇"
|
||||
}
|
||||
|
||||
form action="/name" method="post" {
|
||||
fieldset role="group" {
|
||||
input
|
||||
name="name"
|
||||
placeholder="✨ Your new name starts here ✨"
|
||||
aria-label="Name";
|
||||
input type="submit" value="Save";
|
||||
form action="/name" method="post" {
|
||||
fieldset role="group" {
|
||||
input
|
||||
name="name"
|
||||
placeholder="✨ Your new name starts here ✨"
|
||||
aria-label="Name";
|
||||
input type="submit" value="Save";
|
||||
}
|
||||
}
|
||||
|
||||
p.mb-0 {
|
||||
"You have found "
|
||||
(sightings.len())
|
||||
"/"
|
||||
(amount_total_cameras)
|
||||
" cameras:"
|
||||
progress value=(sightings.len()) max=(amount_total_cameras);
|
||||
}
|
||||
|
||||
ol.flex.small {
|
||||
@for sighting in &sightings {
|
||||
li {
|
||||
(sighting.camera.name)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
p.mb-0 {
|
||||
"You have found "
|
||||
(sightings.len())
|
||||
"/"
|
||||
(amount_total_cameras)
|
||||
" cameras:"
|
||||
progress value=(sightings.len()) max=(amount_total_cameras);
|
||||
}
|
||||
|
||||
ol.flex.small {
|
||||
@for (idx, sighting) in sightings.iter().enumerate() {
|
||||
li {
|
||||
(sighting.camera.name)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
p {
|
||||
h2 { "Highscore" }
|
||||
ul.iterated {
|
||||
@for rank in highscore {
|
||||
li.card {
|
||||
span {
|
||||
span.font-headline.rank.text-muted { (rank.rank) "." }
|
||||
@if rank.client == client { (PreEscaped("<mark id='ranking'>")) }
|
||||
(rank.client.get_display_name())
|
||||
@if rank.client == client { (PreEscaped("</mark>")) }
|
||||
}
|
||||
span.font-headline.font-lg { (rank.amount) (PreEscaped(" ")) "📸" }
|
||||
p {
|
||||
h2 { "Highscore" }
|
||||
ul.iterated {
|
||||
@for rank in highscore {
|
||||
li.card {
|
||||
span {
|
||||
span.font-headline.rank.text-muted { (rank.rank) "." }
|
||||
@if rank.client == client { (PreEscaped("<mark id='ranking'>")) }
|
||||
(rank.client.get_display_name())
|
||||
@if rank.client == client { (PreEscaped("</mark>")) }
|
||||
}
|
||||
span.font-headline.font-lg { (rank.amount) (PreEscaped(" ")) "📸" }
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
req.lang,
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
(cookies, markup).into_response()
|
||||
}
|
||||
@@ -92,6 +93,7 @@ async fn game(
|
||||
State(backend): State<Arc<Backend>>,
|
||||
cookies: CookieJar,
|
||||
headers: HeaderMap,
|
||||
messages: Messages,
|
||||
Path(uuid): Path<String>,
|
||||
) -> Result<Redirect, Response> {
|
||||
let (cookies, client) = backend.client(cookies).await;
|
||||
@@ -104,20 +106,22 @@ async fn game(
|
||||
return Err(not_found(cookies, headers).await.into_response());
|
||||
};
|
||||
|
||||
let succ = backend.client_found_camera(&client, &camera).await;
|
||||
// TODO: show succ/err based on succ
|
||||
if backend.client_found_camera(&client, &camera).await {
|
||||
messages.info(format!("found-cam|{}", camera.name));
|
||||
} else {
|
||||
messages.info(format!(
|
||||
"err|Try to find a new camera!|You have already collected this camera.|"
|
||||
));
|
||||
}
|
||||
|
||||
Ok(Redirect::to("/game"))
|
||||
}
|
||||
|
||||
async fn not_found(cookies: CookieJar, headers: HeaderMap) -> Markup {
|
||||
let lang = language(&cookies, &headers);
|
||||
new(
|
||||
html! {
|
||||
h1 { "uups" }
|
||||
},
|
||||
lang,
|
||||
)
|
||||
Page::new(lang).content(html! {
|
||||
h1 { "uups" }
|
||||
})
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
@@ -128,13 +132,16 @@ struct NameForm {
|
||||
async fn set_name(
|
||||
State(backend): State<Arc<Backend>>,
|
||||
cookies: CookieJar,
|
||||
messages: Messages,
|
||||
Form(form): Form<NameForm>,
|
||||
) -> Response {
|
||||
let (cookies, client) = backend.client(cookies).await;
|
||||
|
||||
// Update the client's name in the backend
|
||||
// TODO: handle succ/err msg
|
||||
let _ = backend.set_client_name(&client, &form.name).await;
|
||||
match backend.set_client_name(&client, &form.name).await {
|
||||
Ok(()) => messages.info("set-name-succ"),
|
||||
Err(NameUpdateError::TooShort(expected, actual)) => messages.info(format!("err|That's too little!|We need more information about you. Give us at least {expected} characters for you new name!|You sent us {actual} characters.")),
|
||||
Err(NameUpdateError::TooLong(expected, actual)) => messages.info(format!("err|That's too much!|We only live in (20)25, so please use less than {expected} characters for your new name.|You sent us {actual} characters.")),
|
||||
};
|
||||
|
||||
// Redirect back to the game page
|
||||
(cookies, Redirect::to("/game")).into_response()
|
||||
|
@@ -8,10 +8,7 @@ pub(super) async fn index(cookies: CookieJar, headers: HeaderMap) -> Markup {
|
||||
|
||||
rust_i18n::set_locale(lang.to_locale());
|
||||
|
||||
let mut page = Page::new(lang);
|
||||
page.succ("Yeah! That worked!".into());
|
||||
page.err("Damn...".into());
|
||||
|
||||
let page = Page::new(lang);
|
||||
page.content(
|
||||
html! {
|
||||
h1 { (t!("digital_shadows")) }
|
||||
|
20
src/main.rs
20
src/main.rs
@@ -1,9 +1,11 @@
|
||||
use crate::model::client::Client;
|
||||
use axum::{http::HeaderMap, routing::get, Router};
|
||||
use axum_extra::extract::{cookie::Cookie, CookieJar};
|
||||
use axum_messages::MessagesManagerLayer;
|
||||
use sqlx::{pool::PoolOptions, sqlite::SqliteConnectOptions, SqlitePool};
|
||||
use std::{fmt::Display, str::FromStr, sync::Arc};
|
||||
use tower_http::services::ServeDir;
|
||||
use tower_sessions::{MemoryStore, SessionManagerLayer};
|
||||
use uuid::Uuid;
|
||||
|
||||
#[macro_use]
|
||||
@@ -68,6 +70,11 @@ struct Req {
|
||||
lang: Language,
|
||||
}
|
||||
|
||||
pub(crate) enum NameUpdateError {
|
||||
TooLong(usize, usize),
|
||||
TooShort(usize, usize),
|
||||
}
|
||||
|
||||
impl Backend {
|
||||
async fn client(&self, cookies: CookieJar) -> (CookieJar, Client) {
|
||||
let existing_uuid = cookies
|
||||
@@ -91,12 +98,12 @@ impl Backend {
|
||||
(cookies, Req { client, lang })
|
||||
}
|
||||
|
||||
async fn set_client_name(&self, client: &Client, name: &str) -> Result<(), String> {
|
||||
async fn set_client_name(&self, client: &Client, name: &str) -> Result<(), NameUpdateError> {
|
||||
if name.len() > 25 {
|
||||
return Err("Maximum 25 chars are allowed".into());
|
||||
return Err(NameUpdateError::TooLong(25, name.len()));
|
||||
}
|
||||
if name.len() < 3 {
|
||||
return Err("Minimum of 3 chars needed".into());
|
||||
return Err(NameUpdateError::TooShort(3, name.len()));
|
||||
}
|
||||
|
||||
match self {
|
||||
@@ -118,6 +125,9 @@ impl Backend {
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
let session_store = MemoryStore::default();
|
||||
let session_layer = SessionManagerLayer::new(session_store).with_secure(false);
|
||||
|
||||
let connection_options = SqliteConnectOptions::from_str("sqlite://db.sqlite").unwrap();
|
||||
let db: SqlitePool = PoolOptions::new()
|
||||
.connect_with(connection_options)
|
||||
@@ -128,7 +138,9 @@ async fn main() {
|
||||
.route("/", get(index::index))
|
||||
.nest_service("/static", ServeDir::new("./static/serve"))
|
||||
.merge(game::routes())
|
||||
.with_state(Arc::new(Backend::Sqlite(db)));
|
||||
.with_state(Arc::new(Backend::Sqlite(db)))
|
||||
.layer(MessagesManagerLayer)
|
||||
.layer(session_layer);
|
||||
|
||||
// run our app with hyper, listening globally on port 3000
|
||||
let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap();
|
||||
|
104
src/page.rs
104
src/page.rs
@@ -1,40 +1,49 @@
|
||||
use crate::Language;
|
||||
use axum_messages::Messages;
|
||||
use maud::{html, Markup, DOCTYPE};
|
||||
|
||||
// TODO: set dynamic meta lang attribute
|
||||
|
||||
// TODO: remove function
|
||||
#[deprecated]
|
||||
pub fn new(content: Markup, lang: Language) -> Markup {
|
||||
let page = Page::new(lang);
|
||||
page.content(content)
|
||||
}
|
||||
|
||||
pub(crate) struct Page {
|
||||
lang: Language,
|
||||
succ: Option<String>,
|
||||
err: Option<String>,
|
||||
found_camera: Option<String>,
|
||||
new_name: bool,
|
||||
err: Option<(String, String, String)>,
|
||||
}
|
||||
|
||||
impl Page {
|
||||
pub fn new(lang: Language) -> Self {
|
||||
Self {
|
||||
lang,
|
||||
succ: None,
|
||||
found_camera: None,
|
||||
new_name: false,
|
||||
err: None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn succ(&mut self, msg: String) {
|
||||
self.succ = Some(msg);
|
||||
}
|
||||
|
||||
pub fn err(&mut self, msg: String) {
|
||||
self.err = Some(msg);
|
||||
}
|
||||
|
||||
fn has_msg(&self) -> bool {
|
||||
self.succ.is_some() || self.err.is_some()
|
||||
pub fn messages(&mut self, messages: Messages) {
|
||||
for message in messages {
|
||||
let text = &message.to_string()[..];
|
||||
match (message.level, text) {
|
||||
(_, "set-name-succ") => {
|
||||
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());
|
||||
}
|
||||
(_, msg) if msg.starts_with("err|") => {
|
||||
let mut parts = msg.splitn(4, '|');
|
||||
let _ = parts.next().expect("just checked |");
|
||||
if let (Some(title), Some(body), Some(footer)) =
|
||||
(parts.next(), parts.next(), parts.next())
|
||||
{
|
||||
self.err = Some((title.into(), body.into(), footer.into()));
|
||||
}
|
||||
}
|
||||
(_, _) => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn content(self, content: Markup) -> Markup {
|
||||
@@ -83,39 +92,44 @@ impl Page {
|
||||
}
|
||||
|
||||
main.container {
|
||||
article class="succ w-full" {
|
||||
header {
|
||||
"Camera found"
|
||||
}
|
||||
"✨ You are a star ✨ Star dust sprinkle, sprinkle. "
|
||||
a href="#ranking" {
|
||||
"See your ranking"
|
||||
}
|
||||
footer {
|
||||
"Rear Exit Cam"
|
||||
@if let Some(found_camera) = &self.found_camera {
|
||||
article class="succ w-full" {
|
||||
header {
|
||||
"Camera "
|
||||
(found_camera)
|
||||
" found"
|
||||
}
|
||||
"✨ You are a star ✨ Star dust sprinkle, sprinkle. "
|
||||
a href="#ranking" {
|
||||
"See your ranking"
|
||||
}
|
||||
footer {
|
||||
"Rear Exit Cam"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
article class="name w-full" {
|
||||
header {
|
||||
"Camera found"
|
||||
}
|
||||
"text"
|
||||
footer {
|
||||
"Name of Camera"
|
||||
@if self.new_name {
|
||||
article class="name w-full" {
|
||||
header {
|
||||
"New name!"
|
||||
}
|
||||
"Welcome"
|
||||
}
|
||||
}
|
||||
|
||||
article class="error w-full" {
|
||||
header {
|
||||
"Camera found"
|
||||
}
|
||||
"text"
|
||||
footer {
|
||||
"Name of Camera"
|
||||
@if let Some(err) = &self.err {
|
||||
article class="error w-full" {
|
||||
header {
|
||||
(err.0)
|
||||
}
|
||||
(err.1)
|
||||
footer {
|
||||
(err.2)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
section { (content) }
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user