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 Dec 4, 2024
1 parent f25c799 commit 71dfef0
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 67 deletions.
3 changes: 2 additions & 1 deletion onchain/src/canvas.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,8 @@ pub mod Canvas {
let caller = starknet::get_caller_address();
assert(
now - self.last_placed_time.read(caller) >= self.time_between_pixels.read(),
'Pixel not available' );
'Pixel not available'
);

place_basic_pixel_inner(ref self, pos, color, now);
}
Expand Down
41 changes: 21 additions & 20 deletions onchain/src/canvas_factory.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ pub trait ICanvasFactory<TContractState> {
fn get_canvas_class_hash(self: @TContractState) -> ClassHash;
fn set_canvas_class_hash(ref self: TContractState, canvas_class_hash: ClassHash);
fn get_canvas_count(self: @TContractState) -> u64;
fn create_canvas(ref self: TContractState, init_params: Canvas::InitParams) -> (ContractAddress, u64);
fn create_canvas(
ref self: TContractState, init_params: Canvas::InitParams
) -> (ContractAddress, u64);
fn get_canvas(self: @TContractState, canvas_id: u64) -> ContractAddress;
fn favorite_canvas(ref self: TContractState, canvas_id: u64);
fn unfavorite_canvas(ref self: TContractState, canvas_id: u64);
Expand Down Expand Up @@ -91,10 +93,7 @@ pub mod CanvasFactory {
let caller = get_caller_address();
assert(self.owner.read() == caller, 'Only owner can change owner');
self.owner.write(owner);
self.emit(Event::HostChanged(HostChanged {
old_host: caller,
new_host: owner,
}));
self.emit(Event::HostChanged(HostChanged { old_host: caller, new_host: owner, }));
}

fn get_canvas_class_hash(self: @ContractState) -> ClassHash {
Expand All @@ -111,23 +110,31 @@ pub mod CanvasFactory {
self.canvas_count.read()
}

fn create_canvas(ref self: ContractState, init_params: super::Canvas::InitParams) -> (ContractAddress, u64) {
fn create_canvas(
ref self: ContractState, init_params: super::Canvas::InitParams
) -> (ContractAddress, u64) {
// TODO: Serialize before calling this function to defer serialization to the contract input
let mut init_params_serialized = array![];
init_params.serialize(ref init_params_serialized);
let deploy_res = deploy_syscall(self.canvas_class_hash.read(), self.canvas_count.read().into(), init_params_serialized.span(), true);
let deploy_res = deploy_syscall(
self.canvas_class_hash.read(),
self.canvas_count.read().into(),
init_params_serialized.span(),
true
);
if deploy_res.is_err() {
panic!("Failed to deploy canvas contract");
}
let (addr, _response) = deploy_res.unwrap();
let canvas_id = self.canvas_count.read();
self.canvases.write(canvas_id, addr);
self.canvas_count.write(canvas_id + 1);
self.emit(Event::CanvasCreated(CanvasCreated {
canvas_id,
canvas_address: addr,
init_params,
}));
self
.emit(
Event::CanvasCreated(
CanvasCreated { canvas_id, canvas_address: addr, init_params, }
)
);
(addr, canvas_id)
}

Expand All @@ -141,10 +148,7 @@ pub mod CanvasFactory {
return;
}
self.canvas_favorites.write((canvas_id, caller), true);
self.emit(Event::CanvasFavorited(CanvasFavorited {
canvas_id,
user: caller,
}));
self.emit(Event::CanvasFavorited(CanvasFavorited { canvas_id, user: caller, }));
}

fn unfavorite_canvas(ref self: ContractState, canvas_id: u64) {
Expand All @@ -153,10 +157,7 @@ pub mod CanvasFactory {
return;
}
self.canvas_favorites.write((canvas_id, caller), false);
self.emit(Event::CanvasUnfavorited(CanvasUnfavorited {
canvas_id,
user: caller,
}));
self.emit(Event::CanvasUnfavorited(CanvasUnfavorited { canvas_id, user: caller, }));
}
}
}
111 changes: 65 additions & 46 deletions onchain/src/multi_canvas.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,18 @@ pub trait IMultiCanvas<TContractState> {
fn check_valid_pixel(self: @TContractState, canvas_id: u32, pos: u128, color: u8);
fn check_timing(self: @TContractState, now: u64);
fn place_pixel(ref self: TContractState, canvas_id: u32, pos: u128, color: u8, now: u64);
fn place_pixel_xy(ref self: TContractState, canvas_id: u32, x: u128, y: u128, color: u8, now: u64);
fn place_pixel_xy(
ref self: TContractState, canvas_id: u32, x: u128, y: u128, color: u8, now: u64
);
fn favorite_canvas(ref self: TContractState, canvas_id: u32);
fn unfavorite_canvas(ref self: TContractState, canvas_id: u32);
fn get_stencil_count(self: @TContractState, canvas_id: u32) -> u32;
fn get_stencil(self: @TContractState, canvas_id: u32, stencil_id: u32) -> MultiCanvas::StencilMetadata;
fn add_stencil(ref self: TContractState, canvas_id: u32, stencil: MultiCanvas::StencilMetadata) -> u32;
fn get_stencil(
self: @TContractState, canvas_id: u32, stencil_id: u32
) -> MultiCanvas::StencilMetadata;
fn add_stencil(
ref self: TContractState, canvas_id: u32, stencil: MultiCanvas::StencilMetadata
) -> u32;
fn remove_stencil(ref self: TContractState, canvas_id: u32, stencil_id: u32);
fn favorite_stencil(ref self: TContractState, canvas_id: u32, stencil_id: u32);
fn unfavorite_stencil(ref self: TContractState, canvas_id: u32, stencil_id: u32);
Expand Down Expand Up @@ -60,7 +66,7 @@ pub mod MultiCanvas {
pub start_time: u64,
pub end_time: u64,
}

#[derive(Drop, Serde, starknet::Store)]
pub struct CanvasMetadata {
name: felt252,
Expand All @@ -77,7 +83,7 @@ pub mod MultiCanvas {
height: u128,
position: u128
}

#[storage]
struct Storage {
// TODO: Game master to host & control extra pixels & ...
Expand Down Expand Up @@ -283,21 +289,31 @@ pub mod MultiCanvas {
assert(init_params.color_palette.len() >= MIN_COLOR_COUNT, 'Too few colors');
assert(init_params.color_palette.len() <= MAX_COLOR_COUNT, 'Too many colors');
let canvas_id = self.canvas_count.read();
self.canvases.write(canvas_id, CanvasMetadata {
name: init_params.name,
width: init_params.width,
height: init_params.height,
start_time: init_params.start_time,
end_time: init_params.end_time,
});
self
.canvases
.write(
canvas_id,
CanvasMetadata {
name: init_params.name,
width: init_params.width,
height: init_params.height,
start_time: init_params.start_time,
end_time: init_params.end_time,
}
);
self.hosts.write(canvas_id, init_params.host);
self.time_between_pixels.write(canvas_id, init_params.time_between_pixels);
let color_count = init_params.color_palette.len().try_into().unwrap();
self.color_counts.write(canvas_id, color_count);
let mut i: u8 = 0;
while i < color_count {
self.color_palettes.write((canvas_id, i), *init_params.color_palette.at(i.into()));
self.emit(CanvasColorAdded { canvas_id, color_key: i, color: *init_params.color_palette.at(i.into()) });
self
.emit(
CanvasColorAdded {
canvas_id, color_key: i, color: *init_params.color_palette.at(i.into())
}
);
i += 1;
};
self.canvas_count.write(canvas_id + 1);
Expand Down Expand Up @@ -328,28 +344,36 @@ pub mod MultiCanvas {
self.canvases.read(canvas_id).height
}

fn get_last_placed_time(self: @ContractState, canvas_id: u32, user: ContractAddress) -> u64 {
fn get_last_placed_time(
self: @ContractState, canvas_id: u32, user: ContractAddress
) -> u64 {
self.last_placed_times.read((canvas_id, user))
}

fn get_time_between_pixels(self: @ContractState, canvas_id: u32) -> u64 {
self.time_between_pixels.read(canvas_id)
}

fn set_time_between_pixels(ref self: ContractState, canvas_id: u32, time_between_pixels: u64) {
fn set_time_between_pixels(
ref self: ContractState, canvas_id: u32, time_between_pixels: u64
) {
let caller = get_caller_address();
assert(caller == self.hosts.read(canvas_id), 'Only host can change timer');
let old_time = self.time_between_pixels.read(canvas_id);
self.time_between_pixels.write(canvas_id, time_between_pixels);
self.emit(CanvasTimeBetweenPixelsChanged { canvas_id, old_time, new_time: time_between_pixels });
self
.emit(
CanvasTimeBetweenPixelsChanged {
canvas_id, old_time, new_time: time_between_pixels
}
);
}

fn award_user(ref self: ContractState, canvas_id: u32, user: ContractAddress, amount: u32) {
// TODO
// let caller = get_caller_address();
// assert(caller == self.hosts.read(canvas_id), 'Only host can award users');
// self.extra_pixels.write((canvas_id, user), self.extra_pixels.read((canvas_id, user)) + amount);
// self.emit(CanvasHostAwardedUser { canvas_id, user, amount });
fn award_user(ref self: ContractState, canvas_id: u32, user: ContractAddress, amount: u32) {// TODO
// let caller = get_caller_address();
// assert(caller == self.hosts.read(canvas_id), 'Only host can award users');
// self.extra_pixels.write((canvas_id, user), self.extra_pixels.read((canvas_id, user)) + amount);
// self.emit(CanvasHostAwardedUser { canvas_id, user, amount });
}

fn get_color_count(self: @ContractState, canvas_id: u32) -> u8 {
Expand Down Expand Up @@ -401,13 +425,21 @@ pub mod MultiCanvas {
self.check_timing(now);
let caller = starknet::get_caller_address();
assert(
now - self.last_placed_times.read((canvas_id, caller)) >= self.time_between_pixels.read(canvas_id),
'Pixel not available' );
now
- self
.last_placed_times
.read((canvas_id, caller)) >= self
.time_between_pixels
.read(canvas_id),
'Pixel not available'
);

place_basic_pixel_inner(ref self, canvas_id, pos, color, now);
}

fn place_pixel_xy(ref self: ContractState, canvas_id: u32, x: u128, y: u128, color: u8, now: u64) {
fn place_pixel_xy(
ref self: ContractState, canvas_id: u32, x: u128, y: u128, color: u8, now: u64
) {
let pos = x + y * self.canvases.read(canvas_id).width;
self.place_pixel(canvas_id, pos, color, now);
}
Expand All @@ -418,10 +450,7 @@ pub mod MultiCanvas {
return;
}
self.canvas_favorites.write((canvas_id, caller), true);
self.emit(Event::CanvasFavorited(CanvasFavorited {
canvas_id,
user: caller,
}));
self.emit(Event::CanvasFavorited(CanvasFavorited { canvas_id, user: caller, }));
}

fn unfavorite_canvas(ref self: ContractState, canvas_id: u32) {
Expand All @@ -430,10 +459,7 @@ pub mod MultiCanvas {
return;
}
self.canvas_favorites.write((canvas_id, caller), false);
self.emit(Event::CanvasUnfavorited(CanvasUnfavorited {
canvas_id,
user: caller,
}));
self.emit(Event::CanvasUnfavorited(CanvasUnfavorited { canvas_id, user: caller, }));
}

fn get_stencil_count(self: @ContractState, canvas_id: u32) -> u32 {
Expand Down Expand Up @@ -465,20 +491,12 @@ pub mod MultiCanvas {

fn favorite_stencil(ref self: ContractState, canvas_id: u32, stencil_id: u32) {
let caller = get_caller_address();
self.emit(StencilFavorited {
canvas_id,
stencil_id,
user: caller,
});
self.emit(StencilFavorited { canvas_id, stencil_id, user: caller, });
}

fn unfavorite_stencil(ref self: ContractState, canvas_id: u32, stencil_id: u32) {
let caller = get_caller_address();
self.emit(StencilUnfavorited {
canvas_id,
stencil_id,
user: caller,
});
self.emit(StencilUnfavorited { canvas_id, stencil_id, user: caller, });
}
}

Expand All @@ -496,12 +514,13 @@ pub mod MultiCanvas {
}

// TODO: Make the function internal
fn place_basic_pixel_inner(ref self: ContractState, canvas_id: u32, pos: u128, color: u8, now: u64) {
fn place_basic_pixel_inner(
ref self: ContractState, canvas_id: u32, pos: u128, color: u8, now: u64
) {
place_pixel_inner(ref self, canvas_id, pos, color);
let caller = starknet::get_caller_address();
self.last_placed_times.write((canvas_id, caller), now);
self.emit(CanvasBasicPixelPlaced { canvas_id, placed_by: caller, timestamp: now });
}

// TODO: Extra pixels
// TODO: Extra pixels
}

0 comments on commit 71dfef0

Please sign in to comment.