From d6459258d7d999640f3d2a0b6b81ca654c3c00c2 Mon Sep 17 00:00:00 2001 From: bal7hazar Date: Wed, 20 Nov 2024 15:23:16 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Introduce=20service=20and=20socials?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/slot/src/helpers/json.cairo | 49 +++++++++-------- packages/slot/src/lib.cairo | 3 ++ packages/slot/src/models/deployment.cairo | 25 ++++----- packages/slot/src/models/game.cairo | 21 +++----- packages/slot/src/models/index.cairo | 3 +- packages/slot/src/types/metadata.cairo | 55 +++++++++++++++++++ packages/slot/src/types/service.cairo | 66 +++++++++++++++++++++++ packages/slot/src/types/socials.cairo | 49 +++++++++++++++++ 8 files changed, 219 insertions(+), 52 deletions(-) create mode 100644 packages/slot/src/types/metadata.cairo create mode 100644 packages/slot/src/types/service.cairo create mode 100644 packages/slot/src/types/socials.cairo diff --git a/packages/slot/src/helpers/json.cairo b/packages/slot/src/helpers/json.cairo index 7a3c218..3d4c10c 100644 --- a/packages/slot/src/helpers/json.cairo +++ b/packages/slot/src/helpers/json.cairo @@ -1,31 +1,31 @@ //! JSON helper functions -pub trait JsonableTrait { +pub trait JsonifiableTrait { fn jsonify(self: T) -> ByteArray; } -pub impl Jsonable, +core::fmt::Display> of JsonableTrait { +pub impl Jsonifiable, +core::fmt::Display> of JsonifiableTrait { fn jsonify(self: T) -> ByteArray { format!("{}", self) } } #[generate_trait] -pub impl JsonableSimple of JsonableSimpleTrait { +pub impl JsonifiableSimple of JsonifiableSimpleTrait { fn jsonify(name: ByteArray, value: ByteArray) -> ByteArray { format!("\"{}\":{}", name, value) } } #[generate_trait] -pub impl JsonableString of JsonableStringTrait { +pub impl JsonifiableString of JsonifiableStringTrait { fn jsonify(name: ByteArray, value: ByteArray) -> ByteArray { format!("\"{}\":\"{}\"", name, value) } } #[generate_trait] -pub impl JsonableArray, +Drop> of JsonableArrayTrait { +pub impl JsonifiableArray, +Drop> of JsonifiableArrayTrait { fn jsonify(name: ByteArray, mut value: Array) -> ByteArray { let mut string = "["; let mut index: u32 = 0; @@ -36,7 +36,7 @@ pub impl JsonableArray, +Drop> of JsonableArrayTrait string += item.jsonify(); index += 1; }; - JsonableSimple::jsonify(name, string + "]") + JsonifiableSimple::jsonify(name, string + "]") } } @@ -44,7 +44,9 @@ pub impl JsonableArray, +Drop> of JsonableArrayTrait mod tests { // Local imports - use super::{Jsonable, JsonableSimple, JsonableString, JsonableArray, JsonableTrait}; + use super::{ + Jsonifiable, JsonifiableSimple, JsonifiableString, JsonifiableArray, JsonifiableTrait + }; #[derive(Drop)] struct BooleanObject { @@ -77,48 +79,49 @@ mod tests { object: IntegerObject, } - pub impl IntegerObjectJsonable of JsonableTrait { + pub impl IntegerObjectJsonifiable of JsonifiableTrait { fn jsonify(self: IntegerObject) -> ByteArray { let mut string = "{"; - string += JsonableSimple::jsonify("value", format!("{}", self.value)); + string += JsonifiableSimple::jsonify("value", format!("{}", self.value)); string + "}" } } - pub impl BooleanObjectJsonable of JsonableTrait { + pub impl BooleanObjectJsonifiable of JsonifiableTrait { fn jsonify(self: BooleanObject) -> ByteArray { let mut string = "{"; - string += JsonableSimple::jsonify("value", format!("{}", self.value)); + string += JsonifiableSimple::jsonify("value", format!("{}", self.value)); string + "}" } } - pub impl FeltObjectJsonable of JsonableTrait { + pub impl FeltObjectJsonifiable of JsonifiableTrait { fn jsonify(self: FeltObject) -> ByteArray { let mut string = "{"; - string += JsonableSimple::jsonify("value", format!("{}", self.value)); + string += JsonifiableSimple::jsonify("value", format!("{}", self.value)); string + "}" } } - pub impl ByteArrayObjectJsonable of JsonableTrait { + pub impl ByteArrayObjectJsonifiable of JsonifiableTrait { fn jsonify(self: ByteArrayObject) -> ByteArray { let mut string = "{"; - string += JsonableString::jsonify("value", format!("{}", self.value)); + string += JsonifiableString::jsonify("value", format!("{}", self.value)); string + "}" } } - pub impl ComplexJsonable of JsonableTrait { + pub impl ComplexJsonifiable of JsonifiableTrait { fn jsonify(self: Complex) -> ByteArray { let mut string = "{"; - string += JsonableSimple::jsonify("boolean", format!("{}", self.boolean)); - string += "," + JsonableSimple::jsonify("integer", format!("{}", self.integer)); - string += "," + JsonableSimple::jsonify("felt", format!("{}", self.felt)); - string += "," + JsonableString::jsonify("byte_array", format!("{}", self.byte_array)); - string += "," + JsonableArray::jsonify("array", self.array); - string += "," + JsonableArray::jsonify("object_array", self.object_array); - string += "," + JsonableSimple::jsonify("object", self.object.jsonify()); + string += JsonifiableSimple::jsonify("boolean", format!("{}", self.boolean)); + string += "," + JsonifiableSimple::jsonify("integer", format!("{}", self.integer)); + string += "," + JsonifiableSimple::jsonify("felt", format!("{}", self.felt)); + string += "," + + JsonifiableString::jsonify("byte_array", format!("{}", self.byte_array)); + string += "," + JsonifiableArray::jsonify("array", self.array); + string += "," + JsonifiableArray::jsonify("object_array", self.object_array); + string += "," + JsonifiableSimple::jsonify("object", self.object.jsonify()); string + "}" } } diff --git a/packages/slot/src/lib.cairo b/packages/slot/src/lib.cairo index a1a77f1..84e65dc 100644 --- a/packages/slot/src/lib.cairo +++ b/packages/slot/src/lib.cairo @@ -9,7 +9,10 @@ mod types { mod method; mod role; mod tier; + mod socials; + mod service; mod status; + mod metadata; } mod models { diff --git a/packages/slot/src/models/deployment.cairo b/packages/slot/src/models/deployment.cairo index 9140413..4a1a889 100644 --- a/packages/slot/src/models/deployment.cairo +++ b/packages/slot/src/models/deployment.cairo @@ -1,6 +1,7 @@ // Intenral imports use arcade_slot::models::index::Deployment; +use arcade_slot::types::service::Service; use arcade_slot::types::status::Status; use arcade_slot::types::tier::Tier; @@ -12,7 +13,7 @@ pub mod errors { pub const DEPLOYMENT_INVALID_IDENTIFIER: felt252 = 'Deployment: invalid identifier'; pub const DEPLOYMENT_INVALID_PROJECT: felt252 = 'Deployment: invalid project'; pub const DEPLOYMENT_INVALID_STATUS: felt252 = 'Deployment: invalid status'; - pub const DEPLOYMENT_INVALID_SERVICE_ID: felt252 = 'Deployment: invalid service id'; + pub const DEPLOYMENT_INVALID_SERVICE: felt252 = 'Deployment: invalid service'; pub const DEPLOYMENT_INVALID_TIER: felt252 = 'Deployment: invalid tier'; pub const DEPLOYMENT_INVALID_REGIONS: felt252 = 'Deployment: invalid regions'; } @@ -25,7 +26,7 @@ impl DeploymentImpl of DeploymentTrait { project: felt252, status: Status, branch: Option, - service_id: felt252, + service: Service, tier: Tier, regions: felt252, auto_upgrade: bool, @@ -35,7 +36,7 @@ impl DeploymentImpl of DeploymentTrait { DeploymentAssert::assert_valid_identifier(id); DeploymentAssert::assert_valid_project(project); DeploymentAssert::assert_valid_status(status); - DeploymentAssert::assert_valid_service_id(service_id); + DeploymentAssert::assert_valid_service(service); DeploymentAssert::assert_valid_tier(tier); DeploymentAssert::assert_valid_regions(regions); // [Return] Deployment @@ -44,7 +45,7 @@ impl DeploymentImpl of DeploymentTrait { project: project, status: status.into(), branch: branch, - service_id: service_id, + service: service.into(), tier: tier.into(), regions: regions, auto_upgrade: auto_upgrade, @@ -81,8 +82,8 @@ impl DeploymentAssert of AssertTrait { } #[inline] - fn assert_valid_service_id(service_id: felt252) { - assert(service_id != 0, errors::DEPLOYMENT_INVALID_SERVICE_ID); + fn assert_valid_service(service: Service) { + assert(service != Service::None, errors::DEPLOYMENT_INVALID_SERVICE); } #[inline] @@ -100,7 +101,7 @@ impl DeploymentAssert of AssertTrait { mod tests { // Local imports - use super::{Deployment, DeploymentTrait, DeploymentAssert, Status, Tier}; + use super::{Deployment, DeploymentTrait, DeploymentAssert, Service, Status, Tier}; // Constants @@ -108,7 +109,7 @@ mod tests { const PROJECT: felt252 = 'PROJECT'; const STATUS: Status = Status::Active; const BRANCH: Option = Option::None; - const SERVICE_ID: felt252 = 'SERVICE_ID'; + const SERVICE: Service = Service::Katana; const TIER: Tier = Tier::Basic; const REGIONS: felt252 = 'REGIONS'; const AUTO_UPGRADE: bool = true; @@ -116,13 +117,13 @@ mod tests { #[test] fn test_deployment_new() { let deployment = DeploymentTrait::new( - IDENTIFIER, PROJECT, STATUS, BRANCH, SERVICE_ID, TIER, REGIONS, AUTO_UPGRADE, "" + IDENTIFIER, PROJECT, STATUS, BRANCH, SERVICE, TIER, REGIONS, AUTO_UPGRADE, "" ); assert_eq!(deployment.id, IDENTIFIER); assert_eq!(deployment.project, PROJECT); assert_eq!(deployment.status, STATUS.into()); assert_eq!(deployment.branch, BRANCH); - assert_eq!(deployment.service_id, SERVICE_ID); + assert_eq!(deployment.service, SERVICE.into()); assert_eq!(deployment.tier, TIER.into()); assert_eq!(deployment.regions, REGIONS); assert_eq!(deployment.auto_upgrade, AUTO_UPGRADE); @@ -132,7 +133,7 @@ mod tests { #[test] fn test_deployment_assert_does_exist() { let deployment = DeploymentTrait::new( - IDENTIFIER, PROJECT, STATUS, BRANCH, SERVICE_ID, TIER, REGIONS, AUTO_UPGRADE, "" + IDENTIFIER, PROJECT, STATUS, BRANCH, SERVICE, TIER, REGIONS, AUTO_UPGRADE, "" ); deployment.assert_does_exist(); } @@ -141,7 +142,7 @@ mod tests { #[should_panic(expected: 'Deployment: already exists')] fn test_deployment_revert_already_exists() { let deployment = DeploymentTrait::new( - IDENTIFIER, PROJECT, STATUS, BRANCH, SERVICE_ID, TIER, REGIONS, AUTO_UPGRADE, "" + IDENTIFIER, PROJECT, STATUS, BRANCH, SERVICE, TIER, REGIONS, AUTO_UPGRADE, "" ); deployment.assert_does_not_exist(); } diff --git a/packages/slot/src/models/game.cairo b/packages/slot/src/models/game.cairo index c14f32a..e2a918d 100644 --- a/packages/slot/src/models/game.cairo +++ b/packages/slot/src/models/game.cairo @@ -15,21 +15,13 @@ pub mod errors { #[generate_trait] impl GameImpl of GameTrait { #[inline] - fn new( - id: felt252, name: felt252, description: ByteArray, socials: ByteArray, metadata: ByteArray, - ) -> Game { + fn new(id: felt252, name: felt252, socials: ByteArray, metadata: ByteArray,) -> Game { // [Check] Inputs GameAssert::assert_valid_identifier(id); GameAssert::assert_valid_name(name); // [Return] Game Game { - id: id, - name: name, - description: description, - priority: 0, - socials: socials, - metadata: metadata, - active: true, + id: id, name: name, priority: 0, socials: socials, metadata: metadata, active: true, } } } @@ -70,10 +62,9 @@ mod tests { #[test] fn test_service_new() { - let service = GameTrait::new(IDENTIFIER, NAME, "", "", ""); + let service = GameTrait::new(IDENTIFIER, NAME, "", ""); assert_eq!(service.id, IDENTIFIER); assert_eq!(service.name, NAME); - assert_eq!(service.description, ""); assert_eq!(service.socials, ""); assert_eq!(service.metadata, ""); assert_eq!(service.active, true); @@ -81,20 +72,20 @@ mod tests { #[test] fn test_service_assert_does_exist() { - let service = GameTrait::new(IDENTIFIER, NAME, "", "", ""); + let service = GameTrait::new(IDENTIFIER, NAME, "", ""); service.assert_does_exist(); } #[test] #[should_panic(expected: 'Game: already exists')] fn test_service_revert_already_exists() { - let service = GameTrait::new(IDENTIFIER, NAME, "", "", ""); + let service = GameTrait::new(IDENTIFIER, NAME, "", ""); service.assert_does_not_exist(); } #[test] #[should_panic(expected: 'Game: invalid name')] fn test_service_revert_invalid_name() { - GameTrait::new(IDENTIFIER, 0, "", "", ""); + GameTrait::new(IDENTIFIER, 0, "", ""); } } diff --git a/packages/slot/src/models/index.cairo b/packages/slot/src/models/index.cairo index 5c3bc0b..79819cd 100644 --- a/packages/slot/src/models/index.cairo +++ b/packages/slot/src/models/index.cairo @@ -66,7 +66,7 @@ pub struct Deployment { project: felt252, status: u8, branch: Option, - service_id: felt252, + service: felt252, tier: u8, regions: felt252, auto_upgrade: bool, @@ -88,7 +88,6 @@ pub struct Game { #[key] id: felt252, name: felt252, - description: ByteArray, priority: u8, socials: ByteArray, metadata: ByteArray, diff --git a/packages/slot/src/types/metadata.cairo b/packages/slot/src/types/metadata.cairo new file mode 100644 index 0000000..fc841fe --- /dev/null +++ b/packages/slot/src/types/metadata.cairo @@ -0,0 +1,55 @@ +// Internal imports + +use arcade_slot::helpers::json::{JsonifiableString, JsonifiableTrait}; + +// Constants + +const COLOR_LENGTH: usize = 7; + +#[derive(Clone, Drop)] +pub struct Metadata { + color: felt252, + name: ByteArray, + description: ByteArray, + image: ByteArray, + banner: ByteArray, +} + +// Implementations + +pub impl MetadataJsonifiable of JsonifiableTrait { + fn jsonify(self: Metadata) -> ByteArray { + let mut color = ""; + color.append_word(self.color, COLOR_LENGTH); + let mut string = "{"; + string += JsonifiableString::jsonify("color", format!("{}", color)); + string += "," + JsonifiableString::jsonify("name", format!("{}", self.name)); + string += "," + JsonifiableString::jsonify("description", format!("{}", self.description)); + string += "," + JsonifiableString::jsonify("image", format!("{}", self.image)); + string += "," + JsonifiableString::jsonify("banner", format!("{}", self.banner)); + string + "}" + } +} + +#[cfg(test)] +mod tests { + // Local imports + + use super::{Metadata, JsonifiableTrait}; + + #[test] + fn test_metadata_jsonify() { + let metadata = Metadata { + color: '#123456', + name: "name", + description: "description", + image: "image", + banner: "banner", + }; + let json = metadata.jsonify(); + assert_eq!( + json, + "{\"color\":\"#123456\",\"name\":\"name\",\"description\":\"description\",\"image\":\"image\",\"banner\":\"banner\"}" + ); + } +} diff --git a/packages/slot/src/types/service.cairo b/packages/slot/src/types/service.cairo new file mode 100644 index 0000000..5fe7d91 --- /dev/null +++ b/packages/slot/src/types/service.cairo @@ -0,0 +1,66 @@ +#[derive(Copy, Drop, PartialEq)] +pub enum Service { + None, + Katana, + Torii, + Saya, +} + +// Constants + +pub const KATANA: felt252 = 'KATANA'; +pub const TORII: felt252 = 'TORII'; +pub const SAYA: felt252 = 'SAYA'; +// Implementations + +impl IntoServiceU8 of core::Into { + #[inline] + fn into(self: Service) -> u8 { + match self { + Service::None => 0, + Service::Katana => 1, + Service::Torii => 2, + Service::Saya => 3, + } + } +} + +impl IntoU8Service of core::Into { + #[inline] + fn into(self: u8) -> Service { + match self { + 0 => Service::None, + 1 => Service::Katana, + 2 => Service::Torii, + 3 => Service::Saya, + _ => Service::None, + } + } +} + +impl IntoServiceFelt252 of core::Into { + #[inline] + fn into(self: Service) -> felt252 { + match self { + Service::None => 0, + Service::Katana => KATANA, + Service::Torii => TORII, + Service::Saya => SAYA, + } + } +} + +impl IntoFelt252Service of core::Into { + #[inline] + fn into(self: felt252) -> Service { + if self == KATANA { + Service::Katana + } else if self == TORII { + Service::Torii + } else if self == SAYA { + Service::Saya + } else { + Service::None + } + } +} diff --git a/packages/slot/src/types/socials.cairo b/packages/slot/src/types/socials.cairo new file mode 100644 index 0000000..66d6ef5 --- /dev/null +++ b/packages/slot/src/types/socials.cairo @@ -0,0 +1,49 @@ +// Internal imports + +use arcade_slot::helpers::json::{JsonifiableString, JsonifiableTrait}; + +#[derive(Clone, Drop)] +pub struct Socials { + discord: ByteArray, + telegram: ByteArray, + twitter: ByteArray, + youtube: ByteArray, + website: ByteArray, +} + +// Implementations + +pub impl SocialsJsonifiable of JsonifiableTrait { + fn jsonify(self: Socials) -> ByteArray { + let mut string = "{"; + string += JsonifiableString::jsonify("discord", format!("{}", self.discord)); + string += "," + JsonifiableString::jsonify("telegram", format!("{}", self.telegram)); + string += "," + JsonifiableString::jsonify("twitter", format!("{}", self.twitter)); + string += "," + JsonifiableString::jsonify("youtube", format!("{}", self.youtube)); + string += "," + JsonifiableString::jsonify("website", format!("{}", self.website)); + string + "}" + } +} + +#[cfg(test)] +mod tests { + // Local imports + + use super::{Socials, JsonifiableTrait}; + + #[test] + fn test_socials_jsonify() { + let socials = Socials { + discord: "discord", + telegram: "telegram", + twitter: "twitter", + youtube: "youtube", + website: "website", + }; + let json = socials.jsonify(); + assert_eq!( + json, + "{\"discord\":\"discord\",\"telegram\":\"telegram\",\"twitter\":\"twitter\",\"youtube\":\"youtube\",\"website\":\"website\"}" + ); + } +}