Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/log
/target
/server_config.yml
1 change: 1 addition & 0 deletions phira-mp-server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ anyhow = { version = "1.0", features = ["backtrace"] }
phira-mp-common = { path = "../phira-mp-common" }
reqwest = { version = "0.11.18", features = ["json"] }
serde = { version = "1.0.163", features = ["derive"] }
serde_yaml = "0.9"
tap = "1.0.1"
tokio = "*"
tracing = "0.1.37"
Expand Down
141 changes: 140 additions & 1 deletion phira-mp-server/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::{vacant_entry, IdMap, Room, SafeMap, Session, User};
use anyhow::Result;
use phira_mp_common::RoomId;
use serde::Deserialize;
use std::sync::Arc;
use std::{fs, sync::Arc};
use tokio::{net::TcpListener, sync::mpsc, task::JoinHandle};
use tracing::{info, warn};
use uuid::Uuid;
Expand All @@ -13,6 +13,18 @@ pub struct Chart {
pub name: String,
}

#[derive(Debug, Deserialize)]
pub struct ServerConfig {
pub monitors: Vec<i32>
}
impl Default for ServerConfig {
fn default() -> Self {
Self {
monitors: vec![2],
}
}
}

#[derive(Debug, Deserialize)]
pub struct Record {
pub id: i32,
Expand All @@ -30,6 +42,7 @@ pub struct Record {
}

pub struct ServerState {
pub config: ServerConfig,
pub sessions: IdMap<Arc<Session>>,
pub users: SafeMap<i32, Arc<User>>,

Expand All @@ -41,14 +54,140 @@ pub struct ServerState {
pub struct Server {
state: Arc<ServerState>,
listener: TcpListener,
lost_con_handle: JoinHandle<()>,
}

impl From<TcpListener> for Server {use crate::{vacant_entry, IdMap, Room, SafeMap, Session, User};
use anyhow::Result;
use phira_mp_common::RoomId;
use serde::Deserialize;
use std::{fs::File, sync::Arc};
use tokio::{net::TcpListener, sync::mpsc, task::JoinHandle};
use tracing::{info, warn};
use uuid::Uuid;

#[derive(Debug, Deserialize)]
pub struct Chart {
pub id: i32,
pub name: String,
}

#[derive(Debug, Deserialize)]
pub struct ServerConfig {
pub monitors: Vec<i32>,
}
impl Default for ServerConfig {
fn default() -> Self {
Self { monitors: vec![2] }
}
}

#[derive(Debug, Deserialize)]
pub struct Record {
pub id: i32,
pub player: i32,
pub score: i32,
pub perfect: i32,
pub good: i32,
pub bad: i32,
pub miss: i32,
pub max_combo: i32,
pub accuracy: f32,
pub full_combo: bool,
pub std: f32,
pub std_score: f32,
}

pub struct ServerState {
pub config: ServerConfig,
pub sessions: IdMap<Arc<Session>>,
pub users: SafeMap<i32, Arc<User>>,

pub rooms: SafeMap<RoomId, Arc<Room>>,

pub lost_con_tx: mpsc::Sender<Uuid>,
}

pub struct Server {
state: Arc<ServerState>,
listener: TcpListener,
lost_con_handle: JoinHandle<()>,
}

impl From<TcpListener> for Server {
fn from(listener: TcpListener) -> Self {
let (lost_con_tx, mut lost_con_rx) = mpsc::channel(16);
let config: ServerConfig = File::open("server_config.yml")
.ok()
.and_then(|f| serde_yaml::from_reader(f).ok())
.unwrap_or_default();
Comment on lines +120 to +123
Copy link
Copy Markdown
Contributor

@YuevUwU YuevUwU Feb 4, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WARNING: Ignore Too Broad Errors
io::Error and serde_yaml::Error are ignored without any message, and then rollback to use the default vec![2].
This will cause allowed users other than Mivik to inexplicably find themselves unable to monitor when attempting to monitor the system.

Fix: #43

let state = Arc::new(ServerState {
config,
sessions: IdMap::default(),
users: SafeMap::default(),

rooms: SafeMap::default(),

lost_con_tx,
});
let lost_con_handle = tokio::spawn({
let state = Arc::clone(&state);
async move {
while let Some(id) = lost_con_rx.recv().await {
warn!("lost connection with {id}");
if let Some(session) = state.sessions.write().await.remove(&id) {
if session
.user
.session
.read()
.await
.as_ref()
.map_or(false, |it| it.ptr_eq(&Arc::downgrade(&session)))
{
Arc::clone(&session.user).dangle().await;
}
}
}
}
});

Self {
listener,
state,

lost_con_handle,
}
}
}

impl Server {
pub async fn accept(&self) -> Result<()> {
let (stream, addr) = self.listener.accept().await?;
let mut guard = self.state.sessions.write().await;
let entry = vacant_entry(&mut guard);
let session = Session::new(*entry.key(), stream, Arc::clone(&self.state)).await?;
info!(
"received connections from {addr} ({}), version: {}",
session.id,
session.version()
);
entry.insert(session);
Ok(())
}
}

impl Drop for Server {
fn drop(&mut self) {
self.lost_con_handle.abort();
}
}

fn from(listener: TcpListener) -> Self {
let (lost_con_tx, mut lost_con_rx) = mpsc::channel(16);
let config:
let config: ServerConfig = serde_yaml::from_str(&fs::read_to_string("server_config.yml").unwrap();
let state = Arc::new(ServerState {
config,
sessions: IdMap::default(),
users: SafeMap::default(),

Expand Down
3 changes: 1 addition & 2 deletions phira-mp-server/src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ use tracing::{debug, debug_span, error, info, trace, warn, Instrument};
use uuid::Uuid;

const HOST: &str = "https://phira.5wyxi.com";
const MONITORS: &[i32] = &[2, 143245];

pub struct User {
pub id: i32,
Expand Down Expand Up @@ -71,7 +70,7 @@ impl User {
}

pub fn can_monitor(&self) -> bool {
MONITORS.contains(&self.id)
self.server.config.monitors.contains(&self.id)
}

pub async fn set_session(&self, session: Weak<Session>) {
Expand Down