Add federation territory ownership listener

This commit is contained in:
Sakuhl 2018-04-04 13:07:24 +02:00
parent 6284f18128
commit efdc559fc0
9 changed files with 147 additions and 4 deletions

2
Cargo.lock generated
View File

@ -1349,7 +1349,7 @@ dependencies = [
[[package]]
name = "wynnrobot"
version = "0.13.1"
version = "0.14.0"
dependencies = [
"chrono 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
"configure 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",

View File

@ -1,7 +1,7 @@
[package]
authors = ["Sakuhl <2012collector@gmail.com>"]
name = "wynnrobot"
version = "0.13.1"
version = "0.14.0"
[dependencies]
chrono = "0.4.0"

View File

@ -0,0 +1 @@
DROP TABLE federationfeed_listeners

View File

@ -0,0 +1,3 @@
CREATE TABLE federationfeed_listeners (
id BIGINT PRIMARY KEY
)

View File

@ -31,7 +31,7 @@ pub fn territory_livefeed() {
let mut timestamp = territories.as_object().unwrap().get("request").unwrap().as_object().unwrap().get("timestamp").unwrap().as_u64().unwrap();
loop {
thread::sleep_ms(20_000); // 10 seconds
thread::sleep_ms(20_000); // 20 seconds
let resp = reqwest::get("https://api.wynncraft.com/public_api.php?action=territoryList").unwrap();
let new_territories: Value = serde_json::from_reader(resp).unwrap();
@ -62,6 +62,84 @@ pub fn territory_livefeed() {
}
}
pub fn federation_livefeed() {
use models::FederationfeedListener;
use schema::federationfeed_listeners::dsl::*;
let ffa = vec!["Qira's Battle Room", "Hive", "Lava Lake", "Temple of Legends", "Bob's Tomb", "Battle Tower", "Detlas", "Cinfras", "Avos Temple",
"Factory Entrance", "Swamp Dark Forest Transition Mid", "Emerald Trail", "Rodoroc", "Molten Reach", "Molten Heights Portal", "Lava Lake Bridge",
"Active Volcano", "Volcanic Slope", "Crater Descent", "Wybel Island", "Herb Cave"];
let federation_allies = vec!["Angels Of Eternal",
"DogsAmongUs",
"DeathReapers",
"Fantasy",
"Hall of Fame",
"Holders Of LE",
"IceBlue Team",
"Immortalish",
"Imperial",
"Kasai Shinrai",
"Kingdom Foxes",
"KingdomPhoenixes",
"LE Flowers",
"Paladins United",
"Serpentem Empire",
"Sins of Seedia",
"The Hive",
"The Divine Swords",
"Titans Valor",
"Illustratus"];
let connection = establish_connection();
let resp = reqwest::get("https://api.wynncraft.com/public_api.php?action=territoryList").unwrap();
let mut territories: Value = serde_json::from_reader(resp).unwrap();
let mut timestamp = territories.as_object().unwrap().get("request").unwrap().as_object().unwrap().get("timestamp").unwrap().as_u64().unwrap();
loop {
thread::sleep_ms(10_000);
let resp = reqwest::get("https://api.wynncraft.com/public_api.php?action=territoryList").unwrap();
let new_territories: Value = serde_json::from_reader(resp).unwrap();
let new_timestamp = new_territories.as_object().unwrap().get("request").unwrap().as_object().unwrap().get("timestamp").unwrap().as_u64().unwrap();
if !(new_timestamp > timestamp) { // time went backwards for wynncraft?
continue;
}
timestamp = new_timestamp;
let mut assigned_owned_by_enemies = vec![];
let mut ffa_owned_by_enemies = vec![];
for key in territories.get("territories").unwrap().as_object().unwrap().keys() {
let new_value = new_territories.get("territories").unwrap().as_object().unwrap().get(key).unwrap();
let new_guild = new_value.get("guild").unwrap().as_str().unwrap();
if !federation_allies.contains(&new_guild) {
if !ffa.contains(&&**key) {
assigned_owned_by_enemies.push(key.to_owned());
} else {
ffa_owned_by_enemies.push(key.to_owned());
}
}
}
for listener in federationfeed_listeners
.load::<FederationfeedListener>(&connection)
.expect("Error loading listeners") {
let _ = ChannelId(listener.id as u64).say(format!("Assigned owned by others: {:?}
FFA owned by others: {:?}",
assigned_owned_by_enemies,
ffa_owned_by_enemies
));
}
territories = new_territories;
}
}
pub fn war_livefeed() {
use models::WarfeedListener;
use schema::warfeedlisteners::dsl::*;
@ -152,6 +230,43 @@ pub fn wc_unlivefeed(msg: &Message) -> Result<(), Error> {
Ok(())
}
pub fn wc_federationfeed(msg: &Message) -> Result<(), Error> {
use models::FederationfeedListener;
use schema::federationfeed_listeners;
let connection = establish_connection();
if let Err(error) = diesel::insert_into(federationfeed_listeners::table)
.values(&FederationfeedListener {
id: msg.channel_id.0 as i64
})
.execute(&connection) {
if let diesel::result::Error::DatabaseError(DatabaseErrorKind::UniqueViolation, _) = error {
bail!(failure::err_msg("already enabled"));
} else {
bail!(error);
}
}
msg.channel_id.say("Success!")?;
Ok(())
}
pub fn wc_unfederationfeed(msg: &Message) -> Result<(), Error> {
use schema::federationfeed_listeners::dsl::*;
let connection = establish_connection();
let channel_id = msg.channel_id.0 as i64;
diesel::delete(federationfeed_listeners.filter(id.eq(channel_id)))
.execute(&connection)?;
msg.channel_id.say("Success!")?;
Ok(())
}
pub fn wc_warfeed(msg: &Message) -> Result<(), Error> {
use models::WarfeedListener;
use schema::warfeedlisteners;

View File

@ -23,7 +23,7 @@ pub mod feed;
pub use player::*;
pub use guild::*;
pub use feed::{wc_livefeed, wc_unlivefeed, wc_warfeed, wc_unwarfeed};
pub use feed::{wc_livefeed, wc_unlivefeed, wc_warfeed, wc_unwarfeed, wc_federationfeed, wc_unfederationfeed};
pub fn wc_prefix(command: &str, msg: &Message) -> Result<(), Error> {
use models::PrefixConfig;

View File

@ -228,6 +228,10 @@ impl Handler {
wc_livefeed(msg)?;
} else if command.starts_with("unlivefeed") {
wc_unlivefeed(msg)?;
} else if command.starts_with("federationfeed") {
wc_federationfeed(msg)?;
} else if command.starts_with("unfederationfeed") {
wc_unfederationfeed(msg)?;
} else if command == "info" || command == "" {
wc_info(msg)?;
} else if command == "help" {
@ -313,5 +317,12 @@ fn start_daemons() {
}
})
.unwrap();
thread::Builder::new()
.spawn(|| {
loop {
println!("{:?}", panic::catch_unwind(|| commands::feed::federation_livefeed()));
}
})
.unwrap();
println!("Started daemons.");
}

View File

@ -6,6 +6,12 @@ pub struct LivefeedListener {
pub id: i64
}
#[derive(Queryable, Insertable)]
#[table_name="federationfeed_listeners"]
pub struct FederationfeedListener {
pub id: i64
}
#[derive(Queryable, Insertable)]
#[table_name="warfeedlisteners"]
pub struct WarfeedListener {

View File

@ -1,3 +1,9 @@
table! {
federationfeed_listeners (id) {
id -> Int8,
}
}
table! {
livefeedlisteners (id) {
id -> Int8,
@ -18,6 +24,7 @@ table! {
}
allow_tables_to_appear_in_same_query!(
federationfeed_listeners,
livefeedlisteners,
prefixes,
warfeedlisteners,