use futures::channel::mpsc::UnboundedSender; use leptos::prelude::*; use trictrac_store::{CheckerMove, Jan}; use crate::app::{GameUiState, NetCommand}; use crate::trictrac::types::{PlayerAction, SerStage, SerTurnStage}; use super::board::Board; use super::score_panel::ScorePanel; #[allow(dead_code)] /// Returns (d0_used, d1_used) by matching each staged move's distance to a die. /// Falls back to position order for exit moves (distance doesn't match any die). fn matched_dice_used(staged: &[(u8, u8)], dice: (u8, u8)) -> (bool, bool) { let mut d0 = false; let mut d1 = false; for &(from, to) in staged { let dist = to.saturating_sub(from); // 0 for empty/same-field moves if !d0 && dist == dice.0 { d0 = true; } else if !d1 && dist == dice.1 { d1 = true; } else if !d0 { d0 = true; } else { d1 = true; } } (d0, d1) } fn jan_label(jan: &Jan) -> &'static str { match jan { Jan::FilledQuarter => "Rempli", Jan::TrueHitSmallJan => "Atteinte vraie (petit jan)", Jan::TrueHitBigJan => "Atteinte vraie (grand jan)", Jan::TrueHitOpponentCorner => "Atteinte coin adverse", Jan::FirstPlayerToExit => "Premier sorti", Jan::SixTables => "Six tables", Jan::TwoTables => "Deux tables", Jan::Mezeas => "Mezeas", Jan::FalseHitSmallJan => "Faux (petit jan)", Jan::FalseHitBigJan => "Faux (grand jan)", Jan::ContreTwoTables => "Contre deux tables", Jan::ContreMezeas => "Contre mezeas", Jan::HelplessMan => "Homme en route", } } #[component] pub fn GameScreen(state: GameUiState) -> impl IntoView { let vs = state.view_state.clone(); let player_id = state.player_id; let is_my_turn = vs.active_mp_player == Some(player_id); let is_move_stage = is_my_turn && matches!( vs.turn_stage, SerTurnStage::Move | SerTurnStage::HoldOrGoChoice ); // ── Staged move state ────────────────────────────────────────────────────── let selected_origin: RwSignal> = RwSignal::new(None); let staged_moves: RwSignal> = RwSignal::new(Vec::new()); // When both move slots are filled, send the action to the backend. let cmd_tx = use_context::>() .expect("UnboundedSender not found in context"); let cmd_tx_effect = cmd_tx.clone(); Effect::new(move |_| { let moves = staged_moves.get(); if moves.len() == 2 { let to_cm = |&(from, to): &(u8, u8)| { CheckerMove::new(from as usize, to as usize).unwrap_or_default() }; cmd_tx_effect .unbounded_send(NetCommand::Action(PlayerAction::Move( to_cm(&moves[0]), to_cm(&moves[1]), ))) .ok(); staged_moves.set(vec![]); selected_origin.set(None); } }); // ── Status text ──────────────────────────────────────────────────────────── let status = match &vs.stage { SerStage::Ended => "Game over".to_string(), SerStage::PreGame => "Waiting for opponent…".to_string(), SerStage::InGame => match (is_my_turn, &vs.turn_stage) { (true, SerTurnStage::RollDice) => "Your turn — roll the dice".to_string(), (true, SerTurnStage::HoldOrGoChoice) => "Hold or Go?".to_string(), (true, SerTurnStage::Move) => "Select move 1 of 2".to_string(), (true, _) => "Your turn".to_string(), (false, _) => "Opponent's turn".to_string(), }, }; let dice = vs.dice; // ── Action bar buttons ───────────────────────────────────────────────────── let cmd_tx2 = cmd_tx.clone(); let cmd_tx_quit = cmd_tx.clone(); let show_roll = is_my_turn && vs.turn_stage == SerTurnStage::RollDice; let show_hold_go = is_my_turn && vs.turn_stage == SerTurnStage::HoldOrGoChoice; view! {
{state.room_id} "Quit"
{move || { if is_move_stage { let n = staged_moves.get().len(); format!("Select move {} of 2", n + 1) } else { status.clone() } }} {(dice != (0, 0)).then(|| view! { "Dice: " {dice.0} " & " {dice.1} })}
{show_roll.then(|| view! { })} {show_hold_go.then(|| view! { })} {is_move_stage.then(|| view! { })}
{(!vs.dice_jans.is_empty()).then(|| { let rows: Vec<_> = vs.dice_jans.iter().map(|(jan, pts)| { let label = jan_label(jan); let pts_str = if *pts >= 0 { format!("+{}", pts) } else { format!("{}", pts) }; let row_class = if *pts >= 0 { "jan-row jan-positive" } else { "jan-row jan-negative" }; view! {
{label} {pts_str}
} }).collect(); view! {
{rows}
} })}
} }