add comments and logic for joining

This commit is contained in:
zoe 2022-01-30 22:40:51 +01:00
parent f62dfca066
commit ab7785ed00
1 changed files with 34 additions and 2 deletions

View File

@ -17,12 +17,17 @@ lazy_static! {
static ref ROOMS: Mutex<HashMap<String, rooms::Room>> = Mutex::new(HashMap::new());
}
/*
* Config options
*/
#[derive(Parser, Debug)]
#[clap(about, version, author)]
struct Args {
// Port number for server
#[clap(short, long, default_value_t = 8000)]
port: u16,
// this is here so that it shows up in help, it's actually used in templates.rs
// sucks
#[clap(short, long, default_value = "http://127.0.0.1:8000")]
base_url: String,
}
@ -37,6 +42,9 @@ fn rocket() -> _ {
.mount("/", routes![index, room])
}
/*
* handles creating rooms, player counts and
*/
#[get("/?<roomname>")]
async fn index(mut roomname: String) -> Template {
// remove whitespace from roomname
@ -72,7 +80,31 @@ async fn index(mut roomname: String) -> Template {
}
}
/*
* handles requests for joining rooms and returns the real room page if token and room combination
* is correct
*/
#[get("/room?<roomname>&<token>&<player1>")]
async fn room(roomname: &str, token: usize, player1: bool) -> String{
"hey gamer".to_string()
async fn room(roomname: &str, token: usize, player1: bool) -> Template {
// lock room list mutex, don't forget to drop again
let rooms = ROOMS.lock().unwrap();
if rooms.contains_key(roomname) {
let room = rooms.get(roomname).unwrap();
// the room is real and the token is correct
if room.token == token {
drop(rooms);
//TODO: placeholder
templates::get_back_to_home("cool!")
}
// the room is real but the token is incorrect
else {
drop(rooms);
templates::get_back_to_home("illegalroom")
}
}
// the room doesn't exist
else {
drop(rooms);
templates::get_back_to_home("illegalroom")
}
}