http: make_move demo
This commit is contained in:
parent
f5820656c3
commit
1368c97ab3
|
@ -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
|
||||
|
|
37
src/main.rs
37
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<SharedState>) -> Result<Json<uuid::Uuid>, &'static str> {
|
||||
#[post("/game", data = "<input>")]
|
||||
fn new_game(
|
||||
input: Option<Json<NewGameOptions>>,
|
||||
shared: &State<SharedState>,
|
||||
) -> Result<Json<uuid::Uuid>, &'static str> {
|
||||
let id = uuid::Uuid::new_v4();
|
||||
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));
|
||||
}
|
||||
|
||||
#[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]
|
||||
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])
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue