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.
|
2024-04-15 09:06:10 +00:00
|
|
|
use std::collections::{HashMap, HashSet};
|
2023-02-03 22:43:59 +00:00
|
|
|
|
2024-04-21 21:00:44 +00:00
|
|
|
use chrono::{DateTime, Utc};
|
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;
|
2024-03-26 16:26:31 +00:00
|
|
|
use tokio::sync::mpsc::{channel, Receiver, Sender};
|
2024-04-15 09:06:10 +00:00
|
|
|
use tokio::sync::RwLock;
|
2024-04-26 10:16:23 +00:00
|
|
|
use tracing::{Instrument, Span};
|
2023-02-03 22:43:59 +00:00
|
|
|
|
2024-05-10 20:44:24 +00:00
|
|
|
use crate::clustering::room::*;
|
2023-09-30 23:12:11 +00:00
|
|
|
use crate::prelude::*;
|
2024-05-13 14:32:45 +00:00
|
|
|
use crate::room::{RoomHandle, RoomId, RoomInfo};
|
2023-09-30 23:12:11 +00:00
|
|
|
use crate::table::{AnonTable, Key as AnonKey};
|
2024-05-13 14:32:45 +00:00
|
|
|
use crate::LavinaCore;
|
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
|
|
|
|
2024-03-26 16:26:31 +00:00
|
|
|
/// Node-local identifier of a connection. It is used to address a connection within a player actor.
|
2023-02-13 18:32:52 +00:00
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
|
|
|
pub struct ConnectionId(pub AnonKey);
|
|
|
|
|
2024-03-26 16:26:31 +00:00
|
|
|
/// Representation of an authenticated client connection.
|
|
|
|
/// The public API available to projections through which all client actions are executed.
|
|
|
|
///
|
|
|
|
/// The connection is used to send commands to the player actor and to receive updates that might be sent to the client.
|
2023-02-13 19:16:00 +00:00
|
|
|
pub struct PlayerConnection {
|
|
|
|
pub connection_id: ConnectionId,
|
2024-04-29 17:24:43 +00:00
|
|
|
pub receiver: Receiver<ConnectionMessage>,
|
2023-02-13 19:16:00 +00:00
|
|
|
player_handle: PlayerHandle,
|
|
|
|
}
|
|
|
|
impl PlayerConnection {
|
2024-05-10 20:44:24 +00:00
|
|
|
/// Handled in [Player::send_room_message].
|
2024-04-26 10:16:23 +00:00
|
|
|
#[tracing::instrument(skip(self, body), name = "PlayerConnection::send_message")]
|
2024-04-21 21:00:44 +00:00
|
|
|
pub async fn send_message(&mut self, room_id: RoomId, body: Str) -> Result<SendMessageResult> {
|
2024-03-26 16:26:31 +00:00
|
|
|
let (promise, deferred) = oneshot();
|
|
|
|
let cmd = ClientCommand::SendMessage { room_id, body, promise };
|
|
|
|
self.player_handle.send(ActorCommand::ClientCommand(cmd, self.connection_id.clone())).await;
|
|
|
|
Ok(deferred.await?)
|
2023-02-13 19:16:00 +00:00
|
|
|
}
|
|
|
|
|
2024-03-26 16:26:31 +00:00
|
|
|
/// Handled in [Player::join_room].
|
2024-04-26 10:16:23 +00:00
|
|
|
#[tracing::instrument(skip(self), name = "PlayerConnection::join_room")]
|
2023-02-16 21:49:17 +00:00
|
|
|
pub async fn join_room(&mut self, room_id: RoomId) -> Result<JoinResult> {
|
2024-03-26 16:26:31 +00:00
|
|
|
let (promise, deferred) = oneshot();
|
|
|
|
let cmd = ClientCommand::JoinRoom { room_id, promise };
|
|
|
|
self.player_handle.send(ActorCommand::ClientCommand(cmd, self.connection_id.clone())).await;
|
|
|
|
Ok(deferred.await?)
|
2023-02-13 19:16:00 +00:00
|
|
|
}
|
2023-02-14 18:28:49 +00:00
|
|
|
|
2024-05-10 20:44:24 +00:00
|
|
|
/// Handled in [Player::change_room_topic].
|
2024-04-26 10:16:23 +00:00
|
|
|
#[tracing::instrument(skip(self, new_topic), name = "PlayerConnection::change_topic")]
|
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();
|
2024-03-26 16:26:31 +00:00
|
|
|
let cmd = ClientCommand::ChangeTopic {
|
2023-02-14 22:22:04 +00:00
|
|
|
room_id,
|
|
|
|
new_topic,
|
|
|
|
promise,
|
|
|
|
};
|
2024-03-26 16:26:31 +00:00
|
|
|
self.player_handle.send(ActorCommand::ClientCommand(cmd, self.connection_id.clone())).await;
|
2023-02-14 22:22:04 +00:00
|
|
|
Ok(deferred.await?)
|
|
|
|
}
|
|
|
|
|
2024-03-26 16:26:31 +00:00
|
|
|
/// Handled in [Player::leave_room].
|
2024-04-26 10:16:23 +00:00
|
|
|
#[tracing::instrument(skip(self), name = "PlayerConnection::leave_room")]
|
2023-02-15 17:54:48 +00:00
|
|
|
pub async fn leave_room(&mut self, room_id: RoomId) -> Result<()> {
|
|
|
|
let (promise, deferred) = oneshot();
|
2024-03-26 16:26:31 +00:00
|
|
|
let cmd = ClientCommand::LeaveRoom { room_id, promise };
|
|
|
|
self.player_handle.send(ActorCommand::ClientCommand(cmd, 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) {
|
2024-03-26 16:26:31 +00:00
|
|
|
self.player_handle.send(ActorCommand::TerminateConnection(self.connection_id)).await;
|
2023-02-15 16:47:48 +00:00
|
|
|
}
|
|
|
|
|
2024-03-26 16:26:31 +00:00
|
|
|
/// Handled in [Player::get_rooms].
|
2024-04-26 10:16:23 +00:00
|
|
|
#[tracing::instrument(skip(self), name = "PlayerConnection::get_rooms")]
|
2023-02-15 20:49:52 +00:00
|
|
|
pub async fn get_rooms(&self) -> Result<Vec<RoomInfo>> {
|
|
|
|
let (promise, deferred) = oneshot();
|
2024-03-26 16:26:31 +00:00
|
|
|
let cmd = ClientCommand::GetRooms { promise };
|
|
|
|
self.player_handle.send(ActorCommand::ClientCommand(cmd, self.connection_id.clone())).await;
|
2023-02-15 20:49:52 +00:00
|
|
|
Ok(deferred.await?)
|
2023-02-14 18:28:49 +00:00
|
|
|
}
|
2024-04-23 16:26:40 +00:00
|
|
|
|
|
|
|
/// Handler in [Player::send_dialog_message].
|
2024-04-26 10:16:23 +00:00
|
|
|
#[tracing::instrument(skip(self, body), name = "PlayerConnection::send_dialog_message")]
|
2024-04-23 16:26:40 +00:00
|
|
|
pub async fn send_dialog_message(&self, recipient: PlayerId, body: Str) -> Result<()> {
|
|
|
|
let (promise, deferred) = oneshot();
|
|
|
|
let cmd = ClientCommand::SendDialogMessage {
|
|
|
|
recipient,
|
|
|
|
body,
|
|
|
|
promise,
|
|
|
|
};
|
|
|
|
self.player_handle.send(ActorCommand::ClientCommand(cmd, self.connection_id.clone())).await;
|
|
|
|
Ok(deferred.await?)
|
|
|
|
}
|
2024-05-05 17:21:40 +00:00
|
|
|
|
|
|
|
/// Handler in [Player::check_user_existence].
|
|
|
|
#[tracing::instrument(skip(self), name = "PlayerConnection::check_user_existence")]
|
|
|
|
pub async fn check_user_existence(&self, recipient: PlayerId) -> Result<GetInfoResult> {
|
|
|
|
let (promise, deferred) = oneshot();
|
|
|
|
let cmd = ClientCommand::GetInfo { recipient, promise };
|
|
|
|
self.player_handle.send(ActorCommand::ClientCommand(cmd, self.connection_id.clone())).await;
|
|
|
|
Ok(deferred.await?)
|
|
|
|
}
|
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 {
|
2024-04-26 10:16:23 +00:00
|
|
|
tx: Sender<(ActorCommand, Span)>,
|
2023-02-03 22:43:59 +00:00
|
|
|
}
|
|
|
|
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();
|
2024-03-26 16:26:31 +00:00
|
|
|
let cmd = ActorCommand::AddConnection { sender, promise };
|
2024-04-26 10:16:23 +00:00
|
|
|
self.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
|
|
|
}
|
|
|
|
|
2024-03-26 16:26:31 +00:00
|
|
|
async fn send(&self, command: ActorCommand) {
|
2024-04-26 10:16:23 +00:00
|
|
|
let span = tracing::span!(tracing::Level::INFO, "PlayerHandle::send");
|
2024-03-26 16:26:31 +00:00
|
|
|
// TODO either handle the error or doc why it is safe to ignore
|
2024-04-26 10:16:23 +00:00
|
|
|
let _ = self.tx.send((command, span)).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) {
|
2024-03-26 16:26:31 +00:00
|
|
|
self.send(ActorCommand::Update(update)).await;
|
2023-02-14 19:42:52 +00:00
|
|
|
}
|
2023-02-03 22:43:59 +00:00
|
|
|
}
|
2023-02-14 19:42:52 +00:00
|
|
|
|
2024-03-26 16:26:31 +00:00
|
|
|
/// Messages sent to the player actor.
|
|
|
|
enum ActorCommand {
|
|
|
|
/// Establish a new connection.
|
2023-02-14 19:45:22 +00:00
|
|
|
AddConnection {
|
2024-04-29 17:24:43 +00:00
|
|
|
sender: Sender<ConnectionMessage>,
|
2023-02-14 22:22:04 +00:00
|
|
|
promise: Promise<ConnectionId>,
|
2023-02-03 22:43:59 +00:00
|
|
|
},
|
2024-03-26 16:26:31 +00:00
|
|
|
/// Terminate an existing connection.
|
2023-02-15 16:47:48 +00:00
|
|
|
TerminateConnection(ConnectionId),
|
2024-03-26 16:26:31 +00:00
|
|
|
/// Player-issued command.
|
|
|
|
ClientCommand(ClientCommand, ConnectionId),
|
|
|
|
/// Update which is sent from a room the player is member of.
|
2023-02-14 22:22:04 +00:00
|
|
|
Update(Updates),
|
2023-10-02 21:35:23 +00:00
|
|
|
Stop,
|
2023-02-14 22:22:04 +00:00
|
|
|
}
|
|
|
|
|
2024-03-26 16:26:31 +00:00
|
|
|
/// Client-issued command sent to the player actor. The actor will respond with by fulfilling the promise.
|
|
|
|
pub enum ClientCommand {
|
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,
|
2024-04-21 21:00:44 +00:00
|
|
|
promise: Promise<SendMessageResult>,
|
2023-02-14 22:22:04 +00:00
|
|
|
},
|
|
|
|
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
|
|
|
},
|
2024-03-26 16:26:31 +00:00
|
|
|
GetRooms {
|
|
|
|
promise: Promise<Vec<RoomInfo>>,
|
|
|
|
},
|
2024-04-23 16:26:40 +00:00
|
|
|
SendDialogMessage {
|
|
|
|
recipient: PlayerId,
|
|
|
|
body: Str,
|
|
|
|
promise: Promise<()>,
|
|
|
|
},
|
2024-05-05 17:21:40 +00:00
|
|
|
GetInfo {
|
|
|
|
recipient: PlayerId,
|
|
|
|
promise: Promise<GetInfoResult>,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
pub enum GetInfoResult {
|
|
|
|
UserExists,
|
|
|
|
UserDoesntExist,
|
2023-02-14 19:42:52 +00:00
|
|
|
}
|
|
|
|
|
2023-02-16 21:49:17 +00:00
|
|
|
pub enum JoinResult {
|
|
|
|
Success(RoomInfo),
|
2024-04-20 15:09:44 +00:00
|
|
|
AlreadyJoined,
|
2023-02-16 21:49:17 +00:00
|
|
|
Banned,
|
|
|
|
}
|
|
|
|
|
2024-04-21 21:00:44 +00:00
|
|
|
pub enum SendMessageResult {
|
|
|
|
Success(DateTime<Utc>),
|
|
|
|
NoSuchRoom,
|
|
|
|
}
|
|
|
|
|
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,
|
2024-04-21 21:00:44 +00:00
|
|
|
created_at: DateTime<Utc>,
|
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),
|
2024-04-23 16:26:40 +00:00
|
|
|
NewDialogMessage {
|
|
|
|
sender: PlayerId,
|
|
|
|
receiver: PlayerId,
|
|
|
|
body: Str,
|
|
|
|
created_at: DateTime<Utc>,
|
|
|
|
},
|
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.
|
2024-05-13 14:32:45 +00:00
|
|
|
pub(crate) struct PlayerRegistry(RwLock<PlayerRegistryInner>);
|
2023-02-03 22:43:59 +00:00
|
|
|
impl PlayerRegistry {
|
2024-05-13 14:32:45 +00:00
|
|
|
pub fn empty(metrics: &mut MetricsRegistry) -> Result<PlayerRegistry> {
|
2023-09-30 23:12:11 +00:00
|
|
|
let metric_active_players = IntGauge::new("chat_players_active", "Number of alive player actors")?;
|
2023-02-12 22:23:52 +00:00
|
|
|
metrics.register(Box::new(metric_active_players.clone()))?;
|
2023-02-03 22:43:59 +00:00
|
|
|
let inner = PlayerRegistryInner {
|
|
|
|
players: HashMap::new(),
|
2023-02-12 22:23:52 +00:00
|
|
|
metric_active_players,
|
2023-02-03 22:43:59 +00:00
|
|
|
};
|
2024-05-13 14:32:45 +00:00
|
|
|
Ok(PlayerRegistry(RwLock::new(inner)))
|
2023-02-03 22:43:59 +00:00
|
|
|
}
|
|
|
|
|
2024-05-13 14:32:45 +00:00
|
|
|
pub fn shutdown(self) {
|
|
|
|
let res = self.0.into_inner();
|
2024-04-23 17:14:46 +00:00
|
|
|
drop(res);
|
|
|
|
}
|
|
|
|
|
2024-04-29 17:24:43 +00:00
|
|
|
#[tracing::instrument(skip(self), name = "PlayerRegistry::get_player")]
|
2024-04-23 16:26:40 +00:00
|
|
|
pub async fn get_player(&self, id: &PlayerId) -> Option<PlayerHandle> {
|
|
|
|
let inner = self.0.read().await;
|
|
|
|
inner.players.get(id).map(|(handle, _)| handle.clone())
|
|
|
|
}
|
|
|
|
|
2024-04-29 17:24:43 +00:00
|
|
|
#[tracing::instrument(skip(self), name = "PlayerRegistry::stop_player")]
|
|
|
|
pub async fn stop_player(&self, id: &PlayerId) -> Result<Option<()>> {
|
|
|
|
let mut inner = self.0.write().await;
|
|
|
|
if let Some((handle, fiber)) = inner.players.remove(id) {
|
|
|
|
handle.send(ActorCommand::Stop).await;
|
|
|
|
drop(handle);
|
|
|
|
fiber.await?;
|
|
|
|
inner.metric_active_players.dec();
|
|
|
|
Ok(Some(()))
|
|
|
|
} else {
|
|
|
|
Ok(None)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-13 14:32:45 +00:00
|
|
|
#[tracing::instrument(skip(self, core), name = "PlayerRegistry::get_or_launch_player")]
|
|
|
|
pub async fn get_or_launch_player(&self, core: &LavinaCore, id: &PlayerId) -> PlayerHandle {
|
2024-04-23 16:31:00 +00:00
|
|
|
let inner = self.0.read().await;
|
2024-04-15 09:06:10 +00:00
|
|
|
if let Some((handle, _)) = inner.players.get(id) {
|
2023-02-13 18:32:52 +00:00
|
|
|
handle.clone()
|
|
|
|
} else {
|
2024-04-23 16:31:00 +00:00
|
|
|
drop(inner);
|
|
|
|
let mut inner = self.0.write().await;
|
|
|
|
if let Some((handle, _)) = inner.players.get(id) {
|
|
|
|
handle.clone()
|
|
|
|
} else {
|
2024-05-13 14:32:45 +00:00
|
|
|
let (handle, fiber) = Player::launch(id.clone(), core.clone()).await;
|
2024-04-23 16:31:00 +00:00
|
|
|
inner.players.insert(id.clone(), (handle.clone(), fiber));
|
|
|
|
inner.metric_active_players.inc();
|
|
|
|
handle
|
|
|
|
}
|
2023-02-13 18:32:52 +00:00
|
|
|
}
|
2023-02-03 22:43:59 +00:00
|
|
|
}
|
2023-02-13 19:16:00 +00:00
|
|
|
|
2024-05-13 14:32:45 +00:00
|
|
|
#[tracing::instrument(skip(self, core), name = "PlayerRegistry::connect_to_player")]
|
|
|
|
pub async fn connect_to_player(&self, core: &LavinaCore, id: &PlayerId) -> PlayerConnection {
|
|
|
|
let player_handle = self.get_or_launch_player(core, id).await;
|
2023-02-13 19:16:00 +00:00
|
|
|
player_handle.subscribe().await
|
|
|
|
}
|
2023-08-24 12:10:31 +00:00
|
|
|
|
2024-05-13 14:32:45 +00:00
|
|
|
pub async fn shutdown_all(&self) -> Result<()> {
|
2024-04-15 09:06:10 +00:00
|
|
|
let mut inner = self.0.write().await;
|
2023-08-24 12:10:31 +00:00
|
|
|
for (i, (k, j)) in inner.players.drain() {
|
2024-03-26 16:26:31 +00:00
|
|
|
k.send(ActorCommand::Stop).await;
|
2023-08-24 12:10:31 +00:00
|
|
|
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 {
|
2024-04-15 09:06:10 +00:00
|
|
|
/// Active player actors.
|
2023-02-03 22:43:59 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2024-05-10 20:44:24 +00:00
|
|
|
enum RoomRef {
|
|
|
|
Local(RoomHandle),
|
|
|
|
Remote { node_id: u32 },
|
|
|
|
}
|
|
|
|
|
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,
|
2024-04-15 09:06:10 +00:00
|
|
|
storage_id: u32,
|
2024-04-29 17:24:43 +00:00
|
|
|
connections: AnonTable<Sender<ConnectionMessage>>,
|
2024-05-10 20:44:24 +00:00
|
|
|
my_rooms: HashMap<RoomId, RoomRef>,
|
2023-02-16 21:49:17 +00:00
|
|
|
banned_from: HashSet<RoomId>,
|
2024-04-26 10:16:23 +00:00
|
|
|
rx: Receiver<(ActorCommand, Span)>,
|
2023-02-14 22:38:40 +00:00
|
|
|
handle: PlayerHandle,
|
2024-05-13 14:32:45 +00:00
|
|
|
services: LavinaCore,
|
2023-02-03 22:43:59 +00:00
|
|
|
}
|
|
|
|
impl Player {
|
2024-05-13 14:32:45 +00:00
|
|
|
async fn launch(player_id: PlayerId, core: LavinaCore) -> (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();
|
2024-05-13 14:32:45 +00:00
|
|
|
let storage_id = core.services.storage.retrieve_user_id_by_name(player_id.as_inner()).await.unwrap().unwrap();
|
2023-02-14 22:38:40 +00:00
|
|
|
let player = Player {
|
|
|
|
player_id,
|
2024-04-15 09:06:10 +00:00
|
|
|
storage_id,
|
|
|
|
// connections are empty when the actor is just started
|
2023-02-14 22:38:40 +00:00
|
|
|
connections: AnonTable::new(),
|
2024-04-15 09:06:10 +00:00
|
|
|
// room handlers will be loaded later in the started task
|
2023-02-14 22:38:40 +00:00
|
|
|
my_rooms: HashMap::new(),
|
2024-04-15 09:06:10 +00:00
|
|
|
// TODO implement and load bans
|
|
|
|
banned_from: HashSet::new(),
|
2023-02-14 22:38:40 +00:00
|
|
|
rx,
|
|
|
|
handle,
|
2024-05-13 14:32:45 +00:00
|
|
|
services: core,
|
2023-02-14 22:38:40 +00:00
|
|
|
};
|
|
|
|
let fiber = tokio::task::spawn(player.main_loop());
|
|
|
|
(handle_clone, fiber)
|
|
|
|
}
|
|
|
|
|
2024-05-10 20:44:24 +00:00
|
|
|
fn room_location(&self, room_id: &RoomId) -> Option<u32> {
|
2024-05-13 14:32:45 +00:00
|
|
|
let res = self.services.cluster_metadata.rooms.get(room_id.as_inner().as_ref()).copied();
|
|
|
|
let node = res.unwrap_or(self.services.cluster_metadata.main_owner);
|
|
|
|
if node == self.services.cluster_metadata.node_id {
|
2024-05-10 20:44:24 +00:00
|
|
|
None
|
|
|
|
} else {
|
|
|
|
Some(node)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-14 22:38:40 +00:00
|
|
|
async fn main_loop(mut self) -> Self {
|
2024-05-13 14:32:45 +00:00
|
|
|
let rooms = self.services.storage.get_rooms_of_a_user(self.storage_id).await.unwrap();
|
2024-04-15 09:06:10 +00:00
|
|
|
for room_id in rooms {
|
2024-05-10 20:44:24 +00:00
|
|
|
if let Some(remote_node) = self.room_location(&room_id) {
|
|
|
|
self.my_rooms.insert(room_id.clone(), RoomRef::Remote { node_id: remote_node });
|
2024-05-13 14:32:45 +00:00
|
|
|
self.services.subscribe(self.player_id.clone(), room_id).await;
|
2024-04-15 09:06:10 +00:00
|
|
|
} else {
|
2024-05-13 14:32:45 +00:00
|
|
|
let room = self.services.rooms.get_room(&self.services, &room_id).await;
|
2024-05-10 20:44:24 +00:00
|
|
|
if let Some(room) = room {
|
|
|
|
self.my_rooms.insert(room_id, RoomRef::Local(room));
|
|
|
|
} else {
|
|
|
|
tracing::error!("Room #{room_id:?} not found");
|
|
|
|
}
|
2024-04-15 09:06:10 +00:00
|
|
|
}
|
|
|
|
}
|
2023-02-14 22:38:40 +00:00
|
|
|
while let Some(cmd) = self.rx.recv().await {
|
2024-04-26 10:16:23 +00:00
|
|
|
let (cmd, span) = cmd;
|
|
|
|
let should_stop = async {
|
|
|
|
match cmd {
|
|
|
|
ActorCommand::AddConnection { sender, promise } => {
|
|
|
|
let connection_id = self.connections.insert(sender);
|
|
|
|
if let Err(connection_id) = promise.send(ConnectionId(connection_id)) {
|
|
|
|
log::warn!("Connection {connection_id:?} terminated before finalization");
|
|
|
|
self.terminate_connection(connection_id);
|
|
|
|
}
|
|
|
|
false
|
|
|
|
}
|
|
|
|
ActorCommand::TerminateConnection(connection_id) => {
|
2023-02-15 16:47:48 +00:00
|
|
|
self.terminate_connection(connection_id);
|
2024-04-26 10:16:23 +00:00
|
|
|
false
|
2023-02-15 16:47:48 +00:00
|
|
|
}
|
2024-04-26 10:16:23 +00:00
|
|
|
ActorCommand::Update(update) => {
|
|
|
|
self.handle_update(update).await;
|
|
|
|
false
|
|
|
|
}
|
|
|
|
ActorCommand::ClientCommand(cmd, connection_id) => {
|
|
|
|
self.handle_cmd(cmd, connection_id).await;
|
|
|
|
false
|
|
|
|
}
|
|
|
|
ActorCommand::Stop => true,
|
2023-02-15 16:47:48 +00:00
|
|
|
}
|
2024-04-26 10:16:23 +00:00
|
|
|
}
|
|
|
|
.instrument(span)
|
|
|
|
.await;
|
|
|
|
if should_stop {
|
|
|
|
break;
|
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
|
|
|
|
}
|
|
|
|
|
2024-03-26 16:26:31 +00:00
|
|
|
/// Handle an incoming update by changing the internal state and broadcasting it to all connections if necessary.
|
2024-04-26 10:16:23 +00:00
|
|
|
#[tracing::instrument(skip(self, update), name = "Player::handle_update")]
|
2024-03-26 16:26:31 +00:00
|
|
|
async fn handle_update(&mut self, update: Updates) {
|
2024-04-23 16:26:40 +00:00
|
|
|
log::debug!(
|
2024-03-26 16:26:31 +00:00
|
|
|
"Player received an update, broadcasting to {} connections",
|
|
|
|
self.connections.len()
|
|
|
|
);
|
|
|
|
match update {
|
|
|
|
Updates::BannedFrom(ref room_id) => {
|
|
|
|
self.banned_from.insert(room_id.clone());
|
|
|
|
self.my_rooms.remove(room_id);
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
for (_, connection) in &self.connections {
|
2024-04-29 17:24:43 +00:00
|
|
|
let _ = connection.send(ConnectionMessage::Update(update.clone())).await;
|
2024-03-26 16:26:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-26 16:26:31 +00:00
|
|
|
/// Dispatches a client command to the appropriate handler.
|
|
|
|
async fn handle_cmd(&mut self, cmd: ClientCommand, connection_id: ConnectionId) {
|
2023-02-14 22:38:40 +00:00
|
|
|
match cmd {
|
2024-03-26 16:26:31 +00:00
|
|
|
ClientCommand::JoinRoom { room_id, promise } => {
|
|
|
|
let result = self.join_room(connection_id, room_id).await;
|
|
|
|
let _ = promise.send(result);
|
2023-02-14 22:38:40 +00:00
|
|
|
}
|
2024-03-26 16:26:31 +00:00
|
|
|
ClientCommand::LeaveRoom { room_id, promise } => {
|
|
|
|
self.leave_room(connection_id, room_id).await;
|
2023-03-21 21:50:40 +00:00
|
|
|
let _ = promise.send(());
|
2023-02-15 17:54:48 +00:00
|
|
|
}
|
2024-03-26 16:26:31 +00:00
|
|
|
ClientCommand::SendMessage { room_id, body, promise } => {
|
2024-05-10 20:44:24 +00:00
|
|
|
let result = self.send_room_message(connection_id, room_id, body).await;
|
2024-04-21 21:00:44 +00:00
|
|
|
let _ = promise.send(result);
|
2023-02-14 22:38:40 +00:00
|
|
|
}
|
2024-03-26 16:26:31 +00:00
|
|
|
ClientCommand::ChangeTopic {
|
2023-02-14 22:38:40 +00:00
|
|
|
room_id,
|
|
|
|
new_topic,
|
|
|
|
promise,
|
|
|
|
} => {
|
2024-05-10 20:44:24 +00:00
|
|
|
self.change_room_topic(connection_id, room_id, new_topic).await;
|
2023-03-21 21:50:40 +00:00
|
|
|
let _ = promise.send(());
|
2023-02-14 22:38:40 +00:00
|
|
|
}
|
2024-03-26 16:26:31 +00:00
|
|
|
ClientCommand::GetRooms { promise } => {
|
|
|
|
let result = self.get_rooms().await;
|
|
|
|
let _ = promise.send(result);
|
|
|
|
}
|
2024-04-23 16:26:40 +00:00
|
|
|
ClientCommand::SendDialogMessage {
|
|
|
|
recipient,
|
|
|
|
body,
|
|
|
|
promise,
|
|
|
|
} => {
|
|
|
|
self.send_dialog_message(connection_id, recipient, body).await;
|
|
|
|
let _ = promise.send(());
|
|
|
|
}
|
2024-05-05 17:21:40 +00:00
|
|
|
ClientCommand::GetInfo { recipient, promise } => {
|
|
|
|
let result = self.check_user_existence(recipient).await;
|
|
|
|
let _ = promise.send(result);
|
|
|
|
}
|
2024-03-26 16:26:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-26 10:16:23 +00:00
|
|
|
#[tracing::instrument(skip(self), name = "Player::join_room")]
|
2024-03-26 16:26:31 +00:00
|
|
|
async fn join_room(&mut self, connection_id: ConnectionId, room_id: RoomId) -> JoinResult {
|
|
|
|
if self.banned_from.contains(&room_id) {
|
|
|
|
return JoinResult::Banned;
|
|
|
|
}
|
2024-04-20 15:09:44 +00:00
|
|
|
if self.my_rooms.contains_key(&room_id) {
|
|
|
|
return JoinResult::AlreadyJoined;
|
|
|
|
}
|
2024-03-26 16:26:31 +00:00
|
|
|
|
2024-05-10 20:44:24 +00:00
|
|
|
if let Some(remote_node) = self.room_location(&room_id) {
|
|
|
|
let req = JoinRoomReq {
|
|
|
|
room_id: room_id.as_inner(),
|
|
|
|
player_id: self.player_id.as_inner(),
|
|
|
|
};
|
2024-05-13 14:32:45 +00:00
|
|
|
self.services.client.join_room(remote_node, req).await.unwrap();
|
|
|
|
let room_storage_id =
|
|
|
|
self.services.storage.create_or_retrieve_room_id_by_name(room_id.as_inner()).await.unwrap();
|
|
|
|
self.services.storage.add_room_member(room_storage_id, self.storage_id).await.unwrap();
|
2024-05-10 20:44:24 +00:00
|
|
|
self.my_rooms.insert(room_id.clone(), RoomRef::Remote { node_id: remote_node });
|
|
|
|
JoinResult::Success(RoomInfo {
|
|
|
|
id: room_id,
|
|
|
|
topic: "unknown".into(),
|
|
|
|
members: vec![],
|
|
|
|
})
|
|
|
|
} else {
|
2024-05-13 14:32:45 +00:00
|
|
|
let room = match self.services.rooms.get_or_create_room(&self.services, room_id.clone()).await {
|
2024-05-10 20:44:24 +00:00
|
|
|
Ok(room) => room,
|
|
|
|
Err(e) => {
|
|
|
|
log::error!("Failed to get or create room: {e}");
|
|
|
|
todo!();
|
|
|
|
}
|
|
|
|
};
|
2024-05-13 14:32:45 +00:00
|
|
|
room.add_member(&self.services, &self.player_id, self.storage_id).await;
|
2024-05-10 20:44:24 +00:00
|
|
|
room.subscribe(&self.player_id, self.handle.clone()).await;
|
|
|
|
self.my_rooms.insert(room_id.clone(), RoomRef::Local(room.clone()));
|
|
|
|
let room_info = room.get_room_info().await;
|
|
|
|
let update = Updates::RoomJoined {
|
|
|
|
room_id,
|
|
|
|
new_member_id: self.player_id.clone(),
|
|
|
|
};
|
|
|
|
self.broadcast_update(update, connection_id).await;
|
|
|
|
JoinResult::Success(room_info)
|
|
|
|
}
|
2024-03-26 16:26:31 +00:00
|
|
|
}
|
|
|
|
|
2024-04-26 10:16:23 +00:00
|
|
|
#[tracing::instrument(skip(self), name = "Player::leave_room")]
|
2024-03-26 16:26:31 +00:00
|
|
|
async fn leave_room(&mut self, connection_id: ConnectionId, room_id: RoomId) {
|
|
|
|
let room = self.my_rooms.remove(&room_id);
|
|
|
|
if let Some(room) = room {
|
2024-05-10 20:44:24 +00:00
|
|
|
match room {
|
|
|
|
RoomRef::Local(room) => {
|
|
|
|
room.unsubscribe(&self.player_id).await;
|
2024-05-13 14:32:45 +00:00
|
|
|
room.remove_member(&self.services, &self.player_id, self.storage_id).await;
|
2024-05-10 20:44:24 +00:00
|
|
|
}
|
|
|
|
RoomRef::Remote { node_id } => {
|
|
|
|
let req = LeaveRoomReq {
|
|
|
|
room_id: room_id.as_inner(),
|
|
|
|
player_id: self.player_id.as_inner(),
|
|
|
|
};
|
2024-05-13 14:32:45 +00:00
|
|
|
self.services.client.leave_room(node_id, req).await.unwrap();
|
2024-05-10 20:44:24 +00:00
|
|
|
let room_storage_id =
|
2024-05-13 14:32:45 +00:00
|
|
|
self.services.storage.create_or_retrieve_room_id_by_name(room_id.as_inner()).await.unwrap();
|
|
|
|
self.services.storage.remove_room_member(room_storage_id, self.storage_id).await.unwrap();
|
2024-05-10 20:44:24 +00:00
|
|
|
}
|
|
|
|
}
|
2024-03-26 16:26:31 +00:00
|
|
|
}
|
|
|
|
let update = Updates::RoomLeft {
|
|
|
|
room_id,
|
|
|
|
former_member_id: self.player_id.clone(),
|
|
|
|
};
|
|
|
|
self.broadcast_update(update, connection_id).await;
|
|
|
|
}
|
|
|
|
|
2024-05-10 20:44:24 +00:00
|
|
|
#[tracing::instrument(skip(self, body), name = "Player::send_room_message")]
|
|
|
|
async fn send_room_message(
|
|
|
|
&mut self,
|
|
|
|
connection_id: ConnectionId,
|
|
|
|
room_id: RoomId,
|
|
|
|
body: Str,
|
|
|
|
) -> SendMessageResult {
|
2024-04-15 09:06:10 +00:00
|
|
|
let Some(room) = self.my_rooms.get(&room_id) else {
|
2024-03-26 16:26:31 +00:00
|
|
|
tracing::info!("no room found");
|
2024-04-21 21:00:44 +00:00
|
|
|
return SendMessageResult::NoSuchRoom;
|
2024-04-15 09:06:10 +00:00
|
|
|
};
|
2024-05-13 14:32:45 +00:00
|
|
|
let created_at = Utc::now();
|
2024-05-10 20:44:24 +00:00
|
|
|
match room {
|
|
|
|
RoomRef::Local(room) => {
|
2024-05-13 14:32:45 +00:00
|
|
|
room.send_message(&self.services, &self.player_id, body.clone(), created_at.clone()).await;
|
2024-05-10 20:44:24 +00:00
|
|
|
}
|
|
|
|
RoomRef::Remote { node_id } => {
|
|
|
|
let req = SendMessageReq {
|
|
|
|
room_id: room_id.as_inner(),
|
|
|
|
player_id: self.player_id.as_inner(),
|
|
|
|
message: &*body,
|
|
|
|
created_at: &*created_at.to_rfc3339(),
|
|
|
|
};
|
2024-05-13 14:32:45 +00:00
|
|
|
self.services.client.send_room_message(*node_id, req).await.unwrap();
|
|
|
|
self.services
|
2024-05-10 20:44:24 +00:00
|
|
|
.broadcast(
|
|
|
|
room_id.clone(),
|
|
|
|
self.player_id.clone(),
|
|
|
|
body.clone(),
|
|
|
|
created_at.clone(),
|
|
|
|
)
|
|
|
|
.await;
|
|
|
|
}
|
|
|
|
}
|
2024-03-26 16:26:31 +00:00
|
|
|
let update = Updates::NewMessage {
|
|
|
|
room_id,
|
|
|
|
author_id: self.player_id.clone(),
|
|
|
|
body,
|
2024-04-21 21:00:44 +00:00
|
|
|
created_at,
|
2024-03-26 16:26:31 +00:00
|
|
|
};
|
|
|
|
self.broadcast_update(update, connection_id).await;
|
2024-04-21 21:00:44 +00:00
|
|
|
SendMessageResult::Success(created_at)
|
2024-03-26 16:26:31 +00:00
|
|
|
}
|
|
|
|
|
2024-05-10 20:44:24 +00:00
|
|
|
#[tracing::instrument(skip(self, new_topic), name = "Player::change_room_topic")]
|
|
|
|
async fn change_room_topic(&mut self, connection_id: ConnectionId, room_id: RoomId, new_topic: Str) {
|
2024-04-15 09:06:10 +00:00
|
|
|
let Some(room) = self.my_rooms.get(&room_id) else {
|
2024-03-26 16:26:31 +00:00
|
|
|
tracing::info!("no room found");
|
2024-04-15 09:06:10 +00:00
|
|
|
return;
|
|
|
|
};
|
2024-05-10 20:44:24 +00:00
|
|
|
match room {
|
|
|
|
RoomRef::Local(room) => {
|
2024-05-13 14:32:45 +00:00
|
|
|
room.set_topic(&self.services, &self.player_id, new_topic.clone()).await;
|
2024-05-10 20:44:24 +00:00
|
|
|
}
|
|
|
|
RoomRef::Remote { node_id } => {
|
|
|
|
let req = SetRoomTopicReq {
|
|
|
|
room_id: room_id.as_inner(),
|
|
|
|
player_id: self.player_id.as_inner(),
|
|
|
|
topic: &*new_topic,
|
|
|
|
};
|
2024-05-13 14:32:45 +00:00
|
|
|
self.services.client.set_room_topic(*node_id, req).await.unwrap();
|
2024-05-10 20:44:24 +00:00
|
|
|
}
|
|
|
|
}
|
2024-03-26 16:26:31 +00:00
|
|
|
let update = Updates::RoomTopicChanged { room_id, new_topic };
|
|
|
|
self.broadcast_update(update, connection_id).await;
|
|
|
|
}
|
|
|
|
|
2024-04-26 10:16:23 +00:00
|
|
|
#[tracing::instrument(skip(self), name = "Player::get_rooms")]
|
2024-03-26 16:26:31 +00:00
|
|
|
async fn get_rooms(&self) -> Vec<RoomInfo> {
|
|
|
|
let mut response = vec![];
|
2024-05-10 20:44:24 +00:00
|
|
|
for (room_id, handle) in &self.my_rooms {
|
|
|
|
match handle {
|
|
|
|
RoomRef::Local(handle) => {
|
|
|
|
response.push(handle.get_room_info().await);
|
|
|
|
}
|
|
|
|
RoomRef::Remote { .. } => {
|
|
|
|
let room_info = RoomInfo {
|
|
|
|
id: room_id.clone(),
|
|
|
|
topic: "unknown".into(),
|
|
|
|
members: vec![],
|
|
|
|
};
|
|
|
|
response.push(room_info);
|
|
|
|
}
|
|
|
|
}
|
2023-02-14 22:38:40 +00:00
|
|
|
}
|
2024-03-26 16:26:31 +00:00
|
|
|
response
|
2023-02-14 22:38:40 +00:00
|
|
|
}
|
|
|
|
|
2024-04-26 10:16:23 +00:00
|
|
|
#[tracing::instrument(skip(self, body), name = "Player::send_dialog_message")]
|
2024-04-23 16:26:40 +00:00
|
|
|
async fn send_dialog_message(&self, connection_id: ConnectionId, recipient: PlayerId, body: Str) {
|
2024-05-13 14:32:45 +00:00
|
|
|
let created_at = Utc::now();
|
|
|
|
self.services
|
|
|
|
.send_dialog_message(self.player_id.clone(), recipient.clone(), body.clone(), &created_at)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
2024-04-23 16:26:40 +00:00
|
|
|
let update = Updates::NewDialogMessage {
|
|
|
|
sender: self.player_id.clone(),
|
|
|
|
receiver: recipient.clone(),
|
|
|
|
body,
|
|
|
|
created_at,
|
|
|
|
};
|
|
|
|
self.broadcast_update(update, connection_id).await;
|
|
|
|
}
|
|
|
|
|
2024-05-05 17:21:40 +00:00
|
|
|
#[tracing::instrument(skip(self), name = "Player::check_user_existence")]
|
|
|
|
async fn check_user_existence(&self, recipient: PlayerId) -> GetInfoResult {
|
2024-05-13 14:32:45 +00:00
|
|
|
if self.services.storage.check_user_existence(recipient.as_inner().as_ref()).await.unwrap() {
|
2024-05-05 17:21:40 +00:00
|
|
|
GetInfoResult::UserExists
|
|
|
|
} else {
|
|
|
|
GetInfoResult::UserDoesntExist
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-26 16:26:31 +00:00
|
|
|
/// Broadcasts an update to all connections except the one with the given id.
|
|
|
|
///
|
|
|
|
/// This is called after handling a client command.
|
|
|
|
/// Sending the update to the connection which sent the command is handled by the connection itself.
|
2024-04-26 10:16:23 +00:00
|
|
|
#[tracing::instrument(skip(self, update), name = "Player::broadcast_update")]
|
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;
|
|
|
|
}
|
2024-04-29 17:24:43 +00:00
|
|
|
let _ = b.send(ConnectionMessage::Update(update.clone())).await;
|
2023-02-14 22:38:40 +00:00
|
|
|
}
|
2023-02-03 22:43:59 +00:00
|
|
|
}
|
|
|
|
}
|
2024-04-29 17:24:43 +00:00
|
|
|
|
|
|
|
pub enum ConnectionMessage {
|
|
|
|
Update(Updates),
|
|
|
|
Stop(StopReason),
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum StopReason {
|
|
|
|
ServerShutdown,
|
|
|
|
InternalError,
|
|
|
|
}
|