diff options
Diffstat (limited to 'src/server/core.rs')
| -rw-r--r-- | src/server/core.rs | 45 |
1 files changed, 15 insertions, 30 deletions
diff --git a/src/server/core.rs b/src/server/core.rs index e0f0ff2..4b68ccb 100644 --- a/src/server/core.rs +++ b/src/server/core.rs | |||
| @@ -49,11 +49,7 @@ impl ServerCore { | |||
| 49 | Self::with_registry(conn, GameRegistry::with_defaults(), DEFAULT_COST) | 49 | Self::with_registry(conn, GameRegistry::with_defaults(), DEFAULT_COST) |
| 50 | } | 50 | } |
| 51 | 51 | ||
| 52 | pub fn with_registry( | 52 | pub fn with_registry(conn: Connection, registry: GameRegistry, bcrypt_cost: u32) -> Self { |
| 53 | conn: Connection, | ||
| 54 | registry: GameRegistry, | ||
| 55 | bcrypt_cost: u32, | ||
| 56 | ) -> Self { | ||
| 57 | Self { | 53 | Self { |
| 58 | conn, | 54 | conn, |
| 59 | sessions: SessionManager::new(), | 55 | sessions: SessionManager::new(), |
| @@ -115,8 +111,7 @@ impl ServerCore { | |||
| 115 | } | 111 | } |
| 116 | "GAME_INFO" => { | 112 | "GAME_INFO" => { |
| 117 | if parts.len() != 2 { | 113 | if parts.len() != 2 { |
| 118 | "ERROR Invalid GAME_INFO format. Usage: GAME_INFO <game_type>" | 114 | "ERROR Invalid GAME_INFO format. Usage: GAME_INFO <game_type>".to_string() |
| 119 | .to_string() | ||
| 120 | } else { | 115 | } else { |
| 121 | match self.registry.get_game(parts[1]) { | 116 | match self.registry.get_game(parts[1]) { |
| 122 | Some(game) => format!( | 117 | Some(game) => format!( |
| @@ -143,16 +138,14 @@ impl ServerCore { | |||
| 143 | } | 138 | } |
| 144 | "CRASH_CASHOUT" => { | 139 | "CRASH_CASHOUT" => { |
| 145 | if parts.len() != 2 { | 140 | if parts.len() != 2 { |
| 146 | "ERROR Invalid CRASH_CASHOUT format. Usage: CRASH_CASHOUT <session>" | 141 | "ERROR Invalid CRASH_CASHOUT format. Usage: CRASH_CASHOUT <session>".to_string() |
| 147 | .to_string() | ||
| 148 | } else { | 142 | } else { |
| 149 | self.handle_crash_end(state, parts[1], false) | 143 | self.handle_crash_end(state, parts[1], false) |
| 150 | } | 144 | } |
| 151 | } | 145 | } |
| 152 | "CRASH_FORFEIT" => { | 146 | "CRASH_FORFEIT" => { |
| 153 | if parts.len() != 2 { | 147 | if parts.len() != 2 { |
| 154 | "ERROR Invalid CRASH_FORFEIT format. Usage: CRASH_FORFEIT <session>" | 148 | "ERROR Invalid CRASH_FORFEIT format. Usage: CRASH_FORFEIT <session>".to_string() |
| 155 | .to_string() | ||
| 156 | } else { | 149 | } else { |
| 157 | self.handle_crash_end(state, parts[1], true) | 150 | self.handle_crash_end(state, parts[1], true) |
| 158 | } | 151 | } |
| @@ -351,12 +344,8 @@ impl ServerCore { | |||
| 351 | if hand.player.len() != 2 { | 344 | if hand.player.len() != 2 { |
| 352 | return "ERROR Double is only allowed on the first two cards".to_string(); | 345 | return "ERROR Double is only allowed on the first two cards".to_string(); |
| 353 | } | 346 | } |
| 354 | if database::extend_reservation( | 347 | if database::extend_reservation(&mut self.conn, session_id, hand.base_bet as i64) |
| 355 | &mut self.conn, | 348 | .is_err() |
| 356 | session_id, | ||
| 357 | hand.base_bet as i64, | ||
| 358 | ) | ||
| 359 | .is_err() | ||
| 360 | { | 349 | { |
| 361 | return "ERROR Insufficient balance".to_string(); | 350 | return "ERROR Insufficient balance".to_string(); |
| 362 | } | 351 | } |
| @@ -515,7 +504,12 @@ impl ServerCore { | |||
| 515 | 504 | ||
| 516 | /// Cash out or forfeit. The multiplier comes from the server clock at the | 505 | /// Cash out or forfeit. The multiplier comes from the server clock at the |
| 517 | /// moment the request is handled; the client never supplies one. | 506 | /// moment the request is handled; the client never supplies one. |
| 518 | fn handle_crash_end(&mut self, state: &ConnectionState, session_id: &str, forfeit: bool) -> String { | 507 | fn handle_crash_end( |
| 508 | &mut self, | ||
| 509 | state: &ConnectionState, | ||
| 510 | session_id: &str, | ||
| 511 | forfeit: bool, | ||
| 512 | ) -> String { | ||
| 519 | let Some(user) = state.user.as_deref() else { | 513 | let Some(user) = state.user.as_deref() else { |
| 520 | return "ERROR Not logged in".to_string(); | 514 | return "ERROR Not logged in".to_string(); |
| 521 | }; | 515 | }; |
| @@ -698,11 +692,7 @@ impl ServerCore { | |||
| 698 | .get("number") | 692 | .get("number") |
| 699 | .cloned() | 693 | .cloned() |
| 700 | .unwrap_or_default(); | 694 | .unwrap_or_default(); |
| 701 | let color = result | 695 | let color = result.result_data.get("color").cloned().unwrap_or_default(); |
| 702 | .result_data | ||
| 703 | .get("color") | ||
| 704 | .cloned() | ||
| 705 | .unwrap_or_default(); | ||
| 706 | let bets = result | 696 | let bets = result |
| 707 | .result_data | 697 | .result_data |
| 708 | .get("bets") | 698 | .get("bets") |
| @@ -712,11 +702,7 @@ impl ServerCore { | |||
| 712 | "ROULETTE_RESULT {session_id} {winnings} {bet_amount} {new_balance} {number} {color} {bets}" | 702 | "ROULETTE_RESULT {session_id} {winnings} {bet_amount} {new_balance} {number} {color} {bets}" |
| 713 | ) | 703 | ) |
| 714 | } else { | 704 | } else { |
| 715 | let reels = result | 705 | let reels = result.result_data.get("reels").cloned().unwrap_or_default(); |
| 716 | .result_data | ||
| 717 | .get("reels") | ||
| 718 | .cloned() | ||
| 719 | .unwrap_or_default(); | ||
| 720 | let win_type = result | 706 | let win_type = result |
| 721 | .result_data | 707 | .result_data |
| 722 | .get("win_type") | 708 | .get("win_type") |
| @@ -753,8 +739,7 @@ impl ServerCore { | |||
| 753 | if let Ok(mut hand) = BlackjackHand::from_json(raw) { | 739 | if let Ok(mut hand) = BlackjackHand::from_json(raw) { |
| 754 | if !hand.is_done() { | 740 | if !hand.is_done() { |
| 755 | hand.forfeit(); | 741 | hand.forfeit(); |
| 756 | let balance = | 742 | let balance = database::get_balance(&self.conn, user).unwrap_or(0); |
| 757 | database::get_balance(&self.conn, user).unwrap_or(0); | ||
| 758 | let final_state = hand.state(&session.session_id, balance); | 743 | let final_state = hand.state(&session.session_id, balance); |
| 759 | let details = serde_json::to_string(&final_state) | 744 | let details = serde_json::to_string(&final_state) |
| 760 | .unwrap_or_else(|_| "{}".to_string()); | 745 | .unwrap_or_else(|_| "{}".to_string()); |
