Skip to content

Commit

Permalink
✨ Add AchievementIcon type
Browse files Browse the repository at this point in the history
  • Loading branch information
bal7hazar committed Oct 18, 2024
1 parent b5a128b commit ada22f6
Show file tree
Hide file tree
Showing 10 changed files with 49 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@
},
{
"name": "icon",
"type": "core::byte_array::ByteArray"
"type": "core::felt252"
},
{
"name": "time",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
kind = "DojoModel"
class_hash = "0x2f4c5d5380b7cca9297f71ce1ed138a433b1f5f451e1d1f95b490d0d1ca3c72"
original_class_hash = "0x2f4c5d5380b7cca9297f71ce1ed138a433b1f5f451e1d1f95b490d0d1ca3c72"
class_hash = "0x535e56e14f5c19117d153ad353efe78e4831b4926126c7783322f137509ca4c"
original_class_hash = "0x535e56e14f5c19117d153ad353efe78e4831b4926126c7783322f137509ca4c"
abi = "manifests/dev/base/abis/models/game_center-AchievementCreation-604e47bb.json"
tag = "game_center-AchievementCreation"
qualified_path = "achievement::events::index::achievement_creation"
Expand Down Expand Up @@ -58,7 +58,7 @@ key = false

[[members]]
name = "icon"
type = "ByteArray"
type = "felt252"
key = false

[[members]]
Expand Down
2 changes: 1 addition & 1 deletion packages/achievement/src/components/achievable.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ mod AchievableComponent {
description: ByteArray,
hidden_description: ByteArray,
image_uri: ByteArray,
icon: ByteArray,
icon: felt252,
) {
// [Setup] Store
let store: Store = StoreTrait::new(world);
Expand Down
14 changes: 9 additions & 5 deletions packages/achievement/src/events/creation.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ impl AchievementCreationImpl of AchievementCreationTrait {
description: ByteArray,
hidden_description: ByteArray,
image_uri: ByteArray,
icon: ByteArray,
icon: felt252,
time: u64,
) -> AchievementCreation {
// [Check] Inputs
Expand Down Expand Up @@ -84,6 +84,7 @@ mod tests {
const HIDDEN: bool = false;
const POINTS: u16 = 100;
const TOTAL: u32 = 100;
const ICON: felt252 = 'ICON';

#[test]
fn test_achievement_creation_new() {
Expand All @@ -98,7 +99,7 @@ mod tests {
"DESCRIPTION",
"HIDDEN_DESCRIPTION",
"IMAGE_URI",
"ICON",
ICON,
1000000000,
);
assert_eq!(achievement.namespace, NAMESPACE);
Expand All @@ -110,6 +111,9 @@ mod tests {
assert_eq!(achievement.hidden_title, "HIDDEN_TITLE");
assert_eq!(achievement.description, "DESCRIPTION");
assert_eq!(achievement.hidden_description, "HIDDEN_DESCRIPTION");
assert_eq!(achievement.image_uri, "IMAGE_URI");
assert_eq!(achievement.icon, ICON);
assert_eq!(achievement.time, 1000000000);
}

#[test]
Expand All @@ -126,7 +130,7 @@ mod tests {
"DESCRIPTION",
"HIDDEN_DESCRIPTION",
"IMAGE_URI",
"ICON",
ICON,
1000000000
);
}
Expand All @@ -145,7 +149,7 @@ mod tests {
"DESCRIPTION",
"HIDDEN_DESCRIPTION",
"IMAGE_URI",
"ICON",
ICON,
1000000000
);
}
Expand All @@ -164,7 +168,7 @@ mod tests {
"DESCRIPTION",
"HIDDEN_DESCRIPTION",
"IMAGE_URI",
"ICON",
ICON,
1000000000
);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/achievement/src/events/index.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub struct AchievementCreation {
description: ByteArray,
hidden_description: ByteArray,
image_uri: ByteArray,
icon: ByteArray,
icon: felt252,
time: u64,
}

Expand Down
4 changes: 4 additions & 0 deletions packages/achievement/src/lib.cairo
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
mod constants;
mod store;

mod types {
mod icon;
}

mod models {
mod index;
mod game;
Expand Down
2 changes: 1 addition & 1 deletion packages/achievement/src/store.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ impl StoreImpl of StoreTrait {
description: ByteArray,
hidden_description: ByteArray,
image_uri: ByteArray,
icon: ByteArray,
icon: felt252,
time: u64,
) {
let _event: AchievementCreation = AchievementCreationTrait::new(
Expand Down
4 changes: 2 additions & 2 deletions packages/achievement/src/tests/mocks/achiever.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ trait IAchiever<TContractState> {
description: ByteArray,
hidden_description: ByteArray,
image_uri: ByteArray,
icon: ByteArray,
icon: felt252,
);
fn update(self: @TContractState, identifier: felt252, player_id: felt252, progress: u32,);
}
Expand Down Expand Up @@ -65,7 +65,7 @@ pub mod Achiever {
description: ByteArray,
hidden_description: ByteArray,
image_uri: ByteArray,
icon: ByteArray,
icon: felt252,
) {
self
.achievable
Expand Down
5 changes: 3 additions & 2 deletions packages/achievement/src/tests/test_achievable.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use starknet::testing;

// Internal imports

use achievement::types::icon::AchievementIcon;
use achievement::events::index::{AchievementCreation, AchievementCompletion};
use achievement::tests::mocks::achiever::{Achiever, IAchieverDispatcher, IAchieverDispatcherTrait};
use achievement::tests::setup::setup::{spawn_game, clear_events, Systems, PLAYER};
Expand Down Expand Up @@ -40,7 +41,7 @@ fn test_achievable_create() {
"Description",
"Hidden Description",
"Image",
"Icon"
AchievementIcon::Khanda.into(),
);
let event = starknet::testing::pop_log::<AchievementCreation>(world.contract_address).unwrap();
// FIXME: Cannot check keys because they are shifted due to dojo macros
Expand All @@ -52,7 +53,7 @@ fn test_achievable_create() {
assert_eq!(event.description, "Description");
assert_eq!(event.hidden_description, "Hidden Description");
assert_eq!(event.image_uri, "Image");
assert_eq!(event.icon, "Icon");
assert_eq!(event.icon, AchievementIcon::Khanda.into());
}

#[test]
Expand Down
24 changes: 24 additions & 0 deletions packages/achievement/src/types/icon.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Types

#[derive(Copy, Drop)]
pub enum AchievementIcon {
None,
Khanda,
Dice,
Bolt,
Skull,
}

impl IntoAchievementIconFelt252 of core::Into<AchievementIcon, felt252> {
#[inline]
fn into(self: AchievementIcon) -> felt252 {
match self {
AchievementIcon::None => 0,
AchievementIcon::Khanda => 'KHANDA',
AchievementIcon::Dice => 'DICE',
AchievementIcon::Bolt => 'BOLT',
AchievementIcon::Skull => 'SKULL',
}
}
}

0 comments on commit ada22f6

Please sign in to comment.