Skip to content

Commit

Permalink
feat: add get sectors and get factions
Browse files Browse the repository at this point in the history
  • Loading branch information
LordOfPolls committed Mar 17, 2024
1 parent b75840a commit 5d0cbd4
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 7 deletions.
22 changes: 21 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ mod error;
pub use models::api::{Status, WarInfo, PlanetStatus, PlanetAttack, Campaign, GlobalEvent, HomeWorld, Position, PlanetInfo, WarTime, NewsItem};
pub use models::{Planet, Faction, Sector, Language};
pub use requests::{get_status, get_war_info, get_war_time, get_news_feed};
pub use utils::{get_total_player_count, get_top_planets_by_player_count, get_faction_distribution};
pub use utils::{get_total_player_count, get_top_planets_by_player_count, get_faction_distribution, get_sectors, get_factions};

/// The base URL for the Helldivers API
pub const BASE_URL: &str = "https://api.live.prod.thehelldiversgame.com/api";
Expand Down Expand Up @@ -207,4 +207,24 @@ mod tests {
}
}
}

#[tokio::test]
async fn test_get_sectors() {
let war_info = match get_war_info(801).await {
Ok(war_info) => war_info,
Err(e) => panic!("Error: {}", e),
};
let sectors = get_sectors(&war_info);
assert!(!sectors.is_empty());
}

#[tokio::test]
async fn test_get_factions() {
let status = match get_status(801, Language::English).await {
Ok(status) => status,
Err(e) => panic!("Error: {}", e),
};
let factions = get_factions(&status);
assert!(!factions.is_empty());
}
}
39 changes: 33 additions & 6 deletions src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
use std::collections::HashMap;
use crate::{PlanetStatus, Status};
use crate::{PlanetStatus, Status, WarInfo, get_sector_name, Faction, get_faction_name};
use crate::models::Sector;

/// Get the total player count for a status
///
/// Arguments:
/// status: Status - The status to get the player count for
/// status: Status - The status to get the player count for
pub fn get_total_player_count(status: &Status) -> i64 {
status.planet_status.iter().map(|ps| ps.players).sum()
}

/// Get the top planets by player count
///
/// Arguments:
/// status: &Status - The status to get the top planets from
/// count: usize - The number of top planets to get
/// status: &Status - The status to get the top planets from
/// count: usize - The number of top planets to get
pub fn get_top_planets_by_player_count(status: &Status, count: usize) -> Vec<(&PlanetStatus, i64)> {
let mut planet_players: Vec<(&PlanetStatus, i64)> = status.planet_status.iter().map(|ps| (ps, ps.players)).collect();
planet_players.sort_by(|a, b| b.1.cmp(&a.1));
Expand All @@ -23,11 +24,37 @@ pub fn get_top_planets_by_player_count(status: &Status, count: usize) -> Vec<(&P
/// Get the faction distribution
///
/// Arguments:
/// status: &Status - The status to get the faction distribution from
/// status: &Status - The status to get the faction distribution from
pub fn get_faction_distribution(status: &Status) -> HashMap<i64, i64> {
let mut distribution = HashMap::new();
for ps in &status.planet_status {
*distribution.entry(ps.owner).or_insert(0) += 1;
}
distribution
}
}

/// Get the sectors from a WarInfo
///
/// Arguments:
/// status: &WarInfo - The WarInfo to get the sectors from
pub fn get_sectors(status: &WarInfo) -> Vec<Sector> {
// intentionally does not use the cached sectors in case there are new sectors in the WarInfo
let mut sectors = status.planet_infos.iter().map(|pi| pi.sector).collect::<Vec<_>>();
sectors.sort();
sectors.dedup();
sectors.into_iter().map(|s| Sector {
id: s, name: get_sector_name(s).unwrap_or_default(), planets: status.planet_infos.iter().filter(|pi| pi.sector == s).map(|pi| pi.index).collect()
}).collect()
}

/// Get the factions from a Status
///
/// Arguments:
/// status: &Status - The Status to get the factions from
pub fn get_factions(status: &Status) -> Vec<Faction> {
let mut factions = status.planet_status.iter().map(|ps| ps.owner).collect::<Vec<_>>();
factions.sort();
factions.dedup();
factions.into_iter().map(|f| Faction { id: f, name: get_faction_name(f).unwrap_or_default() }).collect()
}

0 comments on commit 5d0cbd4

Please sign in to comment.