Skip to content

Commit

Permalink
✨ Update achievement content
Browse files Browse the repository at this point in the history
  • Loading branch information
bal7hazar committed Oct 25, 2024
1 parent 358f418 commit da2d7cc
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 134 deletions.
20 changes: 2 additions & 18 deletions packages/achievement/src/components/achievable.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
83 changes: 26 additions & 57 deletions packages/achievement/src/events/creation.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 }
}
}

Expand All @@ -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);
}
}

Expand All @@ -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);
Expand All @@ -109,35 +90,23 @@ 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
);
}

#[test]
#[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
);
}
}
Expand Down
5 changes: 1 addition & 4 deletions packages/achievement/src/events/index.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}

Expand Down
17 changes: 2 additions & 15 deletions packages/achievement/src/store.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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,));
}
Expand Down
24 changes: 3 additions & 21 deletions packages/achievement/src/tests/mocks/achiever.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,9 @@ trait IAchiever<TContractState> {
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,);
}
Expand Down Expand Up @@ -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,) {
Expand Down
22 changes: 3 additions & 19 deletions packages/achievement/src/tests/test_achievable.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -21,40 +21,24 @@ 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

#[test]
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::<AchievementCreation>(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]
Expand Down

0 comments on commit da2d7cc

Please sign in to comment.