Skip to content

Commit

Permalink
TODO: ManagedFile player.list
Browse files Browse the repository at this point in the history
  • Loading branch information
mbround18 committed Oct 11, 2024
1 parent d5f15ad commit 5716635
Show file tree
Hide file tree
Showing 13 changed files with 176 additions and 35 deletions.
72 changes: 72 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/odin/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ regex = "1.10.4"
tokio = { version = "1", features = ["full"] }
notify = "6.1.1"
json-patch = "*"
cached = "0"

[dev-dependencies]
once_cell = "1.19.0"
Expand Down
18 changes: 16 additions & 2 deletions src/odin/commands/logs.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use crate::notifications::enums::notification_event::NotificationEvent;
use crate::notifications::enums::player::PlayerStatus::{Joined, Left};
use crate::utils::common_paths::log_directory;
use log::{error, info, warn};
use crate::utils::environment::is_env_var_truthy;
use std::collections::HashMap;
use std::ffi::OsStr;
use std::fs::{read_to_string, File};
Expand All @@ -25,6 +27,18 @@ fn handle_line(path: PathBuf, line: String) {
return;
}

if is_env_var_truthy("PLAYER_EVENT_NOTIFICATIONS") {
if line.contains("I HAVE ARRIVED!") {


NotificationEvent::Player(Joined).send_notification(None);
}

if line.contains("Player disconnected") {
NotificationEvent::Player(Left).send_notification(None);
}
}

let file_name = Path::new(&path).file_name().unwrap().to_str().unwrap();

if line.contains("WARNING") {
Expand All @@ -37,12 +51,12 @@ fn handle_line(path: PathBuf, line: String) {

if line.contains("Game server connected") {
NotificationEvent::Start(crate::notifications::enums::event_status::EventStatus::Successful)
.send_notification();
.send_notification(None);
}

if line.contains("Steam manager on destroy") {
NotificationEvent::Stop(crate::notifications::enums::event_status::EventStatus::Successful)
.send_notification();
.send_notification(None);
info!("The game server has been stopped");
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/odin/commands/start.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use std::process::exit;

pub fn invoke(dry_run: bool) {
info!(target: "commands_start", "Setting up start scripts...");
NotificationEvent::Start(EventStatus::Running).send_notification();
NotificationEvent::Start(EventStatus::Running).send_notification(None);
debug!(target: "commands_start", "Loading config file...");
let config = load_config();
debug!(target: "commands_start", "Dry run condition: {}", dry_run);
Expand Down
4 changes: 2 additions & 2 deletions src/odin/commands/stop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::notifications::enums::notification_event::NotificationEvent;
use crate::{constants, server, utils::get_working_dir};

pub fn invoke(dry_run: bool) {
NotificationEvent::Stop(EventStatus::Running).send_notification();
NotificationEvent::Stop(EventStatus::Running).send_notification(None);
debug!("Stopping server, directory needs to be where the server executable is located.");
info!(
"Stopping server, using working directory {}",
Expand All @@ -23,5 +23,5 @@ pub fn invoke(dry_run: bool) {
}
server::blocking_shutdown();
}
NotificationEvent::Stop(EventStatus::Successful).send_notification();
NotificationEvent::Stop(EventStatus::Successful).send_notification(None);
}
30 changes: 19 additions & 11 deletions src/odin/files/discord.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
files::{FileManager, ManagedFile},
notifications::discord::{DiscordWebHookBody},
notifications::discord::DiscordWebHookBody,
utils::{environment::fetch_var, path_exists},
};

Expand Down Expand Up @@ -80,7 +80,7 @@ pub fn read_discord(discord: &dyn FileManager) -> DiscordConfig {
pub fn write_discord(discord: &dyn FileManager) -> bool {
if path_exists(&discord.path()) {
debug!("Discord config file already exists, doing nothing.");
return true;
return true;
}

let template_notification = basic_template();
Expand Down Expand Up @@ -205,15 +205,16 @@ mod tests {
fn test_read_discord_without_events_key() {
let mut mock_file = MockManagedFile::new();
let no_events_content = r#"{}"#;
mock_file.expect_read().return_const(String::from(no_events_content));
mock_file
.expect_read()
.return_const(String::from(no_events_content));

let config = read_discord(&mock_file);

assert!(config.events.contains_key("broadcast"));
assert!(config.events.contains_key("start"));
}


#[test]
fn test_read_discord_with_extra_event_keys() {
let mut mock_file = MockManagedFile::new();
Expand All @@ -231,29 +232,36 @@ mod tests {
}
}
}"#;
mock_file.expect_read().return_const(String::from(extra_keys_content));
mock_file
.expect_read()
.return_const(String::from(extra_keys_content));

let config = read_discord(&mock_file);

assert!(!config.events.contains_key("extra_event"), "Unexpected key should be ignored");
assert!(
!config.events.contains_key("extra_event"),
"Unexpected key should be ignored"
);
assert!(config.events.contains_key("broadcast"));
assert!(config.events.contains_key("start"));
}



#[test]
fn test_read_discord_with_malformed_json() {
let mut mock_file = MockManagedFile::new();
let malformed_json = r#"{ "events": { "broadcast": { "content": "test_broadcast""#;
mock_file.expect_read().return_const(String::from(malformed_json));
mock_file
.expect_read()
.return_const(String::from(malformed_json));

let result = std::panic::catch_unwind(|| read_discord(&mock_file));

assert!(result.is_err(), "Expected a panic when the JSON is malformed");
assert!(
result.is_err(),
"Expected a panic when the JSON is malformed"
);
}


#[test]
fn test_discord_file() {
let managed_file = discord_file();
Expand Down
4 changes: 4 additions & 0 deletions src/odin/log_filters/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
use regex::Regex;

Check failure on line 1 in src/odin/log_filters/mod.rs

View workflow job for this annotation

GitHub Actions / Release Train WooohWoooohh

unused import: `regex::Regex`

Check failure on line 1 in src/odin/log_filters/mod.rs

View workflow job for this annotation

GitHub Actions / Build Rust / Build & Test

unused import: `regex::Regex`

mod player;

30 changes: 30 additions & 0 deletions src/odin/log_filters/player.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
use regex::Regex;
use crate::notifications::enums::notification_event::NotificationEvent;
use crate::notifications::enums::player::PlayerStatus::Joined;

struct Player {

}

struct PlayerList {
players: Vec<>

Check failure on line 10 in src/odin/log_filters/player.rs

View workflow job for this annotation

GitHub Actions / Release Train WooohWoooohh

struct takes at least 1 generic argument but 0 generic arguments were supplied

Check failure on line 10 in src/odin/log_filters/player.rs

View workflow job for this annotation

GitHub Actions / Build Rust / Build & Test

struct takes at least 1 generic argument but 0 generic arguments were supplied
}



pub fn player_joined(message: &str) {
let re = Regex::new(r"<color=orange>(.*?)</color>").unwrap();
if let Some(captures) = re.captures(message) {
let name = captures.get(1).map_or("", |m| m.as_str());
NotificationEvent::Player(Joined).send_notification(Some(format!("Player {name} has joined the server!")));
}
}

pub fn player_left(message: &str) {
let re = Regex::new(r"<color=orange>(.*?)</color>").unwrap();

if let Some(captures) = re.captures(message) {
let name = captures.get(1).map_or("", |m| m.as_str());
NotificationEvent::Player(Left).send_notification(Some(format!("Player {name} has left the server!")));

Check failure on line 28 in src/odin/log_filters/player.rs

View workflow job for this annotation

GitHub Actions / Release Train WooohWoooohh

cannot find value `Left` in this scope

Check failure on line 28 in src/odin/log_filters/player.rs

View workflow job for this annotation

GitHub Actions / Build Rust / Build & Test

cannot find value `Left` in this scope
}
}
1 change: 1 addition & 0 deletions src/odin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ pub mod server;
mod steamcmd;
pub mod traits;
pub mod utils;
mod log_filters;

#[tokio::main]
async fn main() {
Expand Down
Loading

0 comments on commit 5716635

Please sign in to comment.