//! Tracks the per-date daily challenge: a deterministic seed every player //! sees on a given calendar day, plus completion bookkeeping. //! //! When the player wins a game whose seed matches today's daily seed and //! today's date hasn't been completed yet, this plugin: //! - calls `PlayerProgress::record_daily_completion` //! - awards a fixed XP bonus (`DAILY_BONUS_XP`) //! - persists progress //! - emits `DailyChallengeCompletedEvent` //! //! Pressing **C** fires a `NewGameRequestEvent` with today's daily seed so //! the player can start a fresh attempt. use bevy::input::ButtonInput; use bevy::prelude::*; #[cfg(not(target_arch = "wasm32"))] use bevy::tasks::{AsyncComputeTaskPool, Task, futures_lite::future}; use chrono::{DateTime, Duration, Local, NaiveDate, Utc}; use solitaire_data::{daily_seed_for, save_progress_to}; #[cfg(not(target_arch = "wasm32"))] use solitaire_sync::ChallengeGoal; use crate::events::{ GameWonEvent, InfoToastEvent, NewGameRequestEvent, StartDailyChallengeRequestEvent, WarningToastEvent, XpAwardedEvent, }; use crate::game_plugin::GameMutation; use crate::progress_plugin::{ProgressResource, ProgressStoragePath, ProgressUpdate}; use crate::resources::GameStateResource; #[cfg(not(target_arch = "wasm32"))] use crate::sync_plugin::SyncProviderResource; /// Bonus XP awarded for completing today's daily challenge. pub const DAILY_BONUS_XP: u64 = 100; /// Minutes before UTC midnight at which the daily-challenge expiry warning /// fires. The reset is global (UTC), so the warning is global too — local /// midnight may be hours away or already past. pub const DAILY_EXPIRY_WARNING_MINUTES: i64 = 30; /// The active daily challenge — date + RNG seed for that date's deal, /// plus optional goal metadata fetched from the server. #[derive(Resource, Debug, Clone)] pub struct DailyChallengeResource { pub date: NaiveDate, pub seed: u64, /// Human-readable goal description from the server, e.g. "Win in under 5 minutes". pub goal_description: Option, /// Optional target score the server requires for this challenge. pub target_score: Option, /// Optional time limit in seconds the server imposes. pub max_time_secs: Option, } /// Fired when the player presses C to start the daily challenge. /// Carries the current goal description so it can be displayed as a toast. #[derive(Message, Debug, Clone)] pub struct DailyGoalAnnouncementEvent(pub String); impl DailyChallengeResource { pub fn for_today() -> Self { let date = Local::now().date_naive(); Self { date, seed: daily_seed_for(date), goal_description: None, target_score: None, max_time_secs: None, } } } /// Fired when the player has just completed today's daily challenge. #[derive(Message, Debug, Clone, Copy)] pub struct DailyChallengeCompletedEvent { pub date: NaiveDate, pub streak: u32, } /// Holds the in-flight server challenge fetch so the result can be polled /// each frame without blocking the main thread. #[derive(Resource, Default)] #[cfg(not(target_arch = "wasm32"))] struct DailyChallengeTask(Option>>); #[derive(Resource, Default)] #[cfg(target_arch = "wasm32")] struct DailyChallengeTask; /// Tracks which `DailyChallengeResource::date` the expiry-warning toast has /// already fired for, so the toast spawns at most once per day. /// /// `None` until the first warning fires; thereafter holds the date the /// warning was shown for. When `daily.date` advances (a new local day rolls /// over while the app stays open), this becomes stale and the next warning /// can fire. #[derive(Resource, Default, Debug)] struct DailyExpiryWarningShown(Option); /// Throttle timer so `check_date_rollover` does not call `Local::now()` every frame. #[derive(Resource)] struct DateRolloverTimer(Timer); impl Default for DateRolloverTimer { fn default() -> Self { Self(Timer::from_seconds(60.0, TimerMode::Repeating)) } } /// Fetches today's daily challenge seed and goal from the sync server on startup and tracks completion. /// Fires `DailyChallengeCompletedEvent` when the player wins a matching game. pub struct DailyChallengePlugin; impl Plugin for DailyChallengePlugin { fn build(&self, app: &mut App) { app.insert_resource(DailyChallengeResource::for_today()) .init_resource::() .init_resource::() .init_resource::() .add_message::() .add_message::() .add_message::() .add_message::() .add_message::() .add_message::() .add_message::() // record/award after the base ProgressUpdate so we don't fight // ProgressPlugin's add_xp on the same frame. .add_systems(Update, handle_daily_completion.after(ProgressUpdate)) .add_systems(Update, handle_start_daily_request.before(GameMutation)) .add_systems(Update, check_daily_expiry_warning) .add_systems(Update, check_date_rollover); // Server-challenge fetch uses SyncProviderResource (reqwest), not available on wasm. #[cfg(not(target_arch = "wasm32"))] app.add_systems(Startup, fetch_server_challenge) .add_systems(Update, poll_server_challenge); } } #[cfg(not(target_arch = "wasm32"))] /// Startup system: spawns an async task to fetch the server's daily challenge. /// /// Only runs when `SyncProviderResource` is present (i.e. `SyncPlugin` is /// installed). The endpoint is public so authentication is not required. fn fetch_server_challenge( provider: Option>, mut task_res: ResMut, ) { let Some(provider) = provider else { return }; let provider = provider.0.clone(); let task = AsyncComputeTaskPool::get() .spawn(async move { provider.fetch_daily_challenge().await.ok().flatten() }); task_res.0 = Some(task); } #[cfg(not(target_arch = "wasm32"))] /// Update system: polls the server-challenge fetch task. /// /// On success, replaces the locally-computed seed in `DailyChallengeResource` /// with the server's authoritative seed — ensuring all players worldwide get /// the same deal on a given date regardless of their local clock hash. /// /// Silently no-ops if the task is still in flight, already consumed, or /// if the server returned a challenge for a different date. fn poll_server_challenge( mut task_res: ResMut, mut daily: ResMut, ) { let Some(task) = task_res.0.as_mut() else { return; }; let Some(result) = future::block_on(future::poll_once(task)) else { return; }; task_res.0 = None; let Some(goal) = result else { return }; let Ok(date) = NaiveDate::parse_from_str(&goal.date, "%Y-%m-%d") else { return; }; if date == daily.date { let old_seed = daily.seed; daily.seed = goal.seed; daily.goal_description = Some(goal.description.clone()); daily.target_score = goal.target_score; daily.max_time_secs = goal.max_time_secs; info!( "daily challenge seed updated from server: {old_seed} → {} ({})", goal.seed, goal.description ); } } #[allow(clippy::too_many_arguments)] fn handle_daily_completion( mut wins: MessageReader, daily: Res, game: Res, mut progress: ResMut, path: Res, mut completed: MessageWriter, mut xp_awarded: MessageWriter, mut toast: MessageWriter, ) { for ev in wins.read() { if game.0.seed != daily.seed { continue; } // Enforce server-supplied goal constraints when present. if let Some(target) = daily.target_score && ev.score < target { continue; // score goal not met } if let Some(max_secs) = daily.max_time_secs && ev.time_seconds > max_secs { continue; // time limit exceeded } if !progress.0.record_daily_completion(daily.date) { // Already counted today — no-op. continue; } progress.0.add_xp(DAILY_BONUS_XP); xp_awarded.write(XpAwardedEvent { amount: DAILY_BONUS_XP, }); if let Some(target) = &path.0 && let Err(e) = save_progress_to(target, &progress.0) { warn!("failed to save progress after daily completion: {e}"); } completed.write(DailyChallengeCompletedEvent { date: daily.date, streak: progress.0.daily_challenge_streak, }); toast.write(InfoToastEvent( "Daily challenge complete! +100 XP".to_string(), )); } } fn handle_start_daily_request( keys: Res>, mut requests: MessageReader, daily: Res, mut new_game: MessageWriter, mut announce: MessageWriter, ) { // Either C or the HUD Modes-popover "Daily Challenge" row triggers this. let button_clicked = requests.read().count() > 0; if !keys.just_pressed(KeyCode::KeyC) && !button_clicked { return; } new_game.write(NewGameRequestEvent { seed: Some(daily.seed), mode: None, confirmed: false, }); let desc = daily .goal_description .clone() .unwrap_or_else(|| "Daily Challenge".to_string()); announce.write(DailyGoalAnnouncementEvent(desc)); } /// Pure decision logic for the daily-challenge expiry warning. Returns the /// integer minutes-until-UTC-midnight if a warning toast should fire on this /// frame, or `None` if any suppression condition holds. /// /// Suppression rules (in order): /// 1. Player has already completed today's daily challenge. /// 2. The warning has already fired for `daily_date`. /// 3. UTC midnight is more than [`DAILY_EXPIRY_WARNING_MINUTES`] away. /// 4. UTC midnight has already passed for the current calendar day (the /// minutes-remaining is negative — happens for at most one frame at the /// rollover boundary). /// /// Factored out so the threshold/clock behavior is unit-testable without an /// `App`. fn compute_expiry_warning_minutes( daily_date: NaiveDate, last_completed: Option, last_shown: Option, now_utc: DateTime, threshold_mins: i64, ) -> Option { if last_completed == Some(daily_date) { return None; } if last_shown == Some(daily_date) { return None; } let next_midnight = (now_utc.date_naive() + Duration::days(1)) .and_hms_opt(0, 0, 0)? .and_utc(); let mins_remaining = (next_midnight - now_utc).num_minutes(); if !(0..=threshold_mins).contains(&mins_remaining) { return None; } Some(mins_remaining) } /// Each-frame check for the daily-challenge expiry warning. Fires a single /// [`WarningToastEvent`] when the player is within /// [`DAILY_EXPIRY_WARNING_MINUTES`] of UTC midnight reset and hasn't yet /// completed today's challenge. /// /// Idempotent — `DailyExpiryWarningShown` ensures the toast spawns at most /// once per `daily.date`. fn check_daily_expiry_warning( daily: Res, progress: Res, mut shown: ResMut, mut warning: MessageWriter, ) { let Some(mins) = compute_expiry_warning_minutes( daily.date, progress.0.daily_challenge_last_completed, shown.0, Utc::now(), DAILY_EXPIRY_WARNING_MINUTES, ) else { return; }; shown.0 = Some(daily.date); warning.write(WarningToastEvent(format!( "Daily challenge expires in {mins} min" ))); } /// Detects when the local calendar day changes while the app is running /// (e.g. the app stays open past midnight) and refreshes the daily /// challenge resource for the new day. fn check_date_rollover( time: Res