Skip to main content

pinch_points/app/
daily.rs

1//! The daily challenge: one beach a day, the same one for everybody.
2//!
3//! No server and no handshake - determinism means the seed *is* the
4//! agreement, and the seed is the date.
5
6use bevy::prelude::*;
7
8/// The daily challenge: everyone in the world gets the same generated
9/// arena for a given (UTC) day, thanks to determinism. `active` while the
10/// current versus round is the daily.
11#[derive(Resource, Default)]
12pub struct Daily {
13    pub active: bool,
14}
15
16impl Daily {
17    /// Days since the epoch, UTC: the worldwide shared seed basis.
18    pub fn today() -> u32 {
19        (crate::app::clock::now_secs() / 86_400) as u32
20    }
21
22    pub fn seed() -> u64 {
23        Self::seed_for(Self::today())
24    }
25
26    /// The arena seed for a given day number; pure so it can be tested.
27    pub fn seed_for(day: u32) -> u64 {
28        0xDA11_0000 ^ u64::from(day)
29    }
30}
31
32#[cfg(test)]
33mod tests {
34    use super::Daily;
35
36    #[test]
37    fn daily_seed_is_stable_within_a_day_and_fresh_across_days() {
38        assert_eq!(Daily::seed_for(20_662), Daily::seed_for(20_662));
39        assert_ne!(Daily::seed_for(20_662), Daily::seed_for(20_663));
40        // The live seed is derived from today's number, nothing else.
41        assert_eq!(Daily::seed(), Daily::seed_for(Daily::today()));
42    }
43}