Displays available owned games for a GameNight.

This commit is contained in:
2025-07-13 14:29:23 +02:00
parent 0e89bca126
commit 8a48119c80
5 changed files with 96 additions and 4 deletions

View File

@@ -2,4 +2,5 @@ pub mod gamenight;
pub mod user;
pub mod config;
pub mod participants;
pub mod game;
pub mod game;
pub mod owned_games;

View File

@@ -0,0 +1,18 @@
use std::{collections::HashMap, fmt::Display};
use crate::domain::game::Game;
pub struct OwnedGames<'a>(pub &'a HashMap<String, Vec<Game>>);
impl<'a> Display for OwnedGames<'a> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
for (k,v) in self.0 {
write!(f, "{k}:\n")?;
for g in v {
write!(f, "\t{}\n", g.name)?;
}
}
Ok(())
}
}

View File

@@ -1,9 +1,12 @@
use gamenight_api_client_rs::{apis::default_api::{participants_get, user_get}, models::{GamenightId, UserId}};
use std::collections::HashMap;
use futures::future::join_all;
use gamenight_api_client_rs::{apis::default_api::{game_get, owned_games_get, participants_get, user_get, GameGetError}, models::{self, GameId, GamenightId, UserId}};
use inquire::Select;
use uuid::Uuid;
use crate::{domain::{gamenight::Gamenight, participants::Participants, user::User}, flows::{exit::Exit, join::Join, leave::Leave}};
use crate::{domain::{game::Game, gamenight::Gamenight, owned_games::OwnedGames, participants::Participants, user::User}, flows::{exit::Exit, join::Join, leave::Leave}};
use super::*;
@@ -33,6 +36,7 @@ impl<'a> Flow<'a> for ViewGamenight {
async fn run(&self, state: &'a mut GamenightState) -> FlowResult<'a> {
let participants = participants_get(&state.api_configuration, Some(GamenightId{gamenight_id: self.gamenight.id.to_string()})).await?;
let mut users = vec![];
for participant in participants.participants.iter() {
let user = user_get(&state.api_configuration, Some(UserId{user_id: participant.clone()})).await?;
@@ -42,7 +46,18 @@ impl<'a> Flow<'a> for ViewGamenight {
});
}
println!("{}\nwho: {}", self.gamenight, Participants(&users));
let mut user_games: HashMap<String, Vec<Game>> = HashMap::new();
for user in &users {
let request = UserId{ user_id: user.id.to_string() };
let games: Vec<Game> = join_all(owned_games_get(&state.api_configuration, Some(request)).await?
.iter().map(async |game_id| -> Result<models::Game, Error<GameGetError>> {
let request = GameId{ game_id: game_id.clone() };
game_get(&state.api_configuration, Some(request)).await
})).await.into_iter().collect::<Result<Vec<models::Game>, Error<GameGetError>>>()?.into_iter().map(Into::into).collect();
user_games.insert(user.username.clone(), games);
}
println!("{}\nwho: {}\nGames:\n{}", self.gamenight, Participants(&users), OwnedGames(&user_games));
let my_uid = state.get_user_id()?;
let join_or_leave: Box<dyn Flow<'a> + Send> =