Files
Ferrous-Solitaire/solitaire_engine/src/resources.rs
T
funman300 2f373784bf
Test / fmt (pull_request) Successful in 5s
Test / test (pull_request) Successful in 4m22s
feat(engine): Phase M — sync transparency + local data backup
Closes out the 13-phase menu redesign. Three parts:

1. Post-sync merge summary: the pull poller now keeps the
   `ConflictReport`s the merge produces (previously discarded) in a new
   `SyncConflictLog` resource and toasts "Synced — N conflicts, kept
   newer values" when a merge wasn't clean. No wire changes — the
   server-side `SyncResponse.conflicts` field already existed.

2. Account tab detail: a per-field conflict list under the sync row
   ("win_streak_current — this device: 3 / server: 5") plus a
   clean-merge caption, so the last merge is always inspectable.

3. Local data export/import: new `solitaire_data::transfer` module —
   one versioned JSON bundle (settings, stats, achievements, progress)
   written atomically next to the other saves, with a version-gated
   loader that surfaces every failure instead of defaulting. Account
   tab grows an Export/Import button pair (desktop+Android only); the
   handler runs in `Last`, off the annotated Update spine. Import
   rewrites the live resources, persists via the existing save fns,
   and fires SettingsChangedEvent so appliers react normally.

Tests: bundle round-trip/version-gate/missing-file in solitaire_data;
ECS-level export→import round trip and failed-import-warns in
settings_plugin. Gate: workspace tests green, clippy -D warnings clean.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-16 18:06:02 -07:00

176 lines
6.4 KiB
Rust

//! Bevy resources owned by the engine crate.
#[cfg(not(target_arch = "wasm32"))]
use std::sync::Arc;
use bevy::math::Vec2;
use bevy::prelude::Resource;
use chrono::{DateTime, Utc};
use solitaire_core::Card;
use solitaire_core::KlondikePile;
use solitaire_core::game_state::GameState;
/// Wraps the currently active `GameState`. Single source of truth for the in-progress game.
#[derive(Resource, Debug, Clone)]
pub struct GameStateResource(pub GameState);
/// Tracks an in-progress drag operation.
///
/// When `cards` is empty there is no active drag. When non-empty, the listed
/// cards are being moved by the user and should be rendered at the cursor or
/// touch position.
///
/// # Drag threshold
///
/// A drag is *pending* when `!cards.is_empty() && !committed`. The drag does
/// not become *committed* (cards do not visually move) until the pointer has
/// moved at least `AnimationTuning::drag_threshold_px` pixels from `press_pos`.
/// This prevents accidental drags on quick taps, especially on touch screens.
#[derive(Resource, Debug, Clone)]
pub struct DragState {
/// Cards being dragged (bottom-to-top stacking order).
pub cards: Vec<Card>,
/// Pile the drag originated from.
pub origin_pile: Option<KlondikePile>,
/// World-space offset from the cursor/touch to the bottom card's centre.
pub cursor_offset: Vec2,
/// Z coordinate used for the dragged cards.
pub origin_z: f32,
/// Screen-space position (logical pixels) where the press/touch began.
///
/// Used to measure whether the drag threshold has been crossed.
pub press_pos: Vec2,
/// Whether the drag threshold has been crossed and visual drag is active.
///
/// Cards are only lifted and repositioned once `committed = true`.
pub committed: bool,
/// Touch ID driving this drag, or `None` for a mouse drag.
pub active_touch_id: Option<u64>,
}
impl Default for DragState {
fn default() -> Self {
Self {
cards: Vec::new(),
origin_pile: None,
cursor_offset: Vec2::ZERO,
origin_z: 0.0,
press_pos: Vec2::ZERO,
committed: false,
active_touch_id: None,
}
}
}
impl DragState {
/// Returns `true` when no drag (pending or committed) is in progress.
pub fn is_idle(&self) -> bool {
self.cards.is_empty()
}
/// Returns `true` when a drag has been committed (cards are visually lifted).
pub fn is_committed(&self) -> bool {
self.committed
}
/// Resets all drag state to the idle/default values.
pub fn clear(&mut self) {
self.cards.clear();
self.origin_pile = None;
self.cursor_offset = Vec2::ZERO;
self.origin_z = 0.0;
self.press_pos = Vec2::ZERO;
self.committed = false;
self.active_touch_id = None;
}
}
/// Current sync activity — shown in the settings screen.
///
/// Defined here rather than in `solitaire_data` because it is a UI/runtime
/// status value, not part of the persistence layer.
#[derive(Debug, Clone, Default)]
pub enum SyncStatus {
#[default]
Idle,
Syncing,
LastSynced(DateTime<Utc>),
Error(String),
}
/// Bevy resource wrapping the current `SyncStatus`.
#[derive(Resource, Debug, Clone, Default)]
pub struct SyncStatusResource(pub SyncStatus);
/// Outcome of the most recent successful sync merge (Phase M — sync
/// transparency). `None` until the first pull of the session resolves.
///
/// The Account tab renders the conflict details; the sync poller emits a
/// one-line toast summary when `conflicts` is non-empty.
#[derive(Resource, Debug, Clone, Default)]
pub struct SyncConflictLog(pub Option<SyncMergeSummary>);
/// Timestamp + per-field conflict list captured from
/// [`solitaire_sync::merge`] after a pull resolves.
#[derive(Debug, Clone)]
pub struct SyncMergeSummary {
/// When the merge resolved.
pub at: DateTime<Utc>,
/// Fields that could not be merged deterministically (best-effort
/// resolution already applied — see `solitaire_sync::merge`).
pub conflicts: Vec<solitaire_sync::ConflictReport>,
}
/// Tracks which hint the player is currently cycling through.
///
/// Incremented on each H press so repeated presses reveal different moves.
/// Reset to `0` whenever the game state changes (move, draw, undo, new game).
#[derive(Resource, Debug, Clone, Default)]
pub struct HintCycleIndex(pub usize);
/// Remembers the vertical scroll offset of the Settings panel between open/close cycles.
///
/// Saved when the panel is despawned and restored on next spawn so the player
/// returns to the same position in the list without re-scrolling.
#[derive(Resource, Debug, Clone, Default)]
pub struct SettingsScrollPos(pub f32);
/// Set to `true` by an input system when a touch tap is consumed by game logic
/// (e.g. drawing from stock). `toggle_hud_on_tap` checks this flag on
/// `TouchPhase::Ended` and skips the HUD visibility toggle when set, then
/// resets it to `false` so subsequent taps behave normally.
#[derive(Resource, Debug, Clone, Default)]
pub struct GameInputConsumedResource(pub bool);
/// Shared Tokio runtime used by all async-task closures that need HTTP I/O.
///
/// Bevy's `AsyncComputeTaskPool` uses `async-executor` (not Tokio), so spawned
/// closures that call `reqwest`/`hyper` need a Tokio reactor. A single
/// multi-threaded runtime is built once at startup and its `Arc` cloned cheaply
/// into every network task — safe for concurrent `block_on` calls from multiple
/// worker threads.
///
/// Gated to non-wasm because `tokio::runtime::Builder::new_multi_thread()` uses
/// `mio` for OS-level I/O polling which does not compile for wasm32. The
/// plugins that depend on this resource (AudioPlugin, SyncPlugin,
/// AnalyticsPlugin) are also gated out on wasm32 in `CoreGamePlugin`.
#[cfg(not(target_arch = "wasm32"))]
#[derive(Resource, Clone)]
pub struct TokioRuntimeResource(pub Arc<tokio::runtime::Runtime>);
#[cfg(not(target_arch = "wasm32"))]
impl TokioRuntimeResource {
/// Attempts to build the shared multi-threaded Tokio runtime.
///
/// Returns `Err` if the OS refuses to create worker threads (e.g. resource
/// limits on Android). Callers should log the error and disable sync
/// features rather than panicking.
pub fn new() -> Result<Self, tokio::io::Error> {
let rt = tokio::runtime::Builder::new_multi_thread()
.worker_threads(2)
.enable_all()
.build()?;
Ok(Self(Arc::new(rt)))
}
}