Compare commits

..

6 Commits

Author SHA1 Message Date
funman300 3322fd4250 fix(wasm): enable take-from-foundation in web game client
Android Release / build-apk (push) Successful in 3m56s
GameState::new_with_mode defaults take_from_foundation=false (non-
standard; the flag exists so the desktop can offer it as a setting).
The WASM web client has no settings layer, so this flag was never
flipped on — every drag or double-click from a foundation pile was
silently rejected by the rules engine.

Set take_from_foundation=true in both SolitaireGame::new (fresh games)
and SolitaireGame::from_saved (restored games, which may have the old
default serialised).

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-19 15:40:16 -07:00
funman300 90eb5fd207 feat(web): persist game state across page refreshes with resume dialog
Build and Deploy / build-and-push (push) Successful in 2m54s
Android Release / build-apk (push) Successful in 4m38s
- solitaire_wasm: add SolitaireGame::serialize() and from_saved() so JS
  can round-trip the full GameState through localStorage as JSON
- game.js: save {gameState, elapsedSecs, drawThree} to localStorage
  (key: fs_game_save) on every render(); clear the save on win
- game.js: on bootstrap, check for a saved game and show a resume
  dialog if one exists; Resume restores state + timer, New Game discards
  the save and starts fresh with a random seed
- game.html: add #resume-overlay markup (same pattern as win-overlay)
- game.css: add styles for the resume dialog and its secondary button

localStorage failures (private-browsing quota) are silently ignored so
they never block gameplay.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-19 15:38:07 -07:00
funman300 76cf41e7a9 fix(ui): open sync-setup modal when Connect clicked from Settings
Android Release / build-apk (push) Successful in 3m49s
The sync-setup modal was silently blocked by its own guard:
other_modal_scrims checks for any ModalScrim without SyncSetupScreen,
but the Settings panel IS a ModalScrim, so clicking Connect from within
Settings always hit the guard and returned early.

Two fixes:
- handle_sync_buttons: set SettingsScreen.0 = false when ConnectSync
  is pressed so settings closes as the event is fired
- open_sync_setup_modal: exclude SettingsPanel from other_modal_scrims
  to handle the deferred-despawn timing window (settings scrim entity
  still exists in the world until command buffers flush at frame end)
- Make SettingsPanel pub so sync_setup_plugin can reference it

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-19 15:32:14 -07:00
funman300 fae5933d29 fix(engine): enable take-from-foundation for restored and startup games
Android Release / build-apk (push) Successful in 3m42s
GameState serializes take_from_foundation=false (the core default),
so saved games on disk and direct-loaded states never had the setting
applied from SettingsResource — only freshly dealt games did.

Two fixes:
- sync_settings_to_game: new system that reads SettingsChangedEvent
  and patches game.0.take_from_foundation on every settings change
  (covers initial settings load at startup and in-session toggles)
- handle_restore_prompt: apply settings immediately after game.0 =
  restored so the Continue path also respects the current setting
- Register SettingsChangedEvent in GamePlugin::build (idempotent with
  SettingsPlugin) so the message is available in headless test apps

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-19 15:26:42 -07:00
funman300 6cd8c6c013 fix(multi): resolve 3 remaining Android UI bugs
Android Release / build-apk (push) Successful in 3m33s
- radial_menu: replace active_id.unwrap() with let Some guard — no
  runtime panic possible even if DragState races (§2.3)
- card_plugin: add bottom-right AndroidCornerBg overlay to mask the
  rotated baked-in text on classic PNG cards (mirrors top-left treatment)
- hud_plugin: bump Android action button min_width 44→52 px to give
  ~22px glyphs adequate padding after dynamic font-size increase
- layout: fix doc-lazy-continuation clippy lint in BOTTOM_BAR_HEIGHT comment

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-19 15:16:24 -07:00
funman300 ec94cb34aa fix(layout): reserve action-bar height so tableau never hides behind buttons
Android Release / build-apk (push) Successful in 4m15s
compute_layout only subtracted safe_area_bottom (OS gesture/nav bar) from
the vertical budget, but the app's own action bar (≡ ← || ? ! M +) sits
*above* that zone — invisible to safe_area_bottom. On Android the bar is
60 px tall (44 px min-height buttons + 8 px top + 8 px bottom bar padding),
so deep tableau columns scrolled 60 px behind the button row.

Fix: add BOTTOM_BAR_HEIGHT (60 px Android, 0 desktop) to safe_area_bottom
before both affected calculations:
  • card_width_height_based — height-based card sizing
  • avail — budget fed to update_tableau_fan_frac for adaptive fan spacing

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-19 14:55:09 -07:00
11 changed files with 278 additions and 20 deletions
+15 -1
View File
@@ -1147,7 +1147,7 @@ fn add_android_corner_label(
let bg_w = font_size * 2.0; let bg_w = font_size * 2.0;
let bg_h = font_size * 1.25; let bg_h = font_size * 1.25;
// Background covers the PNG's baked-in small corner text. // Background covers the PNG's baked-in small corner text (top-left).
// Classic PNG cards have a white face, so the background must be white too. // Classic PNG cards have a white face, so the background must be white too.
// (CARD_FACE_COLOUR is the Terminal theme's dark face colour — wrong here.) // (CARD_FACE_COLOUR is the Terminal theme's dark face colour — wrong here.)
parent.spawn(( parent.spawn((
@@ -1163,6 +1163,20 @@ fn add_android_corner_label(
0.015, 0.015,
), ),
)); ));
// Cover the matching rotated baked-in text at the bottom-right corner.
parent.spawn((
AndroidCornerBg,
Sprite {
color: Color::WHITE,
custom_size: Some(Vec2::new(bg_w, bg_h)),
..default()
},
Transform::from_xyz(
card_size.x / 2.0 - inset - bg_w / 2.0,
-card_size.y / 2.0 + inset + bg_h / 2.0,
0.015,
),
));
// Large rank+suit text drawn on top of the background. FiraMono must be // Large rank+suit text drawn on top of the background. FiraMono must be
// wired here explicitly — the suit glyphs (U+2660U+2666) are not in // wired here explicitly — the suit glyphs (U+2660U+2666) are not in
+25
View File
@@ -202,6 +202,8 @@ impl Plugin for GamePlugin {
.add_message::<FoundationCompletedEvent>() .add_message::<FoundationCompletedEvent>()
.add_message::<InfoToastEvent>() .add_message::<InfoToastEvent>()
.add_message::<AppLifecycle>() .add_message::<AppLifecycle>()
// add_message is idempotent; SettingsPlugin also registers this.
.add_message::<crate::settings_plugin::SettingsChangedEvent>()
.add_systems( .add_systems(
Update, Update,
poll_pending_new_game_seed.before(GameMutation), poll_pending_new_game_seed.before(GameMutation),
@@ -228,6 +230,7 @@ impl Plugin for GamePlugin {
// GameMutation flow. // GameMutation flow.
.add_systems(Update, spawn_restore_prompt_if_pending) .add_systems(Update, spawn_restore_prompt_if_pending)
.add_systems(Update, handle_restore_prompt.before(GameMutation)) .add_systems(Update, handle_restore_prompt.before(GameMutation))
.add_systems(Update, sync_settings_to_game.before(GameMutation))
.init_resource::<AutoSaveTimer>() .init_resource::<AutoSaveTimer>()
.add_systems(Update, tick_elapsed_time) .add_systems(Update, tick_elapsed_time)
.add_systems(Update, auto_save_game_state) .add_systems(Update, auto_save_game_state)
@@ -235,6 +238,23 @@ impl Plugin for GamePlugin {
} }
} }
/// Forwards `take_from_foundation` from [`SettingsResource`] to the live
/// [`GameStateResource`] every time [`SettingsChangedEvent`] fires.
///
/// This covers two cases that the new-game path misses:
/// 1. The initial settings load at startup: saves on disk default to `false`
/// but `Settings` defaults to `true`; the event fires once when the
/// settings file is first read.
/// 2. A user toggling the setting mid-session in the Settings panel.
fn sync_settings_to_game(
mut events: MessageReader<crate::settings_plugin::SettingsChangedEvent>,
mut game: ResMut<GameStateResource>,
) {
for ev in events.read() {
game.0.take_from_foundation = ev.0.take_from_foundation;
}
}
/// Pure, testable helper. Updates `elapsed_seconds` and drains the /// Pure, testable helper. Updates `elapsed_seconds` and drains the
/// fractional accumulator into whole-second ticks. No-op when `is_won`. /// fractional accumulator into whole-second ticks. No-op when `is_won`.
pub fn advance_elapsed( pub fn advance_elapsed(
@@ -614,6 +634,7 @@ fn handle_restore_prompt(
new_game_buttons: Query<&Interaction, (With<RestoreNewGameButton>, Changed<Interaction>)>, new_game_buttons: Query<&Interaction, (With<RestoreNewGameButton>, Changed<Interaction>)>,
mut pending: ResMut<PendingRestoredGame>, mut pending: ResMut<PendingRestoredGame>,
mut game: ResMut<GameStateResource>, mut game: ResMut<GameStateResource>,
settings: Option<Res<crate::settings_plugin::SettingsResource>>,
mut changed: MessageWriter<StateChangedEvent>, mut changed: MessageWriter<StateChangedEvent>,
mut new_game: MessageWriter<NewGameRequestEvent>, mut new_game: MessageWriter<NewGameRequestEvent>,
mut launch_home_shown: Option<ResMut<crate::home_plugin::LaunchHomeShown>>, mut launch_home_shown: Option<ResMut<crate::home_plugin::LaunchHomeShown>>,
@@ -639,6 +660,10 @@ fn handle_restore_prompt(
let resolved = if key_continue || click_continue { let resolved = if key_continue || click_continue {
if let Some(restored) = pending.0.take() { if let Some(restored) = pending.0.take() {
game.0 = restored; game.0 = restored;
// Patch setting that serialized with the old core default of `false`.
if let Some(s) = settings.as_ref() {
game.0.take_from_foundation = s.0.take_from_foundation;
}
changed.write(StateChangedEvent); changed.write(StateChangedEvent);
} }
for entity in &screens { for entity in &screens {
+1 -1
View File
@@ -989,7 +989,7 @@ fn spawn_action_button<M: Component>(
// centred with room to breathe. On desktop, keep the comfortable 48 dp // centred with room to breathe. On desktop, keep the comfortable 48 dp
// floor and 8 dp side padding. // floor and 8 dp side padding.
#[cfg(target_os = "android")] #[cfg(target_os = "android")]
let (pad, min_w, min_h) = (UiRect::axes(Val::Px(4.0), Val::Px(4.0)), Val::Px(44.0), Val::Px(44.0)); let (pad, min_w, min_h) = (UiRect::axes(Val::Px(4.0), Val::Px(4.0)), Val::Px(52.0), Val::Px(44.0));
#[cfg(not(target_os = "android"))] #[cfg(not(target_os = "android"))]
let (pad, min_w, min_h) = (UiRect::axes(VAL_SPACE_2, VAL_SPACE_2), Val::Px(48.0), Val::Px(48.0)); let (pad, min_w, min_h) = (UiRect::axes(VAL_SPACE_2, VAL_SPACE_2), Val::Px(48.0), Val::Px(48.0));
+23 -2
View File
@@ -107,6 +107,23 @@ pub const HUD_BAND_HEIGHT: f32 = 64.0;
#[cfg(target_os = "android")] #[cfg(target_os = "android")]
pub const HUD_BAND_HEIGHT: f32 = 112.0; pub const HUD_BAND_HEIGHT: f32 = 112.0;
/// Height of the bottom action-bar (the row of ≡ ← || ? ! M + buttons).
///
/// The action bar sits *above* the OS gesture/navigation zone, so it is NOT
/// covered by `safe_area_bottom`. `compute_layout` adds this constant to
/// `safe_area_bottom` before computing the height-based card-size candidate
/// and the available tableau height, ensuring the deepest fanned column
/// never scrolls behind the button row.
///
/// Derivation (Android): `min_height 44 px` buttons
/// + `padding.top 8 px` + `padding.bottom 8 px` outer bar padding = **60 px**.
///
/// Desktop: no persistent bottom bar, so 0.
#[cfg(not(target_os = "android"))]
const BOTTOM_BAR_HEIGHT: f32 = 0.0;
#[cfg(target_os = "android")]
const BOTTOM_BAR_HEIGHT: f32 = 60.0;
/// Table background colour (dark green felt). /// Table background colour (dark green felt).
pub const TABLE_COLOUR: [f32; 3] = [0.059, 0.322, 0.196]; pub const TABLE_COLOUR: [f32; 3] = [0.059, 0.322, 0.196];
@@ -190,9 +207,13 @@ pub fn compute_layout(window: Vec2, safe_area_top: f32, safe_area_bottom: f32, h
// Substituting h_gap = w/4 and h = CARD_ASPECT * w and solving for the // Substituting h_gap = w/4 and h = CARD_ASPECT * w and solving for the
// largest w that fits gives: // largest w that fits gives:
// (window.y - HUD_BAND_HEIGHT) = w * (0.5 + (1 + fan_factor + VERTICAL_GAP_FRAC) * CARD_ASPECT) // (window.y - HUD_BAND_HEIGHT) = w * (0.5 + (1 + fan_factor + VERTICAL_GAP_FRAC) * CARD_ASPECT)
// Reserve space for both the OS gesture/nav bar and the app's own action
// bar, which sits above it and is invisible to safe_area_bottom.
let effective_safe_bottom = safe_area_bottom + BOTTOM_BAR_HEIGHT;
let fan_factor = 1.0 + (MAX_TABLEAU_CARDS - 1.0) * TABLEAU_FAN_FRAC; let fan_factor = 1.0 + (MAX_TABLEAU_CARDS - 1.0) * TABLEAU_FAN_FRAC;
let height_denom = 0.5 + (1.0 + fan_factor + VERTICAL_GAP_FRAC) * CARD_ASPECT; let height_denom = 0.5 + (1.0 + fan_factor + VERTICAL_GAP_FRAC) * CARD_ASPECT;
let card_width_height_based = (window.y - safe_area_top - safe_area_bottom - band_h).max(0.0) / height_denom; let card_width_height_based = (window.y - safe_area_top - effective_safe_bottom - band_h).max(0.0) / height_denom;
let card_width = card_width_width_based.min(card_width_height_based); let card_width = card_width_width_based.min(card_width_height_based);
let card_height = card_width * CARD_ASPECT; let card_height = card_width * CARD_ASPECT;
@@ -241,7 +262,7 @@ pub fn compute_layout(window: Vec2, safe_area_top: f32, safe_area_bottom: f32, h
// //
// avail = distance from the top of the first tableau card to the bottom // avail = distance from the top of the first tableau card to the bottom
// margin — i.e. the space available for 12 fan steps. // margin — i.e. the space available for 12 fan steps.
let avail = (tableau_y - (-window.y / 2.0 + safe_area_bottom + h_gap) - card_height / 2.0).max(0.0); let avail = (tableau_y - (-window.y / 2.0 + effective_safe_bottom + h_gap) - card_height / 2.0).max(0.0);
let ideal_fan_frac = if card_height > 0.0 { let ideal_fan_frac = if card_height > 0.0 {
avail / ((MAX_TABLEAU_CARDS - 1.0) * card_height) avail / ((MAX_TABLEAU_CARDS - 1.0) * card_height)
} else { } else {
+6 -3
View File
@@ -473,8 +473,11 @@ fn radial_open_on_long_press(
mut state: ResMut<RightClickRadialState>, mut state: ResMut<RightClickRadialState>,
) { ) {
// Guard: only count while a touch is down, uncommitted, and radial is idle. // Guard: only count while a touch is down, uncommitted, and radial is idle.
let active_id = drag.active_touch_id; let Some(active_id) = drag.active_touch_id else {
if active_id.is_none() || drag.committed || state.is_active() || paused.is_some_and(|p| p.0) { *hold_timer = 0.0;
return;
};
if drag.committed || state.is_active() || paused.is_some_and(|p| p.0) {
*hold_timer = 0.0; *hold_timer = 0.0;
return; return;
} }
@@ -487,7 +490,7 @@ fn radial_open_on_long_press(
// Resolve current touch world position. // Resolve current touch world position.
let Some(touches) = touches else { return }; let Some(touches) = touches else { return };
let Some(touch) = touches.iter().find(|t| t.id() == active_id.unwrap()) else { let Some(touch) = touches.iter().find(|t| t.id() == active_id) else {
return; return;
}; };
let Some((camera, cam_xf)) = cameras.single().ok() else { return }; let Some((camera, cam_xf)) = cameras.single().ok() else { return };
+8 -2
View File
@@ -94,7 +94,7 @@ pub struct SettingsChangedEvent(pub Settings);
/// Marker on the root Settings panel entity. /// Marker on the root Settings panel entity.
#[derive(Component, Debug)] #[derive(Component, Debug)]
struct SettingsPanel; pub struct SettingsPanel;
/// Marks the `Text` node showing the live SFX volume value. /// Marks the `Text` node showing the live SFX volume value.
#[derive(Component, Debug)] #[derive(Component, Debug)]
@@ -1137,6 +1137,7 @@ fn handle_sync_buttons(
mut configure_sync: MessageWriter<SyncConfigureRequestEvent>, mut configure_sync: MessageWriter<SyncConfigureRequestEvent>,
mut logout_sync: MessageWriter<SyncLogoutRequestEvent>, mut logout_sync: MessageWriter<SyncLogoutRequestEvent>,
mut delete_account: MessageWriter<DeleteAccountRequestEvent>, mut delete_account: MessageWriter<DeleteAccountRequestEvent>,
mut screen: ResMut<SettingsScreen>,
) { ) {
for (interaction, button) in &interaction_query { for (interaction, button) in &interaction_query {
if *interaction != Interaction::Pressed { if *interaction != Interaction::Pressed {
@@ -1144,7 +1145,12 @@ fn handle_sync_buttons(
} }
match button { match button {
SettingsButton::SyncNow => { manual_sync.write(ManualSyncRequestEvent); } SettingsButton::SyncNow => { manual_sync.write(ManualSyncRequestEvent); }
SettingsButton::ConnectSync => { configure_sync.write(SyncConfigureRequestEvent); } SettingsButton::ConnectSync => {
// Close settings before the sync-setup modal opens so the
// guard in open_sync_setup_modal doesn't block on our own scrim.
screen.0 = false;
configure_sync.write(SyncConfigureRequestEvent);
}
SettingsButton::DisconnectSync => { logout_sync.write(SyncLogoutRequestEvent); } SettingsButton::DisconnectSync => { logout_sync.write(SyncLogoutRequestEvent); }
SettingsButton::DeleteAccount => { delete_account.write(DeleteAccountRequestEvent); } SettingsButton::DeleteAccount => { delete_account.write(DeleteAccountRequestEvent); }
_ => {} _ => {}
+6 -2
View File
@@ -52,7 +52,7 @@ use crate::events::{
SyncLogoutRequestEvent, SyncLogoutRequestEvent,
}; };
use crate::font_plugin::FontResource; use crate::font_plugin::FontResource;
use crate::settings_plugin::{SettingsResource, SettingsScreen, SettingsStoragePath}; use crate::settings_plugin::{SettingsPanel, SettingsResource, SettingsScreen, SettingsStoragePath};
use crate::resources::TokioRuntimeResource; use crate::resources::TokioRuntimeResource;
use crate::sync_plugin::SyncProviderResource; use crate::sync_plugin::SyncProviderResource;
use crate::ui_modal::{spawn_modal, ModalScrim}; use crate::ui_modal::{spawn_modal, ModalScrim};
@@ -205,10 +205,14 @@ impl Plugin for SyncSetupPlugin {
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
/// Opens the sync-setup modal when `SyncConfigureRequestEvent` is received. /// Opens the sync-setup modal when `SyncConfigureRequestEvent` is received.
#[allow(clippy::type_complexity)]
fn open_sync_setup_modal( fn open_sync_setup_modal(
mut events: MessageReader<SyncConfigureRequestEvent>, mut events: MessageReader<SyncConfigureRequestEvent>,
existing: Query<(), With<SyncSetupScreen>>, existing: Query<(), With<SyncSetupScreen>>,
other_modal_scrims: Query<(), (With<ModalScrim>, Without<SyncSetupScreen>)>, // Exclude SettingsPanel: the Connect button closes settings in the same
// frame it fires SyncConfigureRequestEvent, but Bevy despawns are deferred
// so the settings scrim still exists in the world during this system.
other_modal_scrims: Query<(), (With<ModalScrim>, Without<SyncSetupScreen>, Without<SettingsPanel>)>,
mut commands: Commands, mut commands: Commands,
mut focused: ResMut<SyncFocusedField>, mut focused: ResMut<SyncFocusedField>,
font_res: Option<Res<FontResource>>, font_res: Option<Res<FontResource>>,
+62
View File
@@ -230,6 +230,68 @@ main {
pointer-events: none; pointer-events: none;
} }
/* ── Resume overlay ──────────────────────────────────────────────────── */
#resume-overlay {
position: fixed;
inset: 0;
background: rgba(21, 21, 21, 0.92);
display: flex;
align-items: center;
justify-content: center;
z-index: 1000;
}
#resume-overlay.hidden { display: none; }
.resume-card {
background: var(--panel);
border: 1px solid rgba(255,255,255,0.12);
border-radius: 16px;
padding: 40px 48px;
text-align: center;
display: flex;
flex-direction: column;
gap: 16px;
box-shadow: 0 20px 60px rgba(0,0,0,0.6);
max-width: 360px;
}
.resume-title {
font-size: 28px;
font-weight: 700;
color: var(--accent);
}
.resume-detail {
font-size: 14px;
color: var(--text-muted);
margin: 0;
line-height: 1.5;
}
.resume-actions {
display: flex;
gap: 12px;
justify-content: center;
margin-top: 8px;
}
.resume-actions button {
padding: 12px 24px;
font-size: 15px;
}
.resume-actions button.secondary {
background: transparent;
border: 1px solid rgba(255,255,255,0.2);
color: var(--text-muted);
}
.resume-actions button.secondary:hover {
background: rgba(255,255,255,0.05);
}
/* ── Win overlay ─────────────────────────────────────────────────────── */ /* ── Win overlay ─────────────────────────────────────────────────────── */
#win-overlay { #win-overlay {
+11
View File
@@ -56,6 +56,17 @@
</section> </section>
</main> </main>
<div id="resume-overlay" class="hidden">
<div class="resume-card">
<div class="resume-title">Resume Game?</div>
<p class="resume-detail">You have an unfinished game saved. Would you like to continue where you left off?</p>
<div class="resume-actions">
<button id="btn-resume">↩ Resume</button>
<button id="btn-resume-new" class="secondary">↺ New Game</button>
</div>
</div>
</div>
<div id="win-overlay" class="hidden"> <div id="win-overlay" class="hidden">
<div class="win-card"> <div class="win-card">
<div class="win-title">You Won!</div> <div class="win-title">You Won!</div>
+92 -5
View File
@@ -69,6 +69,34 @@ function preloadTheme(theme) {
preloadTheme("classic"); preloadTheme("classic");
preloadTheme("dark"); preloadTheme("dark");
// ── Persistence ──────────────────────────────────────────────────────────────
const LS_SAVE_KEY = "fs_game_save";
function saveState() {
if (!game) return;
try {
const gameState = game.serialize();
if (typeof gameState !== "string") return;
localStorage.setItem(LS_SAVE_KEY, JSON.stringify({ gameState, elapsedSecs, drawThree }));
} catch (e) {
// localStorage may be unavailable (private browsing quota, etc.) — never block gameplay.
console.warn("fs: save failed", e);
}
}
function clearSave() {
try { localStorage.removeItem(LS_SAVE_KEY); } catch { /* ignore */ }
}
function loadSave() {
try {
const raw = localStorage.getItem(LS_SAVE_KEY);
if (!raw) return null;
const save = JSON.parse(raw);
return save?.gameState ? save : null;
} catch { return null; }
}
// ── State ──────────────────────────────────────────────────────────────────── // ── State ────────────────────────────────────────────────────────────────────
let game = null; let game = null;
let snap = null; // last rendered GameSnapshot let snap = null; // last rendered GameSnapshot
@@ -138,16 +166,72 @@ async function bootstrap() {
await init(); await init();
syncThemeButton(); syncThemeButton();
buildSlots();
scaleBoard();
window.addEventListener("resize", scaleBoard);
attachHandlers();
const saved = loadSave();
if (saved) {
showResumeDialog(saved);
} else {
const params = new URLSearchParams(window.location.search); const params = new URLSearchParams(window.location.search);
const urlSeed = params.has("seed") ? Number(params.get("seed")) : randomSeed(); const urlSeed = params.has("seed") ? Number(params.get("seed")) : randomSeed();
drawThree = params.has("draw3"); drawThree = params.has("draw3");
chkDraw3.checked = drawThree; chkDraw3.checked = drawThree;
buildSlots();
scaleBoard();
window.addEventListener("resize", scaleBoard);
startGame(urlSeed); startGame(urlSeed);
attachHandlers(); }
}
function showResumeDialog(saved) {
const overlay = document.getElementById("resume-overlay");
if (overlay) overlay.classList.remove("hidden");
document.getElementById("btn-resume").onclick = () => {
if (overlay) overlay.classList.add("hidden");
resumeGame(saved);
};
document.getElementById("btn-resume-new").onclick = () => {
clearSave();
if (overlay) overlay.classList.add("hidden");
drawThree = false;
chkDraw3.checked = false;
startGame(randomSeed());
};
}
function resumeGame(saved) {
let restored;
try {
restored = SolitaireGame.from_saved(saved.gameState);
} catch (e) {
console.warn("fs: restore failed, starting new game", e);
clearSave();
startGame(randomSeed());
return;
}
game = restored;
drawThree = !!saved.drawThree;
elapsedSecs = saved.elapsedSecs || 0;
chkDraw3.checked = drawThree;
const displaySeed = Math.round(game.seed());
hudSeed.textContent = `seed ${displaySeed}`;
winOverlay.classList.add("hidden");
cardEls.clear();
board.querySelectorAll(".card, .recycle-label").forEach(el => el.remove());
const url = new URL(window.location);
url.searchParams.set("seed", displaySeed);
if (drawThree) url.searchParams.set("draw3", "");
else url.searchParams.delete("draw3");
history.replaceState(null, "", url);
const s = game.state();
snap = s;
render(s);
if (!s.is_won) startTimer();
} }
function randomSeed() { function randomSeed() {
@@ -304,9 +388,12 @@ function render(s) {
acTimer = setInterval(doAutoCompleteStep, 380); acTimer = setInterval(doAutoCompleteStep, 380);
} }
if (s.is_won) { if (s.is_won) {
clearSave();
stopTimer(); stopTimer();
if (acTimer) { clearInterval(acTimer); acTimer = null; } if (acTimer) { clearInterval(acTimer); acTimer = null; }
showWin(s); showWin(s);
} else {
saveState();
} }
} }
+28 -3
View File
@@ -366,9 +366,10 @@ impl SolitaireGame {
} else { } else {
DrawMode::DrawOne DrawMode::DrawOne
}; };
SolitaireGame { let mut game = GameState::new_with_mode(seed as u64, dm, GameMode::Classic);
game: GameState::new_with_mode(seed as u64, dm, GameMode::Classic), // The web client has no settings layer; enable standard Klondike rule unconditionally.
} game.take_from_foundation = true;
SolitaireGame { game }
} }
/// Full pile snapshot as a JS object. /// Full pile snapshot as a JS object.
@@ -422,6 +423,30 @@ impl SolitaireGame {
} }
} }
/// Serialise the full game state as a JSON string for `localStorage`.
///
/// Use [`SolitaireGame::from_saved`] to restore it. The returned string is
/// opaque — callers should treat it as a blob and store/restore it verbatim.
pub fn serialize(&self) -> Result<String, JsValue> {
serde_json::to_string(&self.game)
.map_err(|e| JsValue::from_str(&e.to_string()))
}
/// Restore a game from a JSON string previously produced by [`SolitaireGame::serialize`].
///
/// Returns an error string if the JSON is malformed or describes a state
/// that can't be deserialised (e.g. from a future schema version).
pub fn from_saved(json: &str) -> Result<SolitaireGame, JsValue> {
serde_json::from_str::<GameState>(json)
.map(|mut game| {
// Older saves serialised with take_from_foundation=false (the core default).
// The web client has no settings layer, so enforce the standard rule here.
game.take_from_foundation = true;
SolitaireGame { game }
})
.map_err(|e| JsValue::from_str(&e.to_string()))
}
/// Apply one auto-complete move (only valid when `is_auto_completable`). /// Apply one auto-complete move (only valid when `is_auto_completable`).
/// ///
/// If no card can go directly to a foundation this step, advances the /// If no card can go directly to a foundation this step, advances the