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:
@@ -1393,8 +1393,8 @@ mod tests {
|
||||
|
||||
use crate::replay_playback::ReplayPlaybackState;
|
||||
use chrono::NaiveDate;
|
||||
use solitaire_core::{DrawStockConfig, game_state::GameMode};
|
||||
use solitaire_data::{Replay, ReplayMove};
|
||||
use solitaire_core::{DrawStockConfig, KlondikeInstruction, game_state::GameMode};
|
||||
use solitaire_data::Replay;
|
||||
|
||||
/// Headless app variant that injects a default `ReplayPlaybackState`
|
||||
/// directly (no `ReplayPlaybackPlugin`) so we can drive the resource
|
||||
@@ -1414,7 +1414,7 @@ mod tests {
|
||||
10,
|
||||
100,
|
||||
NaiveDate::from_ymd_opt(2026, 5, 5).expect("valid date"),
|
||||
vec![ReplayMove::StockClick],
|
||||
vec![KlondikeInstruction::RotateStock],
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
@@ -15,11 +15,11 @@ use bevy::tasks::{AsyncComputeTaskPool, Task, futures_lite::future};
|
||||
use bevy::window::AppLifecycle;
|
||||
use solitaire_core::KlondikePile;
|
||||
use solitaire_core::{DrawStockConfig, game_state::{GameMode, GameState}};
|
||||
use solitaire_core::{DEFAULT_SOLVE_MOVES_BUDGET, DEFAULT_SOLVE_STATES_BUDGET};
|
||||
use solitaire_core::{DEFAULT_SOLVE_MOVES_BUDGET, DEFAULT_SOLVE_STATES_BUDGET, KlondikeInstruction};
|
||||
#[allow(deprecated)]
|
||||
use solitaire_data::latest_replay_path;
|
||||
use solitaire_data::{
|
||||
Replay, ReplayMove, SOLVER_DEAL_RETRY_CAP, append_replay_to_history, delete_game_state_at,
|
||||
Replay, SOLVER_DEAL_RETRY_CAP, append_replay_to_history, delete_game_state_at,
|
||||
game_state_file_path, load_game_state_from, migrate_legacy_latest_replay, replay_history_path,
|
||||
save_game_state_to,
|
||||
};
|
||||
@@ -105,18 +105,21 @@ pub struct RestoreContinueButton;
|
||||
#[derive(Component, Debug)]
|
||||
pub struct RestoreNewGameButton;
|
||||
|
||||
/// In-memory accumulator for [`ReplayMove`] entries during the current
|
||||
/// game. Cleared on every new-game start; frozen into a [`Replay`] and
|
||||
/// flushed to disk by [`record_replay_on_win`] when the player wins.
|
||||
/// In-memory accumulator for [`KlondikeInstruction`] entries during the
|
||||
/// current game. Cleared on every new-game start; frozen into a [`Replay`]
|
||||
/// and flushed to disk by [`record_replay_on_win`] when the player wins.
|
||||
///
|
||||
/// Recording captures only successful state-mutating events the player
|
||||
/// drove (`MoveRequestEvent`, `DrawRequestEvent`). `UndoRequestEvent` is
|
||||
/// intentionally not recorded — see [`solitaire_data::replay`] for the
|
||||
/// design rationale.
|
||||
/// design rationale. Each entry is the atomic player input as a
|
||||
/// [`KlondikeInstruction`] (a stock click is
|
||||
/// [`KlondikeInstruction::RotateStock`]); pile-position types are
|
||||
/// runtime-only and never persisted.
|
||||
#[derive(Resource, Debug, Default, Clone)]
|
||||
pub struct RecordingReplay {
|
||||
/// Ordered list of moves applied so far this game.
|
||||
pub moves: Vec<ReplayMove>,
|
||||
/// Ordered list of instructions applied so far this game.
|
||||
pub moves: Vec<KlondikeInstruction>,
|
||||
}
|
||||
|
||||
impl RecordingReplay {
|
||||
@@ -851,7 +854,7 @@ fn handle_draw(
|
||||
// the click happens — re-executing on the same starting
|
||||
// deal produces the same effect, so the input alone is
|
||||
// sufficient to recover the move on playback.
|
||||
recording.moves.push(ReplayMove::StockClick);
|
||||
recording.moves.push(KlondikeInstruction::RotateStock);
|
||||
changed.write(StateChangedEvent);
|
||||
}
|
||||
Err(e) => warn!("draw rejected: {e}"),
|
||||
@@ -889,11 +892,17 @@ fn handle_move(
|
||||
// Record the move in the in-flight replay buffer. Done
|
||||
// first so the entry is captured even if a subsequent
|
||||
// event-write or pile-lookup happens to bail out below.
|
||||
recording.moves.push(ReplayMove::Move {
|
||||
from: ev.from.into(),
|
||||
to: ev.to.into(),
|
||||
count: ev.count,
|
||||
});
|
||||
// `move_cards` resolved the pile coordinates to a
|
||||
// `KlondikeInstruction` and pushed it onto the session
|
||||
// history; recover that exact instruction from the tail
|
||||
// (no clone — the instruction is `Copy`). Pile-position
|
||||
// types are runtime-only, so we persist the instruction
|
||||
// rather than the (from, to, count) triple.
|
||||
if let Some(instruction) =
|
||||
game.0.session().history().last().map(|s| *s.instruction())
|
||||
{
|
||||
recording.moves.push(instruction);
|
||||
}
|
||||
// Fire flip event if the candidate card is now face-up.
|
||||
if let Some(fcard) = flip_candidate
|
||||
&& pile_cards(&game.0, &ev.from)
|
||||
@@ -1301,7 +1310,6 @@ fn save_game_state_on_exit(
|
||||
mod tests {
|
||||
use super::*;
|
||||
use solitaire_core::{Foundation, KlondikePile, Tableau};
|
||||
use solitaire_core::klondike_adapter::{SavedKlondikePile, SavedTableau};
|
||||
|
||||
/// Build a minimal headless `App` with just `GamePlugin` installed.
|
||||
/// Disables persistence and overrides the seed so tests are deterministic
|
||||
@@ -2102,7 +2110,7 @@ mod tests {
|
||||
1,
|
||||
"only the draw is recorded; the undo does not erase it nor add a new entry",
|
||||
);
|
||||
assert!(matches!(recording.moves[0], ReplayMove::StockClick));
|
||||
assert!(matches!(recording.moves[0], KlondikeInstruction::RotateStock));
|
||||
}
|
||||
|
||||
/// Starting a new game wipes the recording so the next deal begins
|
||||
@@ -2154,16 +2162,16 @@ mod tests {
|
||||
let mut app = test_app(7654);
|
||||
app.insert_resource(ReplayPath(Some(path.clone())));
|
||||
|
||||
// Push two recorded moves manually so we can verify they survive
|
||||
// the freeze/save round-trip without having to drive a real win.
|
||||
// Push two recorded instructions manually so we can verify they
|
||||
// survive the freeze/save round-trip without having to drive a
|
||||
// real win. Both are `RotateStock` — the only instruction
|
||||
// constructible without the runtime-only `klondike` pile-stack
|
||||
// types (which the engine intentionally does not depend on); the
|
||||
// round-trip shape is identical for any instruction variant.
|
||||
{
|
||||
let mut recording = app.world_mut().resource_mut::<RecordingReplay>();
|
||||
recording.moves.push(ReplayMove::StockClick);
|
||||
recording.moves.push(ReplayMove::Move {
|
||||
from: SavedKlondikePile::Stock,
|
||||
to: SavedKlondikePile::Tableau(SavedTableau(2)),
|
||||
count: 1,
|
||||
});
|
||||
recording.moves.push(KlondikeInstruction::RotateStock);
|
||||
recording.moves.push(KlondikeInstruction::RotateStock);
|
||||
}
|
||||
|
||||
// Fire the win event the engine emits when the last foundation
|
||||
@@ -2197,15 +2205,8 @@ mod tests {
|
||||
"time_seconds must come from the win event"
|
||||
);
|
||||
assert_eq!(loaded.moves.len(), 2, "every recorded move must round-trip");
|
||||
assert!(matches!(loaded.moves[0], ReplayMove::StockClick));
|
||||
match &loaded.moves[1] {
|
||||
ReplayMove::Move { from, to, count } => {
|
||||
assert_eq!(*from, SavedKlondikePile::Stock);
|
||||
assert_eq!(*to, SavedKlondikePile::Tableau(SavedTableau(2)));
|
||||
assert_eq!(*count, 1);
|
||||
}
|
||||
other => panic!("second entry must be a Move, got {other:?}"),
|
||||
}
|
||||
assert!(matches!(loaded.moves[0], KlondikeInstruction::RotateStock));
|
||||
assert!(matches!(loaded.moves[1], KlondikeInstruction::RotateStock));
|
||||
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
let _ = std::fs::remove_file(&path);
|
||||
@@ -2229,7 +2230,7 @@ mod tests {
|
||||
{
|
||||
let mut recording = app.world_mut().resource_mut::<RecordingReplay>();
|
||||
recording.moves.clear();
|
||||
recording.moves.push(ReplayMove::StockClick);
|
||||
recording.moves.push(KlondikeInstruction::RotateStock);
|
||||
}
|
||||
app.world_mut().write_message(GameWonEvent {
|
||||
score: 100,
|
||||
@@ -2241,8 +2242,8 @@ mod tests {
|
||||
{
|
||||
let mut recording = app.world_mut().resource_mut::<RecordingReplay>();
|
||||
recording.moves.clear();
|
||||
recording.moves.push(ReplayMove::StockClick);
|
||||
recording.moves.push(ReplayMove::StockClick);
|
||||
recording.moves.push(KlondikeInstruction::RotateStock);
|
||||
recording.moves.push(KlondikeInstruction::RotateStock);
|
||||
}
|
||||
app.world_mut().write_message(GameWonEvent {
|
||||
score: 200,
|
||||
|
||||
@@ -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() {
|
||||
|
||||
@@ -40,8 +40,8 @@
|
||||
//! flag is threaded through, no every-callsite gate is added.
|
||||
|
||||
use bevy::prelude::*;
|
||||
use solitaire_core::KlondikePile;
|
||||
use solitaire_data::{Replay, ReplayMove};
|
||||
use solitaire_core::KlondikeInstruction;
|
||||
use solitaire_data::Replay;
|
||||
|
||||
use crate::events::{DrawRequestEvent, MoveRequestEvent, StateChangedEvent, UndoRequestEvent};
|
||||
use crate::game_plugin::{GameMutation, RecordingReplay};
|
||||
@@ -94,7 +94,7 @@ pub const REPLAY_COMPLETION_LINGER_SECS: f32 = 5.0;
|
||||
/// replay's recorded deal.
|
||||
/// 3. The tick system [`tick_replay_playback`] advances `cursor` once
|
||||
/// per [`REPLAY_MOVE_INTERVAL_SECS`] and fires the canonical event
|
||||
/// for each [`ReplayMove`].
|
||||
/// for each [`KlondikeInstruction`].
|
||||
/// 4. When `cursor == replay.moves.len()`, the state transitions to
|
||||
/// [`Completed`](Self::Completed). It lingers for
|
||||
/// [`REPLAY_COMPLETION_LINGER_SECS`] (driven by
|
||||
@@ -251,6 +251,7 @@ pub fn toggle_pause_replay_playback(state: &mut ResMut<ReplayPlaybackState>) ->
|
||||
/// normal advance loop takes.
|
||||
pub fn step_replay_playback(
|
||||
state: &mut ResMut<ReplayPlaybackState>,
|
||||
game: Option<&GameStateResource>,
|
||||
moves_writer: &mut MessageWriter<MoveRequestEvent>,
|
||||
draws_writer: &mut MessageWriter<DrawRequestEvent>,
|
||||
) -> bool {
|
||||
@@ -266,31 +267,49 @@ pub fn step_replay_playback(
|
||||
if *cursor >= replay.moves.len() {
|
||||
return false;
|
||||
}
|
||||
match &replay.moves[*cursor] {
|
||||
ReplayMove::Move { from, to, count } => {
|
||||
let (Ok(from), Ok(to)) = (KlondikePile::try_from(*from), KlondikePile::try_from(*to))
|
||||
else {
|
||||
warn!(
|
||||
"skipping replay move with invalid pile encoding at cursor {}",
|
||||
*cursor
|
||||
);
|
||||
*cursor += 1;
|
||||
return false;
|
||||
};
|
||||
moves_writer.write(MoveRequestEvent {
|
||||
from,
|
||||
to,
|
||||
count: *count,
|
||||
});
|
||||
}
|
||||
ReplayMove::StockClick => {
|
||||
draws_writer.write(DrawRequestEvent);
|
||||
}
|
||||
}
|
||||
let instruction = replay.moves[*cursor];
|
||||
dispatch_instruction(instruction, *cursor, game, moves_writer, draws_writer);
|
||||
*cursor += 1;
|
||||
true
|
||||
}
|
||||
|
||||
/// Translates one recorded [`KlondikeInstruction`] into the canonical
|
||||
/// engine event that drives the live animation pipeline.
|
||||
///
|
||||
/// `RotateStock` fires a [`DrawRequestEvent`]; a `Dst*` instruction is
|
||||
/// decoded back to its runtime `(from, to, count)` pile coordinates via
|
||||
/// [`GameState::instruction_to_piles`] against the *current* live state
|
||||
/// (decoded before the event mutates it, so the source pile's face-up
|
||||
/// run length is the one in effect when the move applies) and fires a
|
||||
/// [`MoveRequestEvent`]. A decode that returns `None` (e.g. a malformed
|
||||
/// instruction loaded from disk) is skipped with a warning rather than
|
||||
/// panicking — the cursor still advances so playback never stalls.
|
||||
///
|
||||
/// `game` is `None` only in headless fixtures that install no
|
||||
/// [`GameStateResource`]; in that case only `RotateStock` (which needs
|
||||
/// no live state) is dispatched and `Dst*` instructions are skipped.
|
||||
fn dispatch_instruction(
|
||||
instruction: KlondikeInstruction,
|
||||
cursor: usize,
|
||||
game: Option<&GameStateResource>,
|
||||
moves_writer: &mut MessageWriter<MoveRequestEvent>,
|
||||
draws_writer: &mut MessageWriter<DrawRequestEvent>,
|
||||
) {
|
||||
match instruction {
|
||||
KlondikeInstruction::RotateStock => {
|
||||
draws_writer.write(DrawRequestEvent);
|
||||
}
|
||||
_ => match game.and_then(|g| g.0.instruction_to_piles(instruction)) {
|
||||
Some((from, to, count)) => {
|
||||
moves_writer.write(MoveRequestEvent { from, to, count });
|
||||
}
|
||||
None => {
|
||||
warn!("skipping replay move that did not decode to piles at cursor {cursor}");
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
/// Steps the replay **backwards** by exactly one move while paused.
|
||||
///
|
||||
/// Strategy: the live game's undo system is the source of truth for
|
||||
@@ -355,6 +374,7 @@ pub fn step_backwards_replay_playback(
|
||||
fn tick_replay_playback(
|
||||
time: Res<Time>,
|
||||
settings: Option<Res<SettingsResource>>,
|
||||
game: Option<Res<GameStateResource>>,
|
||||
mut state: ResMut<ReplayPlaybackState>,
|
||||
mut moves_writer: MessageWriter<MoveRequestEvent>,
|
||||
mut draws_writer: MessageWriter<DrawRequestEvent>,
|
||||
@@ -378,27 +398,14 @@ fn tick_replay_playback(
|
||||
if !*paused {
|
||||
*secs_to_next -= dt;
|
||||
while *secs_to_next <= 0.0 && *cursor < replay.moves.len() {
|
||||
match &replay.moves[*cursor] {
|
||||
ReplayMove::Move { from, to, count } => {
|
||||
if let (Ok(from), Ok(to)) =
|
||||
(KlondikePile::try_from(*from), KlondikePile::try_from(*to))
|
||||
{
|
||||
moves_writer.write(MoveRequestEvent {
|
||||
from,
|
||||
to,
|
||||
count: *count,
|
||||
});
|
||||
} else {
|
||||
warn!(
|
||||
"skipping replay move with invalid pile encoding at cursor {}",
|
||||
*cursor
|
||||
);
|
||||
}
|
||||
}
|
||||
ReplayMove::StockClick => {
|
||||
draws_writer.write(DrawRequestEvent);
|
||||
}
|
||||
}
|
||||
let instruction = replay.moves[*cursor];
|
||||
dispatch_instruction(
|
||||
instruction,
|
||||
*cursor,
|
||||
game.as_deref(),
|
||||
&mut moves_writer,
|
||||
&mut draws_writer,
|
||||
);
|
||||
*cursor += 1;
|
||||
*secs_to_next += interval;
|
||||
}
|
||||
@@ -555,9 +562,8 @@ mod tests {
|
||||
use crate::game_plugin::GamePlugin;
|
||||
use bevy::time::TimeUpdateStrategy;
|
||||
use chrono::NaiveDate;
|
||||
use solitaire_core::{KlondikePile, Tableau};
|
||||
use solitaire_core::KlondikeInstruction;
|
||||
use solitaire_core::{DrawStockConfig, game_state::GameMode};
|
||||
use solitaire_core::klondike_adapter::{SavedKlondikePile, SavedTableau};
|
||||
use std::time::Duration;
|
||||
|
||||
/// Builds a headless `App` with `MinimalPlugins`, `GamePlugin`, and
|
||||
@@ -592,9 +598,12 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
/// A 3-move replay covering both `Move` and `StockClick` variants.
|
||||
/// Seed 12345 is arbitrary — the test asserts on event counts and
|
||||
/// move shapes, not on board positions.
|
||||
/// A 3-move replay of `RotateStock` inputs. Pile-position types are
|
||||
/// runtime-only and intentionally not constructible from the engine
|
||||
/// crate, so a `Dst*` fixture can't be hand-built here; `RotateStock`
|
||||
/// exercises the dispatch path (it fires a `DrawRequestEvent` without
|
||||
/// needing a live state to decode piles). Seed 12345 is arbitrary —
|
||||
/// the test asserts on event counts, not board positions.
|
||||
fn sample_replay_three_moves() -> Replay {
|
||||
Replay::new(
|
||||
12345,
|
||||
@@ -604,13 +613,9 @@ mod tests {
|
||||
500,
|
||||
NaiveDate::from_ymd_opt(2026, 5, 5).expect("valid date"),
|
||||
vec![
|
||||
ReplayMove::StockClick,
|
||||
ReplayMove::Move {
|
||||
from: SavedKlondikePile::Stock,
|
||||
to: SavedKlondikePile::Tableau(SavedTableau(3)),
|
||||
count: 1,
|
||||
},
|
||||
ReplayMove::StockClick,
|
||||
KlondikeInstruction::RotateStock,
|
||||
KlondikeInstruction::RotateStock,
|
||||
KlondikeInstruction::RotateStock,
|
||||
],
|
||||
)
|
||||
}
|
||||
@@ -748,20 +753,17 @@ mod tests {
|
||||
let captured_moves = app.world().resource::<CapturedMoves>();
|
||||
let captured_draws = app.world().resource::<CapturedDraws>();
|
||||
|
||||
// Sample replay: StockClick, Move { Waste -> Tableau(3), 1 }, StockClick.
|
||||
// Sample replay: three `RotateStock` inputs — each dispatches a
|
||||
// `DrawRequestEvent` and never a `MoveRequestEvent`.
|
||||
assert_eq!(
|
||||
captured_draws.0, 2,
|
||||
"expected 2 DrawRequestEvent (two StockClicks)",
|
||||
captured_draws.0, 3,
|
||||
"expected 3 DrawRequestEvent (one per RotateStock)",
|
||||
);
|
||||
assert_eq!(
|
||||
captured_moves.0.len(),
|
||||
1,
|
||||
"expected 1 MoveRequestEvent (the single Move variant)",
|
||||
0,
|
||||
"RotateStock inputs must not produce MoveRequestEvent",
|
||||
);
|
||||
let m = &captured_moves.0[0];
|
||||
assert!(matches!(m.from, KlondikePile::Stock));
|
||||
assert!(matches!(m.to, KlondikePile::Tableau(Tableau::Tableau4)));
|
||||
assert_eq!(m.count, 1);
|
||||
}
|
||||
|
||||
/// Driving past one interval on a single-move replay must
|
||||
@@ -776,7 +778,7 @@ mod tests {
|
||||
10,
|
||||
100,
|
||||
NaiveDate::from_ymd_opt(2026, 5, 5).expect("valid date"),
|
||||
vec![ReplayMove::StockClick],
|
||||
vec![KlondikeInstruction::RotateStock],
|
||||
);
|
||||
start_playback(&mut app, one_move);
|
||||
app.update();
|
||||
@@ -822,7 +824,7 @@ mod tests {
|
||||
// Replay — their in-flight recording must not get clobbered.
|
||||
{
|
||||
let mut rec = app.world_mut().resource_mut::<RecordingReplay>();
|
||||
rec.moves.push(ReplayMove::StockClick);
|
||||
rec.moves.push(KlondikeInstruction::RotateStock);
|
||||
}
|
||||
start_playback(&mut app, sample_replay_three_moves());
|
||||
app.update();
|
||||
@@ -885,7 +887,7 @@ mod tests {
|
||||
10,
|
||||
100,
|
||||
NaiveDate::from_ymd_opt(2026, 5, 5).expect("valid date"),
|
||||
vec![ReplayMove::StockClick; 10],
|
||||
vec![KlondikeInstruction::RotateStock; 10],
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user