aboutsummaryrefslogtreecommitdiff
path: root/server/protocol/src/helpers.rs
diff options
context:
space:
mode:
authormetamuffin <metamuffin@disroot.org>2025-10-20 20:11:02 +0200
committermetamuffin <metamuffin@disroot.org>2025-10-20 20:11:02 +0200
commita52785f4869a09e05417f97aff1c0d5b19587463 (patch)
tree9d288a969a6da19ddb2848ac18a22f9d3c1879b7 /server/protocol/src/helpers.rs
parentf8d95d074c36ec35eee8def73b8d9f2b83c922cb (diff)
downloadhurrycurry-a52785f4869a09e05417f97aff1c0d5b19587463.tar
hurrycurry-a52785f4869a09e05417f97aff1c0d5b19587463.tar.bz2
hurrycurry-a52785f4869a09e05417f97aff1c0d5b19587463.tar.zst
Refactor bot input to packet based
Diffstat (limited to 'server/protocol/src/helpers.rs')
-rw-r--r--server/protocol/src/helpers.rs101
1 files changed, 76 insertions, 25 deletions
diff --git a/server/protocol/src/helpers.rs b/server/protocol/src/helpers.rs
index 31e68d02..90dfd6ff 100644
--- a/server/protocol/src/helpers.rs
+++ b/server/protocol/src/helpers.rs
@@ -15,8 +15,7 @@
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
-use crate::{Gamedata, Hand, ItemIndex, ItemLocation, PlayerID, Recipe, RecipeIndex, TileIndex};
-use std::fmt::Display;
+use crate::{Gamedata, ItemIndex, Recipe, RecipeIndex, TileIndex};
impl Gamedata {
pub fn tile_name(&self, index: TileIndex) -> &str {
@@ -102,37 +101,89 @@ impl Recipe {
}
}
-impl Display for ItemLocation {
- fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
- match self {
- ItemLocation::Tile(pos) => write!(f, "tile({}, {})", pos.x, pos.y),
- ItemLocation::Player(player, hand) => write!(f, "{player}-{hand}"),
+mod display {
+ use crate::*;
+ use std::fmt::Display;
+
+ impl Display for ItemLocation {
+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+ match self {
+ ItemLocation::Tile(pos) => write!(f, "tile({}, {})", pos.x, pos.y),
+ ItemLocation::Player(player, hand) => write!(f, "{player}-{hand}"),
+ }
+ }
+ }
+ impl Display for Hand {
+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+ write!(f, "hand#{}", self.0)
+ }
+ }
+ impl Display for PlayerID {
+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+ write!(f, "player#{}", self.0)
+ }
+ }
+ impl Display for TileIndex {
+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+ write!(f, "tile#{}", self.0)
+ }
+ }
+ impl Display for ItemIndex {
+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+ write!(f, "item#{}", self.0)
+ }
+ }
+ impl Display for RecipeIndex {
+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+ write!(f, "recipe#{}", self.0)
}
}
}
-impl Display for Hand {
- fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
- write!(f, "hand#{}", self.0)
+mod enum_is {
+ use crate::*;
+
+ impl PlayerClass {
+ pub fn is_cheflike(&self) -> bool {
+ matches!(self, Self::Bot | Self::Chef)
+ }
}
-}
-impl Display for PlayerID {
- fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
- write!(f, "player#{}", self.0)
+
+ impl ItemLocation {
+ pub fn is_tile(&self) -> bool {
+ matches!(self, Self::Tile(..))
+ }
+ pub fn is_player(&self) -> bool {
+ matches!(self, Self::Player(..))
+ }
}
}
-impl Display for TileIndex {
- fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
- write!(f, "tile#{}", self.0)
+
+pub mod deser {
+ use crate::*;
+ use serde::Deserializer;
+ use std::collections::BTreeMap;
+
+ pub(crate) fn deser_i64<'de, D: Deserializer<'de>>(deserializer: D) -> Result<i64, D::Error> {
+ let x = f64::deserialize(deserializer)?;
+ Ok(x.trunc() as i64)
}
-}
-impl Display for ItemIndex {
- fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
- write!(f, "item#{}", self.0)
+ pub(crate) fn deser_i32<'de, D: Deserializer<'de>>(deserializer: D) -> Result<i32, D::Error> {
+ let x = f64::deserialize(deserializer)?;
+ Ok(x.trunc() as i32)
}
-}
-impl Display for RecipeIndex {
- fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
- write!(f, "recipe#{}", self.0)
+ pub(crate) fn deser_usize<'de, D: Deserializer<'de>>(
+ deserializer: D,
+ ) -> Result<usize, D::Error> {
+ let x = f64::deserialize(deserializer)?;
+ Ok(x.trunc() as usize)
+ }
+ pub(crate) fn deser_tile_index_map<'de, D: Deserializer<'de>>(
+ deserializer: D,
+ ) -> Result<BTreeMap<TileIndex, HashSet<ItemIndex>>, D::Error> {
+ let x = BTreeMap::<String, HashSet<ItemIndex>>::deserialize(deserializer)?;
+ Ok(x.into_iter()
+ .map(|(k, v)| (TileIndex(k.parse().ok().unwrap_or_default()), v))
+ .collect())
}
}