From 67f145ad2fd07832e4debccddd9b8464ec57caed Mon Sep 17 00:00:00 2001 From: Nikita Vilunov Date: Tue, 26 Mar 2024 00:49:16 +0100 Subject: [PATCH] make GetRooms a client command --- crates/lavina-core/src/player.rs | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/crates/lavina-core/src/player.rs b/crates/lavina-core/src/player.rs index 831539b..eec22f8 100644 --- a/crates/lavina-core/src/player.rs +++ b/crates/lavina-core/src/player.rs @@ -96,9 +96,11 @@ impl PlayerConnection { self.player_handle.send(ActorCommand::TerminateConnection(self.connection_id)).await; } + /// Handled in [Player::get_rooms]. pub async fn get_rooms(&self) -> Result> { let (promise, deferred) = oneshot(); - self.player_handle.send(ActorCommand::GetRooms(promise)).await; + let cmd = ClientCommand::GetRooms { promise }; + self.player_handle.send(ActorCommand::ClientCommand(cmd, self.connection_id.clone())).await; Ok(deferred.await?) } } @@ -143,8 +145,6 @@ enum ActorCommand { TerminateConnection(ConnectionId), /// Player-issued command. ClientCommand(ClientCommand, ConnectionId), - /// Query - responds with a list of rooms the player is a member of. - GetRooms(Promise>), /// Update which is sent from a room the player is member of. Update(Updates), Stop, @@ -170,6 +170,9 @@ pub enum ClientCommand { new_topic: Str, promise: Promise<()>, }, + GetRooms { + promise: Promise>, + }, } pub enum JoinResult { @@ -294,13 +297,6 @@ impl Player { ActorCommand::TerminateConnection(connection_id) => { self.terminate_connection(connection_id); } - ActorCommand::GetRooms(promise) => { - let mut response = vec![]; - for (_, handle) in &self.my_rooms { - response.push(handle.get_room_info().await); - } - let _ = promise.send(response); - } ActorCommand::Update(update) => self.handle_update(update).await, ActorCommand::ClientCommand(cmd, connection_id) => self.handle_cmd(cmd, connection_id).await, ActorCommand::Stop => break, @@ -357,6 +353,10 @@ impl Player { self.change_topic(connection_id, room_id, new_topic).await; let _ = promise.send(()); } + ClientCommand::GetRooms { promise } => { + let result = self.get_rooms().await; + let _ = promise.send(result); + } } } @@ -421,6 +421,14 @@ impl Player { self.broadcast_update(update, connection_id).await; } + async fn get_rooms(&self) -> Vec { + let mut response = vec![]; + for (_, handle) in &self.my_rooms { + response.push(handle.get_room_info().await); + } + response + } + /// Broadcasts an update to all connections except the one with the given id. /// /// This is called after handling a client command.