From 8e76aaceb858bc07182885c456eb56c4c773e04f Mon Sep 17 00:00:00 2001 From: flo Date: Tue, 26 Dec 2023 19:55:21 +0000 Subject: [PATCH] make_crustaceans_tameable --- common/src/comp/body/crustacean.rs | 71 ++++++++++++++----- common/src/comp/pet.rs | 5 +- .../src/persistence/character/conversions.rs | 7 ++ server/src/persistence/json_models.rs | 1 + 4 files changed, 67 insertions(+), 17 deletions(-) diff --git a/common/src/comp/body/crustacean.rs b/common/src/comp/body/crustacean.rs index 7d1a18f671..eb2c4ccad3 100644 --- a/common/src/comp/body/crustacean.rs +++ b/common/src/comp/body/crustacean.rs @@ -1,11 +1,16 @@ +use crate::{make_case_elim, make_proj_elim}; use rand::{seq::SliceRandom, thread_rng}; use serde::{Deserialize, Serialize}; +use strum::{Display, EnumString}; -#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)] -pub struct Body { - pub species: Species, - pub body_type: BodyType, -} +make_proj_elim!( + body, + #[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)] + pub struct Body { + pub species: Species, + pub body_type: BodyType, + } +); impl Body { pub fn random() -> Self { @@ -25,11 +30,29 @@ impl From for super::Body { fn from(body: Body) -> Self { super::Body::Crustacean(body) } } -#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)] -#[repr(u32)] -pub enum Species { - Crab = 0, -} +// Renaming any enum entries here (re-ordering is fine) will require a +// database migration to ensure pets correctly de-serialize on player login. +make_case_elim!( + species, + #[derive( + Copy, + Clone, + Debug, + Display, + EnumString, + PartialEq, + Eq, + PartialOrd, + Ord, + Hash, + Serialize, + Deserialize, + )] + #[repr(u32)] + pub enum Species { + Crab = 0, + } +); /// Data representing per-species generic data. #[derive(Clone, Debug, Serialize, Deserialize)] @@ -57,10 +80,26 @@ impl<'a, SpeciesMeta: 'a> IntoIterator for &'a AllSpecies { fn into_iter(self) -> Self::IntoIter { ALL_SPECIES.iter().copied() } } -#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)] -#[repr(u32)] -pub enum BodyType { - Female = 0, - Male = 1, -} +make_case_elim!( + body_type, + #[derive( + Copy, + Clone, + Debug, + Display, + EnumString, + PartialEq, + Eq, + PartialOrd, + Ord, + Hash, + Serialize, + Deserialize, + )] + #[repr(u32)] + pub enum BodyType { + Female = 0, + Male = 1, + } +); pub const ALL_BODY_TYPES: [BodyType; 2] = [BodyType::Female, BodyType::Male]; diff --git a/common/src/comp/pet.rs b/common/src/comp/pet.rs index e954eff273..a3a8a7eb67 100755 --- a/common/src/comp/pet.rs +++ b/common/src/comp/pet.rs @@ -52,7 +52,10 @@ pub fn is_tameable(body: &Body) -> bool { | quadruped_medium::Species::Hirdrasil ) }, - Body::QuadrupedLow(_) | Body::QuadrupedSmall(_) | Body::BirdMedium(_) => true, + Body::QuadrupedLow(_) + | Body::QuadrupedSmall(_) + | Body::BirdMedium(_) + | Body::Crustacean(_) => true, _ => false, } } diff --git a/server/src/persistence/character/conversions.rs b/server/src/persistence/character/conversions.rs index 6629cd7a9a..5636487573 100644 --- a/server/src/persistence/character/conversions.rs +++ b/server/src/persistence/character/conversions.rs @@ -220,6 +220,10 @@ pub fn convert_body_to_database_json( "bird_medium", serde_json::to_string(&GenericBody::from(body))?, ), + Body::Crustacean(body) => ( + "crustacean", + serde_json::to_string(&GenericBody::from(body))?, + ), _ => { return Err(PersistenceError::ConversionError(format!( "Unsupported body type for persistence: {:?}", @@ -597,6 +601,9 @@ pub fn convert_body_from_database( "bird_medium" => { deserialize_body!(body_data, BirdMedium, bird_medium) }, + "crustacean" => { + deserialize_body!(body_data, Crustacean, crustacean) + }, _ => { return Err(PersistenceError::ConversionError(format!( "{} is not a supported body type for deserialization", diff --git a/server/src/persistence/json_models.rs b/server/src/persistence/json_models.rs index 58f80ea109..59706f7d43 100644 --- a/server/src/persistence/json_models.rs +++ b/server/src/persistence/json_models.rs @@ -60,6 +60,7 @@ generic_body_from_impl!(comp::quadruped_low::Body); generic_body_from_impl!(comp::quadruped_medium::Body); generic_body_from_impl!(comp::quadruped_small::Body); generic_body_from_impl!(comp::bird_medium::Body); +generic_body_from_impl!(comp::crustacean::Body); #[derive(Serialize, Deserialize)] pub struct CharacterPosition {