fmt: repo-wide baseline (WI-2, issue #2)

This commit is contained in:
Jan-Peter von Hunnius 2026-10-03 11:53:59 +02:00
parent 899fde8fc3
commit 337e9bcf2c
19 changed files with 847 additions and 335 deletions

View file

@ -1,5 +1,5 @@
use rusqlite::Connection;
use sb_shared::{Character, CharacterStats, Class, Race, User, Role};
use sb_shared::{Character, CharacterStats, Class, Race, Role, User};
use std::collections::HashMap;
use std::path::Path;
use std::sync::Mutex;
@ -11,7 +11,9 @@ pub struct Database {
impl Database {
pub fn open(path: &Path) -> Result<Self, rusqlite::Error> {
let conn = Connection::open(path)?;
let db = Self { conn: Mutex::new(conn) };
let db = Self {
conn: Mutex::new(conn),
};
db.run_migrations()?;
Ok(db)
}
@ -59,7 +61,7 @@ impl Database {
quantity INTEGER NOT NULL DEFAULT 0
);
CREATE INDEX IF NOT EXISTS idx_characters_user ON characters(user_id);
"
",
)?;
Ok(())
}
@ -114,10 +116,15 @@ impl Database {
let inventory_json: String = row.get(22)?;
let known_spells_json: String = row.get(24)?;
let equipment_raw: HashMap<String, String> = serde_json::from_str(&equipment_json).unwrap_or_default();
let equipment: HashMap<String, Option<String>> = equipment_raw.into_iter().map(|(k, v)| (k, Some(v))).collect();
let equipment_raw: HashMap<String, String> =
serde_json::from_str(&equipment_json).unwrap_or_default();
let equipment: HashMap<String, Option<String>> = equipment_raw
.into_iter()
.map(|(k, v)| (k, Some(v)))
.collect();
let inventory: Vec<String> = serde_json::from_str(&inventory_json).unwrap_or_default();
let known_spells: Vec<String> = serde_json::from_str(&known_spells_json).unwrap_or_default();
let known_spells: Vec<String> =
serde_json::from_str(&known_spells_json).unwrap_or_default();
let race_str: String = row.get(3)?;
let class_str: String = row.get(4)?;
@ -177,15 +184,32 @@ impl Database {
VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11, ?12, ?13, ?14, ?15, ?16,
?17, ?18, ?19, ?20, ?21, ?22, ?23, ?24, ?25, ?26, ?27)",
rusqlite::params![
char.id, char.user_id, char.name,
char.race.as_str(), char.class.as_str(),
char.level, char.xp, char.gold,
char.hp, char.max_hp, char.energy, char.max_energy,
char.nerve, char.max_nerve, char.spell_points, char.max_spell_points,
char.stats.strength, char.stats.intelligence, char.stats.dexterity,
char.stats.constitution, char.stats.luck,
equipment_json, inventory_json,
char.active_song, known_spells_json, char.active_spell,
char.id,
char.user_id,
char.name,
char.race.as_str(),
char.class.as_str(),
char.level,
char.xp,
char.gold,
char.hp,
char.max_hp,
char.energy,
char.max_energy,
char.nerve,
char.max_nerve,
char.spell_points,
char.max_spell_points,
char.stats.strength,
char.stats.intelligence,
char.stats.dexterity,
char.stats.constitution,
char.stats.luck,
equipment_json,
inventory_json,
char.active_song,
known_spells_json,
char.active_spell,
char.last_regen,
],
)?;
@ -217,11 +241,9 @@ impl Database {
pub fn next_character_id(&self) -> Result<u32, rusqlite::Error> {
let conn = self.conn.lock().unwrap();
let id: u32 = conn.query_row(
"SELECT COALESCE(MAX(id), 0) FROM characters",
[],
|row| row.get(0),
)?;
let id: u32 = conn.query_row("SELECT COALESCE(MAX(id), 0) FROM characters", [], |row| {
row.get(0)
})?;
Ok(id + 1)
}
}