Skip to content

Commit

Permalink
refactor: use dojo v0.3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
credence0x committed Oct 12, 2023
1 parent 759668f commit ea632ae
Show file tree
Hide file tree
Showing 32 changed files with 646 additions and 571 deletions.
2 changes: 1 addition & 1 deletion .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

npx lint-staged
# npx lint-staged
4 changes: 2 additions & 2 deletions packages/contracts/Scarb.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ version = "0.1.0"
sierra-replace-ids = true

[dependencies]
dojo = { git = "https://github.com/dojoengine/dojo" }
dojo = { git = "https://github.com/dojoengine/dojo", tag="v0.3.0-rc7" }
orion = { git = "https://github.com/gizatechxyz/orion" }
cubit = { git = "https://github.com/influenceth/cubit" }
cubit = { git = "https://github.com/influenceth/cubit.git" }

[[target.dojo]]

Expand Down
10 changes: 5 additions & 5 deletions packages/contracts/scripts/default_auth.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ pushd $(dirname "$0")/..

export WORLD_ADDRESS="0x9b501a522a8d26886ef604bc0b8bf9b12ffcdf88adadc0e6e575f6271c6bde";

# enable system -> component authorizations
# enable system -> model authorizations

COMPONENTS=("Claim" "GameConfig" "Item" "ItemPrototype" "Name" "OwnedBy" "Position" "PositionOccupation" "Recipe" "RecipeReverseLookup" "Stake")
MODELS=("Claim" "GameConfig" "Item" "ItemPrototype" "Name" "OwnedBy" "Position" "PositionOccupation" "Recipe" "RecipeReverseLookup" "Stake")
SYSTEMS=("Build" "Craft" "MakeStake" "Transfer" "MakeClaim" "AddName" "Occurence" "Mine" "Init" "Init2" "CreativeBuild " "BulkStake" "BulkTransfer")

for system in ${SYSTEMS[@]}; do
for component in ${COMPONENTS[@]}; do
echo "Setting authorization for $system -> $component"
sozo auth writer $component $system --world $WORLD_ADDRESS
for model in ${MODELS[@]}; do
echo "Setting authorization for $system -> $model"
sozo auth writer $model $system --world $WORLD_ADDRESS
done
done

Expand Down
2 changes: 1 addition & 1 deletion packages/contracts/src/lib.cairo
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
mod components;
mod models;
mod systems;
mod alias;
mod prototypes;
Expand Down
2 changes: 1 addition & 1 deletion packages/contracts/src/libraries/terrain.cairo
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use comcraft::components::position::{Coord, VoxelCoord};
use comcraft::models::position::{Coord, VoxelCoord};
use comcraft::constants::{Biome, STRUCTURE_CHUNK, STRUCTURE_CHUNK_CENTER};
use comcraft::prototypes::blocks::{AirID, GrassID, DirtID, LogID, StoneID, SandID, GlassID, WaterID, CobblestoneID, MossyCobblestoneID, CoalID, CraftingID, IronID, GoldID, DiamondID, LeavesID, PlanksID, RedFlowerID, GrassPlantID, OrangeFlowerID, MagentaFlowerID, LightBlueFlowerID, LimeFlowerID, PinkFlowerID, GrayFlowerID, LightGrayFlowerID, CyanFlowerID, PurpleFlowerID, BlueFlowerID, GreenFlowerID, BlackFlowerID, KelpID, WoolID, OrangeWoolID, MagentaWoolID, LightBlueWoolID, YellowWoolID, LimeWoolID, PinkWoolID, GrayWoolID, LightGrayWoolID, CyanWoolID, PurpleWoolID, BlueWoolID, BrownWoolID, GreenWoolID, RedWoolID, BlackWoolID, SpongeID, SnowID, ClayID, BedrockID, BricksID};

Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use starknet::ContractAddress;

#[derive(Component, Copy, Drop, Serde, SerdeLen)]
#[derive(Model, Copy, Drop, Serde)]
struct Claim {
#[key]
id: u256,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#[derive(Component, Copy, Drop, Serde, SerdeLen)]
#[derive(Model, Copy, Drop, Serde)]
struct GameConfig{
#[key]
id: u256,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#[derive(Component, Copy, Drop, Serde, SerdeLen)]
#[derive(Model, Copy, Drop, Serde)]
struct Item {
#[key]
id: u256,
value: felt252
}

#[derive(Component, Copy, Drop, Serde, SerdeLen)]
#[derive(Model, Copy, Drop, Serde)]
struct ItemPrototype {
#[key]
id: u256,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#[derive(Component, Copy, Drop, Serde, SerdeLen)]
#[derive(Model, Copy, Drop, Serde)]
struct Name {
#[key]
id: u256,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use starknet::ContractAddress;

#[derive(Component, Copy, Drop, Serde, SerdeLen)]
#[derive(Model, Copy, Drop, Serde)]
struct OwnedBy {
#[key]
id: u256,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use dojo::serde::SerdeLen;

use orion::numbers::signed_integer::i32::{i32, i32Impl};

Expand Down Expand Up @@ -36,7 +35,7 @@ impl VoxelCoordImpl of VoxelCoordTrait {
}
}

#[derive(Component, Copy, Drop, Serde, SerdeLen)]
#[derive(Model, Copy, Drop, Serde)]
struct Position {
#[key]
id: u256,
Expand All @@ -59,7 +58,7 @@ impl PositionIntoVoxelCoord of Into<Position, VoxelCoord> {
}


#[derive(Component, Copy, Drop, Serde, SerdeLen)]
#[derive(Model, Copy, Drop, Serde)]
struct PositionOccupation {
#[key]
hash: felt252,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#[derive(Component, Copy, Drop, Serde, SerdeLen)]
#[derive(Model, Copy, Drop, Serde)]
struct Recipe {
#[key]
id: u256,
value: felt252
}

#[derive(Component, Copy, Drop, Serde, SerdeLen)]
#[derive(Model, Copy, Drop, Serde)]
struct RecipeReverseLookup {
id: u256,
#[key]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#[derive(Component, Copy, Drop, Serde, SerdeLen)]
#[derive(Model, Copy, Drop, Serde)]
struct Stake {
#[key]
id: u256,
Expand Down
2 changes: 1 addition & 1 deletion packages/contracts/src/prototypes/recipes.cairo
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use comcraft::prototypes::blocks;
use comcraft::components::recipe::{Recipe, RecipeReverseLookup};
use comcraft::models::recipe::{Recipe, RecipeReverseLookup};

use dojo::world::{IWorldDispatcher, IWorldDispatcherTrait};

Expand Down
14 changes: 5 additions & 9 deletions packages/contracts/src/systems.cairo
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
mod build;
mod claim;
mod stake;
mod initialization;
mod minecraft;
mod name;
mod occurrence;
mod transfer;
mod mine;
mod init;
mod init2;
mod creative_build;
mod craft;
mod bulk_transfer;
mod bulk_stake;
mod stake;
mod transfer;
147 changes: 105 additions & 42 deletions packages/contracts/src/systems/build.cairo
Original file line number Diff line number Diff line change
@@ -1,58 +1,121 @@
#[system]
mod Build {
#[dojo::contract]
mod build_systems {
use comcraft::alias::ID;
use comcraft::components::position::{Position, PositionOccupation, VoxelCoord, VoxelCoordTrait};
use comcraft::components::owned_by::OwnedBy;
use comcraft::components::claim::Claim;
use comcraft::systems::claim::{MakeClaim};
use comcraft::models::position::{Position, PositionOccupation, VoxelCoord, VoxelCoordTrait};
use comcraft::models::owned_by::OwnedBy;
use comcraft::models::claim::Claim;
use comcraft::models::item::Item;
use comcraft::models::config::GameConfig;

use dojo::world::Context;
use comcraft::constants::GodID;

use comcraft::systems::claim::claim_systems;

use starknet::contract_address_const;

trait IBuildSystems<TContractState> {
fn build(
self: @TContractState, world: IWorldDispatcher, block_id: ID, coord: VoxelCoord
);

fn execute(ctx: Context, block_id: ID, coord: VoxelCoord ) {
let block_owned_by = get!(ctx.world, block_id, OwnedBy);
assert(block_owned_by.address == ctx.origin, 'block not owned by player');

// Require no other ECS blocks at this position except Air
let VoxelCoord { x, y, z } = coord;
let position_occupation = get!(
ctx.world,
coord.hash(),
PositionOccupation
fn creative_build(
self: @TContractState,
world: IWorldDispatcher,
block_id: ID,
coord: VoxelCoord
);
if position_occupation.occupied_by_non_air != 0 {
assert(false, 'cant build at occupied coord');
}
}

// Check claim in chunk
let claim = MakeClaim::get_claim_at_coord(ctx.world, coord);
if claim.claimer != contract_address_const::<0>() && claim.claimer != block_owned_by.address {
assert(false, 'cant build in claimed chunk');
#[external(v0)]
impl BuildSystemsImpl of IBuildSystems<ContractState> {

fn build(
self: @ContractState, world: IWorldDispatcher, block_id: ID, coord: VoxelCoord
) {
let block_owned_by = get!(world, block_id, OwnedBy);
assert(block_owned_by.address == starknet::get_caller_address(), 'block not owned by player');

// Require no other ECS blocks at this position except Air
let VoxelCoord { x, y, z } = coord;
let position_occupation = get!(
world,
coord.hash(),
PositionOccupation
);
if position_occupation.occupied_by_non_air != 0 {
assert(false, 'cant build at occupied coord');
}

// Check claim in chunk
let claim = claim_systems::get_claim_at_coord(world, coord);
if claim.claimer != contract_address_const::<0>() && claim.claimer != block_owned_by.address {
assert(false, 'cant build in claimed chunk');
}

set!(world, (
PositionOccupation {
hash: coord.hash(),
occupied_by_non_air: block_id,
occupied_by_air: position_occupation.occupied_by_air
},
Position {
id: block_id,
x: x.mag,
x_neg: x.sign,
y: y.mag,
y_neg: y.sign,
z: z.mag,
z_neg: z.sign,
},
OwnedBy {
id: block_id,
address: contract_address_const::<0>()
}
)
);
}

set!(ctx.world, (
PositionOccupation {
hash: coord.hash(),
occupied_by_non_air: block_id,
occupied_by_air: position_occupation.occupied_by_air


fn creative_build(
self: @ContractState,
world: IWorldDispatcher,
block_id: ID,
coord: VoxelCoord
) {
let GodID_u256: ID = GodID.into();
let game_config = get!(world, GodID_u256, GameConfig);
assert(game_config.creative_mode == false, 'CREATIVE MODE DISABLED');

let item = get!(world, block_id, Item);
let block_type = item.value;

let entity_id: u256 = world.uuid().into();
let position_occupation = get!(
world,
coord.hash(),
PositionOccupation
);
set!(world, (
Item {
id: entity_id,
value: block_type
},
Position {
id: block_id,
x: x.mag,
x_neg: x.sign,
y: y.mag,
y_neg: y.sign,
z: z.mag,
z_neg: z.sign,
id: entity_id,
x: coord.x.mag,
x_neg: coord.x.sign,
y: coord.y.mag,
y_neg: coord.y.sign,
z: coord.z.mag,
z_neg: coord.z.sign,
},
OwnedBy {
id: block_id,
address: contract_address_const::<0>()
PositionOccupation {
hash: coord.hash(),
occupied_by_non_air: entity_id,
occupied_by_air: position_occupation.occupied_by_air
}
)
);
));
}
}

}
61 changes: 0 additions & 61 deletions packages/contracts/src/systems/bulk_stake.cairo

This file was deleted.

Loading

0 comments on commit ea632ae

Please sign in to comment.