diff options
author | metamuffin <metamuffin@disroot.org> | 2025-09-19 22:27:46 +0200 |
---|---|---|
committer | metamuffin <metamuffin@disroot.org> | 2025-09-19 22:40:39 +0200 |
commit | 402067b8317195fd2bc4ab4d92b5ace94fadb7c0 (patch) | |
tree | 2437c52ae71a11c4d17a6fa4597f8152dae96ddc /server/src/data/mod.rs | |
parent | 2f311fec691cd7a62fa4f95ee0419089913b5dd8 (diff) | |
download | hurrycurry-402067b8317195fd2bc4ab4d92b5ace94fadb7c0.tar hurrycurry-402067b8317195fd2bc4ab4d92b5ace94fadb7c0.tar.bz2 hurrycurry-402067b8317195fd2bc4ab4d92b5ace94fadb7c0.tar.zst |
Refactor book part 1
Diffstat (limited to 'server/src/data/mod.rs')
-rw-r--r-- | server/src/data/mod.rs | 47 |
1 files changed, 25 insertions, 22 deletions
diff --git a/server/src/data/mod.rs b/server/src/data/mod.rs index b56ea1a1..819d8dd1 100644 --- a/server/src/data/mod.rs +++ b/server/src/data/mod.rs @@ -23,19 +23,19 @@ use anyhow::{anyhow, bail, Context, Result}; use demands::generate_demands; use hurrycurry_bot::algos::ALGO_CONSTRUCTORS; use hurrycurry_protocol::{ + book::Book, glam::{IVec2, Vec2}, - DocumentElement, Gamedata, ItemIndex, MapMetadata, Recipe, TileIndex, + Gamedata, ItemIndex, MapMetadata, Recipe, TileIndex, }; use serde::{Deserialize, Serialize}; use std::{ collections::{HashMap, HashSet}, - fs::File, + fs::{read_to_string, File}, path::PathBuf, str::FromStr, sync::{Mutex, RwLock}, time::Duration, }; -use tokio::fs::read_to_string; #[derive(Debug, Deserialize, Serialize, Clone, Copy, Default)] #[serde(rename_all = "snake_case")] @@ -96,7 +96,7 @@ pub struct Serverdata { pub customer_spawn: Vec2, pub score_baseline: i64, pub default_timer: Option<Duration>, - pub book: DocumentElement, + pub book: Book, pub flags: ServerdataFlags, } @@ -122,53 +122,57 @@ fn data_dir() -> PathBuf { } impl DataIndex { - pub async fn load() -> Result<Self> { + pub fn load() -> Result<Self> { let mut s = Self::default(); - s.reload().await?; + s.reload()?; Ok(s) } - pub async fn reload(&mut self) -> Result<()> { + pub fn reload(&mut self) -> Result<()> { *self = serde_yml::from_reader(File::open(data_dir().join("index.yaml"))?)?; Ok(()) } - pub async fn read_map(&self, name: &str) -> Result<String> { + pub fn read_map(&self, name: &str) -> Result<String> { // Scary! if name.contains("..") || name.starts_with("/") || name.contains("//") { bail!("illegal map path"); } let path = data_dir().join(format!("maps/{name}.yaml")); - Ok(read_to_string(path).await?) + Ok(read_to_string(path)?) } - pub async fn read_recipes(&self, name: &str) -> Result<String> { + pub fn read_recipes(&self, name: &str) -> Result<String> { if !self.recipes.contains(name) { bail!("unknown recipes: {name:?}"); } let path = data_dir().join(format!("recipes/{name}.yaml")); - Ok(read_to_string(path).await?) + Ok(read_to_string(path)?) } - pub async fn generate(&self, map: &str) -> Result<(Gamedata, Serverdata, Entities)> { + pub fn generate(&self, map: &str) -> Result<(Gamedata, Serverdata, Entities)> { let map_in: MapDecl = serde_yml::from_str( &self .read_map(map) - .await .context(anyhow!("Failed to read map file ({map})"))?, ) .context(anyhow!("Failed to parse map file ({map})"))?; let recipes_in = serde_yml::from_str( &self .read_recipes(map_in.recipes.as_deref().unwrap_or("default")) - .await .context("Failed read recipe file")?, ) .context("Failed to parse recipe file")?; - let book = serde_json::from_str( - &read_to_string(data_dir().join("book.json")) - .await - .context("Failed to read book file")?, + + build_data(&self.maps, map.to_string(), map_in, recipes_in) + } + pub fn generate_with_book(&self, map: &str) -> Result<(Gamedata, Serverdata, Entities)> { + let (gd, mut sd, es) = self.generate(map)?; + sd.book = self.read_book()?; + Ok((gd, sd, es)) + } + pub fn read_book(&self) -> Result<Book> { + serde_json::from_str( + &read_to_string(data_dir().join("book.json")).context("Failed to read book file")?, ) - .context("Failed to parse book file")?; - build_data(&self.maps, map.to_string(), map_in, recipes_in, book) + .context("Failed to parse book file") } } @@ -177,7 +181,6 @@ pub fn build_data( map_name: String, map_in: MapDecl, recipes_in: Vec<RecipeDecl>, - book: DocumentElement, ) -> Result<(Gamedata, Serverdata, Entities)> { let reg = ItemTileRegistry::default(); let mut recipes = Vec::new(); @@ -330,7 +333,7 @@ pub fn build_data( flags: map_in.flags, customer_spawn, default_timer, - book, + book: Book::default(), score_baseline: map_in.score_baseline, }, entities, |