parent
45a2cba2b8
commit
72c9d913df
@ -0,0 +1,42 @@
|
|||||||
|
use crate::data::{get_version_specific_file, BIOMES_FILE};
|
||||||
|
use crate::models::biome::Biome;
|
||||||
|
use crate::models::version::Version;
|
||||||
|
use crate::DataResult;
|
||||||
|
use itertools::Itertools;
|
||||||
|
use std::collections::HashMap;
|
||||||
|
use std::iter::FromIterator;
|
||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
|
pub struct Biomes {
|
||||||
|
version: Arc<Version>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Biomes {
|
||||||
|
pub fn new(version: Arc<Version>) -> Self {
|
||||||
|
Self { version }
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns all biomes as an unordered list
|
||||||
|
pub fn biomes_array(&self) -> DataResult<Vec<Biome>> {
|
||||||
|
let content = get_version_specific_file(&self.version, BIOMES_FILE)?;
|
||||||
|
let biomes = serde_json::from_str(&content)?;
|
||||||
|
|
||||||
|
Ok(biomes)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns the biomes indexed by id
|
||||||
|
pub fn biomes(&self) -> DataResult<HashMap<u32, Biome>> {
|
||||||
|
let biomes = self.biomes_array()?;
|
||||||
|
let biomes_map = HashMap::from_iter(biomes.into_iter().map(|b| (b.id, b)));
|
||||||
|
|
||||||
|
Ok(biomes_map)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns the biomes indexed by name
|
||||||
|
pub fn biomes_by_name(&self) -> DataResult<HashMap<String, Biome>> {
|
||||||
|
let biomes = self.biomes_array()?;
|
||||||
|
let biomes_map = HashMap::from_iter(biomes.into_iter().map(|b| (b.name.clone(), b)));
|
||||||
|
|
||||||
|
Ok(biomes_map)
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,39 @@
|
|||||||
|
use crate::api::tests::{get_api, get_test_versions};
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
pub fn test_biomes_array() {
|
||||||
|
let versions = get_test_versions();
|
||||||
|
|
||||||
|
for version in versions {
|
||||||
|
let api = get_api(version);
|
||||||
|
assert_ne!(api.biomes.biomes_array().unwrap().len(), 0)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
pub fn test_biomes_by_name() {
|
||||||
|
let versions = get_test_versions();
|
||||||
|
|
||||||
|
for version in versions {
|
||||||
|
let api = get_api(version);
|
||||||
|
let by_name = api.biomes.biomes_by_name().unwrap();
|
||||||
|
assert!(by_name.get("ocean").is_some());
|
||||||
|
assert!(by_name.get("river").is_some());
|
||||||
|
assert_eq!(
|
||||||
|
by_name.get("jungle").unwrap().dimension,
|
||||||
|
"overworld".to_string()
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
pub fn test_biomes_by_id() {
|
||||||
|
let versions = get_test_versions();
|
||||||
|
|
||||||
|
for version in versions {
|
||||||
|
let api = get_api(version);
|
||||||
|
let by_name = api.biomes.biomes().unwrap();
|
||||||
|
assert!(by_name.get(&1).is_some());
|
||||||
|
assert!(by_name.get(&5).is_some());
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,14 @@
|
|||||||
|
#[derive(Deserialize, Debug, Clone)]
|
||||||
|
#[serde(rename_all(deserialize = "camelCase", serialize = "snake_case"))]
|
||||||
|
pub struct Biome {
|
||||||
|
pub id: u32,
|
||||||
|
pub name: String,
|
||||||
|
pub category: String,
|
||||||
|
pub temperature: f32,
|
||||||
|
pub precipitation: String,
|
||||||
|
pub depth: f32,
|
||||||
|
pub dimension: String,
|
||||||
|
pub display_name: String,
|
||||||
|
pub color: u32,
|
||||||
|
pub rainfall: f32,
|
||||||
|
}
|
Loading…
Reference in New Issue