From f5820656c3349fbdd274dc49064dbb6c3a7c6097 Mon Sep 17 00:00:00 2001 From: Daniel Olsen Date: Tue, 30 Jan 2024 07:06:30 +0100 Subject: [PATCH] http: new_game and get_game demo --- Cargo.lock | 13 +++++++++++++ Cargo.toml | 3 ++- src/azul.rs | 6 +++--- src/main.rs | 37 ++++++++++++++++++++++++++++++++++++- 4 files changed, 54 insertions(+), 5 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 8edeb6c..c628731 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -633,6 +633,7 @@ dependencies = [ "rocket", "serde", "serde_json", + "uuid", ] [[package]] @@ -879,6 +880,7 @@ dependencies = [ "tokio-stream", "tokio-util", "ubyte", + "uuid", "version_check", "yansi", ] @@ -925,6 +927,7 @@ dependencies = [ "time", "tokio", "uncased", + "uuid", ] [[package]] @@ -1332,6 +1335,16 @@ version = "0.2.4" source = "registry+https://github.com/rust-lang/crates.io-index" 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]] name = "valuable" version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml index 6f9ebe7..898b613 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,8 +6,9 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -rocket = { version = "0.5.0", features = ["json"] } +rocket = { version = "0.5.0", features = ["json", "uuid"] } serde = "1.0.196" rand = "0.8.5" serde_json = "1.0.113" +uuid = { version = "1.7.0", features = ["serde", "v4"] } diff --git a/src/azul.rs b/src/azul.rs index a91d8c1..9bd6939 100644 --- a/src/azul.rs +++ b/src/azul.rs @@ -7,7 +7,7 @@ use rand::distributions::WeightedIndex; use rand::prelude::*; #[derive(Serialize, Deserialize)] -struct GameState { +pub struct GameState { n_players: usize, current_player: usize, starting_player: usize, @@ -21,10 +21,10 @@ struct GameState { players: Vec, #[serde(skip)] #[serde(default = "make_rng")] - rng: Box, + rng: Box, } -fn make_rng() -> Box { +fn make_rng() -> Box { Box::new(StdRng::from_entropy()) } diff --git a/src/main.rs b/src/main.rs index 0fe82cd..1a4b411 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,15 +1,50 @@ +use rocket::response::status::NotFound; use rocket::serde::{json::Json, Serialize}; +use rocket::State; #[macro_use] extern crate rocket; +use std::collections::HashMap; +use std::sync::{Arc, Mutex}; + mod azul; +#[derive(Default)] +struct SharedState { + games: Arc>>, +} + #[get("/")] fn index() -> String { "Hello, world!".to_string() } +#[get("/game/")] +fn get_game( + id: uuid::Uuid, + shared: &State, +) -> Result, 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) -> Result, &'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] fn rocket() -> _ { - rocket::build().mount("/", routes![index]) + rocket::build() + .manage(SharedState::default()) + .mount("/", routes![index]) + .mount("/api/", routes![get_game, new_game]) }