Skip to content

Commit

Permalink
de_server: Add join game endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
Indy2222 committed Dec 13, 2022
1 parent 8f1d680 commit 5074fe2
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 1 deletion.
1 change: 1 addition & 0 deletions crates/server/src/db.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
pub const SQLITE_CONSTRAINT_PRIMARYKEY: &str = "1555";
pub const SQLITE_CONSTRAINT_UNIQUE: &str = "2067";
pub const SQLITE_CONSTRAINT_FOREIGNKEY: &str = "787";

#[macro_export]
macro_rules! db_error {
Expand Down
13 changes: 12 additions & 1 deletion crates/server/src/games/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use thiserror::Error;
use super::model::{Game, GameConfig};
use crate::{
auth::model::MAX_USERNAME_LEN,
db::{SQLITE_CONSTRAINT_PRIMARYKEY, SQLITE_CONSTRAINT_UNIQUE},
db::{SQLITE_CONSTRAINT_FOREIGNKEY, SQLITE_CONSTRAINT_PRIMARYKEY, SQLITE_CONSTRAINT_UNIQUE},
db_error,
games::model::{MAX_GAME_NAME_LEN, MAX_MAP_NAME_LEN},
};
Expand Down Expand Up @@ -88,6 +88,10 @@ impl Games {
Ok(())
}

pub(super) async fn add_player(&self, username: &str, game: &str) -> Result<(), AdditionError> {
Self::add_player_inner(self.pool, false, username, game).await
}

async fn add_player_inner<'c, E>(
executor: E,
author: bool,
Expand All @@ -104,6 +108,11 @@ impl Games {
.execute(executor)
.await;

db_error!(
result,
AdditionError::UserOrGameDoesNotExist,
SQLITE_CONSTRAINT_FOREIGNKEY
);
db_error!(
result,
AdditionError::AlreadyInAGame,
Expand Down Expand Up @@ -206,6 +215,8 @@ pub(super) enum CreationError {
pub(super) enum AdditionError {
#[error("User is already in another game")]
AlreadyInAGame,
#[error("The user or the game does not exist")]
UserOrGameDoesNotExist,
#[error("A database error encountered")]
Database(#[source] sqlx::Error),
#[error(transparent)]
Expand Down
26 changes: 26 additions & 0 deletions crates/server/src/games/endpoints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pub(super) fn configure(cfg: &mut web::ServiceConfig) {
web::scope("/games")
.service(create)
.service(list)
.service(join)
.service(leave),
);
}
Expand Down Expand Up @@ -58,6 +59,31 @@ async fn list(games: web::Data<Games>) -> impl Responder {
}
}

#[put("/{name}/join")]
async fn join(
claims: web::ReqData<Claims>,
games: web::Data<Games>,
path: web::Path<String>,
) -> impl Responder {
let name = path.into_inner();

match games.add_player(claims.username(), name.as_str()).await {
Ok(_) => HttpResponse::Ok().finish(),
Err(AdditionError::AlreadyInAGame) => {
warn!("Game joining error: a user is already in a different game.");
HttpResponse::Forbidden().json("User is already in a different game.")
}
Err(AdditionError::UserOrGameDoesNotExist) => {
warn!("Game joining error: the game or the user does not exist");
HttpResponse::NotFound().json("Game not found.")
}
Err(error) => {
error!("Error while adding a player to a game: {:?}", error);
HttpResponse::InternalServerError().finish()
}
}
}

#[put("/{name}/leave")]
async fn leave(
claims: web::ReqData<Claims>,
Expand Down
22 changes: 22 additions & 0 deletions docs/content/lobby/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,28 @@ paths:
"409":
description: A different game with the same name already exists.

/a/games/{name}/join:
put:
summary: Join the game.
description: >-
Join the game. The client must not be part of another game (including
this one).
security:
- bearerAuth: []
parameters:
- name: name
in: path
required: true
schema:
type: string
responses:
"200":
description: The user successfully joined the game.
"403":
description: The user is already part of a game.
"404":
description: The game does not exist.

/a/games/{name}/leave:
put:
summary: Leave or abandon a game.
Expand Down

0 comments on commit 5074fe2

Please sign in to comment.