feat(data,sync,engine): per-mode best score and fastest win

Lifetime stats now also track best score and fastest win per game
mode (Classic, Zen, Challenge), additive on top of the existing
all-modes-combined `best_single_score` and `fastest_win_seconds`.
Time Attack is intentionally excluded — its scoring model is
session-level (count of wins inside a 10-minute window) so a
per-game best wouldn't compose. Daily Challenge inherits Classic
scoring and contributes through the Classic row.

- `solitaire_sync::StatsSnapshot` gains six fields (`{mode}_best_score`,
  `{mode}_fastest_win_seconds` × {Classic, Zen, Challenge}). All are
  `#[serde(default)]` so older save files load cleanly to zeros.
- `solitaire_sync::merge` propagates the per-mode bests through the
  same max/min logic as the global counterparts.
- `solitaire_data::StatsExt::update_per_mode_bests` is the engine's
  entry point — called from `update_stats_on_win` alongside the
  existing `update_on_win`.
- Stats overlay grows a "Per-mode bests" section with three rows
  (Classic / Zen / Challenge) tagged with `PerModeBestsRow`. Empty
  rows render an em-dash, matching the first-launch zero-state
  treatment used by the primary cells.
- 3 new tests cover the rendering, the Classic-mode update path,
  and the Zen-mode update path.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
funman300
2026-05-05 18:46:32 +00:00
parent d9f36bf34a
commit 3984231c9b
4 changed files with 550 additions and 2 deletions
+116
View File
@@ -109,10 +109,45 @@ fn merge_stats(
best_single_score: local.best_single_score.max(remote.best_single_score),
draw_one_wins: local.draw_one_wins.max(remote.draw_one_wins),
draw_three_wins: local.draw_three_wins.max(remote.draw_three_wins),
// Per-mode bests. Bests take max; fastest times take a *zero-aware*
// min — see [`min_ignore_zero`] for the rationale (0 means "no win
// yet" for these fields, unlike the lifetime `fastest_win_seconds`
// which uses `u64::MAX` as its sentinel).
classic_best_score: local.classic_best_score.max(remote.classic_best_score),
classic_fastest_win_seconds: min_ignore_zero(
local.classic_fastest_win_seconds,
remote.classic_fastest_win_seconds,
),
zen_best_score: local.zen_best_score.max(remote.zen_best_score),
zen_fastest_win_seconds: min_ignore_zero(
local.zen_fastest_win_seconds,
remote.zen_fastest_win_seconds,
),
challenge_best_score: local.challenge_best_score.max(remote.challenge_best_score),
challenge_fastest_win_seconds: min_ignore_zero(
local.challenge_fastest_win_seconds,
remote.challenge_fastest_win_seconds,
),
last_modified: Utc::now(),
}
}
/// Zero-aware minimum: returns the smaller of `a` and `b`, but treats `0` as
/// "no value recorded yet" so `min_ignore_zero(0, x) == x`.
///
/// The lifetime `fastest_win_seconds` field uses `u64::MAX` as its "no wins"
/// sentinel (see [`StatsSnapshot::default`]) and so a plain `min` works for
/// it. The per-mode `*_fastest_win_seconds` fields, on the other hand, are
/// `#[serde(default)]` — and `u64`'s default is 0, not `u64::MAX`. Using a
/// straight `min` would therefore wrongly resolve "one side has a real time,
/// the other has no win" to 0. This helper preserves the real time instead.
fn min_ignore_zero(a: u64, b: u64) -> u64 {
match (a, b) {
(0, x) | (x, 0) => x,
_ => a.min(b),
}
}
// ---------------------------------------------------------------------------
// Achievements
// ---------------------------------------------------------------------------
@@ -875,6 +910,87 @@ mod tests {
);
}
// -----------------------------------------------------------------------
// Per-mode bests merge
// -----------------------------------------------------------------------
#[test]
fn merge_per_mode_best_takes_max() {
// Classic best score: 1000 vs 2000 → 2000. Mirror behaviour for
// `best_single_score` so per-mode follows the same rule.
let mut local = default_payload();
local.stats.classic_best_score = 1000;
let mut remote = default_payload();
remote.stats.classic_best_score = 2000;
let (merged, _) = merge(&local, &remote);
assert_eq!(merged.stats.classic_best_score, 2000);
}
#[test]
fn merge_per_mode_best_takes_max_for_zen_and_challenge() {
let mut local = default_payload();
local.stats.zen_best_score = 800;
local.stats.challenge_best_score = 5000;
let mut remote = default_payload();
remote.stats.zen_best_score = 1500;
remote.stats.challenge_best_score = 3000;
let (merged, _) = merge(&local, &remote);
assert_eq!(merged.stats.zen_best_score, 1500);
assert_eq!(merged.stats.challenge_best_score, 5000);
}
#[test]
fn merge_per_mode_fastest_ignores_zero() {
// Local has no Zen win (zen_fastest = 0); remote has 180s.
// Straight min(0, 180) would return 0 — wrong. The merge must
// preserve the real time.
let local = default_payload();
let mut remote = default_payload();
remote.stats.zen_fastest_win_seconds = 180;
let (merged, _) = merge(&local, &remote);
assert_eq!(merged.stats.zen_fastest_win_seconds, 180);
}
#[test]
fn merge_per_mode_fastest_takes_min_when_both_present() {
// When both sides have real times, the merge takes the smaller —
// mirroring the lifetime `fastest_win_seconds` behaviour.
let mut local = default_payload();
local.stats.classic_fastest_win_seconds = 240;
let mut remote = default_payload();
remote.stats.classic_fastest_win_seconds = 120;
let (merged, _) = merge(&local, &remote);
assert_eq!(merged.stats.classic_fastest_win_seconds, 120);
}
#[test]
fn merge_per_mode_fastest_both_zero_stays_zero() {
// Neither side has a win — the field must remain 0 rather than
// accidentally becoming non-zero.
let local = default_payload();
let remote = default_payload();
let (merged, _) = merge(&local, &remote);
assert_eq!(merged.stats.classic_fastest_win_seconds, 0);
assert_eq!(merged.stats.zen_fastest_win_seconds, 0);
assert_eq!(merged.stats.challenge_fastest_win_seconds, 0);
}
#[test]
fn merge_per_mode_fastest_local_real_remote_zero() {
// Symmetric to `merge_per_mode_fastest_ignores_zero`: local has the
// real time, remote is the zero-side. The merge must keep local's
// value rather than flooring to 0.
let mut local = default_payload();
local.stats.challenge_fastest_win_seconds = 300;
let remote = default_payload();
let (merged, _) = merge(&local, &remote);
assert_eq!(merged.stats.challenge_fastest_win_seconds, 300);
}
#[test]
fn merge_longest_streak_never_below_current_streak() {
// If a payload's `daily_challenge_longest_streak` was never written
+72
View File
@@ -33,6 +33,56 @@ pub struct StatsSnapshot {
pub draw_one_wins: u32,
/// Wins achieved in Draw-Three mode.
pub draw_three_wins: u32,
// -----------------------------------------------------------------
// Per-mode bests
//
// These mirror `best_single_score` / `fastest_win_seconds` but
// narrowed to one [`solitaire_core::game_state::GameMode`]. They are
// additive: lifetime totals continue to track across all modes, and
// legacy `stats.json` files load to 0 for every new field via
// `#[serde(default)]`.
//
// Time-Attack and Daily-Challenge are intentionally absent here:
// - Time Attack has its own session-level scoring (count of wins
// inside a 10-minute window); a per-game best wouldn't compose.
// - Daily Challenge uses Classic scoring rules and so already
// contributes to `classic_*` here.
//
// Sentinel for `*_fastest_win_seconds` is `0` (not `u64::MAX`),
// because legacy files deserialise unknown fields to the type's
// `Default::default()` — and `u64::default()` is 0. The merge logic
// and the UI must therefore treat 0 as "no win recorded yet".
// -----------------------------------------------------------------
/// Best single score achieved in Classic mode (Draw-One or Draw-Three).
/// 0 means "no Classic win yet".
#[serde(default)]
pub classic_best_score: u32,
/// Fastest Classic-mode win time, in seconds. 0 means "no Classic win yet".
#[serde(default)]
pub classic_fastest_win_seconds: u64,
/// Best single score achieved in Zen mode. Zen has no time pressure but
/// scoring is still on, so players who care about it still play for a high.
/// 0 means "no Zen win yet".
#[serde(default)]
pub zen_best_score: u32,
/// Fastest Zen-mode win time, in seconds. 0 means "no Zen win yet".
#[serde(default)]
pub zen_fastest_win_seconds: u64,
/// Best single score achieved in Challenge mode (the hardest mode — separate
/// leaderboard). 0 means "no Challenge win yet".
#[serde(default)]
pub challenge_best_score: u32,
/// Fastest Challenge-mode win time, in seconds. 0 means "no Challenge win yet".
#[serde(default)]
pub challenge_fastest_win_seconds: u64,
/// Wall-clock time of the last modification (used for conflict detection).
pub last_modified: DateTime<Utc>,
}
@@ -51,6 +101,12 @@ impl Default for StatsSnapshot {
best_single_score: 0,
draw_one_wins: 0,
draw_three_wins: 0,
classic_best_score: 0,
classic_fastest_win_seconds: 0,
zen_best_score: 0,
zen_fastest_win_seconds: 0,
challenge_best_score: 0,
challenge_fastest_win_seconds: 0,
last_modified: DateTime::UNIX_EPOCH,
}
}
@@ -147,4 +203,20 @@ mod tests {
assert_eq!(s.win_streak_best, 7, "best streak must not be reduced on abandon");
assert_eq!(s.win_streak_current, 0);
}
#[test]
fn per_mode_fields_default_to_zero() {
// The new per-mode fields must default to 0 — both in the explicit
// `Default` impl and (because of `#[serde(default)]`) for any
// legacy payload that omits them. The legacy-JSON deserialise
// round-trip lives in `solitaire_data::stats` where `serde_json`
// is in scope.
let s = StatsSnapshot::default();
assert_eq!(s.classic_best_score, 0);
assert_eq!(s.classic_fastest_win_seconds, 0);
assert_eq!(s.zen_best_score, 0);
assert_eq!(s.zen_fastest_win_seconds, 0);
assert_eq!(s.challenge_best_score, 0);
assert_eq!(s.challenge_fastest_win_seconds, 0);
}
}