fix(client_web): list points jans

This commit is contained in:
Henri Bourcereau 2026-03-27 16:29:53 +01:00
parent 6174bc16e6
commit 31ee129262
3 changed files with 127 additions and 33 deletions

View file

@ -3,7 +3,7 @@ use leptos::prelude::*;
use trictrac_store::{CheckerMove, Jan};
use crate::app::{GameUiState, NetCommand};
use crate::trictrac::types::{PlayerAction, SerStage, SerTurnStage};
use crate::trictrac::types::{JanEntry, PlayerAction, SerStage, SerTurnStage};
use super::board::Board;
use super::die::Die;
@ -52,6 +52,61 @@ fn jan_label(jan: &Jan) -> &'static str {
}
}
fn format_move_pair(m1: CheckerMove, m2: CheckerMove) -> String {
let fmt = |m: CheckerMove| -> String {
let (f, t) = (m.get_from(), m.get_to());
if f == 0 && t == 0 { "".to_string() }
else if t == 0 { format!("{f}") } // exit
else { format!("{f}{t}") }
};
format!("{} + {}", fmt(m1), fmt(m2))
}
fn jan_row(idx: usize, entry: JanEntry, expanded: RwSignal<Option<usize>>) -> impl IntoView {
let row_class = if entry.total >= 0 { "jan-row jan-positive" } else { "jan-row jan-negative" };
let label = jan_label(&entry.jan);
let double_tag = if entry.is_double { "double" } else { "simple" };
let ways_tag = format!("×{}", entry.ways);
let pts_str = if entry.total >= 0 { format!("+{}", entry.total) } else { format!("{}", entry.total) };
let can_expand = entry.ways > 1;
let moves = entry.moves.clone();
view! {
<div>
<div
class=row_class
class:jan-expandable=can_expand
on:click=move |_| {
if can_expand {
expanded.update(|s| {
*s = if *s == Some(idx) { None } else { Some(idx) };
});
}
}
>
<span class="jan-label">{label}</span>
<span class="jan-tag">{double_tag}</span>
<span class="jan-tag">{ways_tag}</span>
<span class="jan-pts">{pts_str}</span>
</div>
{can_expand.then(|| {
let move_lines: Vec<_> = moves.iter()
.map(|&(m1, m2)| {
let text = format_move_pair(m1, m2);
view! { <div class="jan-move-line">{text}</div> }
})
.collect();
view! {
<div class="jan-moves" class:hidden=move || expanded.get() != Some(idx)>
{move_lines}
</div>
}
})}
</div>
}
}
#[component]
pub fn GameScreen(state: GameUiState) -> impl IntoView {
let vs = state.view_state.clone();
@ -142,16 +197,9 @@ pub fn GameScreen(state: GameUiState) -> impl IntoView {
// ── Jan panel ────────────────────────────────────────────────────
{(!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! {
<div class=row_class>
<span class="jan-label">{label}</span>
<span class="jan-pts">{pts_str}</span>
</div>
}
let expanded: RwSignal<Option<usize>> = RwSignal::new(None);
let rows: Vec<_> = vs.dice_jans.iter().enumerate().map(|(i, entry)| {
jan_row(i, entry.clone(), expanded)
}).collect();
view! { <div class="jan-panel">{rows}</div> }
})}

View file

@ -39,9 +39,25 @@ pub struct ViewState {
pub scores: [PlayerScore; 2],
/// Last rolled dice values.
pub dice: (u8, u8),
/// Jans (scoring events) triggered by the last dice roll, with their point values.
/// Negative points indicate faux jans (scored against the active player).
pub dice_jans: Vec<(Jan, i8)>,
/// Jans (scoring events) triggered by the last dice roll.
pub dice_jans: Vec<JanEntry>,
}
/// One scoring event from a dice roll.
#[derive(Clone, PartialEq, Serialize, Deserialize, Debug)]
pub struct JanEntry {
pub jan: Jan,
/// True when the dice are doubles (both same value) — changes the point value.
/// Special case for HelplessMan: true when *both* dice are unplayable.
pub is_double: bool,
/// Number of distinct move pairs that produce this jan.
pub ways: usize,
/// Points per way (negative = scored against the active player).
pub points_per: i8,
/// Total = points_per × ways.
pub total: i8,
/// The move pairs that produce this jan (for move display).
pub moves: Vec<(CheckerMove, CheckerMove)>,
}
impl ViewState {
@ -108,23 +124,38 @@ impl ViewState {
.unwrap_or_else(|| PlayerScore { name: String::new(), points: 0, holes: 0 })
};
// Opponent's can_bredouille determines whether the active player scores double.
let opponent_store_id = if gs.active_player_id == host_store_id {
guest_store_id
} else {
host_store_id
};
let is_double = gs.players
.get(&opponent_store_id)
.map(|p| p.can_bredouille)
.unwrap_or(false);
// is_double for scoring: dice show the same value (both dice identical).
// Exception: HelplessMan uses a special rule (see below).
let dice_are_double = gs.dice.values.0 == gs.dice.values.1;
// Collect jans sorted by absolute point value descending for stable display order.
let mut dice_jans: Vec<(Jan, i8)> = gs.dice_jans
.keys()
.map(|jan| (jan.clone(), jan.get_points(is_double)))
// Build JanEntry list from the PossibleJans map.
let empty_move = CheckerMove::new(0, 0).unwrap_or_default();
let mut dice_jans: Vec<JanEntry> = gs.dice_jans
.iter()
.map(|(jan, moves)| {
// HelplessMan: is_double = true only when *both* dice are unplayable
// (the moves list contains a single (empty, empty) sentinel).
let is_double = if *jan == Jan::HelplessMan {
moves.first().map(|&(m1, m2)| m1 == empty_move && m2 == empty_move)
.unwrap_or(false)
} else {
dice_are_double
};
let points_per = jan.get_points(is_double);
let ways = moves.len();
let total = points_per.saturating_mul(ways as i8);
JanEntry {
jan: jan.clone(),
is_double,
ways,
points_per,
total,
moves: moves.clone(),
}
})
.collect();
dice_jans.sort_by_key(|(_, pts)| std::cmp::Reverse(*pts));
// Sort: highest total first, most-negative last.
dice_jans.sort_by_key(|e| std::cmp::Reverse(e.total));
ViewState {
board,