add board

This commit is contained in:
zoe 2022-02-06 13:30:35 +01:00
parent dce8bc5d92
commit f02f0de902
2 changed files with 43 additions and 28 deletions

View File

@ -5,33 +5,23 @@ extern crate lazy_static;
use clap::Parser;
use rocket::fs::{relative, FileServer};
use rocket::{State, Shutdown};
use rocket::serde::{Serialize, Deserialize};
use rocket_dyn_templates::Template;
use rocket::form::Form;
use rocket::tokio::sync::broadcast::{channel, Sender, error::RecvError};
use rocket::response::stream::{EventStream, Event, TextStream};
use rocket::response::stream::{EventStream, Event};
use std::{collections::HashMap, mem::drop, sync::Mutex};
mod names;
mod rooms;
mod templates;
mod plays;
lazy_static! {
static ref ARGS: Args = Args::parse();
static ref ROOMS: Mutex<HashMap<String, rooms::Room>> = Mutex::new(HashMap::new());
/// String is the room list
static ref PLAYS: Mutex<HashMap<String, Vec<plays::Play>>> = Mutex::new(HashMap::new());
}
#[derive(Debug, Clone, FromForm, Serialize, Deserialize)]
#[cfg_attr(test, derive(PartialEq, UriDisplayQuery))]
#[serde(crate = "rocket::serde")]
struct Play {
pub roomname: String,
pub position: String,
pub player1: bool,
}
/*
* Config options
*/
@ -54,7 +44,7 @@ fn rocket() -> _ {
rocket::custom(config)
.attach(Template::fairing())
.mount("/", FileServer::from(relative!("static")))
.mount("/", routes![index, room, stream, post])
.mount("/", routes![index, room])
}
/*
@ -124,20 +114,12 @@ async fn room(roomname: &str, token: usize, player1: bool) -> Template {
}
}
#[get("/stream")]
async fn stream(queue: &State<Sender<Play>>, mut end: Shutdown) -> TextStream![String]{
let mut rx = queue.subscribe();
TextStream! {
#[get("/stream/<roomname>/<token>")]
fn stream(roomname: String, token: usize) -> EventStream![]{
EventStream!{
loop {
yield "hello!".to_string();
let event = plays::Board::new(roomname);
yield Event::json(&event);
}
}
}
/// Receive a message from a form submission and broadcast it to any receivers.
#[post("/message", data = "<form>")]
fn post(form: Form<Play>, queue: &State<Sender<Play>>) {
// A send 'fails' if there are no active subscribers. That's okay.
let _res = queue.send(form.into_inner());
}

33
src/plays.rs Normal file
View File

@ -0,0 +1,33 @@
use rand::{thread_rng, Rng};
use rocket::serde::{Deserialize, Serialize};
#[derive(Debug, Clone, FromForm, Serialize, Deserialize)]
#[cfg_attr(test, derive(PartialEq, UriDisplayQuery))]
#[serde(crate = "rocket::serde")]
pub struct Play {
pub roomname: String,
#[field(validate = len(2...2))]
pub position: String,
pub player1: bool,
}
#[derive(Debug, Clone, FromForm, Serialize, Deserialize)]
#[cfg_attr(test, derive(PartialEq, UriDisplayQuery))]
#[serde(crate = "rocket::serde")]
pub struct Board {
pub roomname: String,
pub position: String,
pub player1: bool,
}
impl Board {
pub fn new(rname: String) -> Board {
let rng = thread_rng();
Board {
roomname: rname,
player1: rng.gen_bool(1.0 / 2.0),
// TODO change this!!! this doesn't make sense! i just put this here for testing
position: "".to_string(),
}
}
}