Skip to content

Commit

Permalink
✨ Introduce service and socials
Browse files Browse the repository at this point in the history
  • Loading branch information
bal7hazar committed Nov 20, 2024
1 parent 8bdd6a6 commit d645925
Show file tree
Hide file tree
Showing 8 changed files with 219 additions and 52 deletions.
49 changes: 26 additions & 23 deletions packages/slot/src/helpers/json.cairo
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
//! JSON helper functions

pub trait JsonableTrait<T> {
pub trait JsonifiableTrait<T> {
fn jsonify(self: T) -> ByteArray;
}

pub impl Jsonable<T, +Drop<T>, +core::fmt::Display<T>> of JsonableTrait<T> {
pub impl Jsonifiable<T, +Drop<T>, +core::fmt::Display<T>> of JsonifiableTrait<T> {
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<T, +JsonableTrait<T>, +Drop<T>> of JsonableArrayTrait<T> {
pub impl JsonifiableArray<T, +JsonifiableTrait<T>, +Drop<T>> of JsonifiableArrayTrait<T> {
fn jsonify(name: ByteArray, mut value: Array<T>) -> ByteArray {
let mut string = "[";
let mut index: u32 = 0;
Expand All @@ -36,15 +36,17 @@ pub impl JsonableArray<T, +JsonableTrait<T>, +Drop<T>> of JsonableArrayTrait<T>
string += item.jsonify();
index += 1;
};
JsonableSimple::jsonify(name, string + "]")
JsonifiableSimple::jsonify(name, string + "]")
}
}

#[cfg(test)]
mod tests {
// Local imports

use super::{Jsonable, JsonableSimple, JsonableString, JsonableArray, JsonableTrait};
use super::{
Jsonifiable, JsonifiableSimple, JsonifiableString, JsonifiableArray, JsonifiableTrait
};

#[derive(Drop)]
struct BooleanObject {
Expand Down Expand Up @@ -77,48 +79,49 @@ mod tests {
object: IntegerObject,
}

pub impl IntegerObjectJsonable of JsonableTrait<IntegerObject> {
pub impl IntegerObjectJsonifiable of JsonifiableTrait<IntegerObject> {
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<BooleanObject> {
pub impl BooleanObjectJsonifiable of JsonifiableTrait<BooleanObject> {
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<FeltObject> {
pub impl FeltObjectJsonifiable of JsonifiableTrait<FeltObject> {
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<ByteArrayObject> {
pub impl ByteArrayObjectJsonifiable of JsonifiableTrait<ByteArrayObject> {
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<Complex> {
pub impl ComplexJsonifiable of JsonifiableTrait<Complex> {
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 + "}"
}
}
Expand Down
3 changes: 3 additions & 0 deletions packages/slot/src/lib.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ mod types {
mod method;
mod role;
mod tier;
mod socials;
mod service;
mod status;
mod metadata;
}

mod models {
Expand Down
25 changes: 13 additions & 12 deletions packages/slot/src/models/deployment.cairo
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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';
}
Expand All @@ -25,7 +26,7 @@ impl DeploymentImpl of DeploymentTrait {
project: felt252,
status: Status,
branch: Option<felt252>,
service_id: felt252,
service: Service,
tier: Tier,
regions: felt252,
auto_upgrade: bool,
Expand All @@ -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
Expand All @@ -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,
Expand Down Expand Up @@ -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]
Expand All @@ -100,29 +101,29 @@ impl DeploymentAssert of AssertTrait {
mod tests {
// Local imports

use super::{Deployment, DeploymentTrait, DeploymentAssert, Status, Tier};
use super::{Deployment, DeploymentTrait, DeploymentAssert, Service, Status, Tier};

// Constants

const IDENTIFIER: felt252 = 'ID';
const PROJECT: felt252 = 'PROJECT';
const STATUS: Status = Status::Active;
const BRANCH: Option<felt252> = 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;

#[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);
Expand All @@ -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();
}
Expand All @@ -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();
}
Expand Down
21 changes: 6 additions & 15 deletions packages/slot/src/models/game.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}
}
Expand Down Expand Up @@ -70,31 +62,30 @@ 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);
}

#[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, "", "");
}
}
3 changes: 1 addition & 2 deletions packages/slot/src/models/index.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ pub struct Deployment {
project: felt252,
status: u8,
branch: Option<felt252>,
service_id: felt252,
service: felt252,
tier: u8,
regions: felt252,
auto_upgrade: bool,
Expand All @@ -88,7 +88,6 @@ pub struct Game {
#[key]
id: felt252,
name: felt252,
description: ByteArray,
priority: u8,
socials: ByteArray,
metadata: ByteArray,
Expand Down
55 changes: 55 additions & 0 deletions packages/slot/src/types/metadata.cairo
Original file line number Diff line number Diff line change
@@ -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<Metadata> {
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\"}"
);
}
}
Loading

0 comments on commit d645925

Please sign in to comment.