summaryrefslogtreecommitdiffstats
path: root/src/server/games/crash.rs
blob: 1728afc7074ab5ca4fe6a7031c64fd83a4bda190 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
use super::traits::{Game, GameError, GamePhase, GameResult, GameState};
use crate::crash::{
    CrashState, SEED_LEN, commit_seed, crash_point_from_seed, payout, seed_to_uniform,
};
use rand::RngCore;
use serde::{Deserialize, Serialize};
use std::collections::{HashMap, VecDeque};
use std::sync::{Arc, Mutex};
use std::time::Instant;

/// Monotonic seconds. Injected so tests can drive a round without sleeping.
pub trait Clock: Send + Sync {
    fn now(&self) -> f64;
}

pub struct RealClock {
    origin: Instant,
}

impl RealClock {
    pub fn new() -> Self {
        Self {
            origin: Instant::now(),
        }
    }
}

impl Clock for RealClock {
    fn now(&self) -> f64 {
        self.origin.elapsed().as_secs_f64()
    }
}

/// A clock a test can move by hand. Clones share the same instant.
#[derive(Clone, Default)]
pub struct ManualClock {
    seconds: Arc<Mutex<f64>>,
}

impl ManualClock {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn set(&self, seconds: f64) {
        *self.seconds.lock().unwrap() = seconds;
    }

    pub fn advance(&self, seconds: f64) {
        *self.seconds.lock().unwrap() += seconds;
    }

    pub fn seconds(&self) -> f64 {
        *self.seconds.lock().unwrap()
    }
}

impl Clock for ManualClock {
    fn now(&self) -> f64 {
        self.seconds()
    }
}

/// One crash round. Lives in the game session's `data` while it runs.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CrashRound {
    pub bet: u64,
    /// Drawn at start, revealed only when the round ends.
    pub crash_point: f64,
    /// The 32 random bytes the crash point commits to, hex encoded. Kept on
    /// the server until the round ends.
    pub seed: String,
    /// `sha256(seed)` in hex, sent with the start reply.
    pub commit: String,
    pub started_at: f64,
    /// Server Unix time at start, milliseconds, for clock sync.
    pub started_at_ms: u64,
    /// `running`, `cashed`, `crashed` or `forfeit`.
    pub phase: String,
    pub result: String,
    /// Live while running; frozen at the final value afterwards.
    pub multiplier: f64,
    pub payout: u64,
}

impl CrashRound {
    pub fn new(bet: u64, seed: [u8; SEED_LEN], started_at: f64, started_at_ms: u64) -> Self {
        let crash_point = crash_point_from_seed(&seed);
        Self {
            bet,
            crash_point,
            seed: seed_hex(&seed),
            commit: commit_seed(&seed),
            started_at,
            started_at_ms,
            phase: "running".to_string(),
            result: "playing".to_string(),
            multiplier: 1.0,
            payout: 0,
        }
    }

    pub fn is_running(&self) -> bool {
        self.phase == "running"
    }

    pub fn cash_out(&mut self, multiplier: f64) -> u64 {
        self.multiplier = multiplier;
        self.payout = payout(self.bet, multiplier);
        self.phase = "cashed".to_string();
        self.result = "cashed".to_string();
        self.payout
    }

    /// The round ended before the player could cash out.
    pub fn crash(&mut self) {
        self.multiplier = self.crash_point;
        self.payout = 0;
        self.phase = "crashed".to_string();
        self.result = "crashed".to_string();
    }

    pub fn forfeit(&mut self) {
        self.payout = 0;
        self.phase = "forfeit".to_string();
        self.result = "forfeit".to_string();
    }

    pub fn state(&self, session_id: &str, balance: i64) -> CrashState {
        CrashState {
            session_id: session_id.to_string(),
            phase: self.phase.clone(),
            bet: self.bet,
            balance,
            multiplier: self.multiplier,
            potential: payout(self.bet, self.multiplier),
            started_at: self.started_at_ms,
            crash_point: if self.is_running() {
                None
            } else {
                Some(self.crash_point)
            },
            payout: self.payout,
            result: self.result.clone(),
            commit: Some(self.commit.clone()),
            seed: if self.is_running() {
                None
            } else {
                Some(self.seed.clone())
            },
        }
    }

    pub fn to_json(&self) -> String {
        serde_json::to_string(self).unwrap_or_default()
    }

    pub fn from_json(raw: &str) -> Result<Self, serde_json::Error> {
        serde_json::from_str(raw)
    }
}

/// Crash-point generator. Tests queue fixed uniforms; production draws random
/// ones. The visible crash point stays hidden until `poll` or cashout ends the
/// round.
pub struct CrashGame {
    uniforms: VecDeque<f64>,
}

/// Hex encode a round seed. Lowercase, no separators.
fn seed_hex(seed: &[u8]) -> String {
    let mut out = String::with_capacity(seed.len() * 2);
    for byte in seed {
        use std::fmt::Write as _;
        let _ = write!(out, "{byte:02x}");
    }
    out
}

/// Draw a seed whose committed point matches a queued test uniform.
///
/// The first eight bytes are the big-endian integer closest to
/// `uniform * 2^64`; the remaining bytes are zero. This keeps the existing
/// deterministic crash-point tests meaningful while every round still has a
/// real seed.
fn seed_from_uniform(uniform: f64) -> [u8; SEED_LEN] {
    let uniform = if uniform.is_finite() {
        uniform.clamp(0.0, 0.999_999_999_999_999_9)
    } else {
        0.0
    };
    let value = (uniform * 18_446_744_073_709_551_616.0) as u64;
    let mut seed = [0u8; SEED_LEN];
    seed[..8].copy_from_slice(&value.to_be_bytes());
    seed
}

impl CrashGame {
    pub fn new() -> Self {
        Self {
            uniforms: VecDeque::new(),
        }
    }

    pub fn with_uniform(uniform: f64) -> Self {
        Self::with_uniforms([uniform])
    }

    pub fn with_uniforms(uniforms: impl IntoIterator<Item = f64>) -> Self {
        Self {
            uniforms: uniforms.into_iter().collect(),
        }
    }

    /// Draw the next round seed. Tests supply uniforms; production uses the
    /// operating system's random source.
    pub fn next_seed(&mut self) -> [u8; SEED_LEN] {
        if let Some(uniform) = self.uniforms.pop_front() {
            return seed_from_uniform(uniform);
        }
        let mut seed = [0u8; SEED_LEN];
        rand::thread_rng().fill_bytes(&mut seed);
        seed
    }

    /// Kept for callers that only need the point a queued uniform encodes.
    pub fn next_crash_point(&mut self) -> f64 {
        crash_point_from_seed(&self.next_seed())
    }

    /// The uniform encoded by a seed, exposed for verification tests.
    pub fn uniform_of(seed: &[u8; SEED_LEN]) -> f64 {
        seed_to_uniform(seed)
    }
}

impl Game for CrashGame {
    fn game_type(&self) -> &'static str {
        "crash"
    }

    fn get_name(&self) -> &'static str {
        "Crash"
    }

    fn get_description(&self) -> &'static str {
        "Crash: cash out before the multiplier crashes."
    }

    fn prepare_state(&self, _username: &str, bet_amount: u64) -> GameState {
        let mut data = HashMap::new();
        data.insert("status".to_string(), "ready".to_string());
        GameState {
            session_id: String::new(),
            game_type: "crash".to_string(),
            bet_amount,
            phase: GamePhase::WaitingToStart,
            data,
        }
    }

    fn process_action(
        &self,
        _state: &GameState,
        _action: &str,
        _params: &[&str],
    ) -> Result<GameState, GameError> {
        Err(GameError::InvalidAction(
            "Crash uses CRASH_CASHOUT and CRASH_FORFEIT".to_string(),
        ))
    }

    fn calculate_result(&self, _state: &GameState) -> Result<GameResult, GameError> {
        Err(GameError::InternalError(
            "Crash settles through CRASH_CASHOUT".to_string(),
        ))
    }
}