clear game state on disconnect

This commit is contained in:
Adam 2024-08-21 22:25:33 -04:00
parent 69bc1714f4
commit 77ac313070

View file

@ -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::<WebSocketContext>();
let connected = move || websocket.ready_state.get() == ConnectionReadyState::Open;
let game_meta = expect_context::<ReadSignal<Option<GameStateMeta>>>();
let judge_round = expect_context::<ReadSignal<Option<JudgeRound>>>();
@ -24,6 +26,23 @@ pub fn Game() -> impl IntoView {
let (card_clicked, set_card_clicked) = create_signal::<String>(String::new());
provide_context::<WriteSignal<String>>(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| {