Skip to content

Commit

Permalink
Include list of current players in get_open_games. Closes #44
Browse files Browse the repository at this point in the history
  • Loading branch information
myOmikron committed Aug 22, 2023
1 parent 42fe77b commit a60e597
Showing 1 changed file with 28 additions and 3 deletions.
31 changes: 28 additions & 3 deletions src/server/handler/games.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use utoipa::ToSchema;
use uuid::Uuid;

use crate::chan::{WsManagerChan, WsManagerMessage, WsMessage};
use crate::models::Game;
use crate::models::{Game, GameAccount};
use crate::server::handler::{AccountResponse, ApiError, ApiResult, PathUuid};
use crate::server::RuntimeSettings;

Expand Down Expand Up @@ -52,6 +52,7 @@ pub struct GameOverviewResponse {
last_activity: DateTime<Utc>,
last_player: AccountResponse,
chat_room_uuid: Uuid,
players: Vec<AccountResponse>,
}

/// An overview of games a player participates in
Expand Down Expand Up @@ -84,8 +85,10 @@ pub async fn get_open_games(
) -> ApiResult<Json<GetGameOverviewResponse>> {
let uuid: Uuid = session.get("uuid")?.ok_or(ApiError::SessionCorrupt)?;

let open_games = query!(
db.as_ref(),
let mut tx = db.start_transaction().await?;

let mut open_games: Vec<GameOverviewResponse> = query!(
&mut tx,
(
Game::F.uuid,
Game::F.data_id,
Expand Down Expand Up @@ -126,11 +129,33 @@ pub async fn get_open_games(
display_name: updated_by_display_name,
},
chat_room_uuid: *chat_room.key(),
players: vec![],
}
},
)
.collect();

for game in &mut open_games {
game.players.extend(
query!(
&mut tx,
(
GameAccount::F.player.uuid,
GameAccount::F.player.username,
GameAccount::F.player.display_name
)
)
.all()
.await?
.into_iter()
.map(|(uuid, username, display_name)| AccountResponse {
uuid,
username,
display_name,
}),
);
}

Ok(Json(GetGameOverviewResponse { games: open_games }))
}

Expand Down

0 comments on commit a60e597

Please sign in to comment.