way too many things oops

This commit is contained in:
zoe 2022-01-29 22:26:32 +01:00
parent 0f930b18f4
commit 26e962e77f
8 changed files with 161 additions and 16 deletions

View file

@ -0,0 +1 @@
<p>Sorry! This room is already full. Maybe try another one?</p>

View file

@ -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<HashMap<String, rooms::Room>> = 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("/?<roomname>")]
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?<roomname>&<token>")]
async fn room(roomname: &str, token: usize) -> String{
"hey gamer".to_string()
}

View file

@ -1,11 +1,17 @@
use std::sync::Mutex;
lazy_static! {
static ref ARRAY: Mutex<Vec<String>> = Mutex::new(vec![]);
}
use rand::{thread_rng, Rng};
#[derive(Debug)]
pub struct Room {
id: String,
players: Vec<String>,
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,
}
}
}

47
src/templates.rs Normal file
View file

@ -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)
}

View file

@ -17,9 +17,9 @@
<div class="contentdiv">
<form>
<input
maxlength=64"
type="text"
name="roomname"
maxlength="32"
placeholder="room name"
/>
<button type="submit">let's go!</button>

View file

@ -1 +1,9 @@
function copyURI(evt) {
evt.preventDefault();
navigator.clipboard.writeText(evt.target.getAttribute('href')).then(() => {
/* clipboard successfully set */
}, () => {
/* clipboard write failed */
});
}

View file

@ -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,

30
templates/join.html.hbs Normal file
View file

@ -0,0 +1,30 @@
<html>
<head>
<meta charset="utf-8" />
<link rel="stylesheet" type="text/css" href="/style.css" media="all" />
<script src="/script.js" charset="utf-8" defer></script>
<title>Reversi Online</title>
<meta
charset="utf-8"
name="viewport"
content="width=device-width, initial-scale=1"
/>
<link rel="icon" type="image/x-icon" href="./static/think.svg" />
</head>
</html>
<body>
<div class="contentdiv">
<p>{{message}}</p><br />
<a
onclick="copyURI(event)"
id="urlForCopy"
class="{{hide_url_button}}"
href="{{url_button_url}}"
type="button"
>{{url_button_url}}</a>
<a href={{link_url}}>{{link}}</a>
</div>
<footer>
<span id="status">reversi</span>
</footer>
</body>