From 16ff78180669411326d42ea32d4a9260c018236c Mon Sep 17 00:00:00 2001 From: metamuffin Date: Tue, 13 Aug 2024 12:48:31 +0200 Subject: refactor server to use client-lib data model (breaks customers) --- server/protocol/src/lib.rs | 41 ++++++++++++++++++++++++++++++++++++++--- 1 file changed, 38 insertions(+), 3 deletions(-) (limited to 'server/protocol/src') diff --git a/server/protocol/src/lib.rs b/server/protocol/src/lib.rs index 2c165a92..05561a12 100644 --- a/server/protocol/src/lib.rs +++ b/server/protocol/src/lib.rs @@ -71,13 +71,12 @@ pub struct MapMetadata { #[derive(Debug, Clone, Serialize, Deserialize, Encode, Decode, Default)] #[rustfmt::skip] -pub struct ClientGamedata { +pub struct Gamedata { pub current_map: String, pub item_names: Vec, pub tile_names: Vec, pub tile_collide: Vec, pub tile_interact: Vec, - pub map_names: HashSet, // for compat with game jam version pub maps: HashMap, pub recipes: Vec, } @@ -144,7 +143,7 @@ pub enum PacketC { id: PlayerID, }, Data { - data: ClientGamedata, + data: Gamedata, }, AddPlayer { id: PlayerID, @@ -257,6 +256,42 @@ pub enum Recipe { }, } +impl Gamedata { + pub fn tile_name(&self, index: TileIndex) -> &String { + &self.tile_names[index.0] + } + pub fn is_tile_colliding(&self, index: TileIndex) -> bool { + self.tile_collide[index.0] + } + pub fn is_tile_interactable(&self, index: TileIndex) -> bool { + self.tile_interact[index.0] + } + pub fn item_name(&self, index: ItemIndex) -> &String { + &self.item_names[index.0] + } + pub fn recipe(&self, index: RecipeIndex) -> &Recipe { + &self.recipes[index.0] + } + pub fn get_tile_by_name(&self, name: &str) -> Option { + self.tile_names + .iter() + .position(|t| t == name) + .map(TileIndex) + } + pub fn get_item_by_name(&self, name: &str) -> Option { + self.item_names + .iter() + .position(|t| t == name) + .map(ItemIndex) + } + pub fn recipes(&self) -> impl Iterator { + self.recipes + .iter() + .enumerate() + .map(|(i, e)| (RecipeIndex(i), e)) + } +} + impl Recipe { pub fn tile(&self) -> Option { match self { -- cgit v1.2.3-70-g09d2