diff options
| author | salmonik <salmonik@tuta.io> | 2026-10-29 18:22:40 +0100 |
|---|---|---|
| committer | salmonik <salmonik@tuta.io> | 2026-10-29 18:22:40 +0100 |
| commit | b5a52fc0312afdf5d5fbe53275dc88d6d881c2ff (patch) | |
| tree | d12e3c7f6c23ae1698199d16ee763551edfd8c12 /src/server/database.rs | |
| parent | v2.1.2: ignore ANSI when measuring board height (diff) | |
| download | zagan-bet-b5a52fc0312afdf5d5fbe53275dc88d6d881c2ff.tar.gz zagan-bet-b5a52fc0312afdf5d5fbe53275dc88d6d881c2ff.zip | |
v2.2.0: add blackjack with reserved bets and hit/stand/double
Diffstat (limited to 'src/server/database.rs')
| -rw-r--r-- | src/server/database.rs | 81 |
1 files changed, 81 insertions, 0 deletions
diff --git a/src/server/database.rs b/src/server/database.rs index 2a1fc40..99e0dde 100644 --- a/src/server/database.rs +++ b/src/server/database.rs | |||
| @@ -177,3 +177,84 @@ pub fn count_plays(conn: &Connection, username: &str) -> i64 { | |||
| 177 | ) | 177 | ) |
| 178 | .unwrap_or(0) | 178 | .unwrap_or(0) |
| 179 | } | 179 | } |
| 180 | |||
| 181 | /// Reserve a bet before a multi-step hand starts. | ||
| 182 | pub fn debit(conn: &mut Connection, username: &str, amount: i64) -> rusqlite::Result<i64> { | ||
| 183 | let tx = conn.transaction()?; | ||
| 184 | let updated = tx.execute( | ||
| 185 | "UPDATE users SET balance = balance - ?1 | ||
| 186 | WHERE username = ?2 AND balance >= ?1", | ||
| 187 | params![amount, username], | ||
| 188 | )?; | ||
| 189 | if updated != 1 { | ||
| 190 | return Err(rusqlite::Error::QueryReturnedNoRows); | ||
| 191 | } | ||
| 192 | let balance = tx.query_row( | ||
| 193 | "SELECT balance FROM users WHERE username = ?1", | ||
| 194 | [username], | ||
| 195 | |row| row.get(0), | ||
| 196 | )?; | ||
| 197 | tx.commit()?; | ||
| 198 | Ok(balance) | ||
| 199 | } | ||
| 200 | |||
| 201 | /// Put a reserved bet back (disconnect or cancel). | ||
| 202 | pub fn credit(conn: &mut Connection, username: &str, amount: i64) -> rusqlite::Result<i64> { | ||
| 203 | let tx = conn.transaction()?; | ||
| 204 | tx.execute( | ||
| 205 | "UPDATE users SET balance = balance + ?1 WHERE username = ?2", | ||
| 206 | params![amount, username], | ||
| 207 | )?; | ||
| 208 | let balance = tx.query_row( | ||
| 209 | "SELECT balance FROM users WHERE username = ?1", | ||
| 210 | [username], | ||
| 211 | |row| row.get(0), | ||
| 212 | )?; | ||
| 213 | tx.commit()?; | ||
| 214 | Ok(balance) | ||
| 215 | } | ||
| 216 | |||
| 217 | /// Settle a hand whose bet was already reserved: credit winnings, write the | ||
| 218 | /// audit row, and return the new balance. | ||
| 219 | #[allow(clippy::too_many_arguments)] | ||
| 220 | pub fn settle_play( | ||
| 221 | conn: &mut Connection, | ||
| 222 | session_id: &str, | ||
| 223 | username: &str, | ||
| 224 | game_type: &str, | ||
| 225 | bet_amount: i64, | ||
| 226 | winnings: i64, | ||
| 227 | details: &str, | ||
| 228 | ) -> rusqlite::Result<i64> { | ||
| 229 | let tx = conn.transaction()?; | ||
| 230 | |||
| 231 | tx.execute( | ||
| 232 | "UPDATE users SET balance = balance + ?1 WHERE username = ?2", | ||
| 233 | params![winnings, username], | ||
| 234 | )?; | ||
| 235 | |||
| 236 | let balance_after: i64 = tx.query_row( | ||
| 237 | "SELECT balance FROM users WHERE username = ?1", | ||
| 238 | [username], | ||
| 239 | |row| row.get(0), | ||
| 240 | )?; | ||
| 241 | |||
| 242 | tx.execute( | ||
| 243 | "INSERT INTO plays | ||
| 244 | (session_id, username, game_type, bet_amount, winnings, | ||
| 245 | balance_after, details) | ||
| 246 | VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7)", | ||
| 247 | params![ | ||
| 248 | session_id, | ||
| 249 | username, | ||
| 250 | game_type, | ||
| 251 | bet_amount, | ||
| 252 | winnings, | ||
| 253 | balance_after, | ||
| 254 | details | ||
| 255 | ], | ||
| 256 | )?; | ||
| 257 | |||
| 258 | tx.commit()?; | ||
| 259 | Ok(balance_after) | ||
| 260 | } | ||
