mirror of
https://github.com/BeamMP/BeamMP-Server.git
synced 2026-10-25 13:30:43 +00:00
wip updating of server status
This commit is contained in:
1 parent
7598261aff
commit
0e75a35c83
3 files changed
+40
-18
No files matched your search
+11
-4
@@ -1,4 +1,5 @@
|
|||||||
use serde::Serialize;
|
use serde::Serialize;
|
||||||
|
use tokio::sync::mpsc::Receiver;
|
||||||
|
|
||||||
#[derive(Serialize)]
|
#[derive(Serialize)]
|
||||||
struct HeartbeatInfo {
|
struct HeartbeatInfo {
|
||||||
@@ -18,10 +19,10 @@ struct HeartbeatInfo {
|
|||||||
desc: String,
|
desc: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn backend_heartbeat(config: std::sync::Arc<crate::config::Config>) {
|
pub async fn backend_heartbeat(config: std::sync::Arc<crate::config::Config>, mut hb_rx: Receiver<crate::server::ServerStatus>) {
|
||||||
let info = HeartbeatInfo {
|
let mut info = HeartbeatInfo {
|
||||||
uuid: config.general.auth_key.clone().unwrap_or(String::from("Unknown name!")),
|
uuid: config.general.auth_key.clone().unwrap_or(String::from("Unknown name!")),
|
||||||
players: 0, // TODO: Implement this. Easiest would probably be to have the server send updates every so often
|
players: 0,
|
||||||
maxplayers: config.general.max_players,
|
maxplayers: config.general.max_players,
|
||||||
port: config.general.port.unwrap_or(30814),
|
port: config.general.port.unwrap_or(30814),
|
||||||
map: config.general.map.clone(),
|
map: config.general.map.clone(),
|
||||||
@@ -32,7 +33,7 @@ pub async fn backend_heartbeat(config: std::sync::Arc<crate::config::Config>) {
|
|||||||
modlist: String::from("-"), // TODO: Implement this
|
modlist: String::from("-"), // TODO: Implement this
|
||||||
modstotalsize: 0, // TODO: Implement this
|
modstotalsize: 0, // TODO: Implement this
|
||||||
modstotal: 0, // TODO: Implement this
|
modstotal: 0, // TODO: Implement this
|
||||||
playerslist: String::from("luuk-bepis;"), // TODO: Implement this
|
playerslist: String::new(),
|
||||||
desc: config.general.description.clone(),
|
desc: config.general.description.clone(),
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -41,6 +42,12 @@ pub async fn backend_heartbeat(config: std::sync::Arc<crate::config::Config>) {
|
|||||||
interval.tick().await;
|
interval.tick().await;
|
||||||
|
|
||||||
heartbeat_post(&info).await;
|
heartbeat_post(&info).await;
|
||||||
|
|
||||||
|
if let Ok(status) = hb_rx.try_recv() {
|
||||||
|
trace!("status update: {:?}", status);
|
||||||
|
info.players = status.player_count;
|
||||||
|
info.playerslist = status.player_list.clone();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+13
-1
@@ -2,6 +2,8 @@
|
|||||||
#[macro_use] extern crate async_trait;
|
#[macro_use] extern crate async_trait;
|
||||||
#[macro_use] extern crate lazy_static;
|
#[macro_use] extern crate lazy_static;
|
||||||
|
|
||||||
|
use tokio::sync::mpsc;
|
||||||
|
|
||||||
mod server;
|
mod server;
|
||||||
mod config;
|
mod config;
|
||||||
mod heartbeat;
|
mod heartbeat;
|
||||||
@@ -40,16 +42,26 @@ async fn main() {
|
|||||||
|
|
||||||
let user_config = std::sync::Arc::new(user_config);
|
let user_config = std::sync::Arc::new(user_config);
|
||||||
|
|
||||||
tokio::spawn(heartbeat::backend_heartbeat(user_config.clone()));
|
let (hb_tx, hb_rx) = mpsc::channel(100);
|
||||||
|
|
||||||
|
tokio::spawn(heartbeat::backend_heartbeat(user_config.clone(), hb_rx));
|
||||||
|
|
||||||
let mut server = server::Server::new(user_config)
|
let mut server = server::Server::new(user_config)
|
||||||
.await
|
.await
|
||||||
.map_err(|e| error!("{:?}", e))
|
.map_err(|e| error!("{:?}", e))
|
||||||
.expect("Failed to start server!");
|
.expect("Failed to start server!");
|
||||||
|
|
||||||
|
let mut status = server.get_server_status();
|
||||||
loop {
|
loop {
|
||||||
if let Err(e) = server.process().await {
|
if let Err(e) = server.process().await {
|
||||||
error!("{:?}", e);
|
error!("{:?}", e);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let new_status = server.get_server_status();
|
||||||
|
|
||||||
|
if status != new_status {
|
||||||
|
status = new_status;
|
||||||
|
hb_tx.send(status.clone());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
+16
-13
@@ -71,19 +71,11 @@ fn load_plugins() -> Vec<Plugin> {
|
|||||||
plugins
|
plugins
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(PartialEq, IntoPrimitive, Copy, Clone, Debug)]
|
#[derive(PartialEq, Clone, Debug)]
|
||||||
#[repr(u8)]
|
pub struct ServerStatus {
|
||||||
enum ServerState {
|
pub player_count: usize,
|
||||||
Unknown = 0,
|
pub player_list: String,
|
||||||
|
pub max_players: usize,
|
||||||
WaitingForClients,
|
|
||||||
WaitingForReady,
|
|
||||||
WaitingForSpawns,
|
|
||||||
Qualifying,
|
|
||||||
LiningUp,
|
|
||||||
Countdown,
|
|
||||||
Race,
|
|
||||||
Finish,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct Server {
|
pub struct Server {
|
||||||
@@ -338,6 +330,17 @@ impl Server {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn get_server_status(&self) -> ServerStatus {
|
||||||
|
ServerStatus {
|
||||||
|
player_count: self.clients.len(),
|
||||||
|
player_list: self.clients.iter().map(|client| {
|
||||||
|
format!("{};", &client.get_name())
|
||||||
|
}).collect(),
|
||||||
|
// max_players: self.max_players, // TODO: Support this
|
||||||
|
max_players: self.config.general.max_players,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
async fn process_udp(&mut self) -> anyhow::Result<()> {
|
async fn process_udp(&mut self) -> anyhow::Result<()> {
|
||||||
// Process UDP packets
|
// Process UDP packets
|
||||||
// TODO: Use a UDP addr -> client ID look up table
|
// TODO: Use a UDP addr -> client ID look up table
|
||||||
|
|||||||
Reference in new issue
Block a user