summaryrefslogtreecommitdiff
path: root/server/protocol/src/lib.rs
diff options
context:
space:
mode:
authormetamuffin <metamuffin@disroot.org>2024-08-13 12:48:31 +0200
committermetamuffin <metamuffin@disroot.org>2024-08-13 16:03:38 +0200
commit16ff78180669411326d42ea32d4a9260c018236c (patch)
treed7c6a7ab498bb1b4f9a3b3db99d54e8781216e05 /server/protocol/src/lib.rs
parent11ff74f034aeec58c06dbe15a3f1ee650ef18c9f (diff)
downloadhurrycurry-16ff78180669411326d42ea32d4a9260c018236c.tar
hurrycurry-16ff78180669411326d42ea32d4a9260c018236c.tar.bz2
hurrycurry-16ff78180669411326d42ea32d4a9260c018236c.tar.zst
refactor server to use client-lib data model (breaks customers)
Diffstat (limited to 'server/protocol/src/lib.rs')
-rw-r--r--server/protocol/src/lib.rs41
1 files changed, 38 insertions, 3 deletions
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<String>,
pub tile_names: Vec<String>,
pub tile_collide: Vec<bool>,
pub tile_interact: Vec<bool>,
- pub map_names: HashSet<String>, // for compat with game jam version
pub maps: HashMap<String, MapMetadata>,
pub recipes: Vec<Recipe>,
}
@@ -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<TileIndex> {
+ self.tile_names
+ .iter()
+ .position(|t| t == name)
+ .map(TileIndex)
+ }
+ pub fn get_item_by_name(&self, name: &str) -> Option<ItemIndex> {
+ self.item_names
+ .iter()
+ .position(|t| t == name)
+ .map(ItemIndex)
+ }
+ pub fn recipes(&self) -> impl Iterator<Item = (RecipeIndex, &Recipe)> {
+ self.recipes
+ .iter()
+ .enumerate()
+ .map(|(i, e)| (RecipeIndex(i), e))
+ }
+}
+
impl Recipe {
pub fn tile(&self) -> Option<TileIndex> {
match self {