way too many things oops
This commit is contained in:
parent
0f930b18f4
commit
26e962e77f
|
@ -0,0 +1 @@
|
||||||
|
<p>Sorry! This room is already full. Maybe try another one?</p>
|
44
src/main.rs
44
src/main.rs
|
@ -6,12 +6,15 @@ extern crate lazy_static;
|
||||||
use clap::Parser;
|
use clap::Parser;
|
||||||
use rocket::fs::{relative, FileServer};
|
use rocket::fs::{relative, FileServer};
|
||||||
use rocket_dyn_templates::Template;
|
use rocket_dyn_templates::Template;
|
||||||
|
use std::{collections::HashMap, mem::drop, sync::Mutex};
|
||||||
|
|
||||||
mod names;
|
mod names;
|
||||||
mod rooms;
|
mod rooms;
|
||||||
|
mod templates;
|
||||||
|
|
||||||
lazy_static! {
|
lazy_static! {
|
||||||
static ref ARGS: Args = Args::parse();
|
static ref ARGS: Args = Args::parse();
|
||||||
|
static ref ROOMS: Mutex<HashMap<String, rooms::Room>> = Mutex::new(HashMap::new());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Parser, Debug)]
|
#[derive(Parser, Debug)]
|
||||||
|
@ -20,6 +23,8 @@ struct Args {
|
||||||
// Port number for server
|
// Port number for server
|
||||||
#[clap(short, long, default_value_t = 8000)]
|
#[clap(short, long, default_value_t = 8000)]
|
||||||
port: u16,
|
port: u16,
|
||||||
|
#[clap(short, long, default_value = "localhost")]
|
||||||
|
base_url: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[launch]
|
#[launch]
|
||||||
|
@ -29,10 +34,43 @@ fn rocket() -> _ {
|
||||||
rocket::custom(config)
|
rocket::custom(config)
|
||||||
.attach(Template::fairing())
|
.attach(Template::fairing())
|
||||||
.mount("/", FileServer::from(relative!("static")))
|
.mount("/", FileServer::from(relative!("static")))
|
||||||
.mount("/", routes![join])
|
.mount("/", routes![index, room])
|
||||||
}
|
}
|
||||||
|
|
||||||
#[get("/?<roomname>")]
|
#[get("/?<roomname>")]
|
||||||
fn join (roomname: &str) -> String {
|
async fn index(mut roomname: String) -> Template {
|
||||||
format!("You're joining {}", roomname)
|
// 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()
|
||||||
}
|
}
|
||||||
|
|
22
src/rooms.rs
22
src/rooms.rs
|
@ -1,11 +1,17 @@
|
||||||
use std::sync::Mutex;
|
use rand::{thread_rng, Rng};
|
||||||
|
|
||||||
lazy_static! {
|
|
||||||
static ref ARRAY: Mutex<Vec<String>> = Mutex::new(vec![]);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
pub struct Room {
|
pub struct Room {
|
||||||
id: String,
|
pub token: usize,
|
||||||
players: Vec<String>,
|
pub full: bool,
|
||||||
full: bool
|
}
|
||||||
|
|
||||||
|
impl Room {
|
||||||
|
pub fn new() -> Room {
|
||||||
|
let random: usize = thread_rng().gen();
|
||||||
|
Room {
|
||||||
|
full: false,
|
||||||
|
token: random,
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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)
|
||||||
|
}
|
|
@ -17,9 +17,9 @@
|
||||||
<div class="contentdiv">
|
<div class="contentdiv">
|
||||||
<form>
|
<form>
|
||||||
<input
|
<input
|
||||||
|
maxlength=64"
|
||||||
type="text"
|
type="text"
|
||||||
name="roomname"
|
name="roomname"
|
||||||
maxlength="32"
|
|
||||||
placeholder="room name"
|
placeholder="room name"
|
||||||
/>
|
/>
|
||||||
<button type="submit">let's go!</button>
|
<button type="submit">let's go!</button>
|
||||||
|
|
|
@ -1 +1,9 @@
|
||||||
|
function copyURI(evt) {
|
||||||
|
evt.preventDefault();
|
||||||
|
navigator.clipboard.writeText(evt.target.getAttribute('href')).then(() => {
|
||||||
|
/* clipboard successfully set */
|
||||||
|
}, () => {
|
||||||
|
/* clipboard write failed */
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
|
|
@ -28,6 +28,15 @@ html {
|
||||||
background-color: var(--accent2);
|
background-color: var(--accent2);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.hidden {
|
||||||
|
display: none;
|
||||||
|
}
|
||||||
|
|
||||||
|
a {
|
||||||
|
color: var(--text);
|
||||||
|
text-decoration: none;
|
||||||
|
}
|
||||||
|
|
||||||
footer {
|
footer {
|
||||||
font-family: "Sen", sans-serif;
|
font-family: "Sen", sans-serif;
|
||||||
background-color: var(--text);
|
background-color: var(--text);
|
||||||
|
@ -39,14 +48,14 @@ footer {
|
||||||
height: 10%;
|
height: 10%;
|
||||||
bottom: 0;
|
bottom: 0;
|
||||||
width: 100%;
|
width: 100%;
|
||||||
font-size: 1.6em;
|
font-size: 3em;
|
||||||
justify-content: center;
|
justify-content: center;
|
||||||
align-items: center;
|
align-items: center;
|
||||||
font-size: 3em;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
.contentdiv {
|
.contentdiv {
|
||||||
display: flex;
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
align-items: center;
|
align-items: center;
|
||||||
align-self: center;
|
align-self: center;
|
||||||
height: 90%;
|
height: 90%;
|
||||||
|
@ -54,15 +63,19 @@ footer {
|
||||||
}
|
}
|
||||||
|
|
||||||
button,
|
button,
|
||||||
.contentdiv * {
|
.contentdiv *,
|
||||||
|
a,
|
||||||
|
p {
|
||||||
line-height: 1.84em;
|
line-height: 1.84em;
|
||||||
border-radius: 0.48em;
|
border-radius: 0.48em;
|
||||||
font-size: 1.84em;
|
font-size: 32pt;
|
||||||
margin: 0.5em;
|
margin: 0.5em;
|
||||||
text-align: center;
|
text-align: center;
|
||||||
font-family: "Sen", sans-serif;
|
font-family: "Sen", sans-serif;
|
||||||
|
line-break: anywhere;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
a,
|
||||||
button,
|
button,
|
||||||
input {
|
input {
|
||||||
outline: none;
|
outline: none;
|
||||||
|
@ -80,6 +93,8 @@ button {
|
||||||
cursor: pointer;
|
cursor: pointer;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
a:hover,
|
||||||
|
a:focus,
|
||||||
button:hover,
|
button:hover,
|
||||||
button:focus,
|
button:focus,
|
||||||
input:hover,
|
input:hover,
|
||||||
|
|
|
@ -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>
|
Loading…
Reference in New Issue