dns_wrapper/api/
nation_resources.rs

1use serde_json::Value;
2use std::collections::HashMap;
3use crate::api::ApiKey;
4
5#[derive(Debug)]
6pub struct NationResources {
7    pub nation: String,
8    pub money: f64,
9    pub tech: f64,
10    pub production: f64,
11    pub minerals: f64,
12    pub uranium: f64,
13    pub rare_metals: f64,
14    pub fuel: f64,
15    pub political_power: f64,
16}
17
18// Function to fetch nation resources from the API
19pub async fn fetch_nation_resources(nation_id: i32) -> Result<NationResources, Box<dyn std::error::Error>> {
20    let api_key = ApiKey::get_apikey().ok_or("API key not set")?;
21    let url = format!(
22        "http://diplomacyandstrifeapi.com/api/AllianceMemberFunds?APICode={}",
23        api_key
24    );
25
26    let response = reqwest::get(&url)
27        .await?
28        .json::<Vec<HashMap<String, Value>>>()
29        .await?;
30
31    if let Some(user_data) = response
32        .into_iter()
33        .find(|entry| entry["NationId"] == nation_id)
34    {
35        Ok(NationResources {
36            nation: user_data["NationName"].as_str().unwrap_or("").to_string(),
37            money: user_data["Cash"].as_f64().unwrap_or(0.0),
38            tech: user_data["Tech"].as_f64().unwrap_or(0.0),
39            production: user_data["Production"].as_f64().unwrap_or(0.0),
40            minerals: user_data["Minerals"].as_f64().unwrap_or(0.0),
41            uranium: user_data["Uranium"].as_f64().unwrap_or(0.0),
42            rare_metals: user_data["RareMetals"].as_f64().unwrap_or(0.0),
43            fuel: user_data["Fuel"].as_f64().unwrap_or(0.0),
44            political_power: user_data["PoliticalPower"].as_f64().unwrap_or(0.0),
45        })
46    } else {
47        Err("Nation not found".into())
48    }
49}