use crate::components::cards::*; use crate::components::websocket::WebSocketContext; use leptos::*; use lib::*; use serde_json::to_string; use std::collections::HashMap; #[component] pub fn Game() -> impl IntoView { let websocket = expect_context::(); let game_meta = expect_context::>>(); let judge_round = expect_context::>>(); let (game_id, set_game_id) = create_signal("".to_string()); let (game_name, set_game_name) = create_signal("".to_string()); let (game_host, set_game_host) = create_signal("".to_string()); let (game_players, set_game_players) = create_signal(vec![]); let (game_czar, set_game_czar) = create_signal("".to_string()); let (game_black, set_game_black) = create_signal(("".to_string(), 0u8)); let (selected_cards_ordered, set_selected_cards_ordered) = create_signal::>(vec![]); let (player_hand, set_player_hand) = create_signal::>(vec![]); let (player_white, set_player_white) = create_signal::>(HashMap::new()); let (card_clicked, set_card_clicked) = create_signal::(String::new()); provide_context::>(set_card_clicked); // Handle incoming judge message create_effect(move |_| { judge_round.with(move |judge_round| { set_player_hand.update(|list| { list.clear(); }); set_player_white.update(|list| { list.clear(); }); set_card_clicked.update(|list| { list.clear(); }); set_selected_cards_ordered.update(|list| { list.clear(); }); // Load hand if let Some(judge) = judge_round { for cards in judge.cards_to_judge.clone() { for card in cards { set_player_white.update(|hand| { hand.insert(card.uuid.clone(), card.clone()); }); set_player_hand.update(|hand| { hand.push(card.uuid.clone()); }); } } } }); }); // Handle incoming state update create_effect(move |_| { if let Some(game) = game_meta() { // Clear in case of (re-)joining game set_card_clicked.update(|list| { list.clear(); }); set_selected_cards_ordered.update(|list| { list.clear(); }); set_player_hand.update(|list| { list.clear(); }); set_player_white.update(|list| { list.clear(); }); // Load meta set_game_id(game.uuid.clone()); set_game_name(game.name.clone()); set_game_host(game.host.clone()); set_game_players(game.players.clone()); set_game_czar(game.czar.clone()); set_game_black(game.black.clone()); // Load hand for card in game.white { set_player_white.update(|hand| { hand.insert(card.uuid.clone(), card.clone()); }); set_player_hand.update(|hand| { hand.push(card.uuid.clone()); }); } } }); // Move cards back and forth between hand and selected when clicked create_effect(move |_| { if let Some(card_index) = player_hand() .iter() .position(|card| *card == card_clicked()) { set_player_hand.update(|list| { list.remove(card_index); }); set_selected_cards_ordered.update(|list| { list.push(card_clicked()); }) } else if let Some(card_index) = selected_cards_ordered() .iter() .position(|card| *card == card_clicked()) { set_selected_cards_ordered.update(|list| { list.remove(card_index); }); set_player_hand.update(|list| { list.push(card_clicked()); }) } }); let submit_cards = move |_| { let tx = websocket.clone(); judge_round.with(move |judge| { let mut _message: Option = None; if judge.is_some() { _message = Some( to_string(&JudgeDecisionRequest { game_id: game_id(), winning_cards: selected_cards_ordered(), }) .unwrap(), ); } else { _message = Some( to_string(&PlayerMoveRequest { game_id: game_id(), card_ids: selected_cards_ordered(), }) .unwrap(), ); } if let Some(msg) = _message { tx.send(&msg); } }) }; view! {

Game

Name: {move || game_name()}

Host: {move || game_host()}

Czar: {move || game_czar()}

Players:

    {move || { game_players() .iter() .map(|player| view! {
  • {player}
  • }) .collect_view() }}
// Game cards // TODO: offset this correctly
// Card selection } } />
// Player cards
} } />
} }