- add: ability to refresh the current session

- add: delete type to delete character request
- add: ability to update key in redis
- add: handle alive packet to refresh the session
- fix: delete now actually returns the time remaining correctly
- fix: character list now has the correct time until character deletion
This commit is contained in:
2025-01-08 02:03:27 -05:00
parent 584892ab97
commit 6d35d15ac3
16 changed files with 158 additions and 25 deletions

View File

@@ -15,7 +15,7 @@ pub struct Character {
pub position: serde_json::Value,
pub created_at: chrono::NaiveDateTime,
pub updated_at: chrono::NaiveDateTime,
pub deleted_at: Option<chrono::NaiveDateTime>,
pub deleted_at: Option<i64>,
pub is_active: bool,
}
@@ -40,7 +40,7 @@ impl CharacterRepository {
// Fetch from database
let character = sqlx::query_as::<_, Character>(
"SELECT id, user_id, name, inventory, stats, looks, position, \
created_at, updated_at, deleted_at, is_active \
created_at, updated_at, extract(epoch from (deleted_at - now()))::BIGINT as deleted_at, is_active \
FROM characters WHERE id = $1 AND is_active = true",
)
.bind(character_id)
@@ -72,9 +72,13 @@ impl CharacterRepository {
Ok(result.get("id"))
}
pub async fn delete_character(&self, character_id: i32) -> Result<(), sqlx::Error> {
pub async fn delete_character(&self, character_id: i32, delete_type: i32) -> Result<i64, sqlx::Error> {
let mut query = "UPDATE characters SET updated_at = NOW(), deleted_at = NOW() + '24 hours' WHERE id = $1 RETURNING user_id, extract(epoch from (deleted_at - now()))::BIGINT as deleted_at";
if 0 == delete_type {
query = "UPDATE characters SET updated_at = NOW(), deleted_at = null WHERE id = $1 RETURNING user_id, 0::BIGINT as deleted_at";
}
let result = sqlx::query(
"UPDATE characters SET deleted_at = NOW(), is_active = false WHERE id = $1 RETURNING user_id",
query,
)
.bind(character_id)
.fetch_one(&self.pool)
@@ -85,7 +89,7 @@ impl CharacterRepository {
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(())
Ok(result.get::<i64, &str>("deleted_at"))
}
pub async fn get_characters_by_user(&self, user_id: i32) -> Result<Vec<Character>, sqlx::Error> {
@@ -98,9 +102,7 @@ impl CharacterRepository {
// Fetch from database
let characters = sqlx::query_as::<_, Character>(
"SELECT id, user_id, name, inventory, stats, looks, position, \
created_at, updated_at, deleted_at, is_active \
FROM characters WHERE user_id = $1 AND is_active = true",
"SELECT id, user_id, name, inventory, stats, looks, position, created_at, updated_at, extract(epoch from (deleted_at - now()))::BIGINT as deleted_at, is_active FROM characters WHERE user_id = $1 AND is_active = true",
)
.bind(user_id)
.fetch_all(&self.pool)