Skip to content

Commit

Permalink
Scarb fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
b-j-roberts committed Apr 10, 2024
1 parent f227ed6 commit be42a38
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 41 deletions.
57 changes: 33 additions & 24 deletions onchain/src/art_peace.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -9,38 +9,33 @@ pub mod ArtPeace {
component!(path: TemplateStoreComponent, storage: templates, event: TemplateEvent);

#[abi(embed_v0)]
impl TemplateStoreComponentImpl = TemplateStoreComponent::TemplateStoreImpl<ContractState>;
impl TemplateStoreComponentImpl =
TemplateStoreComponent::TemplateStoreImpl<ContractState>;

#[storage]
struct Storage {
canvas: LegacyMap::<u128, Pixel>,
canvas_width: u128,
canvas_height: u128,
total_pixels: u128,

// Map: user's address -> last time they placed a pixel
last_placed_time: LegacyMap::<ContractAddress, u64>,
time_between_pixels: u64,
// Map: user's address -> amount of extra pixels they have
extra_pixels: LegacyMap::<ContractAddress, u32>,

color_count: u8,
// Map: color index -> color value in RGBA
color_palette: LegacyMap::<u8, u32>,

creation_time: u64,
end_time: u64,
day_index: u32,

// Map: (day_index, quest_id) -> quest contract address
daily_quests: LegacyMap::<(u32, u32), ContractAddress>,
main_quests_count: u32,
// Map: quest index -> quest contract address
main_quests: LegacyMap::<u32, ContractAddress>,

// Map: (day_index, user's address, color index) -> amount of pixels placed
user_pixels_placed: LegacyMap::<(u32, ContractAddress, u8), u32>,

#[substorage(v0)]
templates: TemplateStoreComponent::Storage,
}
Expand Down Expand Up @@ -163,7 +158,9 @@ pub mod ArtPeace {
let day = self.day_index.read();
self
.user_pixels_placed
.write((day, caller, color), self.user_pixels_placed.read((day, caller, color)) + 1);
.write(
(day, caller, color), self.user_pixels_placed.read((day, caller, color)) + 1
);
self.emit(PixelPlaced { placed_by: caller, pos, day, color });
}

Expand Down Expand Up @@ -191,7 +188,11 @@ pub mod ArtPeace {
assert!(color < color_palette_count);
let pixel = Pixel { color, owner: caller };
self.canvas.write(pos, pixel);
self.user_pixels_placed.write((day, caller, color), self.user_pixels_placed.read((day, caller, color)) + 1);
self
.user_pixels_placed
.write(
(day, caller, color), self.user_pixels_placed.read((day, caller, color)) + 1
);
i += 1;
};
self.extra_pixels.write(caller, extra_pixels - pixel_count);
Expand Down Expand Up @@ -389,29 +390,37 @@ pub mod ArtPeace {
assert!(!self.is_template_complete(template_id));
// TODO: ensure template_image matches the template size & hash
let template_metadata: TemplateMetadata = self.get_template(template_id);
let non_zero_width: core::zeroable::NonZero::<u128> = template_metadata.width.try_into().unwrap();
let (template_pos_y, template_pos_x) = DivRem::div_rem(template_metadata.position, non_zero_width);
let non_zero_width: core::zeroable::NonZero::<u128> = template_metadata
.width
.try_into()
.unwrap();
let (template_pos_y, template_pos_x) = DivRem::div_rem(
template_metadata.position, non_zero_width
);
let canvas_width = self.canvas_width.read();
let (mut x, mut y) = (0, 0);
let mut matches = 0;
while y < template_metadata.height {
x = 0;
while x < template_metadata.width {
let pos = template_pos_x + x + (template_pos_y + y) * canvas_width;
let color = *template_image.at((x + y * template_metadata.width).try_into().unwrap());
if color == self.canvas.read(pos).color {
matches += 1;
}
x += 1;
while y < template_metadata
.height {
x = 0;
while x < template_metadata
.width {
let pos = template_pos_x + x + (template_pos_y + y) * canvas_width;
let color = *template_image
.at((x + y * template_metadata.width).try_into().unwrap());
if color == self.canvas.read(pos).color {
matches += 1;
}
x += 1;
};
y += 1;
};
y += 1;
};

// TODO: Allow some threshold?
if matches == template_metadata.width * template_metadata.height {
self.templates.completed_templates.write(template_id, true);
// TODO: Distribute rewards
// self.emit(Event::TemplateEvent::TemplateCompleted { template_id });
// TODO: Distribute rewards
// self.emit(Event::TemplateEvent::TemplateCompleted { template_id });
}
}
}
Expand Down
5 changes: 4 additions & 1 deletion onchain/src/lib.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ mod templates {
pub mod interface;
mod component;

use interface::{TemplateMetadata, ITemplateVerifier, ITemplateStoreDispatcher, ITemplateStoreDispatcherTrait, ITemplateVerifierDispatcher, ITemplateVerifierDispatcherTrait};
use interface::{
TemplateMetadata, ITemplateVerifier, ITemplateStoreDispatcher,
ITemplateStoreDispatcherTrait, ITemplateVerifierDispatcher, ITemplateVerifierDispatcherTrait
};
}

#[cfg(test)]
Expand Down
16 changes: 11 additions & 5 deletions onchain/src/templates/component.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -29,26 +29,32 @@ mod TemplateStoreComponent {
struct TemplateCompleted {
#[key]
id: u32,
// TODO: Users rewarded, ...
// TODO: Users rewarded, ...
}

#[embeddable_as(TemplateStoreImpl)]
impl TemplateStore<TContractState, +HasComponent<TContractState>> of ITemplateStore<ComponentState<TContractState>> {
impl TemplateStore<
TContractState, +HasComponent<TContractState>
> of ITemplateStore<ComponentState<TContractState>> {
fn get_templates_count(self: @ComponentState<TContractState>) -> u32 {
return self.templates_count.read();
}

fn get_template(self: @ComponentState<TContractState>, template_id: u32) -> TemplateMetadata {
fn get_template(
self: @ComponentState<TContractState>, template_id: u32
) -> TemplateMetadata {
return self.templates.read(template_id);
}

fn get_template_hash(self: @ComponentState<TContractState>, template_id: u32) -> felt252 {
let metadata: TemplateMetadata = self.templates.read(template_id);
return metadata.hash;
}

// TODO: Return idx of the template?
fn add_template(ref self: ComponentState<TContractState>, template_metadata: TemplateMetadata) {
fn add_template(
ref self: ComponentState<TContractState>, template_metadata: TemplateMetadata
) {
let template_id = self.templates_count.read();
self.templates.write(template_id, template_metadata);
self.templates_count.write(template_id + 1);
Expand Down
30 changes: 19 additions & 11 deletions onchain/src/tests/art_peace.cairo
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use art_peace::{IArtPeaceDispatcher, IArtPeaceDispatcherTrait};
use art_peace::ArtPeace::InitParams;
use art_peace::templates::{ITemplateStoreDispatcher, ITemplateStoreDispatcherTrait, ITemplateVerifierDispatcher, ITemplateVerifierDispatcherTrait, TemplateMetadata};
use art_peace::templates::{
ITemplateStoreDispatcher, ITemplateStoreDispatcherTrait, ITemplateVerifierDispatcher,
ITemplateVerifierDispatcherTrait, TemplateMetadata
};

use snforge_std as snf;
use snforge_std::{CheatTarget, ContractClassTrait};
Expand Down Expand Up @@ -232,7 +235,9 @@ fn compute_template_hash(template: Span<u8>) -> felt252 {
fn template_full_basic_test() {
let art_peace = IArtPeaceDispatcher { contract_address: deploy_contract() };
let template_store = ITemplateStoreDispatcher { contract_address: art_peace.contract_address };
let template_verifier = ITemplateVerifierDispatcher { contract_address: art_peace.contract_address };
let template_verifier = ITemplateVerifierDispatcher {
contract_address: art_peace.contract_address
};

assert!(template_store.get_templates_count() == 0, "Templates count is not 0");

Expand All @@ -251,11 +256,10 @@ fn template_full_basic_test() {
template_store.add_template(template_metadata);

assert!(template_store.get_templates_count() == 1, "Templates count is not 1");
assert!(template_store.get_template_hash(0) == template_hash, "Template hash is not correct");
assert!(
template_store.get_template_hash(0) == template_hash, "Template hash is not correct"
);
assert!(
template_store.is_template_complete(0) == false, "Template is completed before it should be (base)"
template_store.is_template_complete(0) == false,
"Template is completed before it should be (base)"
);

warp_to_next_available_time(art_peace);
Expand All @@ -266,7 +270,8 @@ fn template_full_basic_test() {
art_peace.place_pixel(pos, color);
template_verifier.complete_template(0, template_image.span());
assert!(
template_store.is_template_complete(0) == false, "Template is completed before it should be (1)"
template_store.is_template_complete(0) == false,
"Template is completed before it should be (1)"
);

warp_to_next_available_time(art_peace);
Expand All @@ -277,7 +282,8 @@ fn template_full_basic_test() {
art_peace.place_pixel(pos, color);
template_verifier.complete_template(0, template_image.span());
assert!(
template_store.is_template_complete(0) == false, "Template is completed before it should be (2)"
template_store.is_template_complete(0) == false,
"Template is completed before it should be (2)"
);

warp_to_next_available_time(art_peace);
Expand All @@ -288,7 +294,8 @@ fn template_full_basic_test() {
art_peace.place_pixel(pos, color);
template_verifier.complete_template(0, template_image.span());
assert!(
template_store.is_template_complete(0) == false, "Template is completed before it should be (3)"
template_store.is_template_complete(0) == false,
"Template is completed before it should be (3)"
);

warp_to_next_available_time(art_peace);
Expand All @@ -299,8 +306,9 @@ fn template_full_basic_test() {
art_peace.place_pixel(pos, color);
template_verifier.complete_template(0, template_image.span());
assert!(
template_store.is_template_complete(0) == true, "Template is not completed after it should be"
template_store.is_template_complete(0) == true,
"Template is not completed after it should be"
);
}

// TODO: test invalid template inputs

0 comments on commit be42a38

Please sign in to comment.