2023-02-04 01:01:49 +00:00
|
|
|
//! Domain definitions and implementation of common chat logic.
|
2024-05-13 14:32:45 +00:00
|
|
|
use std::ops::Deref;
|
2024-05-10 20:44:24 +00:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
2024-04-21 17:45:50 +00:00
|
|
|
use anyhow::Result;
|
|
|
|
use prometheus::Registry as MetricsRegistry;
|
|
|
|
|
2024-05-10 20:44:24 +00:00
|
|
|
use crate::clustering::broadcast::Broadcasting;
|
2024-05-13 14:32:45 +00:00
|
|
|
use crate::clustering::{ClusterConfig, ClusterMetadata, LavinaClient};
|
2024-04-23 16:26:40 +00:00
|
|
|
use crate::dialog::DialogRegistry;
|
2024-05-13 14:32:45 +00:00
|
|
|
use crate::player::{PlayerConnection, PlayerId, PlayerRegistry};
|
2024-04-21 17:45:50 +00:00
|
|
|
use crate::repo::Storage;
|
2024-05-13 14:32:45 +00:00
|
|
|
use crate::room::{RoomHandle, RoomId, RoomInfo, RoomRegistry};
|
2024-04-21 17:45:50 +00:00
|
|
|
|
2024-04-23 10:10:10 +00:00
|
|
|
pub mod auth;
|
2024-05-10 20:44:24 +00:00
|
|
|
pub mod clustering;
|
2024-04-23 16:26:40 +00:00
|
|
|
pub mod dialog;
|
2023-02-04 01:01:49 +00:00
|
|
|
pub mod player;
|
2023-09-30 23:34:35 +00:00
|
|
|
pub mod prelude;
|
2023-07-07 13:09:24 +00:00
|
|
|
pub mod repo;
|
2023-02-04 01:01:49 +00:00
|
|
|
pub mod room;
|
2023-09-30 23:34:35 +00:00
|
|
|
pub mod terminator;
|
2023-09-30 23:12:11 +00:00
|
|
|
|
|
|
|
mod table;
|
2024-04-21 17:45:50 +00:00
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct LavinaCore {
|
2024-05-13 14:32:45 +00:00
|
|
|
services: Arc<Services>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Deref for LavinaCore {
|
|
|
|
type Target = Services;
|
|
|
|
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
&self.services
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl LavinaCore {
|
|
|
|
pub async fn connect_to_player(&self, player_id: &PlayerId) -> PlayerConnection {
|
|
|
|
self.services.players.connect_to_player(&self, player_id).await
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn get_room(&self, room_id: &RoomId) -> Option<RoomHandle> {
|
|
|
|
self.services.rooms.get_room(&self.services, room_id).await
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn create_player(&self, player_id: &PlayerId) -> Result<()> {
|
|
|
|
self.services.storage.create_user(player_id.as_inner()).await
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn get_all_rooms(&self) -> Vec<RoomInfo> {
|
|
|
|
self.services.rooms.get_all_rooms().await
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn stop_player(&self, player_id: &PlayerId) -> Result<Option<()>> {
|
|
|
|
self.services.players.stop_player(player_id).await
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Services {
|
|
|
|
pub(crate) players: PlayerRegistry,
|
|
|
|
pub(crate) rooms: RoomRegistry,
|
|
|
|
pub(crate) dialogs: DialogRegistry,
|
|
|
|
pub(crate) broadcasting: Broadcasting,
|
|
|
|
pub(crate) client: LavinaClient,
|
|
|
|
pub(crate) storage: Storage,
|
|
|
|
pub(crate) cluster_metadata: ClusterMetadata,
|
2024-04-21 17:45:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl LavinaCore {
|
2024-05-10 20:44:24 +00:00
|
|
|
pub async fn new(
|
2024-05-13 14:32:45 +00:00
|
|
|
metrics: &mut MetricsRegistry,
|
2024-05-10 20:44:24 +00:00
|
|
|
cluster_config: ClusterConfig,
|
|
|
|
storage: Storage,
|
|
|
|
) -> Result<LavinaCore> {
|
2024-04-21 17:45:50 +00:00
|
|
|
// TODO shutdown all services in reverse order on error
|
2024-05-10 20:44:24 +00:00
|
|
|
let broadcasting = Broadcasting::new();
|
|
|
|
let client = LavinaClient::new(cluster_config.addresses.clone());
|
2024-05-13 14:32:45 +00:00
|
|
|
let rooms = RoomRegistry::new(metrics)?;
|
|
|
|
let dialogs = DialogRegistry::new();
|
|
|
|
let players = PlayerRegistry::empty(metrics)?;
|
|
|
|
|
|
|
|
let services = Services {
|
2024-04-23 16:26:40 +00:00
|
|
|
players,
|
|
|
|
rooms,
|
|
|
|
dialogs,
|
2024-05-10 20:44:24 +00:00
|
|
|
broadcasting,
|
2024-05-13 14:32:45 +00:00
|
|
|
client,
|
|
|
|
storage,
|
|
|
|
cluster_metadata: cluster_config.metadata,
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(LavinaCore {
|
|
|
|
services: Arc::new(services),
|
2024-04-23 16:26:40 +00:00
|
|
|
})
|
2024-04-21 17:45:50 +00:00
|
|
|
}
|
|
|
|
|
2024-05-13 14:32:45 +00:00
|
|
|
pub async fn shutdown(self) -> Storage {
|
|
|
|
let _ = self.players.shutdown_all().await;
|
|
|
|
let services = match Arc::try_unwrap(self.services) {
|
|
|
|
Ok(e) => e,
|
|
|
|
Err(_) => {
|
|
|
|
panic!("failed to acquire services ownership on shutdown");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
let _ = services.players.shutdown();
|
|
|
|
let _ = services.dialogs.shutdown();
|
|
|
|
let _ = services.rooms.shutdown();
|
|
|
|
services.storage
|
2024-04-21 17:45:50 +00:00
|
|
|
}
|
|
|
|
}
|