Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: create a increase_day_index function #23

Merged
merged 5 commits into from
Apr 10, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions onchain/src/art_peace.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ pub mod ArtPeace {
creation_time: u64,
end_time: u64,
day_index: u32,
start_day_time: u64,
// Map: (day_index, quest_id) -> quest contract address
daily_quests: LegacyMap::<(u32, u32), ContractAddress>,
main_quests_count: u32,
Expand All @@ -43,11 +44,18 @@ pub mod ArtPeace {
#[event]
#[derive(Drop, starknet::Event)]
enum Event {
Newday: NewDay,
PixelPlaced: PixelPlaced,
#[flat]
TemplateEvent: TemplateStoreComponent::Event,
}

#[derive(Drop, starknet::Event)]
struct NewDay {
#[key]
day_index: u32,
}

#[derive(Drop, starknet::Event)]
struct PixelPlaced {
#[key]
Expand All @@ -71,6 +79,8 @@ pub mod ArtPeace {
pub main_quests: Span<ContractAddress>,
}

const day_time: u64 = consteval_int!(60 * 60 * 24);

#[constructor]
fn constructor(ref self: ContractState, init_params: InitParams) {
self.canvas_width.write(init_params.canvas_width);
Expand All @@ -88,6 +98,7 @@ pub mod ArtPeace {
};

self.creation_time.write(starknet::get_block_timestamp());
self.start_day_time.write(starknet::get_block_timestamp());
b-j-roberts marked this conversation as resolved.
Show resolved Hide resolved
self.end_time.write(init_params.end_time);
self.day_index.write(0);

Expand Down Expand Up @@ -346,6 +357,17 @@ pub mod ArtPeace {
}
}

fn increase_day_index(ref self: ContractState) {
let block_timestamp = starknet::get_block_timestamp();
let start_day_time = self.start_day_time.read();

assert(block_timestamp > start_day_time + day_time, 'day has not passed');

self.day_index.write(self.day_index.read() + 1);
self.start_day_time.write(block_timestamp);
self.emit(NewDay { day_index: self.day_index.read() });
}

fn get_user_pixels_placed(self: @ContractState, user: ContractAddress) -> u32 {
let mut i = 0;
let mut total = 0;
Expand Down Expand Up @@ -425,3 +447,4 @@ pub mod ArtPeace {
}
}
}

3 changes: 3 additions & 0 deletions onchain/src/interface.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ pub trait IArtPeace<TContractState> {
fn claim_today_quest(ref self: TContractState, quest_id: u32);
fn claim_main_quest(ref self: TContractState, quest_id: u32);

// Start a new day
fn increase_day_index(ref self: TContractState);

// Stats
fn get_user_pixels_placed(self: @TContractState, user: starknet::ContractAddress) -> u32;
fn get_user_pixels_placed_day(
Expand Down