forked from lavina/lavina
1
0
Fork 0

make GetRooms a client command

This commit is contained in:
Nikita Vilunov 2024-03-26 00:49:16 +01:00
parent ee10e3837a
commit 67f145ad2f
1 changed files with 18 additions and 10 deletions

View File

@ -96,9 +96,11 @@ impl PlayerConnection {
self.player_handle.send(ActorCommand::TerminateConnection(self.connection_id)).await; self.player_handle.send(ActorCommand::TerminateConnection(self.connection_id)).await;
} }
/// Handled in [Player::get_rooms].
pub async fn get_rooms(&self) -> Result<Vec<RoomInfo>> { pub async fn get_rooms(&self) -> Result<Vec<RoomInfo>> {
let (promise, deferred) = oneshot(); 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?) Ok(deferred.await?)
} }
} }
@ -143,8 +145,6 @@ enum ActorCommand {
TerminateConnection(ConnectionId), TerminateConnection(ConnectionId),
/// Player-issued command. /// Player-issued command.
ClientCommand(ClientCommand, ConnectionId), ClientCommand(ClientCommand, ConnectionId),
/// Query - responds with a list of rooms the player is a member of.
GetRooms(Promise<Vec<RoomInfo>>),
/// Update which is sent from a room the player is member of. /// Update which is sent from a room the player is member of.
Update(Updates), Update(Updates),
Stop, Stop,
@ -170,6 +170,9 @@ pub enum ClientCommand {
new_topic: Str, new_topic: Str,
promise: Promise<()>, promise: Promise<()>,
}, },
GetRooms {
promise: Promise<Vec<RoomInfo>>,
},
} }
pub enum JoinResult { pub enum JoinResult {
@ -294,13 +297,6 @@ impl Player {
ActorCommand::TerminateConnection(connection_id) => { ActorCommand::TerminateConnection(connection_id) => {
self.terminate_connection(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::Update(update) => self.handle_update(update).await,
ActorCommand::ClientCommand(cmd, connection_id) => self.handle_cmd(cmd, connection_id).await, ActorCommand::ClientCommand(cmd, connection_id) => self.handle_cmd(cmd, connection_id).await,
ActorCommand::Stop => break, ActorCommand::Stop => break,
@ -357,6 +353,10 @@ impl Player {
self.change_topic(connection_id, room_id, new_topic).await; self.change_topic(connection_id, room_id, new_topic).await;
let _ = promise.send(()); 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; self.broadcast_update(update, connection_id).await;
} }
async fn get_rooms(&self) -> Vec<RoomInfo> {
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. /// Broadcasts an update to all connections except the one with the given id.
/// ///
/// This is called after handling a client command. /// This is called after handling a client command.