From b0df9b7c27a3d6316969d7feff4d912c3abf99f6 Mon Sep 17 00:00:00 2001 From: metamuffin Date: Mon, 23 Dec 2024 15:37:11 +0100 Subject: two-handed server --- server/protocol/src/helpers.rs | 30 ++++++++++++++++++++++++++++-- server/protocol/src/lib.rs | 10 +++++++++- 2 files changed, 37 insertions(+), 3 deletions(-) (limited to 'server/protocol') diff --git a/server/protocol/src/helpers.rs b/server/protocol/src/helpers.rs index 924d0886..21835101 100644 --- a/server/protocol/src/helpers.rs +++ b/server/protocol/src/helpers.rs @@ -1,7 +1,8 @@ use std::fmt::Display; use crate::{ - DocumentElement, Gamedata, ItemIndex, ItemLocation, PlayerID, Recipe, RecipeIndex, TileIndex, + DocumentElement, Gamedata, Hand, ItemIndex, ItemLocation, PlayerID, Recipe, RecipeIndex, + TileIndex, }; impl Gamedata { @@ -98,7 +99,32 @@ impl Display for ItemLocation { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { ItemLocation::Tile(pos) => write!(f, "tile({pos})"), - ItemLocation::Player(PlayerID(id)) => write!(f, "player({id})"), + ItemLocation::Player(PlayerID(id), hand) => write!(f, "player({id}_{hand})"), + } + } +} + +impl Display for Hand { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.write_str(match self { + Hand::Left => "left", + Hand::Right => "right", + }) + } +} + +impl Hand { + pub fn index(&self) -> usize { + match self { + Hand::Left => 0, + Hand::Right => 1, + } + } + pub fn from_index(i: usize) -> Self { + match i { + 0 => Hand::Left, + 1 => Hand::Right, + _ => Hand::Left, } } } diff --git a/server/protocol/src/lib.rs b/server/protocol/src/lib.rs index 2ef07015..6ccd2367 100644 --- a/server/protocol/src/lib.rs +++ b/server/protocol/src/lib.rs @@ -71,6 +71,12 @@ pub struct RecipeIndex(pub usize); #[serde(transparent)] pub struct DemandIndex(pub usize); +#[derive(Debug, Clone, Copy, Serialize, Deserialize, Encode, Decode, PartialEq, Eq, Hash)] +pub enum Hand { + Left, + Right, +} + #[derive(Debug, Clone, Serialize, Deserialize, Encode, Decode)] pub struct MapMetadata { pub name: String, @@ -124,6 +130,7 @@ pub enum PacketS { }, Interact { player: PlayerID, + hand: Hand, #[bincode(with_serde)] pos: Option, }, @@ -144,6 +151,7 @@ pub enum PacketS { /// For internal use only (customers) ReplaceHand { player: PlayerID, + hand: Hand, item: Option, }, #[serde(skip)] @@ -344,7 +352,7 @@ pub enum Recipe { #[serde(rename_all = "snake_case")] pub enum ItemLocation { Tile(#[bincode(with_serde)] IVec2), - Player(PlayerID), + Player(PlayerID, Hand), } #[derive(Debug, Clone, Serialize, Deserialize, Encode, Decode)] -- cgit v1.2.3-70-g09d2