diff options
Diffstat (limited to '')
| -rw-r--r-- | src/server/core.rs | 52 |
1 files changed, 45 insertions, 7 deletions
diff --git a/src/server/core.rs b/src/server/core.rs index d4c95f6..e0f0ff2 100644 --- a/src/server/core.rs +++ b/src/server/core.rs | |||
| @@ -17,6 +17,17 @@ pub struct ConnectionState { | |||
| 17 | user: Option<String>, | 17 | user: Option<String>, |
| 18 | } | 18 | } |
| 19 | 19 | ||
| 20 | /// Settle reservations left behind by a previous process. | ||
| 21 | /// | ||
| 22 | /// Call this **once at server startup, before accepting connections**. A | ||
| 23 | /// per-connection sweep would be wrong: it would settle another player's live | ||
| 24 | /// hand. Only one process may own the database. | ||
| 25 | pub fn recover_orphaned_reservations(db_path: &str) -> rusqlite::Result<usize> { | ||
| 26 | let mut conn = Connection::open(db_path)?; | ||
| 27 | database::init_db(&conn); | ||
| 28 | database::abandon_pending(&mut conn) | ||
| 29 | } | ||
| 30 | |||
| 20 | impl ConnectionState { | 31 | impl ConnectionState { |
| 21 | pub fn user(&self) -> Option<&str> { | 32 | pub fn user(&self) -> Option<&str> { |
| 22 | self.user.as_deref() | 33 | self.user.as_deref() |
| @@ -269,13 +280,23 @@ impl ServerCore { | |||
| 269 | if balance < bet { | 280 | if balance < bet { |
| 270 | return "ERROR Insufficient balance".to_string(); | 281 | return "ERROR Insufficient balance".to_string(); |
| 271 | } | 282 | } |
| 272 | let balance = match database::debit(&mut self.conn, user, bet) { | ||
| 273 | Ok(balance) => balance, | ||
| 274 | Err(_) => return "ERROR Insufficient balance".to_string(), | ||
| 275 | }; | ||
| 276 | 283 | ||
| 277 | let hand = self.blackjack.deal(bet as u64); | 284 | let hand = self.blackjack.deal(bet as u64); |
| 278 | let session_id = self.create_blackjack_session(user, &hand); | 285 | let session_id = self.create_blackjack_session(user, &hand); |
| 286 | let balance = match database::reserve_play( | ||
| 287 | &mut self.conn, | ||
| 288 | &session_id, | ||
| 289 | user, | ||
| 290 | "blackjack", | ||
| 291 | bet, | ||
| 292 | &hand.to_json(), | ||
| 293 | ) { | ||
| 294 | Ok(balance) => balance, | ||
| 295 | Err(_) => { | ||
| 296 | let _ = self.sessions.remove_session(&session_id); | ||
| 297 | return "ERROR Insufficient balance".to_string(); | ||
| 298 | } | ||
| 299 | }; | ||
| 279 | self.blackjack_response(user, &session_id, hand, balance) | 300 | self.blackjack_response(user, &session_id, hand, balance) |
| 280 | } | 301 | } |
| 281 | 302 | ||
| @@ -330,11 +351,21 @@ impl ServerCore { | |||
| 330 | if hand.player.len() != 2 { | 351 | if hand.player.len() != 2 { |
| 331 | return "ERROR Double is only allowed on the first two cards".to_string(); | 352 | return "ERROR Double is only allowed on the first two cards".to_string(); |
| 332 | } | 353 | } |
| 333 | if database::debit(&mut self.conn, user, hand.base_bet as i64).is_err() { | 354 | if database::extend_reservation( |
| 355 | &mut self.conn, | ||
| 356 | session_id, | ||
| 357 | hand.base_bet as i64, | ||
| 358 | ) | ||
| 359 | .is_err() | ||
| 360 | { | ||
| 334 | return "ERROR Insufficient balance".to_string(); | 361 | return "ERROR Insufficient balance".to_string(); |
| 335 | } | 362 | } |
| 336 | if let Err(error) = hand.double() { | 363 | if let Err(error) = hand.double() { |
| 337 | let _ = database::credit(&mut self.conn, user, hand.base_bet as i64); | 364 | let _ = database::refund_reservation( |
| 365 | &mut self.conn, | ||
| 366 | session_id, | ||
| 367 | hand.base_bet as i64, | ||
| 368 | ); | ||
| 338 | return format!("ERROR {error}"); | 369 | return format!("ERROR {error}"); |
| 339 | } | 370 | } |
| 340 | } | 371 | } |
| @@ -456,7 +487,14 @@ impl ServerCore { | |||
| 456 | .sessions | 487 | .sessions |
| 457 | .create_session(user, "crash".to_string(), game_state); | 488 | .create_session(user, "crash".to_string(), game_state); |
| 458 | 489 | ||
| 459 | let balance = match database::debit(&mut self.conn, user, bet) { | 490 | let balance = match database::reserve_play( |
| 491 | &mut self.conn, | ||
| 492 | &session_id, | ||
| 493 | user, | ||
| 494 | "crash", | ||
| 495 | bet, | ||
| 496 | &round.to_json(), | ||
| 497 | ) { | ||
| 460 | Ok(balance) => balance, | 498 | Ok(balance) => balance, |
| 461 | Err(_) => { | 499 | Err(_) => { |
| 462 | let _ = self.sessions.remove_session(&session_id); | 500 | let _ = self.sessions.remove_session(&session_id); |
