2023-02-04 01:01:49 +00:00
|
|
|
//! Domain of chat participants.
|
|
|
|
//!
|
|
|
|
//! Player is a single user account, which is used to participate in chats,
|
|
|
|
//! including sending messages, receiving messaged, retrieving history and running privileged actions.
|
|
|
|
//! A player corresponds to a single user account. Usually a person has only one account,
|
|
|
|
//! but it is possible to have multiple accounts for one person and therefore multiple player entities.
|
|
|
|
//!
|
|
|
|
//! A player actor is a serial handler of commands from a single player. It is preferable to run all per-player validations in the player actor,
|
|
|
|
//! so that they don't overload the room actor.
|
2023-02-03 22:43:59 +00:00
|
|
|
use std::{
|
2023-02-16 21:49:17 +00:00
|
|
|
collections::{HashMap, HashSet},
|
2023-02-03 22:43:59 +00:00
|
|
|
sync::{Arc, RwLock},
|
|
|
|
};
|
|
|
|
|
2023-02-12 22:23:52 +00:00
|
|
|
use prometheus::{IntGauge, Registry as MetricsRegistry};
|
2023-02-15 17:10:54 +00:00
|
|
|
use serde::Serialize;
|
2023-02-03 22:43:59 +00:00
|
|
|
use tokio::{
|
|
|
|
sync::mpsc::{channel, Receiver, Sender},
|
|
|
|
task::JoinHandle,
|
|
|
|
};
|
|
|
|
|
|
|
|
use crate::{
|
2023-02-14 22:22:04 +00:00
|
|
|
core::room::{RoomHandle, RoomId, RoomInfo, RoomRegistry},
|
2023-02-12 22:23:52 +00:00
|
|
|
prelude::*,
|
2023-02-13 18:32:52 +00:00
|
|
|
util::table::{AnonTable, Key as AnonKey},
|
2023-02-03 22:43:59 +00:00
|
|
|
};
|
|
|
|
|
2023-02-14 19:07:07 +00:00
|
|
|
/// Opaque player identifier. Cannot contain spaces, must be shorter than 32.
|
2023-02-15 17:10:54 +00:00
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize)]
|
2023-04-13 22:38:26 +00:00
|
|
|
pub struct PlayerId(Str);
|
2023-02-14 19:07:07 +00:00
|
|
|
impl PlayerId {
|
2023-04-13 22:38:26 +00:00
|
|
|
pub fn from(str: impl Into<Str>) -> Result<PlayerId> {
|
|
|
|
let bytes = str.into();
|
2023-02-14 19:07:07 +00:00
|
|
|
if bytes.len() > 32 {
|
2023-02-14 19:42:52 +00:00
|
|
|
return Err(fail("Nickname cannot be longer than 32 symbols"));
|
2023-02-14 19:07:07 +00:00
|
|
|
}
|
2023-04-13 19:15:48 +00:00
|
|
|
if bytes.contains(' ') {
|
2023-02-14 19:07:07 +00:00
|
|
|
return Err(anyhow::Error::msg("Nickname cannot contain spaces"));
|
|
|
|
}
|
|
|
|
Ok(PlayerId(bytes))
|
|
|
|
}
|
2023-04-13 22:38:26 +00:00
|
|
|
pub fn as_inner(&self) -> &Str {
|
2023-02-14 19:42:52 +00:00
|
|
|
&self.0
|
|
|
|
}
|
2023-04-13 22:38:26 +00:00
|
|
|
pub fn into_inner(self) -> Str {
|
2023-04-13 19:15:48 +00:00
|
|
|
self.0
|
|
|
|
}
|
2023-02-14 19:07:07 +00:00
|
|
|
}
|
2023-02-03 22:43:59 +00:00
|
|
|
|
2023-02-13 18:32:52 +00:00
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
|
|
|
pub struct ConnectionId(pub AnonKey);
|
|
|
|
|
2023-02-13 19:16:00 +00:00
|
|
|
pub struct PlayerConnection {
|
|
|
|
pub connection_id: ConnectionId,
|
|
|
|
pub receiver: Receiver<Updates>,
|
|
|
|
player_handle: PlayerHandle,
|
|
|
|
}
|
|
|
|
impl PlayerConnection {
|
2023-04-13 22:38:26 +00:00
|
|
|
pub async fn send_message(&mut self, room_id: RoomId, body: Str) -> Result<()> {
|
2023-02-13 19:16:00 +00:00
|
|
|
self.player_handle
|
|
|
|
.send_message(room_id, self.connection_id.clone(), body)
|
|
|
|
.await
|
|
|
|
}
|
|
|
|
|
2023-02-16 21:49:17 +00:00
|
|
|
pub async fn join_room(&mut self, room_id: RoomId) -> Result<JoinResult> {
|
2023-02-14 17:53:43 +00:00
|
|
|
self.player_handle
|
|
|
|
.join_room(room_id, self.connection_id.clone())
|
|
|
|
.await
|
2023-02-13 19:16:00 +00:00
|
|
|
}
|
2023-02-14 18:28:49 +00:00
|
|
|
|
2023-04-13 22:38:26 +00:00
|
|
|
pub async fn change_topic(&mut self, room_id: RoomId, new_topic: Str) -> Result<()> {
|
2023-02-14 22:22:04 +00:00
|
|
|
let (promise, deferred) = oneshot();
|
|
|
|
let cmd = Cmd::ChangeTopic {
|
|
|
|
room_id,
|
|
|
|
new_topic,
|
|
|
|
promise,
|
|
|
|
};
|
|
|
|
self.player_handle
|
|
|
|
.send(PlayerCommand::Cmd(cmd, self.connection_id.clone()))
|
|
|
|
.await;
|
|
|
|
Ok(deferred.await?)
|
|
|
|
}
|
|
|
|
|
2023-02-15 17:54:48 +00:00
|
|
|
pub async fn leave_room(&mut self, room_id: RoomId) -> Result<()> {
|
|
|
|
let (promise, deferred) = oneshot();
|
2023-02-16 17:39:54 +00:00
|
|
|
self.player_handle
|
|
|
|
.send(PlayerCommand::Cmd(
|
|
|
|
Cmd::LeaveRoom { room_id, promise },
|
|
|
|
self.connection_id.clone(),
|
|
|
|
))
|
|
|
|
.await;
|
2023-02-15 17:54:48 +00:00
|
|
|
Ok(deferred.await?)
|
|
|
|
}
|
|
|
|
|
2023-02-15 16:47:48 +00:00
|
|
|
pub async fn terminate(self) {
|
2023-02-15 17:10:54 +00:00
|
|
|
self.player_handle
|
|
|
|
.send(PlayerCommand::TerminateConnection(self.connection_id))
|
|
|
|
.await;
|
2023-02-15 16:47:48 +00:00
|
|
|
}
|
|
|
|
|
2023-02-15 20:49:52 +00:00
|
|
|
pub async fn get_rooms(&self) -> Result<Vec<RoomInfo>> {
|
|
|
|
let (promise, deferred) = oneshot();
|
2023-02-16 21:49:17 +00:00
|
|
|
self.player_handle
|
|
|
|
.send(PlayerCommand::GetRooms(promise))
|
|
|
|
.await;
|
2023-02-15 20:49:52 +00:00
|
|
|
Ok(deferred.await?)
|
2023-02-14 18:28:49 +00:00
|
|
|
}
|
2023-02-13 19:16:00 +00:00
|
|
|
}
|
|
|
|
|
2023-02-04 01:01:49 +00:00
|
|
|
/// Handle to a player actor.
|
2023-02-03 22:43:59 +00:00
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct PlayerHandle {
|
|
|
|
tx: Sender<PlayerCommand>,
|
|
|
|
}
|
|
|
|
impl PlayerHandle {
|
2023-02-14 00:12:27 +00:00
|
|
|
pub async fn subscribe(&self) -> PlayerConnection {
|
2023-02-13 19:16:00 +00:00
|
|
|
let (sender, receiver) = channel(32);
|
2023-02-13 18:32:52 +00:00
|
|
|
let (promise, deferred) = oneshot();
|
2023-03-21 21:50:40 +00:00
|
|
|
let cmd = PlayerCommand::AddConnection { sender, promise };
|
|
|
|
let _ = self.tx.send(cmd).await;
|
2023-02-13 18:32:52 +00:00
|
|
|
let connection_id = deferred.await.unwrap();
|
2023-02-13 19:16:00 +00:00
|
|
|
PlayerConnection {
|
|
|
|
connection_id,
|
|
|
|
player_handle: self.clone(),
|
|
|
|
receiver,
|
|
|
|
}
|
2023-02-03 22:43:59 +00:00
|
|
|
}
|
|
|
|
|
2023-02-14 22:22:04 +00:00
|
|
|
pub async fn send_message(
|
|
|
|
&self,
|
|
|
|
room_id: RoomId,
|
|
|
|
connection_id: ConnectionId,
|
2023-04-13 22:38:26 +00:00
|
|
|
body: Str,
|
2023-02-14 22:22:04 +00:00
|
|
|
) -> Result<()> {
|
|
|
|
let (promise, deferred) = oneshot();
|
|
|
|
let cmd = Cmd::SendMessage {
|
|
|
|
room_id,
|
|
|
|
body,
|
|
|
|
promise,
|
|
|
|
};
|
2023-03-21 21:50:40 +00:00
|
|
|
let _ = self.tx.send(PlayerCommand::Cmd(cmd, connection_id)).await;
|
2023-02-14 22:22:04 +00:00
|
|
|
Ok(deferred.await?)
|
2023-02-03 22:43:59 +00:00
|
|
|
}
|
|
|
|
|
2023-02-14 17:53:43 +00:00
|
|
|
pub async fn join_room(
|
|
|
|
&self,
|
|
|
|
room_id: RoomId,
|
|
|
|
connection_id: ConnectionId,
|
2023-02-16 21:49:17 +00:00
|
|
|
) -> Result<JoinResult> {
|
2023-02-14 00:44:03 +00:00
|
|
|
let (promise, deferred) = oneshot();
|
2023-03-21 21:50:40 +00:00
|
|
|
let cmd = Cmd::JoinRoom { room_id, promise };
|
|
|
|
let _ = self.tx.send(PlayerCommand::Cmd(cmd, connection_id)).await;
|
2023-02-14 00:44:03 +00:00
|
|
|
Ok(deferred.await?)
|
2023-02-03 22:43:59 +00:00
|
|
|
}
|
|
|
|
|
2023-02-14 22:22:04 +00:00
|
|
|
async fn send(&self, command: PlayerCommand) {
|
2023-03-21 21:50:40 +00:00
|
|
|
let _ = self.tx.send(command).await;
|
2023-02-14 17:53:43 +00:00
|
|
|
}
|
2023-02-03 22:43:59 +00:00
|
|
|
|
2023-02-14 19:42:52 +00:00
|
|
|
pub async fn update(&self, update: Updates) {
|
|
|
|
self.send(PlayerCommand::Update(update)).await;
|
|
|
|
}
|
2023-02-03 22:43:59 +00:00
|
|
|
}
|
2023-02-14 19:42:52 +00:00
|
|
|
|
2023-02-15 20:49:52 +00:00
|
|
|
enum PlayerCommand {
|
2023-02-14 17:53:43 +00:00
|
|
|
/** Commands from connections */
|
2023-02-14 19:45:22 +00:00
|
|
|
AddConnection {
|
2023-02-03 22:43:59 +00:00
|
|
|
sender: Sender<Updates>,
|
2023-02-14 22:22:04 +00:00
|
|
|
promise: Promise<ConnectionId>,
|
2023-02-03 22:43:59 +00:00
|
|
|
},
|
2023-02-15 16:47:48 +00:00
|
|
|
TerminateConnection(ConnectionId),
|
2023-02-14 22:22:04 +00:00
|
|
|
Cmd(Cmd, ConnectionId),
|
|
|
|
/// Query - responds with a list of rooms the player is a member of.
|
|
|
|
GetRooms(Promise<Vec<RoomInfo>>),
|
|
|
|
/** Events from rooms */
|
|
|
|
Update(Updates),
|
|
|
|
}
|
|
|
|
|
|
|
|
pub enum Cmd {
|
2023-02-03 22:43:59 +00:00
|
|
|
JoinRoom {
|
|
|
|
room_id: RoomId,
|
2023-02-16 21:49:17 +00:00
|
|
|
promise: Promise<JoinResult>,
|
2023-02-03 22:43:59 +00:00
|
|
|
},
|
2023-02-15 17:54:48 +00:00
|
|
|
LeaveRoom {
|
|
|
|
room_id: RoomId,
|
|
|
|
promise: Promise<()>,
|
|
|
|
},
|
2023-02-03 22:43:59 +00:00
|
|
|
SendMessage {
|
|
|
|
room_id: RoomId,
|
2023-04-13 22:38:26 +00:00
|
|
|
body: Str,
|
2023-02-14 22:22:04 +00:00
|
|
|
promise: Promise<()>,
|
|
|
|
},
|
|
|
|
ChangeTopic {
|
|
|
|
room_id: RoomId,
|
2023-04-13 22:38:26 +00:00
|
|
|
new_topic: Str,
|
2023-02-14 22:22:04 +00:00
|
|
|
promise: Promise<()>,
|
2023-02-03 22:43:59 +00:00
|
|
|
},
|
2023-02-14 19:42:52 +00:00
|
|
|
}
|
|
|
|
|
2023-02-16 21:49:17 +00:00
|
|
|
pub enum JoinResult {
|
|
|
|
Success(RoomInfo),
|
|
|
|
Banned,
|
|
|
|
}
|
|
|
|
|
2023-02-14 19:42:52 +00:00
|
|
|
/// Player update event type which is sent to a player actor and from there to a connection handler.
|
2023-07-22 14:22:49 +00:00
|
|
|
#[derive(Clone, Debug)]
|
2023-02-14 19:42:52 +00:00
|
|
|
pub enum Updates {
|
|
|
|
RoomTopicChanged {
|
|
|
|
room_id: RoomId,
|
2023-04-13 22:38:26 +00:00
|
|
|
new_topic: Str,
|
2023-02-14 19:42:52 +00:00
|
|
|
},
|
|
|
|
NewMessage {
|
2023-02-03 22:43:59 +00:00
|
|
|
room_id: RoomId,
|
2023-02-14 19:42:52 +00:00
|
|
|
author_id: PlayerId,
|
2023-04-13 22:38:26 +00:00
|
|
|
body: Str,
|
2023-02-03 22:43:59 +00:00
|
|
|
},
|
2023-02-14 19:42:52 +00:00
|
|
|
RoomJoined {
|
2023-02-14 17:53:43 +00:00
|
|
|
room_id: RoomId,
|
|
|
|
new_member_id: PlayerId,
|
|
|
|
},
|
2023-02-15 17:54:48 +00:00
|
|
|
RoomLeft {
|
|
|
|
room_id: RoomId,
|
|
|
|
former_member_id: PlayerId,
|
|
|
|
},
|
2023-02-16 21:49:17 +00:00
|
|
|
/// The player was banned from the room and left it immediately.
|
|
|
|
BannedFrom(RoomId),
|
2023-02-03 22:43:59 +00:00
|
|
|
}
|
|
|
|
|
2023-02-04 01:01:49 +00:00
|
|
|
/// Handle to a player registry — a shared data structure containing information about players.
|
2023-02-03 22:43:59 +00:00
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct PlayerRegistry(Arc<RwLock<PlayerRegistryInner>>);
|
|
|
|
impl PlayerRegistry {
|
2023-02-12 22:23:52 +00:00
|
|
|
pub fn empty(
|
|
|
|
room_registry: RoomRegistry,
|
|
|
|
metrics: &mut MetricsRegistry,
|
|
|
|
) -> Result<PlayerRegistry> {
|
|
|
|
let metric_active_players =
|
|
|
|
IntGauge::new("chat_players_active", "Number of alive player actors")?;
|
|
|
|
metrics.register(Box::new(metric_active_players.clone()))?;
|
2023-02-03 22:43:59 +00:00
|
|
|
let inner = PlayerRegistryInner {
|
|
|
|
room_registry,
|
|
|
|
players: HashMap::new(),
|
2023-02-12 22:23:52 +00:00
|
|
|
metric_active_players,
|
2023-02-03 22:43:59 +00:00
|
|
|
};
|
2023-02-12 22:23:52 +00:00
|
|
|
Ok(PlayerRegistry(Arc::new(RwLock::new(inner))))
|
2023-02-03 22:43:59 +00:00
|
|
|
}
|
|
|
|
|
2023-02-12 23:31:16 +00:00
|
|
|
pub async fn get_or_create_player(&mut self, id: PlayerId) -> PlayerHandle {
|
2023-02-03 22:43:59 +00:00
|
|
|
let mut inner = self.0.write().unwrap();
|
2023-02-13 18:32:52 +00:00
|
|
|
if let Some((handle, _)) = inner.players.get(&id) {
|
|
|
|
handle.clone()
|
|
|
|
} else {
|
2023-02-14 22:38:40 +00:00
|
|
|
let (handle, fiber) = Player::launch(id.clone(), inner.room_registry.clone());
|
2023-02-13 18:32:52 +00:00
|
|
|
inner.players.insert(id, (handle.clone(), fiber));
|
|
|
|
inner.metric_active_players.inc();
|
|
|
|
handle
|
|
|
|
}
|
2023-02-03 22:43:59 +00:00
|
|
|
}
|
2023-02-13 19:16:00 +00:00
|
|
|
|
|
|
|
pub async fn connect_to_player(&mut self, id: PlayerId) -> PlayerConnection {
|
2023-02-14 22:49:56 +00:00
|
|
|
let player_handle = self.get_or_create_player(id).await;
|
2023-02-13 19:16:00 +00:00
|
|
|
player_handle.subscribe().await
|
|
|
|
}
|
2023-08-24 12:10:31 +00:00
|
|
|
|
|
|
|
pub async fn shutdown_all(&mut self) -> Result<()> {
|
|
|
|
let mut inner = self.0.write().unwrap();
|
|
|
|
let mut players = HashMap::new();
|
|
|
|
std::mem::swap(&mut players, &mut inner.players);
|
|
|
|
for (i, (k, j)) in inner.players.drain() {
|
|
|
|
drop(k);
|
|
|
|
j.await?;
|
|
|
|
log::debug!("Player stopped #{i:?}")
|
|
|
|
}
|
|
|
|
log::debug!("All players stopped");
|
|
|
|
Ok(())
|
|
|
|
}
|
2023-02-03 22:43:59 +00:00
|
|
|
}
|
|
|
|
|
2023-02-04 01:01:49 +00:00
|
|
|
/// The player registry state representation.
|
2023-02-03 22:43:59 +00:00
|
|
|
struct PlayerRegistryInner {
|
|
|
|
room_registry: RoomRegistry,
|
|
|
|
players: HashMap<PlayerId, (PlayerHandle, JoinHandle<Player>)>,
|
2023-02-12 22:23:52 +00:00
|
|
|
metric_active_players: IntGauge,
|
2023-02-03 22:43:59 +00:00
|
|
|
}
|
|
|
|
|
2023-02-04 01:01:49 +00:00
|
|
|
/// Player actor inner state representation.
|
2023-02-03 22:43:59 +00:00
|
|
|
struct Player {
|
2023-02-14 22:38:40 +00:00
|
|
|
player_id: PlayerId,
|
2023-02-14 19:45:22 +00:00
|
|
|
connections: AnonTable<Sender<Updates>>,
|
2023-02-14 22:38:40 +00:00
|
|
|
my_rooms: HashMap<RoomId, RoomHandle>,
|
2023-02-16 21:49:17 +00:00
|
|
|
banned_from: HashSet<RoomId>,
|
2023-02-14 22:38:40 +00:00
|
|
|
rx: Receiver<PlayerCommand>,
|
|
|
|
handle: PlayerHandle,
|
|
|
|
rooms: RoomRegistry,
|
2023-02-03 22:43:59 +00:00
|
|
|
}
|
|
|
|
impl Player {
|
2023-02-14 22:38:40 +00:00
|
|
|
fn launch(player_id: PlayerId, rooms: RoomRegistry) -> (PlayerHandle, JoinHandle<Player>) {
|
2023-02-14 22:49:56 +00:00
|
|
|
let (tx, rx) = channel(32);
|
2023-02-03 22:43:59 +00:00
|
|
|
let handle = PlayerHandle { tx };
|
|
|
|
let handle_clone = handle.clone();
|
2023-02-14 22:38:40 +00:00
|
|
|
let player = Player {
|
|
|
|
player_id,
|
|
|
|
connections: AnonTable::new(),
|
|
|
|
my_rooms: HashMap::new(),
|
2023-09-15 16:33:25 +00:00
|
|
|
banned_from: HashSet::from([RoomId::from("Empty").unwrap()]),
|
2023-02-14 22:38:40 +00:00
|
|
|
rx,
|
|
|
|
handle,
|
|
|
|
rooms,
|
|
|
|
};
|
|
|
|
let fiber = tokio::task::spawn(player.main_loop());
|
|
|
|
(handle_clone, fiber)
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn main_loop(mut self) -> Self {
|
|
|
|
while let Some(cmd) = self.rx.recv().await {
|
|
|
|
match cmd {
|
|
|
|
PlayerCommand::AddConnection { sender, promise } => {
|
|
|
|
let connection_id = self.connections.insert(sender);
|
2023-02-15 16:47:48 +00:00
|
|
|
if let Err(connection_id) = promise.send(ConnectionId(connection_id)) {
|
|
|
|
log::warn!("Connection {connection_id:?} terminated before finalization");
|
|
|
|
self.terminate_connection(connection_id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
PlayerCommand::TerminateConnection(connection_id) => {
|
|
|
|
self.terminate_connection(connection_id);
|
2023-02-14 22:38:40 +00:00
|
|
|
}
|
|
|
|
PlayerCommand::GetRooms(promise) => {
|
|
|
|
let mut response = vec![];
|
|
|
|
for (_, handle) in &self.my_rooms {
|
|
|
|
response.push(handle.get_room_info().await);
|
2023-02-14 18:28:49 +00:00
|
|
|
}
|
2023-03-21 21:50:40 +00:00
|
|
|
let _ = promise.send(response);
|
2023-02-14 22:38:40 +00:00
|
|
|
}
|
|
|
|
PlayerCommand::Update(update) => {
|
|
|
|
log::info!(
|
|
|
|
"Player received an update, broadcasting to {} connections",
|
|
|
|
self.connections.len()
|
|
|
|
);
|
2023-02-16 21:49:17 +00:00
|
|
|
match update {
|
|
|
|
Updates::BannedFrom(ref room_id) => {
|
|
|
|
self.banned_from.insert(room_id.clone());
|
|
|
|
self.my_rooms.remove(room_id);
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
2023-02-14 22:38:40 +00:00
|
|
|
for (_, connection) in &self.connections {
|
2023-03-21 21:50:40 +00:00
|
|
|
let _ = connection.send(update.clone()).await;
|
2023-02-14 18:46:42 +00:00
|
|
|
}
|
2023-02-03 22:43:59 +00:00
|
|
|
}
|
2023-02-14 22:38:40 +00:00
|
|
|
PlayerCommand::Cmd(cmd, connection_id) => self.handle_cmd(cmd, connection_id).await,
|
2023-02-03 22:43:59 +00:00
|
|
|
}
|
2023-02-14 22:38:40 +00:00
|
|
|
}
|
2023-08-24 12:10:31 +00:00
|
|
|
log::debug!("Shutting down player actor #{:?}", self.player_id);
|
2023-02-14 22:38:40 +00:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2023-02-15 16:47:48 +00:00
|
|
|
fn terminate_connection(&mut self, connection_id: ConnectionId) {
|
|
|
|
if let None = self.connections.pop(connection_id.0) {
|
|
|
|
log::warn!("Connection {connection_id:?} already terminated");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-14 22:38:40 +00:00
|
|
|
async fn handle_cmd(&mut self, cmd: Cmd, connection_id: ConnectionId) {
|
|
|
|
match cmd {
|
|
|
|
Cmd::JoinRoom { room_id, promise } => {
|
2023-02-16 21:49:17 +00:00
|
|
|
if self.banned_from.contains(&room_id) {
|
2023-03-21 21:50:40 +00:00
|
|
|
let _ = promise.send(JoinResult::Banned);
|
2023-02-16 21:49:17 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-08-17 13:41:28 +00:00
|
|
|
let room = match self.rooms.get_or_create_room(room_id.clone()).await {
|
|
|
|
Ok(room) => room,
|
|
|
|
Err(e) => {
|
|
|
|
log::error!("Failed to get or create room: {e}");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
};
|
2023-02-14 22:38:40 +00:00
|
|
|
room.subscribe(self.player_id.clone(), self.handle.clone())
|
|
|
|
.await;
|
|
|
|
self.my_rooms.insert(room_id.clone(), room.clone());
|
2023-02-14 22:43:59 +00:00
|
|
|
let room_info = room.get_room_info().await;
|
2023-03-21 21:50:40 +00:00
|
|
|
let _ = promise.send(JoinResult::Success(room_info));
|
2023-02-14 22:38:40 +00:00
|
|
|
let update = Updates::RoomJoined {
|
|
|
|
room_id,
|
|
|
|
new_member_id: self.player_id.clone(),
|
|
|
|
};
|
2023-02-14 22:43:59 +00:00
|
|
|
self.broadcast_update(update, connection_id).await;
|
2023-02-14 22:38:40 +00:00
|
|
|
}
|
2023-02-15 17:54:48 +00:00
|
|
|
Cmd::LeaveRoom { room_id, promise } => {
|
|
|
|
let room = self.my_rooms.remove(&room_id);
|
|
|
|
if let Some(room) = room {
|
|
|
|
room.unsubscribe(&self.player_id).await;
|
|
|
|
let room_info = room.get_room_info().await;
|
|
|
|
}
|
2023-03-21 21:50:40 +00:00
|
|
|
let _ = promise.send(());
|
2023-02-15 17:54:48 +00:00
|
|
|
let update = Updates::RoomLeft {
|
2023-02-16 17:39:54 +00:00
|
|
|
room_id,
|
|
|
|
former_member_id: self.player_id.clone(),
|
2023-02-15 17:54:48 +00:00
|
|
|
};
|
|
|
|
self.broadcast_update(update, connection_id).await;
|
|
|
|
}
|
2023-02-14 22:38:40 +00:00
|
|
|
Cmd::SendMessage {
|
|
|
|
room_id,
|
|
|
|
body,
|
|
|
|
promise,
|
|
|
|
} => {
|
2023-08-17 13:41:28 +00:00
|
|
|
let room = self.rooms.get_room(&room_id).await;
|
2023-02-14 22:38:40 +00:00
|
|
|
if let Some(room) = room {
|
|
|
|
room.send_message(self.player_id.clone(), body.clone())
|
|
|
|
.await;
|
|
|
|
} else {
|
|
|
|
tracing::info!("no room found");
|
|
|
|
}
|
2023-03-21 21:50:40 +00:00
|
|
|
let _ = promise.send(());
|
2023-02-14 22:38:40 +00:00
|
|
|
let update = Updates::NewMessage {
|
|
|
|
room_id,
|
|
|
|
author_id: self.player_id.clone(),
|
|
|
|
body,
|
|
|
|
};
|
2023-02-14 22:43:59 +00:00
|
|
|
self.broadcast_update(update, connection_id).await;
|
2023-02-14 22:38:40 +00:00
|
|
|
}
|
|
|
|
Cmd::ChangeTopic {
|
|
|
|
room_id,
|
|
|
|
new_topic,
|
|
|
|
promise,
|
|
|
|
} => {
|
2023-08-17 13:41:28 +00:00
|
|
|
let room = self.rooms.get_room(&room_id).await;
|
2023-02-14 22:38:40 +00:00
|
|
|
if let Some(mut room) = room {
|
|
|
|
room.set_topic(self.player_id.clone(), new_topic.clone())
|
|
|
|
.await;
|
|
|
|
} else {
|
|
|
|
tracing::info!("no room found");
|
|
|
|
}
|
2023-03-21 21:50:40 +00:00
|
|
|
let _ = promise.send(());
|
2023-02-14 22:38:40 +00:00
|
|
|
let update = Updates::RoomTopicChanged { room_id, new_topic };
|
2023-02-14 22:43:59 +00:00
|
|
|
self.broadcast_update(update, connection_id).await;
|
2023-02-14 22:38:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn broadcast_update(&self, update: Updates, except: ConnectionId) {
|
|
|
|
for (a, b) in &self.connections {
|
|
|
|
if ConnectionId(a) == except {
|
|
|
|
continue;
|
|
|
|
}
|
2023-03-21 21:50:40 +00:00
|
|
|
let _ = b.send(update.clone()).await;
|
2023-02-14 22:38:40 +00:00
|
|
|
}
|
2023-02-03 22:43:59 +00:00
|
|
|
}
|
|
|
|
}
|