use PlayerConnectionResult enum
check-and-test / check-and-test (push) Failing after 1m16s Details
check-and-test / check-and-test (pull_request) Failing after 1m19s Details

This commit is contained in:
Nikita Vilunov 2024-06-03 17:15:26 +02:00
parent ba09ae6bc5
commit 56df1d03ec
5 changed files with 30 additions and 18 deletions

View File

@ -8,7 +8,7 @@ use prometheus::Registry as MetricsRegistry;
use crate::clustering::broadcast::Broadcasting;
use crate::clustering::{ClusterConfig, ClusterMetadata, LavinaClient};
use crate::dialog::DialogRegistry;
use crate::player::{PlayerConnection, PlayerId, PlayerRegistry};
use crate::player::{PlayerConnectionResult, PlayerId, PlayerRegistry};
use crate::repo::Storage;
use crate::room::{RoomHandle, RoomId, RoomInfo, RoomRegistry};
@ -37,7 +37,7 @@ impl Deref for LavinaCore {
}
impl LavinaCore {
pub async fn connect_to_player(&self, player_id: &PlayerId) -> Result<Option<PlayerConnection>> {
pub async fn connect_to_player(&self, player_id: &PlayerId) -> Result<PlayerConnectionResult> {
self.services.players.connect_to_player(&self, player_id).await
}

View File

@ -317,7 +317,7 @@ impl PlayerRegistry {
}
#[tracing::instrument(skip(self, core), name = "PlayerRegistry::get_or_launch_player")]
pub async fn get_or_launch_player(&self, core: &LavinaCore, id: &PlayerId) -> Result<Option<PlayerHandle>> {
async fn get_or_launch_player(&self, core: &LavinaCore, id: &PlayerId) -> Result<Option<PlayerHandle>> {
let inner = self.0.read().await;
if let Some((handle, _)) = inner.players.get(id) {
Ok(Some(handle.clone()))
@ -338,11 +338,11 @@ impl PlayerRegistry {
}
#[tracing::instrument(skip(self, core), name = "PlayerRegistry::connect_to_player")]
pub async fn connect_to_player(&self, core: &LavinaCore, id: &PlayerId) -> Result<Option<PlayerConnection>> {
pub async fn connect_to_player(&self, core: &LavinaCore, id: &PlayerId) -> Result<PlayerConnectionResult> {
let Some(player_handle) = self.get_or_launch_player(core, id).await? else {
return Ok(None);
return Ok(PlayerConnectionResult::PlayerNotFound);
};
Ok(Some(player_handle.subscribe().await))
Ok(PlayerConnectionResult::Success(player_handle.subscribe().await))
}
pub async fn shutdown_all(&self) {

View File

@ -437,9 +437,12 @@ async fn handle_registered_socket<'a>(
log::info!("Handling registered user: {user:?}");
let player_id = PlayerId::from(user.nickname.clone())?;
let Some(mut connection) = core.connect_to_player(&player_id).await? else {
tracing::error!("Authorized user unexpectedly not found in the database");
return Err(anyhow!("no such user"));
let mut connection = match core.connect_to_player(&player_id).await? {
PlayerConnectionResult::Success(connection) => connection,
PlayerConnectionResult::PlayerNotFound => {
tracing::error!("Authorized user unexpectedly not found in the database");
return Err(anyhow!("no such user"));
}
};
let text: Str = format!("Welcome to {} Server", &config.server_name).into();

View File

@ -23,7 +23,7 @@ use tokio_rustls::rustls::{Certificate, PrivateKey};
use tokio_rustls::TlsAcceptor;
use lavina_core::auth::Verdict;
use lavina_core::player::{ConnectionMessage, PlayerConnection, PlayerId, StopReason};
use lavina_core::player::{ConnectionMessage, PlayerConnection, PlayerConnectionResult, PlayerId, StopReason};
use lavina_core::prelude::*;
use lavina_core::terminator::Terminator;
use lavina_core::LavinaCore;
@ -202,9 +202,12 @@ async fn handle_socket(
authenticated = socket_auth(&mut xml_reader, &mut xml_writer, &mut reader_buf, &core, &hostname) => {
match authenticated {
Ok(authenticated) => {
let Some(mut connection) = core.connect_to_player(&authenticated.player_id).await? else {
tracing::error!("Authorized user unexpectedly not found in the database");
return Err(anyhow!("no such user"));
let mut connection = match core.connect_to_player(&authenticated.player_id).await? {
PlayerConnectionResult::Success(connection) => connection,
PlayerConnectionResult::PlayerNotFound => {
tracing::error!("Authorized user unexpectedly not found in the database");
return Err(anyhow!("no such user"));
}
};
socket_final(
&mut xml_reader,

View File

@ -13,7 +13,7 @@ use serde::{Deserialize, Serialize};
use tokio::net::TcpListener;
use lavina_core::auth::UpdatePasswordResult;
use lavina_core::player::{PlayerId, SendMessageResult};
use lavina_core::player::{PlayerConnectionResult, PlayerId, SendMessageResult};
use lavina_core::prelude::*;
use lavina_core::room::RoomId;
use lavina_core::terminator::Terminator;
@ -170,8 +170,11 @@ async fn endpoint_send_room_message(
let Ok(player_id) = PlayerId::from(req.author_id) else {
return Ok(player_not_found());
};
let Some(mut connection) = core.connect_to_player(&player_id).await? else {
return Ok(player_not_found());
let mut connection = match core.connect_to_player(&player_id).await? {
PlayerConnectionResult::Success(connection) => connection,
PlayerConnectionResult::PlayerNotFound => {
return Ok(player_not_found());
}
};
let res = connection.send_message(room_id, req.message.into()).await?;
match res {
@ -195,8 +198,11 @@ async fn endpoint_set_room_topic(
let Ok(player_id) = PlayerId::from(req.author_id) else {
return Ok(player_not_found());
};
let Some(mut connection) = core.connect_to_player(&player_id).await? else {
return Ok(player_not_found());
let mut connection = match core.connect_to_player(&player_id).await? {
PlayerConnectionResult::Success(connection) => connection,
PlayerConnectionResult::PlayerNotFound => {
return Ok(player_not_found());
}
};
connection.change_topic(room_id, req.topic.into()).await?;
Ok(empty_204_request())