Skip to content

Commit

Permalink
⚡️ Redesign pagination with trophy index
Browse files Browse the repository at this point in the history
  • Loading branch information
bal7hazar committed Oct 29, 2024
1 parent b40f856 commit dbae8b5
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 67 deletions.
7 changes: 2 additions & 5 deletions packages/achievement/src/components/achievable.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ mod AchievableComponent {
world: IWorldDispatcher,
id: felt252,
hidden: bool,
page_count: u8,
index: u8,
points: u16,
group: felt252,
icon: felt252,
Expand All @@ -54,10 +54,7 @@ mod AchievableComponent {
let store: Store = StoreTrait::new(world);

// [Event] Emit achievement creation
store
.create(
id, hidden, page_count, points, group, icon, title, description, tasks, data
);
store.create(id, hidden, index, points, group, icon, title, description, tasks, data);
}

fn update(
Expand Down
3 changes: 1 addition & 2 deletions packages/achievement/src/events/index.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ pub struct Trophy {
#[key]
id: felt252,
hidden: bool,
page_count: u8,
index: u8,
points: u16,
group: felt252,
icon: felt252,
Expand All @@ -21,7 +21,6 @@ pub struct Trophy {
pub struct Task {
#[key]
id: felt252,
page: u8,
total: u32,
description: ByteArray,
}
Expand Down
26 changes: 6 additions & 20 deletions packages/achievement/src/events/task.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,20 @@ pub mod errors {
pub const TROPHY_INVALID_ID: felt252 = 'Task: invalid id';
pub const TROPHY_INVALID_DESCRIPTION: felt252 = 'Task: invalid description';
pub const TROPHY_INVALID_TOTAL: felt252 = 'Task: invalid total';
pub const TROPHY_INVALID_PAGE: felt252 = 'Task: invalid page';
}

// Implementations

#[generate_trait]
impl TaskImpl of TaskTrait {
#[inline]
fn new(id: felt252, page: u8, total: u32, description: ByteArray,) -> Task {
fn new(id: felt252, total: u32, description: ByteArray,) -> Task {
// [Check] Inputs
TaskAssert::assert_valid_id(id);
TaskAssert::assert_valid_page(page);
TaskAssert::assert_valid_total(total);
TaskAssert::assert_valid_description(@description);
// [Return] Task
Task { id, page, total, description }
Task { id, total, description }
}
}

Expand All @@ -34,11 +32,6 @@ impl TaskAssert of AssertTrait {
assert(id != 0, errors::TROPHY_INVALID_ID);
}

#[inline]
fn assert_valid_page(page: u8) {
assert(page > 0, errors::TROPHY_INVALID_PAGE);
}

#[inline]
fn assert_valid_total(total: u32) {
assert(total > 0, errors::TROPHY_INVALID_TOTAL);
Expand All @@ -59,40 +52,33 @@ mod tests {
// Constants

const ID: felt252 = 'TROPHY';
const PAGE: u8 = 1;
const TOTAL: u32 = 100;

#[test]
fn test_achievement_creation_new() {
let achievement = TaskTrait::new(ID, PAGE, TOTAL, "DESCRIPTION");
let achievement = TaskTrait::new(ID, TOTAL, "DESCRIPTION");
assert_eq!(achievement.id, ID);
assert_eq!(achievement.page, PAGE);
assert_eq!(achievement.total, TOTAL);
assert_eq!(achievement.description, "DESCRIPTION");
}

#[test]
#[should_panic(expected: ('Task: invalid id',))]
fn test_achievement_creation_new_invalid_id() {
TaskTrait::new(0, PAGE, TOTAL, "DESCRIPTION");
TaskTrait::new(0, TOTAL, "DESCRIPTION");
}

#[test]
#[should_panic(expected: ('Task: invalid page',))]
fn test_achievement_creation_new_invalid_page() {
TaskTrait::new(ID, 0, TOTAL, "DESCRIPTION");
}

#[test]
#[should_panic(expected: ('Task: invalid total',))]
fn test_achievement_creation_new_invalid_total() {
TaskTrait::new(ID, PAGE, 0, "DESCRIPTION");
TaskTrait::new(ID, 0, "DESCRIPTION");
}

#[test]
#[should_panic(expected: ('Task: invalid description',))]
fn test_achievement_creation_new_invalid_description() {
TaskTrait::new(ID, PAGE, TOTAL, "");
TaskTrait::new(ID, TOTAL, "");
}
}

41 changes: 12 additions & 29 deletions packages/achievement/src/events/trophy.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ pub mod errors {
pub const TROPHY_INVALID_TITLE: felt252 = 'Trophy: invalid title';
pub const TROPHY_INVALID_DESCRIPTION: felt252 = 'Trophy: invalid desc.';
pub const TROPHY_INVALID_TASKS: felt252 = 'Trophy: invalid tasks.';
pub const TROPHY_INVALID_PAGE_COUNT: felt252 = 'Trophy: invalid page count';
}

// Implementations
Expand All @@ -22,7 +21,7 @@ impl TrophyImpl of TrophyTrait {
fn new(
id: felt252,
hidden: bool,
page_count: u8,
index: u8,
points: u16,
group: felt252,
icon: felt252,
Expand All @@ -34,11 +33,10 @@ impl TrophyImpl of TrophyTrait {
// [Check] Inputs
// [Info] We don't check points here, leave free the game to decide
TrophyAssert::assert_valid_id(id);
TrophyAssert::assert_valid_page_count(page_count);
TrophyAssert::assert_valid_title(title);
TrophyAssert::assert_valid_description(@description);
// [Return] Trophy
Trophy { id, hidden, page_count, points, group, icon, title, description, tasks, data }
Trophy { id, hidden, index, points, group, icon, title, description, tasks, data }
}
}

Expand All @@ -49,11 +47,6 @@ impl TrophyAssert of AssertTrait {
assert(id != 0, errors::TROPHY_INVALID_ID);
}

#[inline]
fn assert_valid_page_count(page_count: u8) {
assert(page_count > 0, errors::TROPHY_INVALID_PAGE_COUNT);
}

#[inline]
fn assert_valid_title(title: felt252) {
assert(title != 0, errors::TROPHY_INVALID_TITLE);
Expand All @@ -80,8 +73,7 @@ mod tests {
// Constants

const ID: felt252 = 'TROPHY';
const PAGE_COUNT: u8 = 1;
const PAGE: u8 = 1;
const INDEX: u8 = 0;
const GROUP: felt252 = 'GROUP';
const HIDDEN: bool = false;
const POINTS: u16 = 100;
Expand All @@ -92,13 +84,13 @@ mod tests {

#[test]
fn test_achievement_creation_new() {
let tasks: Array<Task> = array![TaskTrait::new(TASK_ID, PAGE, TOTAL, "TASK DESCRIPTION"),];
let tasks: Array<Task> = array![TaskTrait::new(TASK_ID, TOTAL, "TASK DESCRIPTION"),];
let achievement = TrophyTrait::new(
ID, HIDDEN, PAGE_COUNT, POINTS, GROUP, ICON, TITLE, "DESCRIPTION", tasks.span(), "DATA"
ID, HIDDEN, INDEX, POINTS, GROUP, ICON, TITLE, "DESCRIPTION", tasks.span(), "DATA"
);
assert_eq!(achievement.id, ID);
assert_eq!(achievement.hidden, HIDDEN);
assert_eq!(achievement.page_count, PAGE_COUNT);
assert_eq!(achievement.index, INDEX);
assert_eq!(achievement.points, POINTS);
assert_eq!(achievement.group, GROUP);
assert_eq!(achievement.icon, ICON);
Expand All @@ -111,35 +103,26 @@ mod tests {
#[test]
#[should_panic(expected: ('Trophy: invalid id',))]
fn test_achievement_creation_new_invalid_id() {
let tasks: Array<Task> = array![TaskTrait::new(TASK_ID, PAGE, TOTAL, "TASK DESCRIPTION"),];
TrophyTrait::new(
0, HIDDEN, PAGE_COUNT, POINTS, GROUP, ICON, TITLE, "DESCRIPTION", tasks.span(), ""
);
}

#[test]
#[should_panic(expected: ('Trophy: invalid page count',))]
fn test_achievement_creation_new_invalid_page_count() {
let tasks: Array<Task> = array![TaskTrait::new(TASK_ID, PAGE, TOTAL, "TASK DESCRIPTION"),];
let tasks: Array<Task> = array![TaskTrait::new(TASK_ID, TOTAL, "TASK DESCRIPTION"),];
TrophyTrait::new(
ID, HIDDEN, 0, POINTS, GROUP, ICON, TITLE, "DESCRIPTION", tasks.span(), ""
0, HIDDEN, INDEX, POINTS, GROUP, ICON, TITLE, "DESCRIPTION", tasks.span(), ""
);
}

#[test]
#[should_panic(expected: ('Trophy: invalid title',))]
fn test_achievement_creation_new_invalid_title() {
let tasks: Array<Task> = array![TaskTrait::new(TASK_ID, PAGE, TOTAL, "TASK DESCRIPTION"),];
let tasks: Array<Task> = array![TaskTrait::new(TASK_ID, TOTAL, "TASK DESCRIPTION"),];
TrophyTrait::new(
ID, HIDDEN, PAGE_COUNT, POINTS, GROUP, ICON, 0, "DESCRIPTION", tasks.span(), ""
ID, HIDDEN, INDEX, POINTS, GROUP, ICON, 0, "DESCRIPTION", tasks.span(), ""
);
}

#[test]
#[should_panic(expected: ('Trophy: invalid desc.',))]
fn test_achievement_creation_new_invalid_description() {
let tasks: Array<Task> = array![TaskTrait::new(TASK_ID, PAGE, TOTAL, "TASK DESCRIPTION"),];
TrophyTrait::new(ID, HIDDEN, PAGE_COUNT, POINTS, GROUP, ICON, TITLE, "", tasks.span(), "");
let tasks: Array<Task> = array![TaskTrait::new(TASK_ID, TOTAL, "TASK DESCRIPTION"),];
TrophyTrait::new(ID, HIDDEN, INDEX, POINTS, GROUP, ICON, TITLE, "", tasks.span(), "");
}
}

4 changes: 2 additions & 2 deletions packages/achievement/src/store.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ impl StoreImpl of StoreTrait {
self: Store,
id: felt252,
hidden: bool,
page_count: u8,
index: u8,
points: u16,
group: felt252,
icon: felt252,
Expand All @@ -72,7 +72,7 @@ impl StoreImpl of StoreTrait {
data: ByteArray,
) {
let _event: Trophy = TrophyTrait::new(
id, hidden, page_count, points, group, icon, title, description, tasks, data
id, hidden, index, points, group, icon, title, description, tasks, data
);
emit!(self.world, (_event,));
}
Expand Down
6 changes: 3 additions & 3 deletions packages/achievement/src/tests/mocks/achiever.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ trait IAchiever<TContractState> {
self: @TContractState,
id: felt252,
hidden: bool,
page_count: u8,
index: u8,
points: u16,
group: felt252,
icon: felt252,
Expand Down Expand Up @@ -63,7 +63,7 @@ pub mod Achiever {
self: @ContractState,
id: felt252,
hidden: bool,
page_count: u8,
index: u8,
points: u16,
group: felt252,
icon: felt252,
Expand All @@ -78,7 +78,7 @@ pub mod Achiever {
self.world(),
id,
hidden,
page_count,
index,
points,
group,
icon,
Expand Down
10 changes: 4 additions & 6 deletions packages/achievement/src/tests/test_achievable.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ use achievement::tests::setup::setup::{spawn_game, clear_events, Systems, PLAYER
const TROPHY_ID: felt252 = 'TROPHY';
const TASK_ID: felt252 = 'TASK';
const HIDDEN: bool = false;
const PAGE_COUNT: u8 = 1;
const PAGE: u8 = 1;
const INDEX: u8 = 0;
const POINTS: u16 = 10;
const TOTAL: u32 = 100;
const COUNT: u32 = 1;
Expand All @@ -35,15 +34,14 @@ const TITLE: felt252 = 'Title';
fn test_achievable_create() {
let (world, systems, _context) = spawn_game();
clear_events(world.contract_address);
let tasks = array![TaskTrait::new(TASK_ID, PAGE, TOTAL, "Description")].span();
let tasks = array![TaskTrait::new(TASK_ID, TOTAL, "Description")].span();
systems
.achiever
.create(
TROPHY_ID, HIDDEN, PAGE_COUNT, POINTS, GROUP, ICON, TITLE, "Description", tasks, ""
);
.create(TROPHY_ID, HIDDEN, INDEX, POINTS, GROUP, ICON, TITLE, "Description", tasks, "");
let event = starknet::testing::pop_log::<Trophy>(world.contract_address).unwrap();
// FIXME: Cannot check keys because they are shifted due to dojo macros
assert_eq!(event.hidden, HIDDEN);
assert_eq!(event.index, INDEX);
assert_eq!(event.points, POINTS);
assert_eq!(event.group, GROUP);
assert_eq!(event.title, TITLE);
Expand Down

0 comments on commit dbae8b5

Please sign in to comment.