Skip to content

Commit

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

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

fn update(
Expand Down
2 changes: 2 additions & 0 deletions packages/achievement/src/events/index.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ pub struct Trophy {
#[key]
id: felt252,
hidden: bool,
page_count: u8,
points: u16,
group: felt252,
icon: felt252,
Expand All @@ -20,6 +21,7 @@ pub struct Trophy {
pub struct Task {
#[key]
id: felt252,
page: u8,
total: u32,
description: ByteArray,
}
Expand Down
27 changes: 21 additions & 6 deletions packages/achievement/src/events/task.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,22 @@ 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, total: u32, description: ByteArray,) -> Task {
fn new(id: felt252, page: u8, 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, total, description }
Task { id, page, total, description }
}
}

Expand All @@ -32,6 +34,11 @@ 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 @@ -52,32 +59,40 @@ 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, TOTAL, "DESCRIPTION");
let achievement = TaskTrait::new(ID, PAGE, 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, TOTAL, "DESCRIPTION");
TaskTrait::new(0, PAGE, 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, 0, "DESCRIPTION");
TaskTrait::new(ID, PAGE, 0, "DESCRIPTION");
}

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

42 changes: 33 additions & 9 deletions packages/achievement/src/events/trophy.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ 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 @@ -21,6 +22,7 @@ impl TrophyImpl of TrophyTrait {
fn new(
id: felt252,
hidden: bool,
page_count: u8,
points: u16,
group: felt252,
icon: felt252,
Expand All @@ -32,10 +34,11 @@ 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, points, group, icon, title, description, tasks, data }
Trophy { id, hidden, page_count, points, group, icon, title, description, tasks, data }
}
}

Expand All @@ -46,6 +49,11 @@ 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 @@ -72,6 +80,8 @@ mod tests {
// Constants

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

#[test]
fn test_achievement_creation_new() {
let tasks: Array<Task> = array![TaskTrait::new(TASK_ID, TOTAL, "TASK DESCRIPTION"),];
let tasks: Array<Task> = array![TaskTrait::new(TASK_ID, PAGE, TOTAL, "TASK DESCRIPTION"),];
let achievement = TrophyTrait::new(
ID, HIDDEN, POINTS, GROUP, ICON, TITLE, "DESCRIPTION", tasks.span(), "DATA"
ID, HIDDEN, PAGE_COUNT, 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.points, POINTS);
assert_eq!(achievement.group, GROUP);
assert_eq!(achievement.icon, ICON);
Expand All @@ -100,22 +111,35 @@ 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, TOTAL, "TASK DESCRIPTION"),];
TrophyTrait::new(0, HIDDEN, POINTS, GROUP, ICON, TITLE, "DESCRIPTION", tasks.span(), "");
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"),];
TrophyTrait::new(
ID, HIDDEN, 0, 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, TOTAL, "TASK DESCRIPTION"),];
TrophyTrait::new(ID, HIDDEN, POINTS, GROUP, ICON, 0, "DESCRIPTION", tasks.span(), "");
let tasks: Array<Task> = array![TaskTrait::new(TASK_ID, PAGE, TOTAL, "TASK DESCRIPTION"),];
TrophyTrait::new(
ID, HIDDEN, PAGE_COUNT, 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, TOTAL, "TASK DESCRIPTION"),];
TrophyTrait::new(ID, HIDDEN, POINTS, GROUP, ICON, TITLE, "", tasks.span(), "");
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(), "");
}
}

3 changes: 2 additions & 1 deletion packages/achievement/src/store.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ impl StoreImpl of StoreTrait {
self: Store,
id: felt252,
hidden: bool,
page_count: u8,
points: u16,
group: felt252,
icon: felt252,
Expand All @@ -71,7 +72,7 @@ impl StoreImpl of StoreTrait {
data: ByteArray,
) {
let _event: Trophy = TrophyTrait::new(
id, hidden, points, group, icon, title, description, tasks, data
id, hidden, page_count, points, group, icon, title, description, tasks, data
);
emit!(self.world, (_event,));
}
Expand Down
14 changes: 13 additions & 1 deletion packages/achievement/src/tests/mocks/achiever.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ trait IAchiever<TContractState> {
self: @TContractState,
id: felt252,
hidden: bool,
page_count: u8,
points: u16,
group: felt252,
icon: felt252,
Expand Down Expand Up @@ -62,6 +63,7 @@ pub mod Achiever {
self: @ContractState,
id: felt252,
hidden: bool,
page_count: u8,
points: u16,
group: felt252,
icon: felt252,
Expand All @@ -73,7 +75,17 @@ pub mod Achiever {
self
.achievable
.create(
self.world(), id, hidden, points, group, icon, title, description, tasks, data
self.world(),
id,
hidden,
page_count,
points,
group,
icon,
title,
description,
tasks,
data
);
}

Expand Down
8 changes: 6 additions & 2 deletions packages/achievement/src/tests/test_achievable.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ 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 POINTS: u16 = 10;
const TOTAL: u32 = 100;
const COUNT: u32 = 1;
Expand All @@ -33,10 +35,12 @@ 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, TOTAL, "Description")].span();
let tasks = array![TaskTrait::new(TASK_ID, PAGE, TOTAL, "Description")].span();
systems
.achiever
.create(TROPHY_ID, HIDDEN, POINTS, GROUP, ICON, TITLE, "Description", tasks, "");
.create(
TROPHY_ID, HIDDEN, PAGE_COUNT, 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);
Expand Down

0 comments on commit b40f856

Please sign in to comment.