Skip to content

Commit

Permalink
Fix extra "server started" messages, fix multiple callback on dashboard
Browse files Browse the repository at this point in the history
  • Loading branch information
circlesabound committed Apr 24, 2024
1 parent a57206f commit 5574b08
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 28 deletions.
3 changes: 1 addition & 2 deletions src/agent/util/saves.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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}};

Expand Down
30 changes: 16 additions & 14 deletions src/mgmt-server/clients.rs
Original file line number Diff line number Diff line change
Expand Up @@ -740,20 +740,22 @@ fn tag_server_stdout_message(message: &str, tags: &mut HashMap<TopicName, String
rpc_command
);
} else if let Some(state_change_captures) = STATE_CHANGE_RE.captures(message) {
if let Ok(to) = InternalServerState::from_str(state_change_captures.get(2).unwrap().as_str()) {
// bad cases already logged on agent side, can ignore
tags.insert(
TopicName::new(STDOUT_TOPIC_NAME),
StdoutTopicCategory::ServerState.to_string(),
);
tags.insert(
TopicName::new(SERVERSTATE_TOPIC_NAME),
to.as_ref().to_string(),
);
tags.insert(
TopicName::new(STDOUT_TOPIC_NAME),
StdoutTopicCategory::SystemLog.to_string(),
);
// bad cases already logged on agent side, can ignore
if let Ok(from) = InternalServerState::from_str(state_change_captures.get(1).unwrap().as_str()) {
if let Ok(to) = InternalServerState::from_str(state_change_captures.get(2).unwrap().as_str()) {
tags.insert(
TopicName::new(STDOUT_TOPIC_NAME),
StdoutTopicCategory::ServerState.to_string(),
);
tags.insert(
TopicName::new(SERVERSTATE_TOPIC_NAME),
format!("{} {}", from.as_ref(), to.as_ref()),
);
tags.insert(
TopicName::new(STDOUT_TOPIC_NAME),
StdoutTopicCategory::SystemLog.to_string(),
);
}
}
} else {
tags.insert(
Expand Down
40 changes: 31 additions & 9 deletions src/mgmt-server/discord.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,12 +245,13 @@ impl DiscordClient {
});

let statechange_sub = event_broker
.subscribe(TopicName::new(SERVERSTATE_TOPIC_NAME), |state| {
// we only care about "server started" and "server stopped"
if let Ok(state) = InternalServerState::from_str(state) {
match state {
InternalServerState::InGame
| InternalServerState::Closed => 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 {
Expand All @@ -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,
Expand All @@ -281,6 +282,27 @@ impl DiscordClient {
);
});
}


}

fn parse_serverstate_topic_value(states_str: impl AsRef<str>) -> 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 {
Expand Down
2 changes: 1 addition & 1 deletion src/mgmt-server/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion web/src/app/dashboard2/dashboard2.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
Expand Down

0 comments on commit 5574b08

Please sign in to comment.