Skip to content

Commit

Permalink
Check online state of lobby owner on lobby creation. Closes #38
Browse files Browse the repository at this point in the history
  • Loading branch information
myOmikron committed Apr 27, 2023
1 parent daf36a0 commit a6b6736
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/server/handler/lobbies.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,7 @@ pub struct CreateLobbyResponse {
/// If you are already in another lobby, an error is returned.
/// `max_players` must be between 2 and 34 (inclusive).
/// If `password` is an empty string, an error is returned.
/// If you are not connected via websocket, an error is returned.
///
/// You are placed in the lobby and in the corresponding chatroom
#[utoipa::path(
Expand All @@ -293,6 +294,7 @@ pub async fn create_lobby(
req: Json<CreateLobbyRequest>,
db: Data<Database>,
session: Session,
ws_manager_chan: Data<WsManagerChan>,
) -> ApiResult<Json<CreateLobbyResponse>> {
let uuid: Uuid = session.get("uuid")?.ok_or(ApiError::SessionCorrupt)?;

Expand All @@ -303,6 +305,28 @@ pub async fn create_lobby(
return Err(ApiError::InvalidMaxPlayersCount);
}

// Check if the websocket of the executing user is connected
let (sender, receiver) = oneshot::channel();
if let Err(err) = ws_manager_chan
.send(WsManagerMessage::RetrieveOnlineState(uuid, sender))
.await
{
warn!("Could not send to ws manager chan: {err}");
return Err(ApiError::InternalServerError);
}

match receiver.await {
Ok(online) => {
if !online {
return Err(ApiError::WsNotConnected);
}
}
Err(err) => {
warn!("Error receiving online state: {err}");
return Err(ApiError::InternalServerError);
}
}

// Check if the executing account is already in a lobby
if query!(&mut tx, (LobbyAccount::F.uuid,))
.condition(LobbyAccount::F.player.equals(uuid.as_ref()))
Expand Down

0 comments on commit a6b6736

Please sign in to comment.