mv test new game

This commit is contained in:
Adam 2024-08-01 20:32:49 -04:00
parent dd36a5f455
commit bbc48a6619
2 changed files with 26 additions and 28 deletions

View file

@ -1,31 +1,46 @@
use crate::components::websocket::WebSocketContext; use crate::components::websocket::WebSocketContext;
use leptos::*; use leptos::*;
use leptos_use::core::ConnectionReadyState; use leptos_use::core::ConnectionReadyState;
use lib::models::GamesUpdate; use lib::models::*;
use serde_json::to_string;
#[component] #[component]
pub fn Browser() -> impl IntoView { pub fn Browser() -> impl IntoView {
let websocket = expect_context::<WebSocketContext>(); let websocket = expect_context::<WebSocketContext>();
let game_update_context = expect_context::<ReadSignal<Option<GamesUpdate>>>(); let connected = move || websocket.ready_state.get() == ConnectionReadyState::Open;
let game_update_context = expect_context::<ReadSignal<Option<GamesUpdate>>>();
let (active_games, set_active_games) = create_signal::<Vec<String>>(vec![]); let (active_games, set_active_games) = create_signal::<Vec<String>>(vec![]);
let fake_new_game_request = NewGameRequest {
name: String::from("Ligma"),
host: Player {
name: String::from("Adam"),
role: PlayerRole::Host,
white: vec![],
black: vec![],
},
packs: vec![0],
};
// Game stuff
let new_game_test = move |_| {
(websocket.send)(&to_string(&fake_new_game_request).unwrap());
};
create_effect(move |_| { create_effect(move |_| {
game_update_context.with(move |games| { game_update_context.with(move |games| {
if let Some(games) = games { if let Some(games) = games {
set_active_games(games.games.clone()); set_active_games(games.games.clone());
logging::log!("{:#?}",active_games());
} }
}) })
}); });
// Clear games list on disconnect // Clear games list on disconnect
create_effect(move |_| { create_effect(move |_| {
websocket.ready_state.with(move |status| { if !connected() {
if *status == ConnectionReadyState::Closed {
set_active_games(vec![]); set_active_games(vec![]);
} }
})
}); });
view! { view! {
@ -34,6 +49,9 @@ pub fn Browser() -> impl IntoView {
<h2 class="text-2xl">Game Browser</h2> <h2 class="text-2xl">Game Browser</h2>
{move || active_games().into_iter().map(|n| view! { <li>{n}</li> }).collect_view()} {move || active_games().into_iter().map(|n| view! { <li>{n}</li> }).collect_view()}
</ul> </ul>
<button on:click=new_game_test disabled=move || !connected()>
"Test New Game"
</button>
</div> </div>
} }
} }

View file

@ -2,7 +2,6 @@ use crate::components::websocket::WebSocketContext;
use leptos::*; use leptos::*;
use leptos_use::core::ConnectionReadyState; use leptos_use::core::ConnectionReadyState;
use lib::models::*; use lib::models::*;
use serde_json::to_string;
#[component] #[component]
pub fn Debug() -> impl IntoView { pub fn Debug() -> impl IntoView {
@ -26,22 +25,6 @@ pub fn Debug() -> impl IntoView {
set_active_games(0); set_active_games(0);
}; };
let fake_new_game_request = NewGameRequest {
name: String::from("Ligma"),
host: Player {
name: String::from("Adam"),
role: PlayerRole::Host,
white: vec![],
black: vec![],
},
packs: vec![0],
};
// Game stuff
let new_game_test = move |_| {
(websocket.send)(&to_string(&fake_new_game_request).unwrap());
};
// Update server info -> move this to a new component // Update server info -> move this to a new component
create_effect(move |_| { create_effect(move |_| {
state_summary.with(move |state_summary| { state_summary.with(move |state_summary| {
@ -66,9 +49,6 @@ pub fn Debug() -> impl IntoView {
<button on:click=close_connection disabled=move || !connected()> <button on:click=close_connection disabled=move || !connected()>
"Disconnect" "Disconnect"
</button> </button>
<button on:click=new_game_test disabled=move || !connected()>
"Test New Game"
</button>
</div> </div>
</div> </div>
} }