summaryrefslogtreecommitdiffstats
path: root/src/client/games/response.rs
diff options
context:
space:
mode:
authorsalmonik <salmonik@tuta.io>2026-10-28 22:28:20 +0100
committersalmonik <salmonik@tuta.io>2026-10-28 22:28:20 +0100
commit86d5f832c8452aad2313489b82eb8fd514215b95 (patch)
tree596a9be39c3d91ebc20cc8b877549e3d10895901 /src/client/games/response.rs
parentv0.2.2: split client/server logic into the library (diff)
downloadzagan-bet-86d5f832c8452aad2313489b82eb8fd514215b95.tar.gz
zagan-bet-86d5f832c8452aad2313489b82eb8fd514215b95.zip
v0.2.3: add ClientCore and fix the game board rendering
Diffstat (limited to 'src/client/games/response.rs')
-rw-r--r--src/client/games/response.rs60
1 files changed, 60 insertions, 0 deletions
diff --git a/src/client/games/response.rs b/src/client/games/response.rs
new file mode 100644
index 0000000..6948805
--- /dev/null
+++ b/src/client/games/response.rs
@@ -0,0 +1,60 @@
1#[derive(Debug, Clone, PartialEq, Eq)]
2pub struct SpinResult {
3 pub session_id: String,
4 pub winnings: u64,
5 pub bet_amount: u64,
6 pub balance: i64,
7 pub reels: String,
8 pub win_type: String,
9}
10
11/// Parse one server reply to `PLAY_START`.
12///
13/// `Ok` carries the settled spin; `Err` carries a message that is safe to
14/// show to the player (server errors are reported without the `ERROR ` prefix).
15pub fn parse_spin_response(response: &str) -> Result<SpinResult, String> {
16 let response = response.trim();
17 if response.is_empty() {
18 return Err("No response from server".to_string());
19 }
20
21 if let Some(message) = response.strip_prefix("ERROR") {
22 let message = message.trim();
23 return Err(if message.is_empty() {
24 "Server error".to_string()
25 } else {
26 message.to_string()
27 });
28 }
29
30 let parts: Vec<&str> = response.split_whitespace().collect();
31 if parts.first() != Some(&"GAME_RESULT") {
32 return Err(format!("Unexpected server response: {response}"));
33 }
34 if parts.len() < 7 {
35 return Err("Malformed GAME_RESULT from server".to_string());
36 }
37
38 let winnings = parts[2]
39 .parse::<u64>()
40 .map_err(|_| format!("Invalid winnings from server: {}", parts[2]))?;
41 let bet_amount = parts[3]
42 .parse::<u64>()
43 .map_err(|_| format!("Invalid bet from server: {}", parts[3]))?;
44 let balance = parts[4]
45 .parse::<i64>()
46 .map_err(|_| format!("Invalid balance from server: {}", parts[4]))?;
47
48 if parts[5].chars().count() != 3 {
49 return Err(format!("Invalid reels from server: {}", parts[5]));
50 }
51
52 Ok(SpinResult {
53 session_id: parts[1].to_string(),
54 winnings,
55 bet_amount,
56 balance,
57 reels: parts[5].to_string(),
58 win_type: parts[6].to_string(),
59 })
60}