summaryrefslogtreecommitdiffstats
path: root/src/history.rs
blob: 5ef375b8e5a064ad689c3597a07d6b60936e2d2c (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
//! Play history as sent to the client.

use serde::{Deserialize, Serialize};

pub const DEFAULT_HISTORY: u32 = 20;
pub const MAX_HISTORY: u32 = 50;

/// One settled play. `details` is the game's own audit blob.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct PlayRecord {
    pub game: String,
    pub bet: i64,
    pub winnings: i64,
    pub balance_after: i64,
    /// Timestamp as stored in SQLite (`YYYY-MM-DD HH:MM:SS`, UTC).
    pub at: String,
    #[serde(default)]
    pub details: serde_json::Value,
}

impl PlayRecord {
    /// Short human summary of the game-specific details.
    pub fn summary(&self) -> String {
        let field = |name: &str| -> Option<String> {
            self.details.get(name).map(|value| match value.as_str() {
                Some(text) => text.to_string(),
                None => value.to_string(),
            })
        };

        match self.game.as_str() {
            "slots" => field("win_type").unwrap_or_else(|| "spin".to_string()),
            "roulette" => match (field("number"), field("color")) {
                (Some(number), Some(color)) => format!("{number} {color}"),
                (Some(number), None) => number,
                _ => String::new(),
            },
            "blackjack" => field("result").unwrap_or_default(),
            "crash" => {
                let result = field("result").unwrap_or_default();
                match self.details.get("multiplier").and_then(|value| value.as_f64()) {
                    Some(multiplier) => format!("{result} {multiplier:.2}x"),
                    None => result,
                }
            }
            _ => String::new(),
        }
    }

    /// `YYYY-MM-DD HH:MM`, dropping the seconds.
    pub fn short_time(&self) -> &str {
        self.at.get(..16).unwrap_or(&self.at)
    }
}