From d00c8297e38499594cacf2a13301e2dc20d9dc05 Mon Sep 17 00:00:00 2001 From: Katalina Okano Date: Sat, 8 Jul 2023 01:29:21 -0400 Subject: [PATCH] refactor niumside source --- src/sources.rs | 85 +++++++++++++++----------------------------------- 1 file changed, 25 insertions(+), 60 deletions(-) diff --git a/src/sources.rs b/src/sources.rs index 4a71e62..b15a04b 100644 --- a/src/sources.rs +++ b/src/sources.rs @@ -287,68 +287,33 @@ pub async fn niumside(world: i32) -> Result { .await .unwrap(); - let vs: i32 = response - .pop - .iter() - .map(|pop| { - pop.zones - .iter() - .map(|zone| { - zone.factions - .iter() - .find(|faction| faction.faction_id == 1 || faction.faction_id == 4) - .unwrap() - .teams - .iter() - .find(|team| team.team_id == 1) - .unwrap() - .team_population - }) - .sum::() - }) - .sum::(); + fn extract(root: &Root, team_id: i32) -> i32 { + root.pop + .iter() + .map(|pop| { + pop.zones + .iter() + .map(|zone| { + let faction = match zone.factions.iter().find(|faction| { + faction.faction_id == team_id || faction.faction_id == 4 + }) { + Some(faction) => faction, + None => return 0, + }; - let nc: i32 = response - .pop - .iter() - .map(|pop| { - pop.zones - .iter() - .map(|zone| { - zone.factions - .iter() - .find(|faction| faction.faction_id == 2 || faction.faction_id == 4) - .unwrap() - .teams - .iter() - .find(|team| team.team_id == 2) - .unwrap() - .team_population - }) - .sum::() - }) - .sum::(); + match faction.teams.iter().find(|team| team.team_id == team_id) { + Some(team) => team.team_population, + None => 0, + } + }) + .sum::() + }) + .sum::() + } - let tr: i32 = response - .pop - .iter() - .map(|pop| { - pop.zones - .iter() - .map(|zone| { - zone.factions - .iter() - .find(|faction| faction.faction_id == 3 || faction.faction_id == 4) - .unwrap() - .teams - .iter() - .find(|team| team.team_id == 3) - .unwrap() - .team_population - }) - .sum::() - }) - .sum::(); + let vs = extract(&response, 1); + let nc = extract(&response, 2); + let tr = extract(&response, 3); Ok(Population { nc,