Add biomes api

Signed-off-by: trivernis <trivernis@protonmail.com>
pull/1/head
trivernis 4 years ago
parent 45a2cba2b8
commit 72c9d913df
Signed by: Trivernis
GPG Key ID: DFFFCC2C7A02DB45

@ -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)
}
}

@ -1,3 +1,4 @@
use crate::api::biomes::Biomes;
use crate::api::blocks::Blocks; use crate::api::blocks::Blocks;
use crate::api::enchantments::Enchantments; use crate::api::enchantments::Enchantments;
use crate::api::foods::Foods; use crate::api::foods::Foods;
@ -10,6 +11,7 @@ use std::sync::Arc;
#[cfg(test)] #[cfg(test)]
mod tests; mod tests;
pub mod biomes;
pub mod blocks; pub mod blocks;
pub mod enchantments; pub mod enchantments;
pub mod foods; pub mod foods;
@ -26,6 +28,7 @@ pub struct Api {
pub loot: Loot, pub loot: Loot,
pub blocks: Blocks, pub blocks: Blocks,
pub foods: Foods, pub foods: Foods,
pub biomes: Biomes,
} }
impl Api { impl Api {
@ -39,6 +42,7 @@ impl Api {
loot: Loot::new(Arc::clone(&version)), loot: Loot::new(Arc::clone(&version)),
blocks: Blocks::new(Arc::clone(&version)), blocks: Blocks::new(Arc::clone(&version)),
foods: Foods::new(Arc::clone(&version)), foods: Foods::new(Arc::clone(&version)),
biomes: Biomes::new(Arc::clone(&version)),
} }
} }
} }

@ -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());
}
}

@ -2,6 +2,7 @@ use crate::api::versions::{available_versions, versions};
use crate::api::Api; use crate::api::Api;
use crate::models::version::Version; use crate::models::version::Version;
mod biomes;
mod blocks; mod blocks;
mod enchantments; mod enchantments;
mod foods; mod foods;

@ -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,
}

@ -1,8 +1,9 @@
pub mod biome;
pub mod block; pub mod block;
pub mod block_loot; pub mod block_loot;
pub mod enchantment; pub mod enchantment;
pub mod entity_loot; pub mod entity_loot;
pub mod food;
pub mod item; pub mod item;
pub mod recipe; pub mod recipe;
pub mod version; pub mod version;
pub mod food;

Loading…
Cancel
Save