backend: Some refactoring
Reuse "check if player exists" fn where needed. Also, the same fn was refactored to handle using an existing DB Connection.
This commit is contained in:
parent
fe56adf562
commit
c5b9156df3
1 changed files with 28 additions and 21 deletions
|
@ -53,19 +53,19 @@ impl DBHandler {
|
||||||
}
|
}
|
||||||
let conn = conn_result.unwrap();
|
let conn = conn_result.unwrap();
|
||||||
loop {
|
loop {
|
||||||
let stmt_result = conn.prepare("SELECT id FROM players WHERE id = ?;");
|
let exists_result = self.check_if_player_exists(Some(&conn), player_id);
|
||||||
if let Err(e) = stmt_result {
|
if let Ok(exists) = exists_result {
|
||||||
println!("Failed to create sqlite statement: {:?}", e);
|
if exists {
|
||||||
|
player_id = thread_rng().gen();
|
||||||
|
} else {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
let error = exists_result.unwrap_err();
|
||||||
|
println!("Failed to check if player exists in db: {:?}", error);
|
||||||
self.shutdown_tx.send(()).ok();
|
self.shutdown_tx.send(()).ok();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
let mut stmt = stmt_result.unwrap();
|
|
||||||
match stmt.query_row([player_id], |_row| Ok(())) {
|
|
||||||
Ok(_) => {
|
|
||||||
player_id = thread_rng().gen();
|
|
||||||
}
|
|
||||||
Err(_) => break,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
let insert_result = conn.execute(
|
let insert_result = conn.execute(
|
||||||
"INSERT INTO players (id, date_added) VALUES (?, datetime());",
|
"INSERT INTO players (id, date_added) VALUES (?, datetime());",
|
||||||
|
@ -243,7 +243,7 @@ impl DBHandler {
|
||||||
|
|
||||||
fn check_if_player_is_paired(&self, player_id: u32) -> Result<CheckPairingType, String> {
|
fn check_if_player_is_paired(&self, player_id: u32) -> Result<CheckPairingType, String> {
|
||||||
{
|
{
|
||||||
let player_exists_result = self.check_if_player_exists(player_id);
|
let player_exists_result = self.check_if_player_exists(None, player_id);
|
||||||
if player_exists_result.is_err() || !player_exists_result.unwrap() {
|
if player_exists_result.is_err() || !player_exists_result.unwrap() {
|
||||||
// player doesn't exist
|
// player doesn't exist
|
||||||
return Ok((false, false, true));
|
return Ok((false, false, true));
|
||||||
|
@ -271,9 +271,12 @@ impl DBHandler {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn check_if_player_exists(&self, player_id: u32) -> Result<bool, String> {
|
fn check_if_player_exists(
|
||||||
let conn = self.get_conn(DBFirstRun::NotFirstRun)?;
|
&self,
|
||||||
|
conn: Option<&Connection>,
|
||||||
|
player_id: u32,
|
||||||
|
) -> Result<bool, String> {
|
||||||
|
if let Some(conn) = conn {
|
||||||
let check_player_row =
|
let check_player_row =
|
||||||
conn.query_row("SELECT id FROM players WHERE id = ?;", [player_id], |row| {
|
conn.query_row("SELECT id FROM players WHERE id = ?;", [player_id], |row| {
|
||||||
row.get::<usize, u32>(0)
|
row.get::<usize, u32>(0)
|
||||||
|
@ -283,6 +286,10 @@ impl DBHandler {
|
||||||
} else {
|
} else {
|
||||||
Ok(false)
|
Ok(false)
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
let conn = self.get_conn(DBFirstRun::NotFirstRun)?;
|
||||||
|
self.check_if_player_exists(Some(&conn), player_id)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue