Skip to content

Commit

Permalink
♻️ Update achievements
Browse files Browse the repository at this point in the history
  • Loading branch information
bal7hazar committed Oct 25, 2024
1 parent ada22f6 commit 6c2a61a
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 138 deletions.
19 changes: 6 additions & 13 deletions packages/achievement/src/components/achievable.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ mod AchievableComponent {

// Dojo imports

use dojo::world::{IWorldDispatcher, IWorldProvider};
use dojo::contract::{IContract, IContractDispatcher, IContractDispatcherTrait};
use dojo::world::IWorldDispatcher;

// Internal imports

Expand All @@ -34,7 +33,7 @@ mod AchievableComponent {

#[generate_trait]
impl InternalImpl<
TContractState, +IContract<TContractState>, +HasComponent<TContractState>
TContractState, +HasComponent<TContractState>
> of InternalTrait<TContractState> {
fn create(
self: @ComponentState<TContractState>,
Expand All @@ -47,20 +46,16 @@ mod AchievableComponent {
hidden_title: ByteArray,
description: ByteArray,
hidden_description: ByteArray,
image_uri: ByteArray,
icon: felt252,
icon_style: felt252,
) {
// [Setup] Store
let store: Store = StoreTrait::new(world);

// [Event] Emit achievement creation
let contract_address = get_contract_address();
let contract = IContractDispatcher { contract_address };
let namespace = contract.namespace_hash();
let time: u64 = get_block_timestamp();
store
.create(
namespace,
identifier,
hidden,
points,
Expand All @@ -69,8 +64,8 @@ mod AchievableComponent {
hidden_title,
description,
hidden_description,
image_uri,
icon,
icon_style,
time
);
}
Expand All @@ -80,16 +75,14 @@ mod AchievableComponent {
world: IWorldDispatcher,
identifier: felt252,
player_id: felt252,
progress: u32,
count: u32,
) {
// [Setup] Store
let store: Store = StoreTrait::new(world);

// [Event] Emit achievement completion
let contract_address = get_contract_address();
let namespace = IContractDispatcher { contract_address }.namespace_hash();
let time: u64 = get_block_timestamp();
store.update(namespace, identifier, player_id, progress, time);
store.update(player_id, identifier, count, time);
}
}
}
29 changes: 6 additions & 23 deletions packages/achievement/src/events/completion.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,17 @@ pub mod errors {
impl AchievementCompletionImpl of AchievementCompletionTrait {
#[inline]
fn new(
namespace: felt252, identifier: felt252, player_id: felt252, progress: u32, time: u64,
identifier: felt252, player_id: felt252, count: u32, time: u64,
) -> AchievementCompletion {
// [Check] Inputs
AchievementCompletionAssert::assert_valid_namespace(namespace);
AchievementCompletionAssert::assert_valid_identifier(identifier);
// [Return] Achievement
AchievementCompletion { namespace, identifier, player_id, progress, time }
AchievementCompletion { identifier, player_id, count, time }
}
}

#[generate_trait]
impl AchievementCompletionAssert of AssertTrait {
#[inline]
fn assert_valid_namespace(namespace: felt252) {
assert(namespace != 0, errors::ACHIEVEMENT_INVALID_NAMESPACE);
}

#[inline]
fn assert_valid_identifier(identifier: felt252) {
assert(identifier != 0, errors::ACHIEVEMENT_INVALID_ACHIEVEMENT);
Expand All @@ -47,34 +41,23 @@ mod tests {

// Constants

const NAMESPACE: felt252 = 'NAMESPACE';
const IDENTIFIER: felt252 = 'ACHIEVEMENT';
const PLAYER_ID: felt252 = 'PLAYER_ID';
const PROGRESS: u32 = 100;
const COUNT: u32 = 100;
const TIME: u64 = 1000000000;

#[test]
fn test_achievement_completion_new() {
let completion = AchievementCompletionTrait::new(
NAMESPACE, IDENTIFIER, PLAYER_ID, PROGRESS, TIME,
);
let completion = AchievementCompletionTrait::new(IDENTIFIER, PLAYER_ID, COUNT, TIME,);

assert_eq!(completion.namespace, NAMESPACE);
assert_eq!(completion.identifier, IDENTIFIER);
assert_eq!(completion.player_id, PLAYER_ID);
assert_eq!(completion.progress, PROGRESS);
assert_eq!(completion.count, COUNT);
assert_eq!(completion.time, TIME);
}

#[test]
#[should_panic(expected: ('Achievement: invalid namespace',))]
fn test_achievement_completion_new_invalid_namespace() {
AchievementCompletionTrait::new(0, IDENTIFIER, PLAYER_ID, PROGRESS, TIME);
}

#[test]
#[should_panic(expected: ('Achievement: invalid identifier',))]
fn test_achievement_completion_new_invalid_identifier() {
AchievementCompletionTrait::new(NAMESPACE, 0, PLAYER_ID, PROGRESS, TIME);
AchievementCompletionTrait::new(0, PLAYER_ID, COUNT, TIME);
}
}
46 changes: 7 additions & 39 deletions packages/achievement/src/events/creation.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ pub mod errors {
impl AchievementCreationImpl of AchievementCreationTrait {
#[inline]
fn new(
namespace: felt252,
identifier: felt252,
hidden: bool,
points: u16,
Expand All @@ -26,18 +25,16 @@ impl AchievementCreationImpl of AchievementCreationTrait {
hidden_title: ByteArray,
description: ByteArray,
hidden_description: ByteArray,
image_uri: 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_namespace(namespace);
AchievementCreationAssert::assert_valid_identifier(identifier);
AchievementCreationAssert::assert_valid_title(@title);
// [Return] Achievement
AchievementCreation {
namespace,
identifier,
hidden,
points,
Expand All @@ -46,20 +43,15 @@ impl AchievementCreationImpl of AchievementCreationTrait {
hidden_title,
description,
hidden_description,
image_uri,
icon,
icon_style,
time
}
}
}

#[generate_trait]
impl AchievementCreationAssert of AssertTrait {
#[inline]
fn assert_valid_namespace(namespace: felt252) {
assert(namespace != 0, errors::ACHIEVEMENT_INVALID_NAMESPACE);
}

#[inline]
fn assert_valid_identifier(identifier: felt252) {
assert(identifier != 0, errors::ACHIEVEMENT_INVALID_ACHIEVEMENT);
Expand All @@ -79,17 +71,15 @@ mod tests {

// Constants

const NAMESPACE: felt252 = 'NAMESPACE';
const IDENTIFIER: felt252 = 'ACHIEVEMENT';
const HIDDEN: bool = false;
const POINTS: u16 = 100;
const TOTAL: u32 = 100;
const ICON: felt252 = 'ICON';

const ICON_STYLE: felt252 = 'ICON_STYLE';
#[test]
fn test_achievement_creation_new() {
let achievement = AchievementCreationTrait::new(
NAMESPACE,
IDENTIFIER,
HIDDEN,
POINTS,
Expand All @@ -98,11 +88,10 @@ mod tests {
"HIDDEN_TITLE",
"DESCRIPTION",
"HIDDEN_DESCRIPTION",
"IMAGE_URI",
ICON,
ICON_STYLE,
1000000000,
);
assert_eq!(achievement.namespace, NAMESPACE);
assert_eq!(achievement.identifier, IDENTIFIER);
assert_eq!(achievement.hidden, HIDDEN);
assert_eq!(achievement.points, POINTS);
Expand All @@ -111,35 +100,15 @@ 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]
#[should_panic(expected: ('Achievement: invalid namespace',))]
fn test_achievement_creation_new_invalid_namespace() {
AchievementCreationTrait::new(
0,
IDENTIFIER,
HIDDEN,
POINTS,
TOTAL,
"TITLE",
"HIDDEN_TITLE",
"DESCRIPTION",
"HIDDEN_DESCRIPTION",
"IMAGE_URI",
ICON,
1000000000
);
assert_eq!(achievement.time, 1000000000);
}

#[test]
#[should_panic(expected: ('Achievement: invalid id',))]
fn test_achievement_creation_new_invalid_identifier() {
AchievementCreationTrait::new(
NAMESPACE,
0,
HIDDEN,
POINTS,
Expand All @@ -148,8 +117,8 @@ mod tests {
"HIDDEN_TITLE",
"DESCRIPTION",
"HIDDEN_DESCRIPTION",
"IMAGE_URI",
ICON,
ICON_STYLE,
1000000000
);
}
Expand All @@ -158,7 +127,6 @@ mod tests {
#[should_panic(expected: ('Achievement: invalid title',))]
fn test_achievement_creation_new_invalid_title() {
AchievementCreationTrait::new(
NAMESPACE,
IDENTIFIER,
HIDDEN,
POINTS,
Expand All @@ -167,8 +135,8 @@ mod tests {
"HIDDEN_TITLE",
"DESCRIPTION",
"HIDDEN_DESCRIPTION",
"IMAGE_URI",
ICON,
ICON_STYLE,
1000000000
);
}
Expand Down
10 changes: 3 additions & 7 deletions packages/achievement/src/events/index.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
#[dojo::model]
#[dojo::event]
pub struct AchievementCreation {
#[key]
namespace: felt252,
#[key]
identifier: felt252,
hidden: bool,
Expand All @@ -13,8 +11,8 @@ pub struct AchievementCreation {
hidden_title: ByteArray,
description: ByteArray,
hidden_description: ByteArray,
image_uri: ByteArray,
icon: felt252,
icon_style: felt252,
time: u64,
}

Expand All @@ -23,11 +21,9 @@ pub struct AchievementCreation {
#[dojo::event]
pub struct AchievementCompletion {
#[key]
namespace: felt252,
player_id: felt252,
#[key]
identifier: felt252,
#[key]
player_id: felt252,
progress: u32,
count: u32,
time: u64,
}
4 changes: 0 additions & 4 deletions packages/achievement/src/lib.cairo
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
mod constants;
mod store;

mod types {
mod icon;
}

mod models {
mod index;
mod game;
Expand Down
17 changes: 4 additions & 13 deletions packages/achievement/src/store.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ impl StoreImpl of StoreTrait {
#[inline]
fn create(
self: Store,
namespace: felt252,
identifier: felt252,
hidden: bool,
points: u16,
Expand All @@ -68,12 +67,11 @@ impl StoreImpl of StoreTrait {
hidden_title: ByteArray,
description: ByteArray,
hidden_description: ByteArray,
image_uri: ByteArray,
icon: felt252,
icon_style: felt252,
time: u64,
) {
let _event: AchievementCreation = AchievementCreationTrait::new(
namespace,
identifier,
hidden,
points,
Expand All @@ -82,24 +80,17 @@ impl StoreImpl of StoreTrait {
hidden_title,
description,
hidden_description,
image_uri,
icon,
icon_style,
time
);
emit!(self.world, (_event,));
}

#[inline]
fn update(
self: Store,
namespace: felt252,
identifier: felt252,
player_id: felt252,
progress: u32,
time: u64,
) {
fn update(self: Store, player_id: felt252, identifier: felt252, count: u32, time: u64,) {
let _event: AchievementCompletion = AchievementCompletionTrait::new(
namespace, identifier, player_id, progress, time
player_id, identifier, count, time
);
emit!(self.world, (_event,));
}
Expand Down
Loading

0 comments on commit 6c2a61a

Please sign in to comment.