From 26e962e77fbca2d6b0a94163efb87d159cb2d2f6 Mon Sep 17 00:00:00 2001 From: zoe Date: Sat, 29 Jan 2022 22:26:32 +0100 Subject: [PATCH] way too many things oops --- snippets/back_to_home.html | 1 + src/main.rs | 44 ++++++++++++++++++++++++++++++++--- src/rooms.rs | 22 +++++++++++------- src/templates.rs | 47 ++++++++++++++++++++++++++++++++++++++ static/index.html | 2 +- static/script.js | 8 +++++++ static/style.css | 23 +++++++++++++++---- templates/join.html.hbs | 30 ++++++++++++++++++++++++ 8 files changed, 161 insertions(+), 16 deletions(-) create mode 100644 snippets/back_to_home.html create mode 100644 src/templates.rs create mode 100644 templates/join.html.hbs diff --git a/snippets/back_to_home.html b/snippets/back_to_home.html new file mode 100644 index 0000000..605c874 --- /dev/null +++ b/snippets/back_to_home.html @@ -0,0 +1 @@ +

Sorry! This room is already full. Maybe try another one?

diff --git a/src/main.rs b/src/main.rs index b6186ef..a20b552 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,12 +6,15 @@ extern crate lazy_static; use clap::Parser; use rocket::fs::{relative, FileServer}; use rocket_dyn_templates::Template; +use std::{collections::HashMap, mem::drop, sync::Mutex}; mod names; mod rooms; +mod templates; lazy_static! { static ref ARGS: Args = Args::parse(); + static ref ROOMS: Mutex> = Mutex::new(HashMap::new()); } #[derive(Parser, Debug)] @@ -20,6 +23,8 @@ struct Args { // Port number for server #[clap(short, long, default_value_t = 8000)] port: u16, + #[clap(short, long, default_value = "localhost")] + base_url: String, } #[launch] @@ -29,10 +34,43 @@ fn rocket() -> _ { rocket::custom(config) .attach(Template::fairing()) .mount("/", FileServer::from(relative!("static"))) - .mount("/", routes![join]) + .mount("/", routes![index, room]) } #[get("/?")] -fn join (roomname: &str) -> String { - format!("You're joining {}", roomname) +async fn index(mut roomname: String) -> Template { + // generate roomname if there is none yet + if roomname.chars().count() == 0 { + roomname = names::get_random_name().await; + print!("{}", roomname) + } + // lock list of rooms + let mut rooms = ROOMS.lock().unwrap(); + // do this if there are already people in the room + // first let the new player join and then lock the room for others + if rooms.contains_key(&roomname) { + let mut room = rooms.get_mut(&roomname).unwrap(); + if room.full { + drop(rooms); + return templates::get_back_to_home(&roomname); + } + room.full = true; + let templ = templates::join_room(&roomname, room.token); + drop(rooms); + return templ; + } + //create a new room if it doesn't exist yet + else { + // create new room + let room = rooms::Room::new(); + let templ = templates::join_new_room(&roomname, room.token); + rooms.insert(roomname.to_string(), room); + drop(rooms); + return templ; + } +} + +#[get("/room?&")] +async fn room(roomname: &str, token: usize) -> String{ + "hey gamer".to_string() } diff --git a/src/rooms.rs b/src/rooms.rs index f9db392..e4621ee 100644 --- a/src/rooms.rs +++ b/src/rooms.rs @@ -1,11 +1,17 @@ -use std::sync::Mutex; - -lazy_static! { - static ref ARRAY: Mutex> = Mutex::new(vec![]); -} +use rand::{thread_rng, Rng}; +#[derive(Debug)] pub struct Room { - id: String, - players: Vec, - full: bool + pub token: usize, + pub full: bool, +} + +impl Room { + pub fn new() -> Room { + let random: usize = thread_rng().gen(); + Room { + full: false, + token: random, + } + } } diff --git a/src/templates.rs b/src/templates.rs new file mode 100644 index 0000000..c9c2f10 --- /dev/null +++ b/src/templates.rs @@ -0,0 +1,47 @@ +use std::collections::HashMap; +use rocket_dyn_templates::Template; +use clap::Parser; + + + +lazy_static! { + static ref ARGS: Args = Args::parse(); +} + +#[derive(Parser, Debug)] +#[clap(about, version, author)] +struct Args { + #[clap(short, long, default_value = "localhost")] + base_url: String, +} + +pub fn get_back_to_home(roomname: &str) -> Template { + join_template_from("sorry! looks like this room is already full!", "try another!", "./", "hidden", roomname) +} + +pub fn join_new_room(roomname: &str, token: usize) -> Template { + let message = format!("your room is \"{}\"! copy your link and send it to a friend to let them join", roomname); + let link_url = link_url_from(roomname, token); + join_template_from(&message, "let me in!", &link_url, "url_button", roomname) +} + +pub fn join_room(roomname: &str, token: usize) -> Template { + let message = format!("your room is \"{}\"! someone is already waiting in this room!", roomname); + let link_url = link_url_from(roomname, token); + join_template_from(&message, "let me in!", &link_url, "hidden", roomname) +} + +fn join_template_from(message: &str, link: &str, link_url: &str, hide_url_button: &str, roomname: &str) -> Template { + let url_button_url = &format!("{}/?roomname={}", ARGS.base_url, roomname); + let mut context: HashMap<&str, &str> = HashMap::new(); + context.insert("message", message); + context.insert("link", link); + context.insert("link_url", link_url); + context.insert("hide_url_button", hide_url_button); + context.insert("url_button_url", url_button_url); + Template::render("join", context) +} + +fn link_url_from(roomname: &str, token: usize) -> String{ + format!("./room?roomname={}&token={}", roomname, token) +} diff --git a/static/index.html b/static/index.html index 8916942..4870508 100644 --- a/static/index.html +++ b/static/index.html @@ -17,9 +17,9 @@
diff --git a/static/script.js b/static/script.js index 8b13789..2c953c5 100644 --- a/static/script.js +++ b/static/script.js @@ -1 +1,9 @@ +function copyURI(evt) { + evt.preventDefault(); + navigator.clipboard.writeText(evt.target.getAttribute('href')).then(() => { + /* clipboard successfully set */ + }, () => { + /* clipboard write failed */ + }); +} diff --git a/static/style.css b/static/style.css index c3a1550..5dd81dc 100644 --- a/static/style.css +++ b/static/style.css @@ -28,6 +28,15 @@ html { background-color: var(--accent2); } +.hidden { + display: none; +} + +a { + color: var(--text); + text-decoration: none; +} + footer { font-family: "Sen", sans-serif; background-color: var(--text); @@ -39,14 +48,14 @@ footer { height: 10%; bottom: 0; width: 100%; - font-size: 1.6em; + font-size: 3em; justify-content: center; align-items: center; - font-size: 3em; } .contentdiv { display: flex; + flex-direction: column; align-items: center; align-self: center; height: 90%; @@ -54,15 +63,19 @@ footer { } button, -.contentdiv * { +.contentdiv *, +a, +p { line-height: 1.84em; border-radius: 0.48em; - font-size: 1.84em; + font-size: 32pt; margin: 0.5em; text-align: center; font-family: "Sen", sans-serif; + line-break: anywhere; } +a, button, input { outline: none; @@ -80,6 +93,8 @@ button { cursor: pointer; } +a:hover, +a:focus, button:hover, button:focus, input:hover, diff --git a/templates/join.html.hbs b/templates/join.html.hbs new file mode 100644 index 0000000..e6ecb08 --- /dev/null +++ b/templates/join.html.hbs @@ -0,0 +1,30 @@ + + + + + + Reversi Online + + + + + +
+

{{message}}


+ {{url_button_url}} + {{link}} +
+ +