forked from lavina/lavina
1
0
Fork 0

persist room leaves

This commit is contained in:
Nikita Vilunov 2024-04-15 01:54:21 +02:00
parent 79d626330c
commit b3af10a70a
3 changed files with 19 additions and 3 deletions

View File

@ -412,7 +412,7 @@ impl Player {
let room = self.my_rooms.remove(&room_id); let room = self.my_rooms.remove(&room_id);
if let Some(room) = room { if let Some(room) = room {
room.unsubscribe(&self.player_id).await; 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 { let update = Updates::RoomLeft {
room_id, room_id,

View File

@ -3,7 +3,7 @@ use anyhow::Result;
use crate::repo::Storage; use crate::repo::Storage;
impl 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; let mut executor = self.conn.lock().await;
sqlx::query( sqlx::query(
"insert into memberships(user_id, room_id, status) "insert into memberships(user_id, room_id, status)
@ -16,4 +16,18 @@ impl Storage {
Ok(()) 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(())
}
} }

View File

@ -162,10 +162,12 @@ impl RoomHandle {
lock.subscriptions.remove(player_id); 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; let mut lock = self.0.write().await;
tracing::info!("Removing a member from a room"); tracing::info!("Removing a member from a room");
let storage_id = lock.storage_id;
lock.members.remove(player_id); lock.members.remove(player_id);
lock.storage.remove_room_member(&storage_id, &player_storage_id).await.unwrap();
let update = Updates::RoomLeft { let update = Updates::RoomLeft {
room_id: lock.room_id.clone(), room_id: lock.room_id.clone(),
former_member_id: player_id.clone(), former_member_id: player_id.clone(),