summaryrefslogtreecommitdiffstats
path: root/src/crash.rs
diff options
context:
space:
mode:
authorsalmonik <salmonik@tuta.io>2026-11-08 03:29:35 +0100
committersalmonik <salmonik@tuta.io>2026-11-08 03:29:35 +0100
commit058432130628d00764663ecf819a9adc6793768d (patch)
treed3f49f111fbfaa48647cca2600adcf9017af2d30 /src/crash.rs
parentweb: serve latest.php through PHP and refuse stray PHP files (diff)
downloadzagan-bet-058432130628d00764663ecf819a9adc6793768d.tar.gz
zagan-bet-058432130628d00764663ecf819a9adc6793768d.zip
v5.0.0: protocol v5 and the browser frontend
Protocol v5 replaces newline lines with bounded [u32 len][text] frames and REQ/RES/EVT envelopes. Requests carry correlation ids and the server replays the last 256 replies per connection, so a gateway retry cannot double-debit and a late reply cannot clobber a newer request. HELLO/PONG carry server time; crash rounds announce started_at and push coalesced 10 Hz ticks. Crash now commits sha256(seed) before a round and reveals the seed when it ends, so clients recompute the crash point. Adds the webd/ gateway: one blocking thread, one ClientCore and one pinned upstream TLS connection per session, an HttpOnly session cookie, takeover, and a 60-second grace window. The browser sends Command JSON and renders View JSON; it never speaks the game protocol or stores credentials. Adds the vanilla TypeScript browser app at /play/ (login, lobby, all four games, history, notices, crash verification), the nginx /play and /ws routes, and the headless DOM smoke test. webd and the app stay out of the TUI/GUI/server build graph. The client/server version moves to 5.0.0 in lockstep; v4 peers are refused at HELLO.
Diffstat (limited to 'src/crash.rs')
-rw-r--r--src/crash.rs54
1 files changed, 54 insertions, 0 deletions
diff --git a/src/crash.rs b/src/crash.rs
index 9d6e98c..40b8743 100644
--- a/src/crash.rs
+++ b/src/crash.rs
@@ -6,6 +6,7 @@
6//! clock, never from anything the client sends. 6//! clock, never from anything the client sends.
7 7
8use serde::{Deserialize, Serialize}; 8use serde::{Deserialize, Serialize};
9use sha2::{Digest, Sha256};
9use std::time::Duration; 10use std::time::Duration;
10 11
11/// `exp(GROWTH_RATE * t)` doubles the multiplier every five seconds. 12/// `exp(GROWTH_RATE * t)` doubles the multiplier every five seconds.
@@ -47,6 +48,53 @@ pub fn crash_point_from_uniform(u: f64) -> f64 {
47 ((1.0 - HOUSE_EDGE) / (1.0 - u)).clamp(1.0, MAX_MULTIPLIER) 48 ((1.0 - HOUSE_EDGE) / (1.0 - u)).clamp(1.0, MAX_MULTIPLIER)
48} 49}
49 50
51/// How many bytes a round seed contains. The first eight bytes choose the
52/// crash point; the rest are revealed and hashed with it.
53pub const SEED_LEN: usize = 32;
54
55/// Hex-encoded SHA-256 of a round seed, sent before the round starts.
56pub fn commit_seed(seed: &[u8]) -> String {
57 let digest = Sha256::digest(seed);
58 format!("{digest:x}")
59}
60
61/// Uniform draw encoded by a seed: the first eight bytes, big-endian.
62pub fn seed_to_uniform(seed: &[u8]) -> f64 {
63 let mut bytes = [0u8; 8];
64 let take = seed.len().min(8);
65 bytes[..take].copy_from_slice(&seed[..take]);
66 u64::from_be_bytes(bytes) as f64 / 18_446_744_073_709_551_616.0
67}
68
69/// The crash point a seed commits to. A client recomputes this after the
70/// reveal and checks it against the published commit.
71pub fn crash_point_from_seed(seed: &[u8]) -> f64 {
72 crash_point_from_uniform(seed_to_uniform(seed))
73}
74
75/// Decode a lowercase or uppercase hex seed. Returns `None` for odd lengths,
76/// bad digits or a seed that is not exactly [`SEED_LEN`] bytes.
77pub fn seed_from_hex(raw: &str) -> Option<Vec<u8>> {
78 if raw.len() != SEED_LEN * 2 {
79 return None;
80 }
81 let mut seed = Vec::with_capacity(SEED_LEN);
82 for pair in raw.as_bytes().chunks_exact(2) {
83 let high = (pair[0] as char).to_digit(16)?;
84 let low = (pair[1] as char).to_digit(16)?;
85 seed.push((high * 16 + low) as u8);
86 }
87 Some(seed)
88}
89
90/// Check a revealed seed against the commit published before the round.
91pub fn verify_seed(seed_hex: &str, commit: &str) -> bool {
92 let Some(seed) = seed_from_hex(seed_hex) else {
93 return false;
94 };
95 commit_seed(&seed).eq_ignore_ascii_case(commit)
96}
97
50/// Whole-Kr cashout value. Balances are integers, so the payout is floored. 98/// Whole-Kr cashout value. Balances are integers, so the payout is floored.
51pub fn payout(bet: u64, multiplier: f64) -> u64 { 99pub fn payout(bet: u64, multiplier: f64) -> u64 {
52 if bet == 0 || !multiplier.is_finite() || multiplier <= 0.0 { 100 if bet == 0 || !multiplier.is_finite() || multiplier <= 0.0 {
@@ -72,10 +120,16 @@ pub struct CrashState {
72 pub multiplier: f64, 120 pub multiplier: f64,
73 /// `floor(bet * multiplier)` for the value above. 121 /// `floor(bet * multiplier)` for the value above.
74 pub potential: u64, 122 pub potential: u64,
123 /// Server Unix time when the round started, milliseconds.
124 pub started_at: u64,
75 /// Hidden until the round ends. 125 /// Hidden until the round ends.
76 pub crash_point: Option<f64>, 126 pub crash_point: Option<f64>,
77 pub payout: u64, 127 pub payout: u64,
78 pub result: String, 128 pub result: String,
129 /// `sha256(seed)`, present for the whole round.
130 pub commit: Option<String>,
131 /// The seed itself, present only after the round ends.
132 pub seed: Option<String>,
79} 133}
80 134
81impl CrashState { 135impl CrashState {