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
+177 -2
View File
@@ -5,16 +5,35 @@
//! `update_on_win` method that depends on [`DrawMode`] from `solitaire_core`.
use chrono::Utc;
use solitaire_core::game_state::DrawMode;
use solitaire_core::game_state::{DrawMode, GameMode};
pub use solitaire_sync::StatsSnapshot;
/// Extension trait providing game-logic mutation helpers for [`StatsSnapshot`].
///
/// Import this trait alongside `StatsSnapshot` to use `update_on_win`.
/// Import this trait alongside `StatsSnapshot` to use `update_on_win`
/// and [`StatsExt::update_per_mode_bests`].
pub trait StatsExt {
/// Updates rolling statistics from a completed game win. Call once per `GameWonEvent`.
///
/// Tracks lifetime totals only — per-mode best scores and times are
/// updated separately via [`StatsExt::update_per_mode_bests`] so the
/// long-standing call sites that only know about [`DrawMode`] keep
/// compiling.
fn update_on_win(&mut self, score: i32, time_seconds: u64, draw_mode: &DrawMode);
/// Updates the per-mode best score and fastest-win-time fields for the
/// given [`GameMode`]. Call alongside [`StatsExt::update_on_win`] from
/// the win handler.
///
/// Behaviour:
/// - `Classic`, `Zen`, `Challenge`: updates the matching `*_best_score`
/// (max) and `*_fastest_win_seconds` (zero-aware min — 0 means
/// "no win recorded yet").
/// - `TimeAttack`: no-op. Time Attack uses session-level scoring (count
/// of wins in 10 minutes); a per-game best wouldn't compose with
/// the other modes' single-game scoring.
fn update_per_mode_bests(&mut self, score: i32, time_seconds: u64, mode: GameMode);
}
impl StatsExt for StatsSnapshot {
@@ -51,6 +70,43 @@ impl StatsExt for StatsSnapshot {
self.last_modified = Utc::now();
}
fn update_per_mode_bests(&mut self, score: i32, time_seconds: u64, mode: GameMode) {
let score_u32 = score.max(0) as u32;
// Zero-aware min — 0 means "no win recorded yet" for the per-mode
// fastest fields, so we must not let a real time get clobbered to 0.
// (Mirrors the merge logic in `solitaire_sync::merge`.)
let min_ignore_zero = |existing: u64, candidate: u64| -> u64 {
if existing == 0 {
candidate
} else if candidate == 0 {
existing
} else {
existing.min(candidate)
}
};
match mode {
GameMode::Classic => {
self.classic_best_score = self.classic_best_score.max(score_u32);
self.classic_fastest_win_seconds =
min_ignore_zero(self.classic_fastest_win_seconds, time_seconds);
}
GameMode::Zen => {
self.zen_best_score = self.zen_best_score.max(score_u32);
self.zen_fastest_win_seconds =
min_ignore_zero(self.zen_fastest_win_seconds, time_seconds);
}
GameMode::Challenge => {
self.challenge_best_score = self.challenge_best_score.max(score_u32);
self.challenge_fastest_win_seconds =
min_ignore_zero(self.challenge_fastest_win_seconds, time_seconds);
}
// Time Attack uses its own session-level scoring; a per-game best
// wouldn't compose with the other modes' single-game numbers.
GameMode::TimeAttack => {}
}
self.last_modified = Utc::now();
}
}
#[cfg(test)]
@@ -177,4 +233,123 @@ mod tests {
s.update_on_win(200, 60, &DrawMode::DrawOne);
assert_eq!(s.lifetime_score, u64::MAX, "lifetime_score must saturate, not overflow");
}
// -----------------------------------------------------------------------
// Per-mode bests
// -----------------------------------------------------------------------
#[test]
fn classic_win_updates_classic_best_score_only() {
let mut s = StatsSnapshot::default();
s.update_per_mode_bests(1500, 200, GameMode::Classic);
assert_eq!(s.classic_best_score, 1500);
assert_eq!(s.classic_fastest_win_seconds, 200);
// Other modes untouched.
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);
}
#[test]
fn zen_win_updates_zen_best_score_only() {
let mut s = StatsSnapshot::default();
s.update_per_mode_bests(1800, 600, GameMode::Zen);
assert_eq!(s.zen_best_score, 1800);
assert_eq!(s.zen_fastest_win_seconds, 600);
assert_eq!(s.classic_best_score, 0);
assert_eq!(s.challenge_best_score, 0);
}
#[test]
fn challenge_win_updates_challenge_best_score_only() {
let mut s = StatsSnapshot::default();
s.update_per_mode_bests(2400, 480, GameMode::Challenge);
assert_eq!(s.challenge_best_score, 2400);
assert_eq!(s.challenge_fastest_win_seconds, 480);
assert_eq!(s.classic_best_score, 0);
assert_eq!(s.zen_best_score, 0);
}
#[test]
fn time_attack_win_does_not_touch_per_mode_bests() {
let mut s = StatsSnapshot::default();
s.update_per_mode_bests(9999, 1, GameMode::TimeAttack);
assert_eq!(s.classic_best_score, 0);
assert_eq!(s.zen_best_score, 0);
assert_eq!(s.challenge_best_score, 0);
assert_eq!(s.classic_fastest_win_seconds, 0);
assert_eq!(s.zen_fastest_win_seconds, 0);
assert_eq!(s.challenge_fastest_win_seconds, 0);
}
#[test]
fn per_mode_best_score_takes_max_across_calls() {
let mut s = StatsSnapshot::default();
s.update_per_mode_bests(500, 200, GameMode::Classic);
s.update_per_mode_bests(200, 200, GameMode::Classic);
s.update_per_mode_bests(900, 200, GameMode::Classic);
assert_eq!(s.classic_best_score, 900);
}
#[test]
fn per_mode_fastest_uses_zero_aware_min() {
// First Classic win: 240s. Field starts at 0 (no win yet) — we
// must adopt 240, not stay at 0 like a naive `min` would.
let mut s = StatsSnapshot::default();
s.update_per_mode_bests(100, 240, GameMode::Classic);
assert_eq!(s.classic_fastest_win_seconds, 240);
// Faster Classic win replaces it.
s.update_per_mode_bests(100, 120, GameMode::Classic);
assert_eq!(s.classic_fastest_win_seconds, 120);
// Slower Classic win does not.
s.update_per_mode_bests(100, 300, GameMode::Classic);
assert_eq!(s.classic_fastest_win_seconds, 120);
}
#[test]
fn negative_score_treated_as_zero_in_per_mode() {
let mut s = StatsSnapshot::default();
s.update_per_mode_bests(-50, 240, GameMode::Classic);
assert_eq!(s.classic_best_score, 0);
// Time still recorded — a win with a low score is still a win.
assert_eq!(s.classic_fastest_win_seconds, 240);
}
#[test]
fn legacy_stats_without_per_mode_fields_deserializes_to_zero() {
// A pre-per-mode `stats.json` must still deserialise cleanly:
// every new field falls back to 0 via `#[serde(default)]` so
// updating the binary never wipes the player's old stats file.
let legacy_json = r#"{
"games_played": 12,
"games_won": 5,
"games_lost": 7,
"win_streak_current": 1,
"win_streak_best": 3,
"avg_time_seconds": 240,
"fastest_win_seconds": 180,
"lifetime_score": 8500,
"best_single_score": 2200,
"draw_one_wins": 4,
"draw_three_wins": 1,
"last_modified": "2026-04-29T12:00:00Z"
}"#;
let s: StatsSnapshot = serde_json::from_str(legacy_json)
.expect("legacy payload must deserialise without per-mode fields");
// Pre-existing fields kept their values.
assert_eq!(s.games_played, 12);
assert_eq!(s.best_single_score, 2200);
assert_eq!(s.fastest_win_seconds, 180);
// Every new per-mode field defaulted to 0 ("no win yet").
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);
}
}