Why doesn't play serialize??? 😢
This commit is contained in:
124
src/main.rs
124
src/main.rs
@ -8,12 +8,12 @@ use rocket::response::status::BadRequest;
|
||||
use rocket::State;
|
||||
use std::collections::HashMap;
|
||||
use std::sync::{Arc, Mutex};
|
||||
use uuid::Uuid;
|
||||
use uuid::{uuid, Uuid};
|
||||
|
||||
#[get("/")]
|
||||
fn index(player_uuids: &State<PlayerUuids>) -> String {
|
||||
format!(
|
||||
"a: localhost:8000/{}/\nb: localhost:8000/{}/",
|
||||
"localhost:8000/{}/\nlocalhost:8000/{}/",
|
||||
player_uuids.a, player_uuids.b
|
||||
)
|
||||
}
|
||||
@ -57,6 +57,58 @@ fn pass(
|
||||
Ok(format!("{}", game::get_events(&game_state)))
|
||||
}
|
||||
|
||||
#[get("/<uuid>/bounce/<play_id>")]
|
||||
fn bounce(
|
||||
uuid: Uuid,
|
||||
play_id: Uuid,
|
||||
game_state_arc: &State<ArcMutexGameState>,
|
||||
player_uuids: &State<PlayerUuids>,
|
||||
) -> Result<String, BadRequest<String>> {
|
||||
match player_uuids.map.get(&uuid) {
|
||||
Some(_) => (),
|
||||
None => return Err(BadRequest(format!("Invalid player {}.", uuid))),
|
||||
};
|
||||
let game_state_mutex = Arc::clone(&game_state_arc.state);
|
||||
let mut game_state = game_state_mutex.lock().unwrap();
|
||||
game::bounce(&mut game_state, play_id);
|
||||
Ok(format!("{}", game::get_events(&game_state)))
|
||||
}
|
||||
|
||||
#[get("/<uuid>/tap/<play_id>")]
|
||||
fn tap(
|
||||
uuid: Uuid,
|
||||
play_id: Uuid,
|
||||
game_state_arc: &State<ArcMutexGameState>,
|
||||
player_uuids: &State<PlayerUuids>,
|
||||
) -> Result<String, BadRequest<String>> {
|
||||
match player_uuids.map.get(&uuid) {
|
||||
Some(_) => (),
|
||||
None => return Err(BadRequest(format!("Invalid player {}.", uuid))),
|
||||
};
|
||||
let game_state_mutex = Arc::clone(&game_state_arc.state);
|
||||
let mut game_state = game_state_mutex.lock().unwrap();
|
||||
game::tap(&mut game_state, play_id);
|
||||
Ok(format!("{}", game::get_events(&game_state)))
|
||||
}
|
||||
|
||||
#[get("/<uuid>/play/<index>")]
|
||||
fn play(
|
||||
uuid: Uuid,
|
||||
index: usize,
|
||||
game_state_arc: &State<ArcMutexGameState>,
|
||||
player_uuids: &State<PlayerUuids>,
|
||||
) -> Result<String, BadRequest<String>> {
|
||||
let player = match player_uuids.map.get(&uuid) {
|
||||
Some(player) => player,
|
||||
None => return Err(BadRequest(format!("Invalid player {}.", uuid))),
|
||||
};
|
||||
let game_state_mutex = Arc::clone(&game_state_arc.state);
|
||||
let mut game_state = game_state_mutex.lock().unwrap();
|
||||
game::play_from_hand(&mut game_state, index, player.clone());
|
||||
//println!("{:#?}", game_state);
|
||||
Ok(format!("{}", game::get_events(&game_state)))
|
||||
}
|
||||
|
||||
#[get("/<uuid>/get_state")]
|
||||
fn get_state(
|
||||
uuid: Uuid,
|
||||
@ -95,6 +147,46 @@ fn draw(
|
||||
))
|
||||
}
|
||||
|
||||
#[get("/<uuid>/fade/<index>")]
|
||||
fn fade(
|
||||
uuid: Uuid,
|
||||
index: usize,
|
||||
game_state_arc: &State<ArcMutexGameState>,
|
||||
player_uuids: &State<PlayerUuids>,
|
||||
) -> Result<String, BadRequest<String>> {
|
||||
let game_state_mutex = Arc::clone(&game_state_arc.state);
|
||||
let mut game_state = game_state_mutex.lock().unwrap();
|
||||
let player = match player_uuids.map.get(&uuid) {
|
||||
Some(player) => player,
|
||||
None => return Err(BadRequest(format!("Invalid player {}.", uuid))),
|
||||
};
|
||||
game::fade_from_hand(&mut game_state, index, player.clone(), false);
|
||||
Ok(format!(
|
||||
"{}",
|
||||
game::get_game_one_player(&game_state, player.clone())
|
||||
))
|
||||
}
|
||||
|
||||
#[get("/<uuid>/fade-bottom/<index>")]
|
||||
fn fade_bottom(
|
||||
uuid: Uuid,
|
||||
index: usize,
|
||||
game_state_arc: &State<ArcMutexGameState>,
|
||||
player_uuids: &State<PlayerUuids>,
|
||||
) -> Result<String, BadRequest<String>> {
|
||||
let game_state_mutex = Arc::clone(&game_state_arc.state);
|
||||
let mut game_state = game_state_mutex.lock().unwrap();
|
||||
let player = match player_uuids.map.get(&uuid) {
|
||||
Some(player) => player,
|
||||
None => return Err(BadRequest(format!("Invalid player {}.", uuid))),
|
||||
};
|
||||
game::fade_from_hand(&mut game_state, index, player.clone(), true);
|
||||
Ok(format!(
|
||||
"{}",
|
||||
game::get_game_one_player(&game_state, player.clone())
|
||||
))
|
||||
}
|
||||
|
||||
#[get("/<uuid>/life/<count>")]
|
||||
fn life(
|
||||
uuid: Uuid,
|
||||
@ -127,15 +219,26 @@ struct PlayerUuids {
|
||||
|
||||
#[launch]
|
||||
fn rocket() -> _ {
|
||||
let game_state = game::new();
|
||||
let mut game_state = game::new();
|
||||
game::draw(&mut game_state, 7, Player::A);
|
||||
game::draw(&mut game_state, 7, Player::B);
|
||||
let game_state_arc = Arc::new(Mutex::new(game_state));
|
||||
|
||||
#[cfg(debug_assertions)]
|
||||
let a_uuid: Uuid = uuid!("9b0a2a95-72a9-4e03-930e-a9583d2a2a5a");
|
||||
#[cfg(not(debug_assertions))]
|
||||
let a_uuid: Uuid = Uuid::new_v4();
|
||||
#[cfg(debug_assertions)]
|
||||
let b_uuid: Uuid = uuid!("2efc0332-975d-4f71-9dd0-ffc9213098a5");
|
||||
#[cfg(not(debug_assertions))]
|
||||
let b_uuid: Uuid = Uuid::new_v4();
|
||||
|
||||
let uuid_map = HashMap::from([(a_uuid, Player::A), (b_uuid, Player::B)]);
|
||||
println!("A: {}", a_uuid);
|
||||
println!("B: {}", b_uuid);
|
||||
|
||||
rocket::build()
|
||||
.mount("/", FileServer::from(relative!("static/assets")).rank(0))
|
||||
.mount(
|
||||
format!("/{}", a_uuid.hyphenated()),
|
||||
FileServer::from(relative!("static")),
|
||||
@ -146,7 +249,20 @@ fn rocket() -> _ {
|
||||
)
|
||||
.mount(
|
||||
"/",
|
||||
routes![index, get_events, get_state, draw, shuffle, pass, life,],
|
||||
routes![
|
||||
index,
|
||||
get_events,
|
||||
get_state,
|
||||
draw,
|
||||
shuffle,
|
||||
pass,
|
||||
life,
|
||||
fade,
|
||||
fade_bottom,
|
||||
play,
|
||||
bounce,
|
||||
tap,
|
||||
],
|
||||
)
|
||||
.manage(ArcMutexGameState {
|
||||
state: game_state_arc,
|
||||
|
||||
Reference in New Issue
Block a user