Kon/src/commands/status.rs

94 lines
2.9 KiB
Rust
Raw Normal View History

2024-01-30 07:17:59 -05:00
use crate::{
Error,
2024-07-19 19:56:58 -04:00
internals::{
config::BINARY_PROPERTIES,
http::HttpClient,
utils::token_path
}
2024-01-30 07:17:59 -05:00
};
2023-12-07 22:01:13 -05:00
2024-07-19 19:56:58 -04:00
use serde_json::Value;
use std::collections::HashMap;
2024-02-05 06:49:04 -05:00
use tokio::join;
2024-07-19 19:56:58 -04:00
use poise::{
CreateReply,
serenity_prelude::builder::CreateEmbed
};
async fn pms_serverstatus(url: &str) -> Result<Vec<(String, Vec<Value>)>, Error> {
let client = HttpClient::new();
2024-07-19 19:56:58 -04:00
let req = client.get(url, "PMS-Status").await?;
let response = req.json::<HashMap<String, Value>>().await?;
let data = response["data"].as_array().unwrap();
let mut servers = Vec::new();
for item in data {
if let Some(title) = item["title"].as_str() {
if let Some(servers_statuses) = item["servers_statuses"]["data"].as_array() {
if !servers_statuses.is_empty() {
servers.push((title.to_owned(), servers_statuses.clone()));
}
}
}
}
Ok(servers)
}
fn process_pms_statuses(servers: Vec<(String, Vec<Value>)>) -> Vec<(String, String, bool)> {
let mut server_map: HashMap<String, Vec<(String, String)>> = HashMap::new();
let id_name_map: HashMap<&str, &str> = [
("wotbsg", "ASIA"),
("wowssg", "WoWS (ASIA)"),
("wowseu", "WoWS (EU)")
].iter().cloned().collect();
for (title, mapped_servers) in servers {
for server in mapped_servers {
2024-03-20 17:55:48 -04:00
let name = server["name"].as_str().unwrap();
let id = server["id"].as_str().unwrap().split(":").next().unwrap_or("");
2024-03-20 17:55:48 -04:00
let status = match server["availability"].as_str().unwrap() {
"1" => "Online",
"-1" => "Offline",
_ => "Unknown"
};
let name = id_name_map.get(id).unwrap_or(&name);
server_map.entry(title.clone()).or_insert_with(Vec::new).push((name.to_owned().to_string(), status.to_owned()));
2024-03-20 17:55:48 -04:00
}
}
let mut statuses = Vec::new();
for (title, servers) in server_map {
let servers_str = servers.iter().map(|(name, status)| format!("{}: {}", name, status)).collect::<Vec<String>>().join("\n");
statuses.push((title, servers_str, true));
}
2024-03-20 17:55:48 -04:00
statuses
}
2024-02-05 06:49:04 -05:00
/// Query the server statuses
2024-07-19 19:56:58 -04:00
#[poise::command(
slash_command,
subcommands("wg"),
subcommand_required
)]
2024-02-05 06:49:04 -05:00
pub async fn status(_: poise::Context<'_, (), Error>) -> Result<(), Error> {
Ok(())
}
/// Retrieve the server statuses from Wargaming
2023-12-07 22:01:13 -05:00
#[poise::command(slash_command)]
pub async fn wg(ctx: poise::Context<'_, (), Error>) -> Result<(), Error> {
let pms_asia = token_path().await.wg_pms;
let pms_eu = pms_asia.replace("asia", "eu");
2024-07-19 19:56:58 -04:00
let embed = CreateEmbed::new().color(BINARY_PROPERTIES.embed_color);
2023-12-07 22:01:13 -05:00
let (servers_asia, servers_eu) = join!(pms_serverstatus(&pms_asia), pms_serverstatus(&pms_eu));
let joined_pms_servers = [servers_eu.unwrap(), servers_asia.unwrap()].concat();
let pms_servers = process_pms_statuses(joined_pms_servers.to_vec());
2023-12-07 22:01:13 -05:00
ctx.send(CreateReply::default().embed(embed.title("Wargaming Server Status").fields(pms_servers))).await?;
2023-12-07 22:01:13 -05:00
Ok(())
}