osrs-prometheus-exporter/src/collectors/stats.rs
2023-04-06 18:31:40 +02:00

123 lines
3.3 KiB
Rust

use eyre::eyre;
use std::fmt::{Display, Formatter};
#[derive(Debug)]
pub struct SkillInfo {
pub rank: String,
pub level: String,
pub xp: String,
pub name: String,
pub player: String,
pub profile: PlayerProfiles,
}
#[derive(Debug)]
pub enum PlayerProfiles {
Standard,
// Beta,
// QuestSpeedrunning,
// Deadman,
// PVPArena,
// TrailblazerLeague,
// DeadmanReborn,
// ShatteredRelicsLeague,
}
impl Display for PlayerProfiles {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
PlayerProfiles::Standard => write!(f, "Standard"),
// PlayerProfiles::Beta => write!(f, "Beta"),
// PlayerProfiles::QuestSpeedrunning => write!(f, "QuestSpeedrunning"),
// PlayerProfiles::Deadman => write!(f, "Deadman"),
// PlayerProfiles::PVPArena => write!(f, "PVPArena"),
// PlayerProfiles::TrailblazerLeague => write!(f, "TrailblazerLeague"),
// PlayerProfiles::DeadmanReborn => write!(f, "DeadmanReborn"),
// PlayerProfiles::ShatteredRelicsLeague => write!(f, "ShatteredRelicsLeague"),
}
}
}
const SKILLS: [&str; 25] = [
"Overall",
"Attack",
"Defence",
"Strength",
"Hitpoints",
"Ranged",
"Prayer",
"Magic",
"Cooking",
"Woodcutting",
"Fletching",
"Fishing",
"Firemaking",
"Crafting",
"Smithing",
"Mining",
"Herblore",
"Agility",
"Thieving",
"Slayer",
"Farming",
"Runecrafting",
"Hunter",
"Construction",
"Stuff",
];
pub async fn get_player_stats(rsn: &str) -> eyre::Result<Vec<SkillInfo>> {
let req_url = format!(
"https://oldschool.runescape.wiki/cors/m=hiscore_oldschool/index_lite.ws?player={}",
rsn
);
let resp = crate::transport::http::new().get(req_url).send().await?;
if resp.status() != 200 {
return Err(eyre!("Player not found"));
}
let raw = resp.text().await?;
let mut n = SKILLS.into_iter();
let skills: Vec<SkillInfo> = raw
.split("\n")
.into_iter()
.map(|x| x.split(',').collect::<Vec<&str>>())
.filter(|x| x.len() == 3)
.map(|x| {
let name = n.next().unwrap();
SkillInfo {
rank: x[0].to_string(),
level: x[1].to_string(),
xp: x[2].to_string(),
name: name.to_string(),
profile: PlayerProfiles::Standard,
player: rsn.to_string(),
}
})
.collect();
Ok(skills)
}
impl super::PromMetric for SkillInfo {
fn to_metric_string(self: &Self) -> String {
let lines: Vec<String> = vec![
format!(
"osrs_player_rank{{skill=\"{}\",player=\"{}\",profile=\"{}\"}} {}",
self.name, self.player, self.profile, self.rank
),
format!(
"osrs_player_level{{skill=\"{}\",player=\"{}\",profile=\"{}\"}} {}",
self.name, self.player, self.profile, self.level
),
format!(
"osrs_player_xp{{skill=\"{}\",player=\"{}\",profile=\"{}\"}} {}",
self.name, self.player, self.profile, self.xp
),
];
lines.join("\n")
}
}