From 3951a63d9393ed7e1381bbb9da95dfb2dd2d6b13 Mon Sep 17 00:00:00 2001 From: metamuffin Date: Mon, 14 Oct 2024 00:24:15 +0200 Subject: book intermediate representation and packets for sending to client --- server/src/commands.rs | 2 +- server/src/data/mod.rs | 19 ++++++++++++++----- server/src/entity/book.rs | 2 +- server/src/entity/customers.rs | 1 + server/src/entity/mod.rs | 7 ++++++- server/src/server.rs | 4 ++++ 6 files changed, 27 insertions(+), 8 deletions(-) (limited to 'server/src') diff --git a/server/src/commands.rs b/server/src/commands.rs index feb6ab65..25b51bff 100644 --- a/server/src/commands.rs +++ b/server/src/commands.rs @@ -195,7 +195,7 @@ impl Server { .await .map_err(|e| TrError::Plain(e.to_string()))?; } - Command::Book => replies.push(PacketC::Menu(Menu::Book)), + Command::Book => replies.push(PacketC::Menu(Menu::Document(self.data.book.clone()))), Command::Download { r#type, name } => { let source = match r#type { DownloadType::Map => self.index.read_map(&name).await, diff --git a/server/src/data/mod.rs b/server/src/data/mod.rs index 39684c6f..549574c7 100644 --- a/server/src/data/mod.rs +++ b/server/src/data/mod.rs @@ -20,12 +20,12 @@ pub mod demands; pub mod index; use crate::entity::{construct_entity, Entities, EntityDecl}; -use anyhow::{anyhow, bail, Result}; +use anyhow::{anyhow, bail, Context, Result}; use demands::generate_demands; use hurrycurry_bot::algos::ALGO_CONSTRUCTORS; use hurrycurry_protocol::{ glam::{IVec2, Vec2}, - Gamedata, ItemIndex, MapMetadata, Recipe, TileIndex, + DocumentElement, Gamedata, ItemIndex, MapMetadata, Recipe, TileIndex, }; use serde::{Deserialize, Serialize}; use std::{ @@ -87,14 +87,15 @@ pub struct DemandDecl { points: i64, } -#[derive(Debug,Clone, Default)] +#[derive(Debug, Clone, Default)] #[rustfmt::skip] pub struct Serverdata { pub initial_map: HashMap)>, pub chef_spawn: Vec2, pub customer_spawn: Vec2, pub score_baseline: i64, - pub default_timer: Option + pub default_timer: Option, + pub book: DocumentElement } #[derive(Debug, Deserialize, Default)] @@ -145,7 +146,13 @@ impl DataIndex { .read_recipes(map_in.recipes.as_deref().unwrap_or("default")) .await?, )?; - build_data(&self.maps, map.to_string(), map_in, recipes_in) + let book = serde_json::from_str( + &read_to_string(data_dir().join("book.json")) + .await + .context("loading book")?, + ) + .context("invalid book")?; + build_data(&self.maps, map.to_string(), map_in, recipes_in, book) } } @@ -154,6 +161,7 @@ pub fn build_data( map_name: String, map_in: MapDecl, recipes_in: Vec, + book: DocumentElement, ) -> Result<(Gamedata, Serverdata, Entities)> { let reg = ItemTileRegistry::default(); let mut recipes = Vec::new(); @@ -304,6 +312,7 @@ pub fn build_data( chef_spawn, customer_spawn, default_timer, + book, score_baseline: map_in.score_baseline, }, entities, diff --git a/server/src/entity/book.rs b/server/src/entity/book.rs index 70197764..8b152bb9 100644 --- a/server/src/entity/book.rs +++ b/server/src/entity/book.rs @@ -32,7 +32,7 @@ impl Entity for Book { ) -> Result { if pos == Some(self.0) { if let Some(r) = c.replies { - r.push(PacketC::Menu(Menu::Book)); + r.push(PacketC::Menu(Menu::Document(c.serverdata.book.clone()))); } return Ok(true); } diff --git a/server/src/entity/customers.rs b/server/src/entity/customers.rs index 1e8b6b13..0c3ced8d 100644 --- a/server/src/entity/customers.rs +++ b/server/src/entity/customers.rs @@ -65,6 +65,7 @@ impl Entity for Customers { for bot in &mut self.customers { bot.tick(EntityContext { game: c.game, + serverdata: c.serverdata, packet_out: c.packet_out, packet_in: c.packet_in, score_changed: c.score_changed, diff --git a/server/src/entity/mod.rs b/server/src/entity/mod.rs index 87656f0d..044fccab 100644 --- a/server/src/entity/mod.rs +++ b/server/src/entity/mod.rs @@ -25,7 +25,11 @@ pub mod item_portal; pub mod player_portal; pub mod tutorial; -use crate::{data::ItemTileRegistry, message::TrError, scoreboard::ScoreboardStore}; +use crate::{ + data::{ItemTileRegistry, Serverdata}, + message::TrError, + scoreboard::ScoreboardStore, +}; use anyhow::{anyhow, Result}; use book::Book; use campaign::{Gate, GateCondition, Map}; @@ -47,6 +51,7 @@ pub type Entities = Vec; pub struct EntityContext<'a> { pub game: &'a mut Game, + pub serverdata: &'a Serverdata, pub packet_out: &'a mut VecDeque, pub packet_in: &'a mut VecDeque, pub score_changed: &'a mut bool, diff --git a/server/src/server.rs b/server/src/server.rs index 18f78fc0..d3e9ab59 100644 --- a/server/src/server.rs +++ b/server/src/server.rs @@ -340,6 +340,7 @@ impl Server { packet_in: &mut self.packet_loopback, score_changed: &mut self.score_changed, scoreboard: &self.scoreboard, + serverdata: self.data.as_ref(), replies: None, dt: 0., load_map: &mut None, @@ -447,6 +448,7 @@ impl Server { packet_in: &mut self.packet_loopback, score_changed: &mut self.score_changed, load_map: &mut None, + serverdata: &self.data, scoreboard: &self.scoreboard, replies: Some(replies), dt: 0., @@ -699,6 +701,7 @@ impl Server { score_changed: &mut self.score_changed, packet_in: &mut self.packet_loopback, scoreboard: &self.scoreboard, + serverdata: &self.data, replies: None, dt, }) { @@ -714,6 +717,7 @@ impl Server { score_changed: &mut self.score_changed, packet_in: &mut self.packet_loopback, scoreboard: &self.scoreboard, + serverdata: &self.data, replies: None, dt: 0., }); -- cgit v1.2.3-70-g09d2