Fix territory feed sometimes showing an update twice

This commit is contained in:
Sakuhl 2018-03-19 19:02:51 +01:00
parent 19bd9f7328
commit a0141a3ee2
3 changed files with 10 additions and 2 deletions

2
Cargo.lock generated
View File

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

View File

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

View File

@ -28,12 +28,20 @@ pub fn territory_livefeed() {
let mut territories: Value = serde_json::from_reader(resp).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 { loop {
thread::sleep_ms(10_000); // 10 seconds thread::sleep_ms(10_000); // 10 seconds
let resp = reqwest::get("https://api.wynncraft.com/public_api.php?action=territoryList").unwrap(); 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_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;
for key in territories.get("territories").unwrap().as_object().unwrap().keys() { for key in territories.get("territories").unwrap().as_object().unwrap().keys() {
let value = territories.get("territories").unwrap().as_object().unwrap().get(key).unwrap(); let value = territories.get("territories").unwrap().as_object().unwrap().get(key).unwrap();
let new_value = new_territories.get("territories").unwrap().as_object().unwrap().get(key).unwrap(); let new_value = new_territories.get("territories").unwrap().as_object().unwrap().get(key).unwrap();