2024-01-30 07:17:59 -05:00
|
|
|
use crate::{
|
2024-02-01 02:00:57 -05:00
|
|
|
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;
|
2024-03-26 18:57:00 -04:00
|
|
|
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
|
|
|
|
};
|
2024-02-01 02:00:57 -05:00
|
|
|
|
2024-03-22 19:06:05 -04:00
|
|
|
async fn pms_serverstatus(url: &str) -> Result<Vec<(String, Vec<Value>)>, Error> {
|
2024-03-15 18:41:33 -04:00
|
|
|
let client = HttpClient::new();
|
2024-07-19 19:56:58 -04:00
|
|
|
let req = client.get(url, "PMS-Status").await?;
|
2024-03-15 18:41:33 -04:00
|
|
|
|
2023-12-30 23:57:27 -05:00
|
|
|
let response = req.json::<HashMap<String, Value>>().await?;
|
2024-03-22 19:41:29 -04:00
|
|
|
let data = response["data"].as_array().unwrap();
|
2024-03-22 19:06:05 -04:00
|
|
|
|
|
|
|
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()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-12-30 23:57:27 -05:00
|
|
|
|
|
|
|
Ok(servers)
|
|
|
|
}
|
|
|
|
|
2024-03-22 19:06:05 -04:00
|
|
|
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();
|
2024-03-22 19:06:05 -04:00
|
|
|
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"
|
|
|
|
};
|
2024-03-22 19:06:05 -04:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
2024-03-22 19:06:05 -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> {
|
2023-12-30 23:57:27 -05:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2023-12-08 06:25:44 -05:00
|
|
|
/// Retrieve the server statuses from Wargaming
|
2023-12-07 22:01:13 -05:00
|
|
|
#[poise::command(slash_command)]
|
2023-12-30 23:57:27 -05:00
|
|
|
pub async fn wg(ctx: poise::Context<'_, (), Error>) -> Result<(), Error> {
|
2024-03-26 18:57:00 -04:00
|
|
|
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
|
|
|
|
2023-12-08 06:25:44 -05:00
|
|
|
let (servers_asia, servers_eu) = join!(pms_serverstatus(&pms_asia), pms_serverstatus(&pms_eu));
|
2024-03-22 19:06:05 -04:00
|
|
|
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
|
|
|
|
2024-03-22 19:06:05 -04:00
|
|
|
ctx.send(CreateReply::default().embed(embed.title("Wargaming Server Status").fields(pms_servers))).await?;
|
2023-12-07 22:01:13 -05:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|