add locked_since

This commit is contained in:
41666 2023-06-09 15:30:46 -04:00
parent 019bca8000
commit 4d4ab3cde7
4 changed files with 22 additions and 12 deletions

View file

@ -8,9 +8,9 @@ use serde::Deserialize;
use serde_aux::prelude::*;
use std::collections::HashMap;
pub async fn get_alerts(world_id: i32) -> Result<Vec<Alert>, ()> {
pub async fn get_alerts(world_id: i32) -> Result<(Vec<Alert>, Vec<Alert>), ()> {
let response = reqwest::get(format!(
"https://census.daybreakgames.com/s:{}/get/{}/world_event/?world_id={}&type=METAGAME&c:limit=10",
"https://census.daybreakgames.com/s:{}/get/{}/world_event/?world_id={}&type=METAGAME&c:limit=15",
misc::service_id(),
misc::platform(world_id),
world_id
@ -50,15 +50,14 @@ pub async fn get_alerts(world_id: i32) -> Result<Vec<Alert>, ()> {
}
}
let mut active_alerts: Vec<Alert> = vec![];
let alerts: Vec<Alert> = alerts.into_iter().map(|(_, v)| v).collect();
let active_alerts: Vec<Alert> = alerts
.clone()
.into_iter()
.filter(|alert| alert.end_time.is_none())
.collect();
for (_, alert) in alerts {
if alert.end_time.is_none() {
active_alerts.push(alert);
}
}
Ok(active_alerts)
Ok((active_alerts, alerts))
}
#[derive(Deserialize)]

View file

@ -115,17 +115,26 @@ pub async fn get_world(
}
}
let alerts = get_alerts(world).await.unwrap();
let (active_alerts, most_recent_alerts) = get_alerts(world).await.unwrap();
let zones = get_zone_states(world).await.unwrap();
let converged_zones: Vec<Zone> = zones
.into_iter()
.map(|zone| {
let mut zone = zone;
let alert = alerts.iter().find(|alert| alert.zone == zone.id);
let alert = active_alerts.iter().find(|alert| alert.zone == zone.id);
zone.alert = alert.cloned();
if zone.locked {
zone.locked_since = most_recent_alerts
.iter()
.find(|alert| alert.zone == zone.id)
.cloned()
.unwrap()
.end_time
}
zone
})
.collect();

View file

@ -14,6 +14,7 @@ pub struct Zone {
pub locked: bool,
pub alert: Option<Alert>,
pub territory: FactionPercents,
pub locked_since: Option<DateTime<Utc>>,
}
#[derive(Deserialize, Serialize, Clone, Default, Debug)]

View file

@ -50,6 +50,7 @@ pub async fn get_zone_states(world_id: i32) -> Result<Vec<Zone>, ()> {
&& warpgate_factions[1] == warpgate_factions[2],
territory: calculate_faction_percents(&map_zone.regions.row),
alert: None,
locked_since: None,
};
zones.push(zone);