http: new_game and get_game demo
This commit is contained in:
parent
a0edf8f87e
commit
f5820656c3
|
@ -633,6 +633,7 @@ dependencies = [
|
||||||
"rocket",
|
"rocket",
|
||||||
"serde",
|
"serde",
|
||||||
"serde_json",
|
"serde_json",
|
||||||
|
"uuid",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
@ -879,6 +880,7 @@ dependencies = [
|
||||||
"tokio-stream",
|
"tokio-stream",
|
||||||
"tokio-util",
|
"tokio-util",
|
||||||
"ubyte",
|
"ubyte",
|
||||||
|
"uuid",
|
||||||
"version_check",
|
"version_check",
|
||||||
"yansi",
|
"yansi",
|
||||||
]
|
]
|
||||||
|
@ -925,6 +927,7 @@ dependencies = [
|
||||||
"time",
|
"time",
|
||||||
"tokio",
|
"tokio",
|
||||||
"uncased",
|
"uncased",
|
||||||
|
"uuid",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
@ -1332,6 +1335,16 @@ version = "0.2.4"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "f962df74c8c05a667b5ee8bcf162993134c104e96440b663c8daa176dc772d8c"
|
checksum = "f962df74c8c05a667b5ee8bcf162993134c104e96440b663c8daa176dc772d8c"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "uuid"
|
||||||
|
version = "1.7.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "f00cc9702ca12d3c81455259621e676d0f7251cec66a21e98fe2e9a37db93b2a"
|
||||||
|
dependencies = [
|
||||||
|
"getrandom",
|
||||||
|
"serde",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "valuable"
|
name = "valuable"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
|
|
|
@ -6,8 +6,9 @@ edition = "2021"
|
||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
rocket = { version = "0.5.0", features = ["json"] }
|
rocket = { version = "0.5.0", features = ["json", "uuid"] }
|
||||||
serde = "1.0.196"
|
serde = "1.0.196"
|
||||||
|
|
||||||
rand = "0.8.5"
|
rand = "0.8.5"
|
||||||
serde_json = "1.0.113"
|
serde_json = "1.0.113"
|
||||||
|
uuid = { version = "1.7.0", features = ["serde", "v4"] }
|
||||||
|
|
|
@ -7,7 +7,7 @@ use rand::distributions::WeightedIndex;
|
||||||
use rand::prelude::*;
|
use rand::prelude::*;
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize)]
|
#[derive(Serialize, Deserialize)]
|
||||||
struct GameState {
|
pub struct GameState {
|
||||||
n_players: usize,
|
n_players: usize,
|
||||||
current_player: usize,
|
current_player: usize,
|
||||||
starting_player: usize,
|
starting_player: usize,
|
||||||
|
@ -21,10 +21,10 @@ struct GameState {
|
||||||
players: Vec<Player>,
|
players: Vec<Player>,
|
||||||
#[serde(skip)]
|
#[serde(skip)]
|
||||||
#[serde(default = "make_rng")]
|
#[serde(default = "make_rng")]
|
||||||
rng: Box<dyn RngCore>,
|
rng: Box<dyn RngCore + Send>,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn make_rng() -> Box<dyn RngCore> {
|
fn make_rng() -> Box<dyn RngCore + Send> {
|
||||||
Box::new(StdRng::from_entropy())
|
Box::new(StdRng::from_entropy())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
37
src/main.rs
37
src/main.rs
|
@ -1,15 +1,50 @@
|
||||||
|
use rocket::response::status::NotFound;
|
||||||
use rocket::serde::{json::Json, Serialize};
|
use rocket::serde::{json::Json, Serialize};
|
||||||
|
use rocket::State;
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate rocket;
|
extern crate rocket;
|
||||||
|
|
||||||
|
use std::collections::HashMap;
|
||||||
|
use std::sync::{Arc, Mutex};
|
||||||
|
|
||||||
mod azul;
|
mod azul;
|
||||||
|
|
||||||
|
#[derive(Default)]
|
||||||
|
struct SharedState {
|
||||||
|
games: Arc<Mutex<HashMap<uuid::Uuid, azul::GameState>>>,
|
||||||
|
}
|
||||||
|
|
||||||
#[get("/")]
|
#[get("/")]
|
||||||
fn index() -> String {
|
fn index() -> String {
|
||||||
"Hello, world!".to_string()
|
"Hello, world!".to_string()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[get("/game/<id>")]
|
||||||
|
fn get_game(
|
||||||
|
id: uuid::Uuid,
|
||||||
|
shared: &State<SharedState>,
|
||||||
|
) -> Result<Json<azul::GameState>, NotFound<&'static str>> {
|
||||||
|
let games = shared.games.lock().unwrap();
|
||||||
|
let game = games.get(&id).ok_or(NotFound("Game not found"))?;
|
||||||
|
|
||||||
|
return Ok(Json(game.to_owned()));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[get("/new_game")]
|
||||||
|
fn new_game(shared: &State<SharedState>) -> Result<Json<uuid::Uuid>, &'static str> {
|
||||||
|
let id = uuid::Uuid::new_v4();
|
||||||
|
let game = azul::GameState::new(2)?;
|
||||||
|
|
||||||
|
let mut games = shared.games.lock().unwrap();
|
||||||
|
games.insert(id, game);
|
||||||
|
|
||||||
|
return Ok(Json(id));
|
||||||
|
}
|
||||||
|
|
||||||
#[launch]
|
#[launch]
|
||||||
fn rocket() -> _ {
|
fn rocket() -> _ {
|
||||||
rocket::build().mount("/", routes![index])
|
rocket::build()
|
||||||
|
.manage(SharedState::default())
|
||||||
|
.mount("/", routes![index])
|
||||||
|
.mount("/api/", routes![get_game, new_game])
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue