- add: Character service now loads the data from the database and sends it in the character list packet

- add: character id list to the connection state for tracking the real character database id's for when the client requests actions on the character
- fix: sql error when trying to create a character
This commit is contained in:
2025-01-07 22:16:47 -05:00
parent cb6ee657f0
commit db868cc1ac
9 changed files with 117 additions and 62 deletions

View File

@@ -9,8 +9,6 @@ pub struct Character {
pub id: i32,
pub user_id: i32,
pub name: String,
pub level: i16,
pub experience: i64,
pub inventory: serde_json::Value,
pub stats: serde_json::Value,
pub looks: serde_json::Value,
@@ -57,7 +55,7 @@ impl CharacterRepository {
pub async fn create_character(&self, user_id: i32, name: &str, inventory: serde_json::Value, stats: serde_json::Value, looks: serde_json::Value, position: serde_json::Value) -> Result<i32, sqlx::Error> {
let result = sqlx::query(
"INSERT INTO characters (user_id, name, inventory, stats, looks, position, created_at, updated_at, is_active) \
VALUES ($1, $2, 1, 0, $3, $4, $5, $6, NOW(), NOW(), true) RETURNING id",
VALUES ($1, $2, $3, $4, $5, $6, NOW(), NOW(), true) RETURNING id",
)
.bind(user_id)
.bind(name)
@@ -68,18 +66,23 @@ impl CharacterRepository {
.fetch_one(&self.pool)
.await?;
// Invalidate cache
let cache_key = format!("character:user:{}", user_id);
self.cache.lock().await.delete(&cache_key).await.map_err(|_| sqlx::Error::RowNotFound)?;
Ok(result.get("id"))
}
pub async fn delete_character(&self, character_id: i32) -> Result<(), sqlx::Error> {
sqlx::query(
"UPDATE characters SET deleted_at = NOW(), is_active = false WHERE id = $1",
let result = sqlx::query(
"UPDATE characters SET deleted_at = NOW(), is_active = false WHERE id = $1 RETURNING user_id",
)
.bind(character_id)
.execute(&self.pool)
.fetch_one(&self.pool)
.await?;
// Invalidate cache
let cache_key = format!("character:user:{}", result.get::<i32, &str>("user_id"));
self.cache.lock().await.delete(&cache_key).await.map_err(|_| sqlx::Error::RowNotFound)?;
let cache_key = format!("character:{}", character_id);
self.cache.lock().await.delete(&cache_key).await.map_err(|_| sqlx::Error::RowNotFound)?;
Ok(())