refactor: consolidate card_to_id into solitaire_core
Three byte-identical copies of the stable 0..=51 card-id helper (suit_index*13 + rank-1) lived in feedback_anim_plugin, radial_menu, and solitaire_wasm. The WASM copy's own comment notes it MUST match the engine for cross-platform replay parity — exactly the kind of invariant a single source of truth should enforce. Add `solitaire_core::card::card_to_id(&Card) -> u32` and have all three call sites import it. No behaviour change (same formula). cargo test --workspace and cargo clippy --workspace --all-targets -- -D warnings pass. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -1 +1,23 @@
|
||||
pub use card_game::{Card, Deck, Rank, Suit};
|
||||
|
||||
/// Maps a [`Card`] to a stable `0..=51` numeric identity, independent of the
|
||||
/// upstream `card_game::Card` bit-packing.
|
||||
///
|
||||
/// Encoding: `suit_index * 13 + (rank as u32 - 1)`, where `suit_index` is
|
||||
/// Clubs=0, Diamonds=1, Hearts=2, Spades=3 and `rank` is 1 (Ace) ..= 13 (King).
|
||||
/// The deck id is intentionally ignored so the id depends only on the visible
|
||||
/// face.
|
||||
///
|
||||
/// This is the single source of truth shared by `CardEntity` numeric tracking,
|
||||
/// deterministic per-card animation jitter, and the WASM replay layer — those
|
||||
/// must agree byte-for-byte so replay snapshots are identical across the
|
||||
/// desktop and browser builds.
|
||||
pub fn card_to_id(card: &Card) -> u32 {
|
||||
let suit_index: u32 = match card.suit() {
|
||||
Suit::Clubs => 0,
|
||||
Suit::Diamonds => 1,
|
||||
Suit::Hearts => 2,
|
||||
Suit::Spades => 3,
|
||||
};
|
||||
suit_index * 13 + (card.rank() as u32 - 1)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user