From 77ac3130708c9650b1e7b43c9f182d27d2b45dd9 Mon Sep 17 00:00:00 2001 From: Adam <24621027+adoyle0@users.noreply.github.com> Date: Wed, 21 Aug 2024 22:25:33 -0400 Subject: [PATCH] clear game state on disconnect --- client/src/components/game.rs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/client/src/components/game.rs b/client/src/components/game.rs index dfaa7f1..ce96565 100644 --- a/client/src/components/game.rs +++ b/client/src/components/game.rs @@ -1,6 +1,7 @@ use crate::components::cards::*; use crate::components::websocket::WebSocketContext; use leptos::*; +use leptos_use::core::ConnectionReadyState; use lib::*; use serde_json::to_string; use std::collections::HashMap; @@ -8,6 +9,7 @@ use std::collections::HashMap; #[component] pub fn Game() -> impl IntoView { let websocket = expect_context::(); + let connected = move || websocket.ready_state.get() == ConnectionReadyState::Open; let game_meta = expect_context::>>(); let judge_round = expect_context::>>(); @@ -24,6 +26,23 @@ pub fn Game() -> impl IntoView { let (card_clicked, set_card_clicked) = create_signal::(String::new()); provide_context::>(set_card_clicked); + // Clear everything on disconnect + create_effect(move |_| { + if !connected() { + set_game_id("".to_string()); + set_game_name("".to_string()); + set_game_host("".to_string()); + set_game_players(vec![]); + set_game_czar("".to_string()); + set_game_black(("".to_string(), 0u8)); + set_selected_cards_ordered(vec![]); + set_player_hand(vec![]); + set_player_white.update(|hand| { + hand.clear(); + }); + } + }); + // Handle incoming judge message create_effect(move |_| { judge_round.with(move |judge_round| {