reversi/src/main.rs

77 lines
2.1 KiB
Rust

#[macro_use]
extern crate rocket;
#[macro_use]
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)]
#[clap(about, version, author)]
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]
fn rocket() -> _ {
let mut config = rocket::Config::default();
config.port = ARGS.port;
rocket::custom(config)
.attach(Template::fairing())
.mount("/", FileServer::from(relative!("static")))
.mount("/", routes![index, room])
}
#[get("/?<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()
}