refactor: persist replay/save moves as KlondikeInstruction, not pile coords (#89)
Pile-position types (Tableau, Foundation, KlondikePile, KlondikePileStack) are runtime-only and have no serde upstream. Per Rhys's guidance, the persistence layer now stores the moves (KlondikeInstruction) rather than board coordinates, decoding back to runtime pile positions on demand. Core / data: - game_state: instruction_history() -> Vec<KlondikeInstruction>; add instruction_to_piles() and apply_instruction(); drop AnyInstruction. - klondike_adapter: delete the entire Saved* serde mirror section (SavedTableau/Foundation/SkipCards/KlondikePile/TableauStack/ KlondikePileStack/DstFoundation/DstTableau/SavedInstruction). - replay: drop the bespoke ReplayMove serde mirror; Replay.moves is now Vec<KlondikeInstruction>; REPLAY_SCHEMA_VERSION 2 -> 3. - storage: game_state save format v3 rejected (v4/v5 only). Engine / wasm consumers: - record via KlondikeInstruction (stock click = RotateStock). - playback decodes each instruction to (from, to, count) against the live state via instruction_to_piles, then fires the canonical event; undecodable instructions are skipped with a warning, never panic. - remove all use solitaire_data::ReplayMove and Saved* imports. Workspace check, clippy -D warnings, and the full test suite all pass. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -1,10 +1,8 @@
|
||||
use super::ReplayPlaybackState;
|
||||
use chrono::Datelike;
|
||||
use solitaire_core::{Foundation, KlondikePile, Tableau};
|
||||
use solitaire_core::{Card, Rank, Suit};
|
||||
use solitaire_core::game_state::GameState;
|
||||
use solitaire_core::klondike_adapter::SavedKlondikePile;
|
||||
use solitaire_data::ReplayMove;
|
||||
use solitaire_core::{Card, Rank, Suit};
|
||||
use solitaire_core::{Foundation, KlondikeInstruction, KlondikePile, Tableau};
|
||||
|
||||
/// Pure helper — formats the `GAME #YYYY-DDD` caption for the given
|
||||
/// state. Returns `None` for `Inactive` / `Completed` (the replay is
|
||||
@@ -60,12 +58,6 @@ pub(crate) fn format_pile(p: &KlondikePile) -> String {
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn format_saved_pile(p: &SavedKlondikePile) -> String {
|
||||
KlondikePile::try_from(*p)
|
||||
.map(|pile| format_pile(&pile))
|
||||
.unwrap_or_else(|_| "unknown pile".to_string())
|
||||
}
|
||||
|
||||
fn foundation_number(foundation: Foundation) -> u8 {
|
||||
match foundation {
|
||||
Foundation::Foundation1 => 1,
|
||||
@@ -87,20 +79,36 @@ fn tableau_number(tableau: Tableau) -> u8 {
|
||||
}
|
||||
}
|
||||
|
||||
/// Pure helper — formats a [`ReplayMove`] as the body of a
|
||||
/// move-log row. `StockClick` reads as `"stock cycle"`; `Move`
|
||||
/// reads as `"{from} → {to}"` using [`format_pile`] for both
|
||||
/// endpoints. The `count` field is omitted from the row body —
|
||||
/// at row scale it adds visual noise without meaningful
|
||||
/// Pure helper — formats a [`KlondikeInstruction`] as the body of a
|
||||
/// move-log row. `RotateStock` reads as `"stock cycle"`; a `Dst*`
|
||||
/// instruction reads as `"{from} → {to}"` using [`format_pile`] for
|
||||
/// each nameable endpoint. The card count is omitted from the row
|
||||
/// body — at row scale it adds visual noise without meaningful
|
||||
/// information for the typical 1-card moves.
|
||||
pub(crate) fn format_move_body(m: &ReplayMove) -> String {
|
||||
match m {
|
||||
ReplayMove::StockClick => "stock cycle".to_string(),
|
||||
ReplayMove::Move { from, to, .. } => {
|
||||
///
|
||||
/// The destination pile is always recoverable directly from the
|
||||
/// instruction. The source pile is shown when it is statically
|
||||
/// nameable (a `DstFoundation` carries a [`KlondikePile`] source);
|
||||
/// a `DstTableau`'s source is the runtime-only `KlondikePileStack`
|
||||
/// type — not re-exported across the `solitaire_core` boundary and so
|
||||
/// not pattern-matchable here — so its row renders `"→ {to}"` without
|
||||
/// a leading source label. Faithful full-coordinate decoding lives in
|
||||
/// [`GameState::instruction_to_piles`] on the playback path; the
|
||||
/// move-log is a display-only digest.
|
||||
pub(crate) fn format_move_body(instruction: &KlondikeInstruction) -> String {
|
||||
match instruction {
|
||||
KlondikeInstruction::RotateStock => "stock cycle".to_string(),
|
||||
KlondikeInstruction::DstFoundation(dst) => {
|
||||
format!(
|
||||
"{} \u{2192} {}",
|
||||
format_saved_pile(from),
|
||||
format_saved_pile(to)
|
||||
format_pile(&dst.src),
|
||||
format_pile(&KlondikePile::Foundation(dst.foundation))
|
||||
)
|
||||
}
|
||||
KlondikeInstruction::DstTableau(dst) => {
|
||||
format!(
|
||||
"\u{2192} {}",
|
||||
format_pile(&KlondikePile::Tableau(dst.tableau))
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@ use crate::replay_playback::{
|
||||
ReplayPlaybackState, step_backwards_replay_playback, step_replay_playback,
|
||||
stop_replay_playback, toggle_pause_replay_playback,
|
||||
};
|
||||
use crate::resources::GameStateResource;
|
||||
|
||||
/// Per-arrow-key time-since-last-fire accumulators that drive the
|
||||
/// continuous-scrub repeat behaviour for held arrow keys. Each
|
||||
@@ -1033,6 +1034,7 @@ pub(crate) fn handle_pause_button(
|
||||
/// guard lives inside `step_replay_playback`.
|
||||
pub(crate) fn handle_step_button(
|
||||
mut state: ResMut<ReplayPlaybackState>,
|
||||
game: Option<Res<GameStateResource>>,
|
||||
mut moves_writer: MessageWriter<MoveRequestEvent>,
|
||||
mut draws_writer: MessageWriter<DrawRequestEvent>,
|
||||
buttons: Query<&Interaction, (With<ReplayStepButton>, Changed<Interaction>)>,
|
||||
@@ -1040,7 +1042,12 @@ pub(crate) fn handle_step_button(
|
||||
if !buttons.iter().any(|i| *i == Interaction::Pressed) {
|
||||
return;
|
||||
}
|
||||
step_replay_playback(&mut state, &mut moves_writer, &mut draws_writer);
|
||||
step_replay_playback(
|
||||
&mut state,
|
||||
game.as_deref(),
|
||||
&mut moves_writer,
|
||||
&mut draws_writer,
|
||||
);
|
||||
}
|
||||
|
||||
/// Repaints the Pause / Resume button's label whenever
|
||||
@@ -1112,6 +1119,7 @@ pub(crate) fn handle_pause_keyboard(
|
||||
pub(crate) fn handle_arrow_keyboard(
|
||||
keys: Option<Res<ButtonInput<KeyCode>>>,
|
||||
time: Res<Time>,
|
||||
game: Option<Res<GameStateResource>>,
|
||||
mut hold: ResMut<ReplayScrubKeyHold>,
|
||||
mut state: ResMut<ReplayPlaybackState>,
|
||||
mut moves_writer: MessageWriter<MoveRequestEvent>,
|
||||
@@ -1136,12 +1144,22 @@ pub(crate) fn handle_arrow_keyboard(
|
||||
// Right (forward step) — initial press fires immediately;
|
||||
// held repeats fire when the accumulator crosses the interval.
|
||||
if keys.just_pressed(KeyCode::ArrowRight) {
|
||||
step_replay_playback(&mut state, &mut moves_writer, &mut draws_writer);
|
||||
step_replay_playback(
|
||||
&mut state,
|
||||
game.as_deref(),
|
||||
&mut moves_writer,
|
||||
&mut draws_writer,
|
||||
);
|
||||
hold.right_held_secs = 0.0;
|
||||
} else if keys.pressed(KeyCode::ArrowRight) {
|
||||
hold.right_held_secs += dt;
|
||||
if hold.right_held_secs >= SCRUB_REPEAT_INTERVAL_SECS {
|
||||
step_replay_playback(&mut state, &mut moves_writer, &mut draws_writer);
|
||||
step_replay_playback(
|
||||
&mut state,
|
||||
game.as_deref(),
|
||||
&mut moves_writer,
|
||||
&mut draws_writer,
|
||||
);
|
||||
hold.right_held_secs = 0.0;
|
||||
}
|
||||
} else {
|
||||
|
||||
@@ -1,13 +1,12 @@
|
||||
use super::*;
|
||||
use chrono::NaiveDate;
|
||||
use solitaire_core::{Foundation, KlondikePile, Tableau};
|
||||
use solitaire_core::{Rank, Suit};
|
||||
use solitaire_core::{DrawStockConfig, game_state::GameMode};
|
||||
use solitaire_core::klondike_adapter::{SavedKlondikePile, SavedTableau};
|
||||
use solitaire_data::{Replay, ReplayMove};
|
||||
use solitaire_core::{Foundation, KlondikeInstruction, KlondikePile, Tableau};
|
||||
use solitaire_core::{Rank, Suit};
|
||||
use solitaire_data::Replay;
|
||||
|
||||
/// Build a minimal but well-formed [`Replay`] with `move_count` no-op
|
||||
/// `StockClick` entries. Tests only ever read `replay.moves.len()`
|
||||
/// `RotateStock` entries. Tests only ever read `replay.moves.len()`
|
||||
/// (denominator of the progress indicator), so the move kind is
|
||||
/// irrelevant beyond producing the right count.
|
||||
fn synthetic_replay(move_count: usize) -> Replay {
|
||||
@@ -18,7 +17,9 @@ fn synthetic_replay(move_count: usize) -> Replay {
|
||||
120,
|
||||
1_000,
|
||||
NaiveDate::from_ymd_opt(2026, 5, 2).expect("valid date"),
|
||||
(0..move_count).map(|_| ReplayMove::StockClick).collect(),
|
||||
(0..move_count)
|
||||
.map(|_| KlondikeInstruction::RotateStock)
|
||||
.collect(),
|
||||
)
|
||||
}
|
||||
|
||||
@@ -1123,20 +1124,17 @@ fn format_pile_uses_one_indexed_lowercase_names() {
|
||||
);
|
||||
}
|
||||
|
||||
/// Move-body formatter renders `StockClick` as a label and
|
||||
/// `Move` as a `from → to` arrow. The `count` field is
|
||||
/// deliberately omitted — at row scale it adds noise.
|
||||
/// Move-body formatter renders `RotateStock` as a label. The
|
||||
/// `Dst*` variants render as a `→ to` arrow, but their pile-stack
|
||||
/// source types are runtime-only and not constructible from this
|
||||
/// crate, so only the stock-cycle label is asserted here; the
|
||||
/// arrow path is exercised end-to-end through the move-log
|
||||
/// integration tests.
|
||||
#[test]
|
||||
fn format_move_body_handles_both_variants() {
|
||||
assert_eq!(format_move_body(&ReplayMove::StockClick), "stock cycle");
|
||||
fn format_move_body_handles_stock_cycle() {
|
||||
assert_eq!(
|
||||
format_move_body(&ReplayMove::Move {
|
||||
from: SavedKlondikePile::Stock,
|
||||
to: SavedKlondikePile::Tableau(SavedTableau(4)),
|
||||
count: 1,
|
||||
}),
|
||||
"waste \u{2192} tableau 5",
|
||||
"Move variant must render as `{{from}} → {{to}}` with 1-indexed pile numbers",
|
||||
format_move_body(&KlondikeInstruction::RotateStock),
|
||||
"stock cycle"
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -8,8 +8,7 @@ use super::*;
|
||||
use crate::layout::LayoutResource;
|
||||
use crate::replay_playback::ReplayPlaybackState;
|
||||
use crate::resources::GameStateResource;
|
||||
use solitaire_core::KlondikePile;
|
||||
use solitaire_data::ReplayMove;
|
||||
use solitaire_core::{KlondikeInstruction, KlondikePile};
|
||||
|
||||
/// Overwrites the banner label whenever the resource changes — covers the
|
||||
/// `Playing → Completed` transition by swapping "▌ replay" for
|
||||
@@ -85,9 +84,15 @@ pub(crate) fn update_floating_progress_chip(
|
||||
// the most-recently-applied move sits at `cursor - 1`.
|
||||
let dest_pile = match state.as_ref() {
|
||||
ReplayPlaybackState::Playing { replay, cursor, .. } if *cursor > 0 => {
|
||||
// The destination pile is recoverable directly from the
|
||||
// instruction — no live state needed. `RotateStock` has no
|
||||
// destination (the chip hides over the stock pile).
|
||||
match &replay.moves[cursor - 1] {
|
||||
ReplayMove::Move { to, .. } => Some(*to),
|
||||
ReplayMove::StockClick => None,
|
||||
KlondikeInstruction::DstFoundation(dst) => {
|
||||
Some(KlondikePile::Foundation(dst.foundation))
|
||||
}
|
||||
KlondikeInstruction::DstTableau(dst) => Some(KlondikePile::Tableau(dst.tableau)),
|
||||
KlondikeInstruction::RotateStock => None,
|
||||
}
|
||||
}
|
||||
_ => None,
|
||||
@@ -95,8 +100,7 @@ pub(crate) fn update_floating_progress_chip(
|
||||
|
||||
let Some(world_pos) = dest_pile
|
||||
.as_ref()
|
||||
.and_then(|p| KlondikePile::try_from(*p).ok())
|
||||
.and_then(|p| layout.0.pile_positions.get(&p).copied())
|
||||
.and_then(|p| layout.0.pile_positions.get(p).copied())
|
||||
else {
|
||||
// Nothing to point at — hide every chip and exit.
|
||||
for (_, mut visibility, _) in chips.iter_mut() {
|
||||
|
||||
Reference in New Issue
Block a user