http: make_move demo

This commit is contained in:
Daniel Lovbrotte Olsen 2024-01-30 21:03:38 +01:00
parent f5820656c3
commit 1368c97ab3
2 changed files with 36 additions and 7 deletions

View File

@ -216,7 +216,7 @@ impl GameState {
/// After scoring, either ends the game and calculates bonus /// After scoring, either ends the game and calculates bonus
/// or fills the factories again and sets the starting player /// or fills the factories again and sets the starting player
/// Also tracks rounds and days /// 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 { if self.game_end {
return Err(MoveErr::Other("Game is over")); return Err(MoveErr::Other("Game is over"));
} }
@ -377,7 +377,7 @@ impl GameState {
} }
} }
enum MoveErr { pub enum MoveErr {
Player(&'static str), Player(&'static str),
Color(&'static str), Color(&'static str),
Src(&'static str), Src(&'static str),
@ -793,7 +793,7 @@ type Row = [bool; 5];
type Wall = [Row; 5]; type Wall = [Row; 5];
#[derive(Serialize, Deserialize, Clone, Copy)] #[derive(Serialize, Deserialize, Clone, Copy)]
struct GameMove { pub struct GameMove {
player: usize, // Safeguard to check that the bot knows which player it is 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 policy: Policy, // What policy to run moves under, so bots can do less error-handling
color: Color, // What color to select color: Color, // What color to select

View File

@ -1,5 +1,5 @@
use rocket::response::status::NotFound; use rocket::response::status::NotFound;
use rocket::serde::{json::Json, Serialize}; use rocket::serde::{json::Json, Deserialize, Serialize};
use rocket::State; use rocket::State;
#[macro_use] #[macro_use]
extern crate rocket; extern crate rocket;
@ -30,8 +30,11 @@ fn get_game(
return Ok(Json(game.to_owned())); return Ok(Json(game.to_owned()));
} }
#[get("/new_game")] #[post("/game", data = "<input>")]
fn new_game(shared: &State<SharedState>) -> Result<Json<uuid::Uuid>, &'static str> { fn new_game(
input: Option<Json<NewGameOptions>>,
shared: &State<SharedState>,
) -> Result<Json<uuid::Uuid>, &'static str> {
let id = uuid::Uuid::new_v4(); let id = uuid::Uuid::new_v4();
let game = azul::GameState::new(2)?; let game = azul::GameState::new(2)?;
@ -41,10 +44,36 @@ fn new_game(shared: &State<SharedState>) -> Result<Json<uuid::Uuid>, &'static st
return Ok(Json(id)); return Ok(Json(id));
} }
#[put("/game/<id>", data = "<input>")]
fn make_move(
id: uuid::Uuid,
input: Json<azul::GameMove>,
shared: &State<SharedState>,
) -> Result<Json<azul::GameState>, 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<String>,
},
Premade {
player_tokens: Vec<String>,
game: azul::GameState,
},
}
#[launch] #[launch]
fn rocket() -> _ { fn rocket() -> _ {
rocket::build() rocket::build()
.manage(SharedState::default()) .manage(SharedState::default())
.mount("/", routes![index]) .mount("/", routes![index])
.mount("/api/", routes![get_game, new_game]) .mount("/api/", routes![get_game, new_game, make_move])
} }