diff --git a/packages/achievement/src/components/achievable.cairo b/packages/achievement/src/components/achievable.cairo index 29ccb13..a6066e8 100644 --- a/packages/achievement/src/components/achievable.cairo +++ b/packages/achievement/src/components/achievable.cairo @@ -42,32 +42,16 @@ mod AchievableComponent { hidden: bool, points: u16, total: u32, - title: ByteArray, - hidden_title: ByteArray, + title: felt252, description: ByteArray, - hidden_description: ByteArray, icon: felt252, - icon_style: felt252, ) { // [Setup] Store let store: Store = StoreTrait::new(world); // [Event] Emit achievement creation let time: u64 = get_block_timestamp(); - store - .create( - identifier, - hidden, - points, - total, - title, - hidden_title, - description, - hidden_description, - icon, - icon_style, - time - ); + store.create(identifier, hidden, points, total, title, description, icon, time); } fn update( diff --git a/packages/achievement/src/events/creation.cairo b/packages/achievement/src/events/creation.cairo index b16bbe1..7515266 100644 --- a/packages/achievement/src/events/creation.cairo +++ b/packages/achievement/src/events/creation.cairo @@ -9,6 +9,7 @@ pub mod errors { pub const ACHIEVEMENT_INVALID_NAMESPACE: felt252 = 'Achievement: invalid namespace'; pub const ACHIEVEMENT_INVALID_ACHIEVEMENT: felt252 = 'Achievement: invalid id'; pub const ACHIEVEMENT_INVALID_TITLE: felt252 = 'Achievement: invalid title'; + pub const ACHIEVEMENT_INVALID_DESCRIPTION: felt252 = 'Achievement: invalid desc.'; } // Implementations @@ -21,32 +22,18 @@ impl AchievementCreationImpl of AchievementCreationTrait { hidden: bool, points: u16, total: u32, - title: ByteArray, - hidden_title: ByteArray, + title: felt252, description: ByteArray, - hidden_description: ByteArray, icon: felt252, - icon_style: felt252, time: u64, ) -> AchievementCreation { // [Check] Inputs // [Info] We don't check points here, leave free the game to decide AchievementCreationAssert::assert_valid_identifier(identifier); - AchievementCreationAssert::assert_valid_title(@title); + AchievementCreationAssert::assert_valid_title(title); + AchievementCreationAssert::assert_valid_description(@description); // [Return] Achievement - AchievementCreation { - identifier, - hidden, - points, - total, - title, - hidden_title, - description, - hidden_description, - icon, - icon_style, - time - } + AchievementCreation { identifier, hidden, points, total, title, description, icon, time } } } @@ -58,8 +45,13 @@ impl AchievementCreationAssert of AssertTrait { } #[inline] - fn assert_valid_title(title: @ByteArray) { - assert(title.len() > 0, errors::ACHIEVEMENT_INVALID_TITLE); + fn assert_valid_title(title: felt252) { + assert(title != 0, errors::ACHIEVEMENT_INVALID_TITLE); + } + + #[inline] + fn assert_valid_description(description: @ByteArray) { + assert(description.len() > 0, errors::ACHIEVEMENT_INVALID_DESCRIPTION); } } @@ -75,31 +67,20 @@ mod tests { const HIDDEN: bool = false; const POINTS: u16 = 100; const TOTAL: u32 = 100; + const TITLE: felt252 = 'TITLE'; const ICON: felt252 = 'ICON'; - const ICON_STYLE: felt252 = 'ICON_STYLE'; + #[test] fn test_achievement_creation_new() { let achievement = AchievementCreationTrait::new( - IDENTIFIER, - HIDDEN, - POINTS, - TOTAL, - "TITLE", - "HIDDEN_TITLE", - "DESCRIPTION", - "HIDDEN_DESCRIPTION", - ICON, - ICON_STYLE, - 1000000000, + IDENTIFIER, HIDDEN, POINTS, TOTAL, TITLE, "DESCRIPTION", ICON, 1000000000, ); assert_eq!(achievement.identifier, IDENTIFIER); assert_eq!(achievement.hidden, HIDDEN); assert_eq!(achievement.points, POINTS); assert_eq!(achievement.total, TOTAL); - assert_eq!(achievement.title, "TITLE"); - assert_eq!(achievement.hidden_title, "HIDDEN_TITLE"); + assert_eq!(achievement.title, TITLE); assert_eq!(achievement.description, "DESCRIPTION"); - assert_eq!(achievement.hidden_description, "HIDDEN_DESCRIPTION"); assert_eq!(achievement.icon, ICON); assert_eq!(achievement.time, 1000000000); @@ -109,17 +90,7 @@ mod tests { #[should_panic(expected: ('Achievement: invalid id',))] fn test_achievement_creation_new_invalid_identifier() { AchievementCreationTrait::new( - 0, - HIDDEN, - POINTS, - TOTAL, - "TITLE", - "HIDDEN_TITLE", - "DESCRIPTION", - "HIDDEN_DESCRIPTION", - ICON, - ICON_STYLE, - 1000000000 + 0, HIDDEN, POINTS, TOTAL, TITLE, "DESCRIPTION", ICON, 1000000000 ); } @@ -127,17 +98,15 @@ mod tests { #[should_panic(expected: ('Achievement: invalid title',))] fn test_achievement_creation_new_invalid_title() { AchievementCreationTrait::new( - IDENTIFIER, - HIDDEN, - POINTS, - TOTAL, - "", - "HIDDEN_TITLE", - "DESCRIPTION", - "HIDDEN_DESCRIPTION", - ICON, - ICON_STYLE, - 1000000000 + IDENTIFIER, HIDDEN, POINTS, TOTAL, 0, "DESCRIPTION", ICON, 1000000000 + ); + } + + #[test] + #[should_panic(expected: ('Achievement: invalid desc.',))] + fn test_achievement_creation_new_invalid_description() { + AchievementCreationTrait::new( + IDENTIFIER, HIDDEN, POINTS, TOTAL, TITLE, "", ICON, 1000000000 ); } } diff --git a/packages/achievement/src/events/index.cairo b/packages/achievement/src/events/index.cairo index 0ecf97a..c7c66aa 100644 --- a/packages/achievement/src/events/index.cairo +++ b/packages/achievement/src/events/index.cairo @@ -7,12 +7,9 @@ pub struct AchievementCreation { hidden: bool, points: u16, total: u32, - title: ByteArray, - hidden_title: ByteArray, + title: felt252, description: ByteArray, - hidden_description: ByteArray, icon: felt252, - icon_style: felt252, time: u64, } diff --git a/packages/achievement/src/store.cairo b/packages/achievement/src/store.cairo index a584599..0ebb107 100644 --- a/packages/achievement/src/store.cairo +++ b/packages/achievement/src/store.cairo @@ -63,26 +63,13 @@ impl StoreImpl of StoreTrait { hidden: bool, points: u16, total: u32, - title: ByteArray, - hidden_title: ByteArray, + title: felt252, description: ByteArray, - hidden_description: ByteArray, icon: felt252, - icon_style: felt252, time: u64, ) { let _event: AchievementCreation = AchievementCreationTrait::new( - identifier, - hidden, - points, - total, - title, - hidden_title, - description, - hidden_description, - icon, - icon_style, - time + identifier, hidden, points, total, title, description, icon, time ); emit!(self.world, (_event,)); } diff --git a/packages/achievement/src/tests/mocks/achiever.cairo b/packages/achievement/src/tests/mocks/achiever.cairo index 1920279..edb0ca2 100644 --- a/packages/achievement/src/tests/mocks/achiever.cairo +++ b/packages/achievement/src/tests/mocks/achiever.cairo @@ -6,12 +6,9 @@ trait IAchiever { hidden: bool, points: u16, total: u32, - title: ByteArray, - hidden_title: ByteArray, + title: felt252, description: ByteArray, - hidden_description: ByteArray, icon: felt252, - icon_style: felt252, ); fn update(self: @TContractState, player_id: felt252, identifier: felt252, count: u32,); } @@ -60,28 +57,13 @@ pub mod Achiever { hidden: bool, points: u16, total: u32, - title: ByteArray, - hidden_title: ByteArray, + title: felt252, description: ByteArray, - hidden_description: ByteArray, icon: felt252, - icon_style: felt252, ) { self .achievable - .create( - self.world(), - identifier, - hidden, - points, - total, - title, - hidden_title, - description, - hidden_description, - icon, - icon_style, - ); + .create(self.world(), identifier, hidden, points, total, title, description, icon,); } fn update(self: @ContractState, player_id: felt252, identifier: felt252, count: u32,) { diff --git a/packages/achievement/src/tests/test_achievable.cairo b/packages/achievement/src/tests/test_achievable.cairo index 9815de4..9ff11a2 100644 --- a/packages/achievement/src/tests/test_achievable.cairo +++ b/packages/achievement/src/tests/test_achievable.cairo @@ -21,8 +21,8 @@ const HIDDEN: bool = false; const POINTS: u16 = 10; const TOTAL: u32 = 100; const COUNT: u32 = 1; +const TITLE: felt252 = 'Title'; const ICON: felt252 = 'fa-khanda'; -const ICON_STYLE: felt252 = 'fa-solid'; // Tests @@ -30,31 +30,15 @@ const ICON_STYLE: felt252 = 'fa-solid'; fn test_achievable_create() { let (world, systems, _context) = spawn_game(); clear_events(world.contract_address); - systems - .achiever - .create( - IDENTIFIER, - HIDDEN, - POINTS, - TOTAL, - "Title", - "Hidden Title", - "Description", - "Hidden Description", - ICON, - ICON_STYLE, - ); + systems.achiever.create(IDENTIFIER, HIDDEN, POINTS, TOTAL, TITLE, "Description", ICON,); let event = starknet::testing::pop_log::(world.contract_address).unwrap(); // FIXME: Cannot check keys because they are shifted due to dojo macros assert_eq!(event.hidden, HIDDEN); assert_eq!(event.points, POINTS); assert_eq!(event.total, TOTAL); - assert_eq!(event.title, "Title"); - assert_eq!(event.hidden_title, "Hidden Title"); + assert_eq!(event.title, TITLE); assert_eq!(event.description, "Description"); - assert_eq!(event.hidden_description, "Hidden Description"); assert_eq!(event.icon, ICON); - assert_eq!(event.icon_style, ICON_STYLE); } #[test]