say command + playerlist works now

This commit is contained in:
Luuk van Oijen
2023-11-24 14:37:07 +01:00
parent 145b74aefc
commit b06d2fe151
4 changed files with 45 additions and 10 deletions

View File

@@ -38,13 +38,14 @@ pub struct GeneralSettings {
#[serde(rename = "Map")]
pub map: String,
#[serde(rename = "ResourceFolder")]
pub resource_folder: String,
// Options below are not yet supported
#[serde(rename = "LogChat")]
pub log_chat: bool,
#[serde(rename = "Debug")]
pub debug: bool,
#[serde(rename = "ResourceFolder")]
pub resource_folder: String,
}
impl GeneralSettings {

View File

@@ -68,15 +68,16 @@ async fn main() {
let user_config = Arc::new(user_config);
let (cmd_tx, cmd_rx) = mpsc::channel(100);
let (status_tx, status_rx) = mpsc::channel(100);
if !args.disable_tui {
tokio::spawn(tui::tui_main(user_config.clone(), cmd_tx));
tokio::spawn(tui::tui_main(user_config.clone(), cmd_tx, status_rx));
}
server_main(user_config, cmd_rx).await;
server_main(user_config, cmd_rx, status_tx).await;
}
async fn server_main(user_config: Arc<config::Config>, mut cmd_rx: mpsc::Receiver<Vec<String>>) {
async fn server_main(user_config: Arc<config::Config>, mut cmd_rx: mpsc::Receiver<Vec<String>>, status_tx: mpsc::Sender<server::ServerStatus>) {
let (hb_tx, hb_rx) = mpsc::channel(100);
tokio::spawn(heartbeat::backend_heartbeat(user_config.clone(), hb_rx));
@@ -88,6 +89,7 @@ async fn server_main(user_config: Arc<config::Config>, mut cmd_rx: mpsc::Receive
let mut status = server.get_server_status();
hb_tx.send(status.clone()).await;
status_tx.send(status.clone()).await;
'server: loop {
// TODO: Error handling
if server.clients.len() > 0 {
@@ -121,6 +123,7 @@ async fn server_main(user_config: Arc<config::Config>, mut cmd_rx: mpsc::Receive
if status != new_status {
status = new_status;
hb_tx.send(status.clone()).await;
status_tx.send(status.clone()).await;
}
// Process commands
@@ -141,6 +144,10 @@ async fn server_main(user_config: Arc<config::Config>, mut cmd_rx: mpsc::Receive
}
info!("{}", pl);
},
"say" => {
let msg = cmd[1..].iter().map(|s| s.as_str()).collect::<Vec<&str>>().join(" ");
server.send_chat_message(&msg, None).await;
},
_ => info!("Unknown command!"),
}
} else {

View File

@@ -73,7 +73,7 @@ fn load_plugins(server_resource_folder: String) -> Vec<Plugin> {
plugins
}
#[derive(PartialEq, Eq, Clone, Debug)]
#[derive(PartialEq, Eq, Clone, Debug, Default)]
pub struct ServerStatus {
pub player_count: usize,
pub player_list: Vec<(u8, String)>,
@@ -551,6 +551,18 @@ impl Server {
Ok(())
}
pub async fn send_chat_message(&self, message: &str, target: Option<u8>) {
let packet = Packet::Raw(RawPacket::from_str(&format!("C:Server: {message}")));
if let Some(_id) = target {
// TODO: Implement this!
todo!("Sending server chat messages to specific player is not supported yet!");
// info!("[CHAT] Server @ {id}: {message}");
} else {
info!("[CHAT] Server: {message}");
self.broadcast(packet, None).await;
}
}
// NOTE: Skips all clients that are currently connecting or syncing resources!
async fn broadcast(&self, packet: Packet, owner: Option<u8>) {
for client in &self.clients {

View File

@@ -16,12 +16,13 @@ use ratatui::{
use tokio::sync::mpsc;
use crate::config::Config;
use crate::server::ServerStatus;
pub async fn tui_main(config: Arc<Config>, cmd_tx: mpsc::Sender<Vec<String>>) {
pub async fn tui_main(config: Arc<Config>, cmd_tx: mpsc::Sender<Vec<String>>, mut info_rx: mpsc::Receiver<ServerStatus>) {
let mut tui = Tui::new().expect("Failed to initialize tui!");
'app: loop {
if tui.update(&cmd_tx).await.expect("Failed to run tui.update!") {
if tui.update(&cmd_tx, &mut info_rx).await.expect("Failed to run tui.update!") {
cmd_tx.send(vec!["exit".to_string()]).await.unwrap();
break 'app;
}
@@ -37,6 +38,8 @@ struct Tui {
history_scroll: usize,
input_history: Vec<String>,
input: String,
server_status: ServerStatus,
}
impl Drop for Tui {
@@ -62,6 +65,8 @@ impl Tui {
history_scroll: 0,
input_history: Vec::new(),
input: String::new(),
server_status: ServerStatus::default(),
})
}
@@ -71,7 +76,7 @@ impl Tui {
Ok(())
}
async fn update(&mut self, cmd_tx: &mpsc::Sender<Vec<String>>) -> Result<bool> {
async fn update(&mut self, cmd_tx: &mpsc::Sender<Vec<String>>, info_rx: &mut mpsc::Receiver<ServerStatus>) -> Result<bool> {
for (level, msg) in crate::logger::drain_log_buffer().await {
self.log_buffer.push_front((level, msg));
if self.log_buffer.len() > 100 {
@@ -79,6 +84,12 @@ impl Tui {
}
}
match info_rx.try_recv() {
Ok(status) => self.server_status = status,
Err(mpsc::error::TryRecvError::Empty) => {},
Err(e) => return Ok(true),
}
if event::poll(std::time::Duration::from_millis(16))? {
if let event::Event::Key(key) = event::read()? {
if key.kind == KeyEventKind::Press {
@@ -166,8 +177,12 @@ impl Tui {
horiz_layout[0],
);
let mut lines = Vec::new();
for (id, name) in &self.server_status.player_list {
lines.push(Line::from(format!("{id} - {name}")));
}
frame.render_widget(
Paragraph::new("0 - luuk-bepis\n1 - lion guy\n2 - the_racc").block(Block::new().borders(Borders::ALL).title("Player List")),
Paragraph::new(lines).block(Block::new().borders(Borders::ALL).title("Player List")),
horiz_layout[1],
);