Refactor
This commit is contained in:
parent
56bf12b4de
commit
37c369cfcb
324
src/main.rs
324
src/main.rs
@ -1,130 +1,17 @@
|
||||
extern crate serenity;
|
||||
extern crate wynncraft;
|
||||
use wynncraft::{Guild, Player};
|
||||
|
||||
extern crate serenity;
|
||||
use serenity::prelude::*;
|
||||
use serenity::model::*;
|
||||
|
||||
extern crate reqwest;
|
||||
|
||||
extern crate serde;
|
||||
#[macro_use]
|
||||
extern crate serde_derive;
|
||||
extern crate serde_json;
|
||||
use serde_json::Value;
|
||||
|
||||
use std::collections::HashMap;
|
||||
use std::env;
|
||||
|
||||
#[derive(Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct Guild {
|
||||
pub name: String,
|
||||
pub prefix: String,
|
||||
pub members: Vec<Member>,
|
||||
pub xp: f64,
|
||||
pub level: u64,
|
||||
pub created: String,
|
||||
pub created_friendly: String,
|
||||
pub territories: u64
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct Member {
|
||||
pub name: String,
|
||||
pub rank: Rank,
|
||||
pub contributed: u64,
|
||||
pub joined: String,
|
||||
pub joined_friendly: String,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
|
||||
pub enum Rank {
|
||||
Owner,
|
||||
Chief,
|
||||
Captain,
|
||||
Recruiter,
|
||||
Recruit
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
#[allow(non_snake_case)]
|
||||
pub struct Player {
|
||||
pub username: String,
|
||||
pub rank: String,
|
||||
pub tag: String,
|
||||
pub displayTag: bool,
|
||||
pub veteran: bool,
|
||||
pub playtime: u64,
|
||||
pub first_join: String,
|
||||
pub last_join: String,
|
||||
pub first_join_friendly: String,
|
||||
pub last_join_friendly: String,
|
||||
pub current_server: String,
|
||||
pub global: GlobalPlayerInfo,
|
||||
pub classes: HashMap<String, Class>,
|
||||
pub wizard_fortress: WFPlayerInfo,
|
||||
pub rankings: PlayerRankings,
|
||||
pub guild: PlayerGuildInfo
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct GlobalPlayerInfo {
|
||||
pub items_identified: u64,
|
||||
pub mobs_killed: u64,
|
||||
pub pvp_kills: u64,
|
||||
pub pvp_deaths: u64,
|
||||
pub chests_found: u64,
|
||||
pub blocks_walked: u64,
|
||||
pub logins: u64,
|
||||
pub deaths: u64,
|
||||
pub total_level: u64
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
#[allow(non_snake_case)]
|
||||
pub struct Class {
|
||||
pub level: u64,
|
||||
pub xp: f64,
|
||||
pub dungeons: Value,
|
||||
pub quests: Vec<String>,
|
||||
pub dungeonsAmount: String,
|
||||
pub questsAmount: String,
|
||||
pub skills: Value,
|
||||
pub items_identified: u64,
|
||||
pub mobs_killed: u64,
|
||||
pub pvp_kills: u64,
|
||||
pub pvp_deaths: u64,
|
||||
pub chests_found: u64,
|
||||
pub blocks_walked: u64,
|
||||
pub logins: u64,
|
||||
pub deaths: u64,
|
||||
pub events_won: u64,
|
||||
/// in minutes
|
||||
pub playtime: u64
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct WFPlayerInfo {
|
||||
pub kills: u64,
|
||||
pub deaths: u64,
|
||||
pub wins: u64,
|
||||
pub losses: u64
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct PlayerRankings {
|
||||
pub pvp: u64,
|
||||
pub player: u64,
|
||||
pub guild: u64
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct PlayerGuildInfo {
|
||||
pub name: String,
|
||||
pub rank: String
|
||||
}
|
||||
|
||||
struct Handler;
|
||||
|
||||
impl EventHandler for Handler {
|
||||
@ -135,102 +22,13 @@ impl EventHandler for Handler {
|
||||
// of a single event can be dispatched simultaneously.
|
||||
fn on_message(&self, _: Context, msg: Message) {
|
||||
if msg.content.starts_with("wc!guild ") {
|
||||
let guild = &msg.content[9..];
|
||||
let resp = reqwest::get(&format!("https://api.wynncraft.com/public_api.php?action=guildStats&command={}", guild)).unwrap();
|
||||
assert!(resp.status().is_success());
|
||||
|
||||
if let Ok(guild) = serde_json::from_reader(resp) {
|
||||
let guild: Guild = guild;
|
||||
|
||||
let resp = reqwest::get("https://api.wynncraft.com/public_api.php?action=territoryList").unwrap();
|
||||
assert!(resp.status().is_success());
|
||||
|
||||
let territories: Value = serde_json::from_reader(resp).unwrap();
|
||||
|
||||
let mut message = String::new();
|
||||
for value in territories.get("territories").unwrap().as_object().unwrap().values() {
|
||||
if value.get("guild").unwrap().as_str().unwrap() == guild.name {
|
||||
message.push_str(&format!("
|
||||
**Territory**: {}",
|
||||
value.get("territory").unwrap().as_str().unwrap()
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
msg.channel_id.say(
|
||||
format!(
|
||||
"**Guild**: {}
|
||||
**prefix**: {}
|
||||
**Created**: {}
|
||||
**Level**: {}
|
||||
**Members**: {}",
|
||||
guild.name,
|
||||
guild.prefix,
|
||||
guild.created_friendly,
|
||||
guild.level,
|
||||
guild.members.len()
|
||||
) + &message
|
||||
).unwrap();
|
||||
}
|
||||
wc_guild(msg);
|
||||
} else if msg.content.starts_with("wc!territory ") {
|
||||
let territory = &msg.content[13..];
|
||||
|
||||
let resp = reqwest::get("https://api.wynncraft.com/public_api.php?action=territoryList").unwrap();
|
||||
assert!(resp.status().is_success());
|
||||
|
||||
let territories: Value = serde_json::from_reader(resp).unwrap();
|
||||
|
||||
let mut message = String::new();
|
||||
for value in territories.get("territories").unwrap().as_object().unwrap().values() {
|
||||
if value.get("territory").unwrap().as_str().unwrap().contains(territory) {
|
||||
message.push_str(&format!("
|
||||
**Territory**: {}
|
||||
**Owner**: {}
|
||||
**Acquired at**: {}
|
||||
",
|
||||
value.get("territory").unwrap().as_str().unwrap(),
|
||||
value.get("guild").unwrap().as_str().unwrap(),
|
||||
value.get("acquired").unwrap().as_str().unwrap()
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
if !message.is_empty() {
|
||||
msg.channel_id.say(
|
||||
message
|
||||
).unwrap();
|
||||
}
|
||||
wc_territory(msg);
|
||||
} else if msg.content.starts_with("wc!status") {
|
||||
let resp = reqwest::get("https://api.wynncraft.com/public_api.php?action=onlinePlayersSum").unwrap();
|
||||
|
||||
let value: Value = serde_json::from_reader(resp).unwrap();
|
||||
|
||||
msg.channel_id.say(
|
||||
format!("{} players online", value.get("players_online").unwrap().as_u64().unwrap())
|
||||
).unwrap();
|
||||
wc_status(msg);
|
||||
} else if msg.content.starts_with("wc!player ") {
|
||||
let player = &msg.content[10..];
|
||||
|
||||
let resp = reqwest::get(&format!("https://api.wynncraft.com/public_api.php?action=playerStats&command={}", player)).unwrap();
|
||||
|
||||
let player: Player = serde_json::from_reader(resp).unwrap();
|
||||
|
||||
msg.channel_id.say(
|
||||
format!(
|
||||
"**Player name**: {}
|
||||
**Guild**: {}
|
||||
**Playtime**: {} hours
|
||||
**Kills**: {} mobs & {} players
|
||||
**Chests looted**: {}
|
||||
**Total level**: {}",
|
||||
player.username,
|
||||
player.guild.name,
|
||||
player.playtime / 60,
|
||||
player.global.mobs_killed, player.global.pvp_kills,
|
||||
player.global.chests_found,
|
||||
player.classes.iter().map(|(_, x)| x.level).sum::<u64>()
|
||||
)
|
||||
).unwrap();
|
||||
wc_player(msg);
|
||||
}
|
||||
}
|
||||
|
||||
@ -245,6 +43,114 @@ impl EventHandler for Handler {
|
||||
}
|
||||
}
|
||||
|
||||
fn wc_guild(msg: Message) {
|
||||
let guild = &msg.content[9..];
|
||||
|
||||
if let Ok(guild) = wynncraft::guild(guild) {
|
||||
let guild: Guild = guild;
|
||||
|
||||
let resp = reqwest::get("https://api.wynncraft.com/public_api.php?action=territoryList").unwrap();
|
||||
assert!(resp.status().is_success());
|
||||
|
||||
let territories: Value = serde_json::from_reader(resp).unwrap();
|
||||
|
||||
let mut message = String::new();
|
||||
for value in territories.get("territories").unwrap().as_object().unwrap().values() {
|
||||
if value.get("guild").unwrap().as_str().unwrap() == guild.name {
|
||||
message.push_str(&format!("
|
||||
**Territory**: {}",
|
||||
value.get("territory").unwrap().as_str().unwrap()
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
msg.channel_id.say(
|
||||
format!(
|
||||
"**Guild**: {}
|
||||
**prefix**: {}
|
||||
**Created**: {}
|
||||
**Level**: {}
|
||||
**Members**: {}",
|
||||
guild.name,
|
||||
guild.prefix,
|
||||
guild.created_friendly,
|
||||
guild.level,
|
||||
guild.members.len()
|
||||
) + &message
|
||||
).unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
fn wc_status(msg: Message) {
|
||||
let resp = reqwest::get("https://api.wynncraft.com/public_api.php?action=onlinePlayersSum").unwrap();
|
||||
|
||||
let value: Value = serde_json::from_reader(resp).unwrap();
|
||||
|
||||
msg.channel_id.say(
|
||||
format!("{} players online", value.get("players_online").unwrap().as_u64().unwrap())
|
||||
).unwrap();
|
||||
}
|
||||
|
||||
fn wc_territory(msg: Message) {
|
||||
let territory = &msg.content[13..];
|
||||
|
||||
let resp = reqwest::get("https://api.wynncraft.com/public_api.php?action=territoryList").unwrap();
|
||||
assert!(resp.status().is_success());
|
||||
|
||||
let territories: Value = serde_json::from_reader(resp).unwrap();
|
||||
|
||||
let mut message = String::new();
|
||||
for value in territories.get("territories").unwrap().as_object().unwrap().values() {
|
||||
if value.get("territory").unwrap().as_str().unwrap().contains(territory) {
|
||||
message.push_str(&format!("
|
||||
**Territory**: {}
|
||||
**Owner**: {}
|
||||
**Acquired at**: {}
|
||||
",
|
||||
value.get("territory").unwrap().as_str().unwrap(),
|
||||
value.get("guild").unwrap().as_str().unwrap(),
|
||||
value.get("acquired").unwrap().as_str().unwrap()
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
if !message.is_empty() {
|
||||
msg.channel_id.say(
|
||||
message
|
||||
).unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
fn wc_player(msg: Message) {
|
||||
let player = &msg.content[10..];
|
||||
|
||||
let player: Player = wynncraft::player(player).unwrap();
|
||||
|
||||
let current_server_msg = if player.current_server != "null" {
|
||||
format!("\n**Currently online on**: {}", player.current_server)
|
||||
} else {
|
||||
String::new()
|
||||
};
|
||||
|
||||
msg.channel_id.say(
|
||||
format!(
|
||||
"**Player name**: {}
|
||||
**Guild**: {}
|
||||
**Playtime**: {} hours
|
||||
**Kills**: {} mobs & {} players
|
||||
**Chests looted**: {}
|
||||
**Total level**: {}{}",
|
||||
player.username,
|
||||
player.guild.name,
|
||||
player.playtime / 60,
|
||||
player.global.mobs_killed, player.global.pvp_kills,
|
||||
player.global.chests_found,
|
||||
player.classes.iter().map(|(_, x)| x.level).sum::<u64>(),
|
||||
current_server_msg
|
||||
)
|
||||
).unwrap();
|
||||
}
|
||||
|
||||
fn main() {
|
||||
// Configure the client with your Discord bot token in the environment.
|
||||
let token = env::var("DISCORD_TOKEN").expect("DISCORD_TOKEN must be set");
|
||||
|
Loading…
Reference in New Issue
Block a user