From 1368c97ab3c5b78090fb14c3e985a4c6f639de6c Mon Sep 17 00:00:00 2001 From: Daniel Olsen Date: Tue, 30 Jan 2024 21:03:38 +0100 Subject: [PATCH] http: make_move demo --- src/azul.rs | 6 +++--- src/main.rs | 37 +++++++++++++++++++++++++++++++++---- 2 files changed, 36 insertions(+), 7 deletions(-) diff --git a/src/azul.rs b/src/azul.rs index 9bd6939..c5ca759 100644 --- a/src/azul.rs +++ b/src/azul.rs @@ -216,7 +216,7 @@ impl GameState { /// After scoring, either ends the game and calculates bonus /// or fills the factories again and sets the starting player /// Also tracks rounds and days - fn do_iter(&mut self, game_move: GameMove) -> Result<(), MoveErr> { + pub fn do_iter(&mut self, game_move: GameMove) -> Result<(), MoveErr> { if self.game_end { return Err(MoveErr::Other("Game is over")); } @@ -377,7 +377,7 @@ impl GameState { } } -enum MoveErr { +pub enum MoveErr { Player(&'static str), Color(&'static str), Src(&'static str), @@ -793,7 +793,7 @@ type Row = [bool; 5]; type Wall = [Row; 5]; #[derive(Serialize, Deserialize, Clone, Copy)] -struct GameMove { +pub struct GameMove { player: usize, // Safeguard to check that the bot knows which player it is policy: Policy, // What policy to run moves under, so bots can do less error-handling color: Color, // What color to select diff --git a/src/main.rs b/src/main.rs index 1a4b411..e3d196b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,5 @@ use rocket::response::status::NotFound; -use rocket::serde::{json::Json, Serialize}; +use rocket::serde::{json::Json, Deserialize, Serialize}; use rocket::State; #[macro_use] extern crate rocket; @@ -30,8 +30,11 @@ fn get_game( return Ok(Json(game.to_owned())); } -#[get("/new_game")] -fn new_game(shared: &State) -> Result, &'static str> { +#[post("/game", data = "")] +fn new_game( + input: Option>, + shared: &State, +) -> Result, &'static str> { let id = uuid::Uuid::new_v4(); let game = azul::GameState::new(2)?; @@ -41,10 +44,36 @@ fn new_game(shared: &State) -> Result, &'static st return Ok(Json(id)); } +#[put("/game/", data = "")] +fn make_move( + id: uuid::Uuid, + input: Json, + shared: &State, +) -> Result, NotFound<&'static str>> { + let mut games = shared.games.lock().unwrap(); + let game = games.get_mut(&id).ok_or(NotFound("Game not found"))?; + + game.do_iter(input.into_inner()); + + return Ok(Json(game.to_owned())); +} + +#[derive(Serialize, Deserialize)] +#[serde(untagged)] +enum NewGameOptions { + New { + player_tokens: Vec, + }, + Premade { + player_tokens: Vec, + game: azul::GameState, + }, +} + #[launch] fn rocket() -> _ { rocket::build() .manage(SharedState::default()) .mount("/", routes![index]) - .mount("/api/", routes![get_game, new_game]) + .mount("/api/", routes![get_game, new_game, make_move]) }