diff options
| author | salmonik <raisersoft@proton.me> | 2026-03-01 14:39:56 +0100 |
|---|---|---|
| committer | salmonik <raisersoft@proton.me> | 2026-03-01 14:39:56 +0100 |
| commit | 7ca1b5aeacd9633e5a3dca9f7596975dbc686508 (patch) | |
| tree | 62de1c6d904e4c2dc1cf60539a16ed1dbb07458b /src/bin/server/database.rs | |
| parent | Revert "folder restructuring" (diff) | |
| download | zagan-bet-7ca1b5aeacd9633e5a3dca9f7596975dbc686508.tar.gz zagan-bet-7ca1b5aeacd9633e5a3dca9f7596975dbc686508.zip | |
slowly preparing for actual casino
Diffstat (limited to 'src/bin/server/database.rs')
| -rw-r--r-- | src/bin/server/database.rs | 44 |
1 files changed, 38 insertions, 6 deletions
diff --git a/src/bin/server/database.rs b/src/bin/server/database.rs index 9c51997..0fac387 100644 --- a/src/bin/server/database.rs +++ b/src/bin/server/database.rs | |||
| @@ -1,22 +1,34 @@ | |||
| 1 | use rusqlite::Connection; | 1 | use rusqlite::Connection; |
| 2 | 2 | ||
| 3 | pub fn init_db() -> Connection { | 3 | pub fn init_db() -> Connection { |
| 4 | let conn = Connection::open("server.db").unwrap(); | 4 | let db_path = std::env::var("SERVER_DB").unwrap_or_else(|_| "server.db".to_string()); |
| 5 | let conn = Connection::open(&db_path).unwrap(); | ||
| 5 | conn.execute( | 6 | conn.execute( |
| 6 | "CREATE TABLE IF NOT EXISTS users ( | 7 | "CREATE TABLE IF NOT EXISTS users ( |
| 7 | username TEXT PRIMARY KEY, | 8 | username TEXT PRIMARY KEY, |
| 8 | hashed_password TEXT NOT NULL | 9 | hashed_password TEXT NOT NULL, |
| 10 | account_id INTEGER UNIQUE, | ||
| 11 | balance INTEGER DEFAULT 1000 | ||
| 9 | )", | 12 | )", |
| 10 | [], | 13 | [], |
| 11 | ).unwrap(); | 14 | ).unwrap(); |
| 15 | // Add column if not exists (for migration) | ||
| 16 | let _ = conn.execute("ALTER TABLE users ADD COLUMN account_id INTEGER UNIQUE", []); | ||
| 17 | let _ = conn.execute("ALTER TABLE users ADD COLUMN balance INTEGER DEFAULT 1000", []); | ||
| 12 | conn | 18 | conn |
| 13 | } | 19 | } |
| 14 | 20 | ||
| 15 | pub fn add_user(conn: &Connection, username: &str, hashed_password: &str) -> Result<(), Box<dyn std::error::Error>> { | 21 | pub fn add_user(conn: &Connection, username: &str, hashed_password: &str) -> Result<(), Box<dyn std::error::Error>> { |
| 16 | conn.execute( | 22 | loop { |
| 17 | "INSERT INTO users (username, hashed_password) VALUES (?1, ?2)", | 23 | let account_id: u32 = rand::random::<u32>(); |
| 18 | [username, hashed_password], | 24 | match conn.execute( |
| 19 | )?; | 25 | "INSERT INTO users (username, hashed_password, account_id, balance) VALUES (?1, ?2, ?3, ?4)", |
| 26 | [username, hashed_password, &account_id.to_string(), "1000"], | ||
| 27 | ) { | ||
| 28 | Ok(_) => break, | ||
| 29 | Err(_) => continue, // Retry if account_id conflict | ||
| 30 | } | ||
| 31 | } | ||
| 20 | Ok(()) | 32 | Ok(()) |
| 21 | } | 33 | } |
| 22 | 34 | ||
| @@ -29,3 +41,23 @@ pub fn get_hashed_password(conn: &Connection, username: &str) -> Option<String> | |||
| 29 | None | 41 | None |
| 30 | } | 42 | } |
| 31 | } | 43 | } |
| 44 | |||
| 45 | pub fn get_account_id(conn: &Connection, username: &str) -> Option<u32> { | ||
| 46 | let mut stmt = conn.prepare("SELECT account_id FROM users WHERE username = ?1").unwrap(); | ||
| 47 | let mut rows = stmt.query([username]).unwrap(); | ||
| 48 | if let Ok(Some(row)) = rows.next() { | ||
| 49 | Some(row.get::<_, u32>(0).ok()?) | ||
| 50 | } else { | ||
| 51 | None | ||
| 52 | } | ||
| 53 | } | ||
| 54 | |||
| 55 | pub fn get_balance(conn: &Connection, username: &str) -> Option<i64> { | ||
| 56 | let mut stmt = conn.prepare("SELECT balance FROM users WHERE username = ?1").unwrap(); | ||
| 57 | let mut rows = stmt.query([username]).unwrap(); | ||
| 58 | if let Ok(Some(row)) = rows.next() { | ||
| 59 | Some(row.get::<_, i64>(0).ok()?) | ||
| 60 | } else { | ||
| 61 | None | ||
| 62 | } | ||
| 63 | } | ||
