From b3af10a70a1b4d4107f822e262870d4dcf1903c6 Mon Sep 17 00:00:00 2001 From: Nikita Vilunov Date: Mon, 15 Apr 2024 01:54:21 +0200 Subject: [PATCH] persist room leaves --- crates/lavina-core/src/player.rs | 2 +- crates/lavina-core/src/repo/room.rs | 16 +++++++++++++++- crates/lavina-core/src/room.rs | 4 +++- 3 files changed, 19 insertions(+), 3 deletions(-) diff --git a/crates/lavina-core/src/player.rs b/crates/lavina-core/src/player.rs index 4f1a699..426cc00 100644 --- a/crates/lavina-core/src/player.rs +++ b/crates/lavina-core/src/player.rs @@ -412,7 +412,7 @@ impl Player { let room = self.my_rooms.remove(&room_id); if let Some(room) = room { room.unsubscribe(&self.player_id).await; - room.remove_member(&self.player_id).await; + room.remove_member(&self.player_id, self.storage_id).await; } let update = Updates::RoomLeft { room_id, diff --git a/crates/lavina-core/src/repo/room.rs b/crates/lavina-core/src/repo/room.rs index 7b68a3d..079deb6 100644 --- a/crates/lavina-core/src/repo/room.rs +++ b/crates/lavina-core/src/repo/room.rs @@ -3,7 +3,7 @@ use anyhow::Result; use crate::repo::Storage; impl Storage { - pub async fn add_room_member(&mut self, room_id: &u32, player_id: &u32) -> Result<()> { + pub async fn add_room_member(&self, room_id: &u32, player_id: &u32) -> Result<()> { let mut executor = self.conn.lock().await; sqlx::query( "insert into memberships(user_id, room_id, status) @@ -16,4 +16,18 @@ impl Storage { Ok(()) } + + pub async fn remove_room_member(&self, room_id: &u32, player_id: &u32) -> Result<()> { + let mut executor = self.conn.lock().await; + sqlx::query( + "delete from memberships + where user_id = ? and room_id = ?;", + ) + .bind(player_id) + .bind(room_id) + .execute(&mut *executor) + .await?; + + Ok(()) + } } diff --git a/crates/lavina-core/src/room.rs b/crates/lavina-core/src/room.rs index 66507c0..e7e8776 100644 --- a/crates/lavina-core/src/room.rs +++ b/crates/lavina-core/src/room.rs @@ -162,10 +162,12 @@ impl RoomHandle { lock.subscriptions.remove(player_id); } - pub async fn remove_member(&self, player_id: &PlayerId) { + pub async fn remove_member(&self, player_id: &PlayerId, player_storage_id: u32) { let mut lock = self.0.write().await; tracing::info!("Removing a member from a room"); + let storage_id = lock.storage_id; lock.members.remove(player_id); + lock.storage.remove_room_member(&storage_id, &player_storage_id).await.unwrap(); let update = Updates::RoomLeft { room_id: lock.room_id.clone(), former_member_id: player_id.clone(),