Skip to content

Commit

Permalink
added start game endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
Timtam committed Mar 6, 2024
1 parent 1bb6261 commit 87446df
Show file tree
Hide file tree
Showing 6 changed files with 392 additions and 41 deletions.
1 change: 1 addition & 0 deletions server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ dotenvy = "0.15.7"
ffmpeg-cli = "0.1.0"
hex = "0.4.3"
names = "0.14.0"
rand = "0.8.5"
regex = "1.10.3"
rocket = { version = "0.5.0", features = ["json", "secrets"]}
rocket_db_pools = { version = "0.1.0", default-features = false, features = ["sqlx_sqlite" ]}
Expand Down
6 changes: 6 additions & 0 deletions server/src/games.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ use serde::{Deserialize, Serialize};
pub enum GameState {
/// the game is currently accepting new players
Open,
/// the player has to guess, a song is currently available for playback
Guessing,
/// a different player has to confirm the choices
Confirming,
}

#[derive(Deserialize, Serialize, JsonSchema, Clone, Eq, PartialEq, Debug)]
Expand All @@ -14,4 +18,6 @@ pub struct Game {
pub creator: u32,
pub players: Vec<u32>,
pub state: GameState,
/// 0
pub turn_player: u32,
}
1 change: 1 addition & 0 deletions server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ fn rocket_from_config(figment: Figment) -> Rocket<Build> {
games_routes::get_all_games,
games_routes::join_game,
games_routes::leave_game,
games_routes::start_game,
],
)
.mount(
Expand Down
72 changes: 71 additions & 1 deletion server/src/responses.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ impl OpenApiResponderInner for LeaveGameError {

impl std::fmt::Display for LeaveGameError {
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(formatter, "Join game error `{}`", self.message,)
write!(formatter, "Leave game error `{}`", self.message,)
}
}

Expand All @@ -134,6 +134,76 @@ impl<'r> Responder<'r, 'static> for LeaveGameError {
}
}

#[derive(Debug, Serialize, JsonSchema)]
pub struct StartGameError {
pub message: String,
#[serde(skip)]
pub http_status_code: u16,
}

impl OpenApiResponderInner for StartGameError {
fn responses(_generator: &mut OpenApiGenerator) -> Result<Responses, OpenApiError> {
let mut responses = Map::new();
responses.insert(
"403".to_string(),
RefOr::Object(OpenApiResponse {
description: "\
# [403 Forbidden](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/403)\n\
You are not the creator of the selected game.\
"
.to_string(),
..Default::default()
}),
);
responses.insert(
"404".to_string(),
RefOr::Object(OpenApiResponse {
description: "\
# [404 Not Found](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/404)\n\
A game with that ID doesn't exist.\
"
.to_string(),
..Default::default()
}),
);
responses.insert(
"409".to_string(),
RefOr::Object(OpenApiResponse {
description: "\
# [409 Conflict](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/409)\n\
There are to few players in this game or the game is already running.\
"
.to_string(),
..Default::default()
}),
);
Ok(Responses {
responses,
..Default::default()
})
}
}

impl std::fmt::Display for StartGameError {
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(formatter, "Start game error `{}`", self.message,)
}
}

impl std::error::Error for StartGameError {}

impl<'r> Responder<'r, 'static> for StartGameError {
fn respond_to(self, _: &'r Request<'_>) -> response::Result<'static> {
// Convert object to json
let body = serde_json::to_string(&self).unwrap();
Response::build()
.sized_body(body.len(), std::io::Cursor::new(body))
.header(ContentType::JSON)
.status(Status::new(self.http_status_code))
.ok()
}
}

#[derive(Serialize, Deserialize, JsonSchema)]
pub struct GamesResponse {
pub games: Vec<GameResponse>,
Expand Down
Loading

0 comments on commit 87446df

Please sign in to comment.