From 54d16b2c010e742453128ef077ed5b0fe3a36e4c Mon Sep 17 00:00:00 2001 From: Adam <24621027+adoyle0@users.noreply.github.com> Date: Tue, 13 Aug 2024 18:16:31 -0400 Subject: [PATCH] idk lol --- client/src/components/browser.rs | 91 ++++++++++++++++++++++-------- client/src/components/game.rs | 35 ++++++++---- client/src/components/websocket.rs | 6 +- lib/src/lib.rs | 20 ++++++- server/src/game_handler.rs | 15 ++++- server/src/user_handler.rs | 16 +++--- 6 files changed, 133 insertions(+), 50 deletions(-) diff --git a/client/src/components/browser.rs b/client/src/components/browser.rs index 05d31eb..2d0f2d5 100644 --- a/client/src/components/browser.rs +++ b/client/src/components/browser.rs @@ -9,11 +9,14 @@ use std::collections::BTreeSet; #[component] pub fn Browser() -> impl IntoView { let websocket = expect_context::(); + + // TODO: don't do this + let tx = websocket.clone(); + let connected = move || websocket.ready_state.get() == ConnectionReadyState::Open; - let game_update_context = expect_context::>>(); + let game_browser_context = expect_context::>>(); let card_packs = expect_context::>(); - let (active_games, set_active_games) = create_signal::>(vec![]); let new_game_name_ref = create_node_ref::(); let (selected_packs, set_selected_packs) = create_signal::>(BTreeSet::new()); @@ -24,18 +27,15 @@ pub fn Browser() -> impl IntoView { // Game stuff let new_game = move |_| { if let Some(input) = new_game_name_ref.get() { - if input.value() == String::from("") { + if input.value() == *"" { logging::log!("New game name is empty!"); } else if selected_packs().is_empty() { logging::log!("New game selected packs is empty!"); } else { - (websocket.send)( + tx.send( &to_string(&NewGameRequest { name: input.value(), - packs: selected_packs() - .into_iter() - .map(|n| n.clone()) // hax - .collect::>(), + packs: selected_packs().into_iter().collect::>(), }) .unwrap(), ); @@ -44,33 +44,65 @@ pub fn Browser() -> impl IntoView { } }; - create_effect(move |_| { - game_update_context.with(move |games| { - if let Some(games) = games { - set_active_games(games.games.clone()); - } - }) - }); - - // Clear games list on disconnect - create_effect(move |_| { - if !connected() { - set_active_games(vec![]); - } - }); let (show_packs, set_show_packs) = create_signal(false); let show_packs_button = move |_| set_show_packs(!show_packs()); + let (join_id, set_join_id) = create_signal("".to_string()); + + create_effect(move |_| { + websocket.send(&to_string(&GameJoinRequest { id: join_id() }).unwrap()); + }); + view! {

Game Browser

-
    - {move || active_games().into_iter().map(|n| view! {
  • {n}
  • }).collect_view()} -
+ + + + + + + + + + + {move || { + game_browser_context() + .iter() + .map(|game| { + view! { + + + + + + + + } + }) + .collect_view() + }} +
NameHostPlayersCard Packs
{&game.name}{&game.host} + {&game.players.to_string()} + + {&game.packs.len().to_string()} + + + +

+

Create Game

@@ -129,6 +161,8 @@ pub fn Browser() -> impl IntoView {
+ + // hax {set_selected_packs .update(|packs| { packs.insert(n.pack); @@ -148,6 +182,7 @@ pub fn Browser() -> impl IntoView { .map(|n| { view! { impl IntoView {
+ + // hax + {set_selected_packs + .update(|packs| { + packs.insert(n.pack); + })} } }) .collect_view() diff --git a/client/src/components/game.rs b/client/src/components/game.rs index 7076b8d..e8ab4d0 100644 --- a/client/src/components/game.rs +++ b/client/src/components/game.rs @@ -1,10 +1,10 @@ -use crate::components::websocket::WebSocketContext; +// use crate::components::websocket::WebSocketContext; use leptos::*; use lib::*; #[component] pub fn Game() -> impl IntoView { - let websocket = expect_context::(); + // let websocket = expect_context::(); let game_meta = expect_context::>>(); let (game_name, set_game_name) = create_signal("".to_string()); @@ -27,18 +27,31 @@ pub fn Game() -> impl IntoView { view! {
-

Game

- -

Name: {move || game_name()}

-

Host: {move || game_host()}

-

Czar: {move || game_czar()}

-

Players: {move || game_players()}

-
-
+
+

Game

+ +

Name: {move || game_name()}

+

Host: {move || game_host()}

+

Czar: {move || game_czar()}

+
+ +

Players:

+
    + {move || { + game_players() + .iter() + .map(|player| view! {
  • {player}
  • }) + .collect_view() + }} + +
+
+
+

{move || game_black().0}

Pick: {move || game_black().1}

-
+
{move || { game_white() .iter() diff --git a/client/src/components/websocket.rs b/client/src/components/websocket.rs index 47ae079..c4cb983 100644 --- a/client/src/components/websocket.rs +++ b/client/src/components/websocket.rs @@ -72,7 +72,7 @@ pub fn Websocket() -> impl IntoView { let (user_update, set_user_update) = create_signal::>(Option::None); let (chat_update, set_chat_update) = create_signal::>(Option::None); let (chat_message, set_chat_message) = create_signal::>(Option::None); - let (active_games, set_active_games) = create_signal::>(Option::None); + let (active_games, set_active_games) = create_signal::>(vec![]); let (current_game, set_current_game) = create_signal::>(Option::None); let (card_packs_meta, set_card_packs_meta) = create_signal::(CardPacksMeta { official_meta: vec![], @@ -83,7 +83,7 @@ pub fn Websocket() -> impl IntoView { provide_context::>>(user_update); provide_context::>>(chat_update); provide_context::>>(chat_message); - provide_context::>>(active_games); + provide_context::>>(active_games); provide_context::>>(current_game); provide_context::>(card_packs_meta); provide_context::>>(state_summary); @@ -106,7 +106,7 @@ pub fn Websocket() -> impl IntoView { } else if let Ok(chat_update) = from_str::(message) { set_chat_update(Some(chat_update)); } else if let Ok(games_update) = from_str::(message) { - set_active_games(Some(games_update)); + set_active_games(games_update.games); } else if let Ok(game_update) = from_str::(message) { set_current_game(Some(game_update)); } else if let Ok(packs_meta_update) = from_str::(message) { diff --git a/lib/src/lib.rs b/lib/src/lib.rs index 3b03501..9d2f1b5 100644 --- a/lib/src/lib.rs +++ b/lib/src/lib.rs @@ -1,14 +1,32 @@ use serde::{Deserialize, Serialize}; +/// Game join request +#[derive(Debug, Serialize, Deserialize)] +pub struct GameJoinRequest { + pub id: String, +} + /// Game meta #[derive(Clone, Debug, Serialize, Deserialize)] pub struct GameMeta { + pub uuid: String, pub name: String, pub host: String, pub players: Vec, pub czar: String, pub black: (String, u8), pub white: Vec, + pub packs: Vec, +} + +/// Game browser meta +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct GameBrowserMeta { + pub uuid: String, + pub name: String, + pub host: String, + pub players: usize, + pub packs: Vec, } /// Card Pack Meta @@ -30,7 +48,7 @@ pub struct CardPacksMeta { /// Games update #[derive(Serialize, Deserialize, Debug)] pub struct GamesUpdate { - pub games: Vec, + pub games: Vec, } /// Chat update diff --git a/server/src/game_handler.rs b/server/src/game_handler.rs index 0012fed..2042b81 100644 --- a/server/src/game_handler.rs +++ b/server/src/game_handler.rs @@ -161,7 +161,7 @@ struct NewGameManifest { /// A struct that represents a player #[derive(Debug)] -struct Player { +pub struct Player { /// The player's hand white: Vec, /// The player's wins @@ -171,6 +171,8 @@ struct Player { /// The game master #[derive(Debug)] pub struct Game { + /// Game's UUID + pub uuid: Uuid, /// The name of the game pub name: String, /// The host user of the game @@ -179,20 +181,25 @@ pub struct Game { white: Vec, /// Black draw pile black: Vec, - players: HashMap, + pub players: HashMap, /// Black card for the current round current_black: Option, + pub packs: Vec, } impl Game { fn new(state: Arc, request: NewGameManifest) -> Result { + tracing::debug!("{:#?}", request.packs); + tracing::debug!("{:#?}", request.packs.len()); let mut game = Game { + uuid: Uuid::now_v7(), name: request.host.read().unwrap().name.clone(), host: request.host.clone(), white: vec![], black: vec![], players: HashMap::new(), current_black: Option::None, + packs: request.packs.clone(), }; tracing::debug!( "Creating game {} with {} as host", @@ -367,6 +374,7 @@ impl GameHandler { } let meta = GameMeta { + uuid: new_game_object.uuid.to_string(), name: new_game_object.name.clone(), host: new_game_object.host.read().unwrap().name.clone(), players: new_game_object @@ -377,7 +385,7 @@ impl GameHandler { .user_uuid .read() .unwrap() - .get(&player.0) + .get(player.0) .unwrap() .read() .unwrap() @@ -395,6 +403,7 @@ impl GameHandler { .iter() .map(|card| card.text.clone()) .collect(), + packs: new_game_object.packs.clone(), }; tx.send(serde_json::to_string(&meta).unwrap()) diff --git a/server/src/user_handler.rs b/server/src/user_handler.rs index e9730e6..ceaa3b0 100644 --- a/server/src/user_handler.rs +++ b/server/src/user_handler.rs @@ -283,17 +283,19 @@ pub fn meta_server_summary_update(state: &Arc) -> String { /// Generate games list update pub fn meta_games_browser_update(state: &Arc) -> String { // TODO: this may get expensive if there are many games - let mut names = vec![]; + let mut games = vec![]; for game in state.games.read().unwrap().values() { - names.push(format!( - "Name: {} Host: {}", - game.read().unwrap().name, - game.read().unwrap().host.read().unwrap().name - )); + games.push(GameBrowserMeta { + uuid: game.read().unwrap().uuid.to_string(), + name: game.read().unwrap().name.clone(), + host: game.read().unwrap().host.read().unwrap().name.clone(), + players: game.read().unwrap().players.len(), + packs: game.read().unwrap().packs.clone(), + }); } - to_string::(&GamesUpdate { games: names }).unwrap() + to_string::(&GamesUpdate { games }).unwrap() } /// Generate chatroom join announcement