From 5574b085b72c507ea917f0d38ca6dba961097d9e Mon Sep 17 00:00:00 2001 From: circlesabound Date: Wed, 24 Apr 2024 18:08:05 +1000 Subject: [PATCH] Fix extra "server started" messages, fix multiple callback on dashboard --- src/agent/util/saves.rs | 3 +- src/mgmt-server/clients.rs | 30 +++++++------- src/mgmt-server/discord.rs | 40 ++++++++++++++----- src/mgmt-server/main.rs | 2 +- src/schema.rs | 2 +- .../app/dashboard2/dashboard2.component.ts | 2 +- 6 files changed, 51 insertions(+), 28 deletions(-) diff --git a/src/agent/util/saves.rs b/src/agent/util/saves.rs index e518133..1d49468 100644 --- a/src/agent/util/saves.rs +++ b/src/agent/util/saves.rs @@ -5,8 +5,7 @@ use factorio_file_parser::SaveHeader; use fctrl::schema::{Save, SaveBytes}; use futures::AsyncReadExt; use log::{error, info, warn}; -use tokio::{fs::{self, OpenOptions}, io::{AsyncSeekExt, AsyncWriteExt, BufReader}}; -use tokio_util::compat::TokioAsyncReadCompatExt; +use tokio::{fs::{self, OpenOptions}, io::{AsyncSeekExt, AsyncWriteExt}}; use crate::{consts::*, error::{Error, Result}}; diff --git a/src/mgmt-server/clients.rs b/src/mgmt-server/clients.rs index 99bd85b..7cf63e0 100644 --- a/src/mgmt-server/clients.rs +++ b/src/mgmt-server/clients.rs @@ -740,20 +740,22 @@ fn tag_server_stdout_message(message: &str, tags: &mut HashMap true, + .subscribe(TopicName::new(SERVERSTATE_TOPIC_NAME), |states_str| { + if let Some((from, to)) = parse_serverstate_topic_value(states_str) { + // we only care about "InGame" and "Closed" + // special handling for "InGame" -> "InGameSavingMap" -> "InGame" sequence + match to { + InternalServerState::InGame => from != InternalServerState::InGameSavingMap, + InternalServerState::Closed => true, _ => false, } } else { @@ -261,9 +262,9 @@ impl DiscordClient { tokio::spawn(async move { pin_mut!(statechange_sub); while let Some(event) = statechange_sub.next().await { - let state = event.tags.get(&TopicName::new(SERVERSTATE_TOPIC_NAME)).unwrap(); - if let Ok(iss) = InternalServerState::from_str(state) { - let message = match iss { + let serverstate_val = event.tags.get(&TopicName::new(SERVERSTATE_TOPIC_NAME)).unwrap(); + if let Some((_from, to)) = parse_serverstate_topic_value(serverstate_val) { + let message = match to { InternalServerState::InGame => Some(format!("**Server started**")), InternalServerState::Closed => Some(format!("**Server stopped**")), _ => None, @@ -281,6 +282,27 @@ impl DiscordClient { ); }); } + + +} + +fn parse_serverstate_topic_value(states_str: impl AsRef) -> Option<(InternalServerState, InternalServerState)> { + if let Some((from, to)) = states_str.as_ref().split_once(' ') { + if let Ok(from) = InternalServerState::from_str(from) { + if let Ok(to) = InternalServerState::from_str(to) { + Some((from, to)) + } else { + error!("invalid serverstate topic value split to: {}", to); + None + } + } else { + error!("invalid serverstate topic value split from: {}", from); + None + } + } else { + error!("invalid serverstate topic value: {}", states_str.as_ref()); + None + } } struct Handler { diff --git a/src/mgmt-server/main.rs b/src/mgmt-server/main.rs index e285239..8a12245 100644 --- a/src/mgmt-server/main.rs +++ b/src/mgmt-server/main.rs @@ -7,7 +7,7 @@ use auth::{AuthnManager, AuthnProvider, AuthzManager}; use events::*; use futures::{pin_mut, StreamExt}; use log::{debug, error, info}; -use rocket::{async_trait, catchers, data::ToByteUnit, fairing::Fairing, fs::FileServer, routes}; +use rocket::{async_trait, catchers, fairing::Fairing, fs::FileServer, routes}; use crate::{ auth::UserIdentity, clients::AgentApiClient, db::{Cf, Db, Record}, discord::DiscordClient, events::broker::EventBroker, link_download::LinkDownloadManager, rpc::RpcHandler, ws::WebSocketServer diff --git a/src/schema.rs b/src/schema.rs index 890a967..4234247 100644 --- a/src/schema.rs +++ b/src/schema.rs @@ -364,7 +364,7 @@ pub enum AllowCommandsValue { } /// Internal state of the Factorio multiplayer server as tracked by output logs -#[derive(Clone, Debug, EnumString, AsRefStr)] +#[derive(Clone, Debug, PartialEq, EnumString, AsRefStr)] pub enum InternalServerState { Ready, PreparedToHostGame, diff --git a/web/src/app/dashboard2/dashboard2.component.ts b/web/src/app/dashboard2/dashboard2.component.ts index cb711fc..4f185d7 100644 --- a/web/src/app/dashboard2/dashboard2.component.ts +++ b/web/src/app/dashboard2/dashboard2.component.ts @@ -231,7 +231,7 @@ export class Dashboard2Component implements OnInit { // do them in order concat(...offsetsObservableArray).pipe( delay(3000), - ).subscribe(() => { + ).subscribe().add(() => { this.uploadSavefileButtonShowTickIcon = false this.internalUpdateSaves(); });