diff options
Diffstat (limited to '')
| -rw-r--r-- | src/history.rs | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/src/history.rs b/src/history.rs new file mode 100644 index 0000000..5ef375b --- /dev/null +++ b/src/history.rs | |||
| @@ -0,0 +1,54 @@ | |||
| 1 | //! Play history as sent to the client. | ||
| 2 | |||
| 3 | use serde::{Deserialize, Serialize}; | ||
| 4 | |||
| 5 | pub const DEFAULT_HISTORY: u32 = 20; | ||
| 6 | pub const MAX_HISTORY: u32 = 50; | ||
| 7 | |||
| 8 | /// One settled play. `details` is the game's own audit blob. | ||
| 9 | #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] | ||
| 10 | pub struct PlayRecord { | ||
| 11 | pub game: String, | ||
| 12 | pub bet: i64, | ||
| 13 | pub winnings: i64, | ||
| 14 | pub balance_after: i64, | ||
| 15 | /// Timestamp as stored in SQLite (`YYYY-MM-DD HH:MM:SS`, UTC). | ||
| 16 | pub at: String, | ||
| 17 | #[serde(default)] | ||
| 18 | pub details: serde_json::Value, | ||
| 19 | } | ||
| 20 | |||
| 21 | impl PlayRecord { | ||
| 22 | /// Short human summary of the game-specific details. | ||
| 23 | pub fn summary(&self) -> String { | ||
| 24 | let field = |name: &str| -> Option<String> { | ||
| 25 | self.details.get(name).map(|value| match value.as_str() { | ||
| 26 | Some(text) => text.to_string(), | ||
| 27 | None => value.to_string(), | ||
| 28 | }) | ||
| 29 | }; | ||
| 30 | |||
| 31 | match self.game.as_str() { | ||
| 32 | "slots" => field("win_type").unwrap_or_else(|| "spin".to_string()), | ||
| 33 | "roulette" => match (field("number"), field("color")) { | ||
| 34 | (Some(number), Some(color)) => format!("{number} {color}"), | ||
| 35 | (Some(number), None) => number, | ||
| 36 | _ => String::new(), | ||
| 37 | }, | ||
| 38 | "blackjack" => field("result").unwrap_or_default(), | ||
| 39 | "crash" => { | ||
| 40 | let result = field("result").unwrap_or_default(); | ||
| 41 | match self.details.get("multiplier").and_then(|value| value.as_f64()) { | ||
| 42 | Some(multiplier) => format!("{result} {multiplier:.2}x"), | ||
| 43 | None => result, | ||
| 44 | } | ||
| 45 | } | ||
| 46 | _ => String::new(), | ||
| 47 | } | ||
| 48 | } | ||
| 49 | |||
| 50 | /// `YYYY-MM-DD HH:MM`, dropping the seconds. | ||
| 51 | pub fn short_time(&self) -> &str { | ||
| 52 | self.at.get(..16).unwrap_or(&self.at) | ||
| 53 | } | ||
| 54 | } | ||
