diff --git a/client/src/components/auth.rs b/client/src/components/auth.rs index f9f8563..5efcba3 100644 --- a/client/src/components/auth.rs +++ b/client/src/components/auth.rs @@ -35,22 +35,19 @@ pub fn Auth() -> impl IntoView {

Sign in:

Username:

-
- -
- -
+
+ +
+ +
} } diff --git a/client/src/components/browser.rs b/client/src/components/browser.rs new file mode 100644 index 0000000..a20fc9a --- /dev/null +++ b/client/src/components/browser.rs @@ -0,0 +1,39 @@ +use crate::components::websocket::WebSocketContext; +use leptos::*; +use leptos_use::core::ConnectionReadyState; +use lib::models::GamesUpdate; + +#[component] +pub fn Browser() -> impl IntoView { + let websocket = expect_context::(); + let game_update_context = expect_context::>>(); + + let (active_games, set_active_games) = create_signal::>(vec![]); + + create_effect(move |_| { + game_update_context.with(move |games| { + if let Some(games) = games { + set_active_games(games.games.clone()); + logging::log!("{:#?}",active_games()); + } + }) + }); + + // Clear games list on disconnect + create_effect(move |_| { + websocket.ready_state.with(move |status| { + if *status == ConnectionReadyState::Closed { + set_active_games(vec![]); + } + }) + }); + + view! { +
+
    +

    Game Browser

    + {move || active_games().into_iter().map(|n| view! {
  • {n}
  • }).collect_view()} +
+
+ } +} diff --git a/client/src/components/chat.rs b/client/src/components/chat.rs index 743f0d1..e78b44d 100644 --- a/client/src/components/chat.rs +++ b/client/src/components/chat.rs @@ -95,37 +95,34 @@ pub fn Chat() -> impl IntoView {

Chat:

- -
    -

    Users:

    - {move || users().into_iter().map(|n| view! {
  • {n}
  • }).collect_view()} -
+ +
    +

    Users:

    + {move || users().into_iter().map(|n| view! {
  • {n}
  • }).collect_view()} +

-
- - -
+
+ + +

diff --git a/client/src/components/mod.rs b/client/src/components/mod.rs index 9e8cf65..bee9b73 100644 --- a/client/src/components/mod.rs +++ b/client/src/components/mod.rs @@ -1,4 +1,5 @@ pub mod auth; +pub mod browser; pub mod chat; pub mod debug; pub mod websocket; diff --git a/client/src/components/websocket.rs b/client/src/components/websocket.rs index ae89700..8b29132 100644 --- a/client/src/components/websocket.rs +++ b/client/src/components/websocket.rs @@ -73,11 +73,13 @@ 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); provide_context::>>(game_object); provide_context::>>(user_update); provide_context::>>(chat_update); provide_context::>>(chat_message); + provide_context::>>(active_games); provide_context::>>(state_summary); // Message handler @@ -96,6 +98,8 @@ pub fn Websocket() -> impl IntoView { set_user_update(Some(user_update)); } 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)); } else { logging::log!("Unhandled message: {}", message); } diff --git a/client/src/pages/home.rs b/client/src/pages/home.rs index 77a20a8..6575059 100644 --- a/client/src/pages/home.rs +++ b/client/src/pages/home.rs @@ -1,4 +1,5 @@ use crate::components::auth::*; +use crate::components::browser::*; use crate::components::chat::*; use crate::components::debug::*; use crate::components::websocket::*; @@ -34,6 +35,8 @@ pub fn Home() -> impl IntoView {

+ +

diff --git a/lib/src/models.rs b/lib/src/models.rs index 94a0d1b..3839ddf 100644 --- a/lib/src/models.rs +++ b/lib/src/models.rs @@ -1,5 +1,11 @@ use serde::{Deserialize, Serialize}; +/// Games update +#[derive(Serialize, Deserialize, Debug)] +pub struct GamesUpdate { + pub games: Vec, +} + /// Chat update #[derive(Serialize, Deserialize, Debug)] pub struct ChatUpdate { @@ -7,7 +13,6 @@ pub struct ChatUpdate { pub users: Vec, } - /// User update #[derive(Serialize, Deserialize, Debug)] pub struct UserUpdate { @@ -21,7 +26,7 @@ pub struct UserLogIn { } /// Chat message -#[derive(Clone, Serialize, Deserialize, Debug)] +#[derive(Serialize, Deserialize, Debug)] pub struct ChatMessage { pub text: String, } diff --git a/server/src/api.rs b/server/src/api.rs index 18d0ebe..e68679b 100644 --- a/server/src/api.rs +++ b/server/src/api.rs @@ -41,6 +41,16 @@ fn generate_chat_update(state: &Arc) -> ChatUpdate { } } +fn generate_games_update(state: &Arc) -> GamesUpdate { + let mut names = vec![]; + + for game in state.games.lock().unwrap().iter() { + names.push(game.name.clone()); + } + + GamesUpdate { games: names } +} + pub async fn on_websocket_connection(stream: WebSocket, state: Arc, addr: SocketAddr) { // Add User to users let new_user = User { @@ -80,6 +90,11 @@ pub async fn on_websocket_connection(stream: WebSocket, state: Arc, ad to_string(&server_sum_update(&state)).unwrap(), )) .await; + let _ = &sender + .send(Message::Text( + to_string(&generate_games_update(&state)).unwrap(), + )) + .await; // ANNOUNCE THY PRESENCE let msg = ChatMessage { diff --git a/server/src/api/message_handler.rs b/server/src/api/message_handler.rs index 06faf18..58b1dc3 100644 --- a/server/src/api/message_handler.rs +++ b/server/src/api/message_handler.rs @@ -21,6 +21,7 @@ pub async fn message_handler(message: Message, state: &Arc, addr: Sock tracing::error!("Failed to convert Game object to JSON.") } state.games.lock().unwrap().push(new_game_object); + let _ = tx.send(to_string(&generate_games_update(state)).unwrap()); let _ = tx.send(to_string(&server_sum_update(state)).unwrap()); // let _update = tx.send(motd()); } else { @@ -33,7 +34,13 @@ pub async fn message_handler(message: Message, state: &Arc, addr: Sock } else if let Ok(user_log_in) = serde_json::from_str::(&text) { let old_name = state.users.lock().unwrap().get(&addr).unwrap().name.clone(); let new_name = user_log_in.username.clone(); - state.users.lock().unwrap().get_mut(&addr).unwrap().change_name(user_log_in.username); + state + .users + .lock() + .unwrap() + .get_mut(&addr) + .unwrap() + .change_name(user_log_in.username); let msg = format! { "{0} changed name to {1}.", old_name, @@ -62,7 +69,10 @@ pub async fn message_handler(message: Message, state: &Arc, addr: Sock tracing::debug!("close received without close frame") } let msg = ChatMessage { - text: format!("{0} left.", state.users.lock().unwrap().get(&addr).unwrap().name), + text: format!( + "{0} left.", + state.users.lock().unwrap().get(&addr).unwrap().name + ), }; tracing::debug!("{}", msg.text); let _ = tx.send(to_string::(&msg).unwrap());