diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index cd7cc39..848650f 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -10,7 +10,7 @@ jobs: steps: - uses: actions/checkout@v3 - run: curl -L https://install.dojoengine.org | bash - - run: /home/runner/.config/.dojo/bin/dojoup + - run: /home/runner/.config/.dojo/bin/dojoup -v v0.3.0-rc4 - run: | /home/runner/.config/.dojo/bin/sozo build /home/runner/.config/.dojo/bin/sozo test \ No newline at end of file diff --git a/Scarb.toml b/Scarb.toml index f8f0e13..b00fc9d 100644 --- a/Scarb.toml +++ b/Scarb.toml @@ -7,7 +7,7 @@ version = "0.1.0" sierra-replace-ids = true [dependencies] -dojo = { git = "https://github.com/dojoengine/dojo", tag = "nightly-97162926c52d9c1ad294267462e217c09ea01477" } +dojo = { git = "https://github.com/dojoengine/dojo", tag = "nightly-e96f08d6302b2ce1277bc13b17a0723f74cb6177" } [[target.dojo]] diff --git a/src/lib.cairo b/src/lib.cairo index 1a585cb..b0a80e5 100644 --- a/src/lib.cairo +++ b/src/lib.cairo @@ -1,4 +1,4 @@ -mod components; +mod models; mod systems { // example with #[system] decorator diff --git a/src/components.cairo b/src/models.cairo similarity index 79% rename from src/components.cairo rename to src/models.cairo index 0fa9761..2532ead 100644 --- a/src/components.cairo +++ b/src/models.cairo @@ -2,7 +2,7 @@ use array::ArrayTrait; use core::debug::PrintTrait; use starknet::ContractAddress; use dojo::database::schema::{ - EnumMember, Member, Ty, Struct, SchemaIntrospection, serialize_member, serialize_member_type + Enum, Member, Ty, Struct, SchemaIntrospection, serialize_member, serialize_member_type }; #[derive(Serde, Copy, Drop)] @@ -28,15 +28,15 @@ impl DirectionSchemaIntrospectionImpl of SchemaIntrospection { #[inline(always)] fn ty() -> Ty { Ty::Enum( - EnumMember { + Enum { name: 'Direction', attrs: array![].span(), - values: array![ - serialize_member_type(@Ty::Simple('None')), - serialize_member_type(@Ty::Simple('Left')), - serialize_member_type(@Ty::Simple('Right')), - serialize_member_type(@Ty::Simple('Up')), - serialize_member_type(@Ty::Simple('Down')) + children: array![ + ('None', serialize_member_type(@Ty::Tuple(array![].span()))), + ('Left', serialize_member_type(@Ty::Tuple(array![].span()))), + ('Right', serialize_member_type(@Ty::Tuple(array![].span()))), + ('Up', serialize_member_type(@Ty::Tuple(array![].span()))), + ('Down', serialize_member_type(@Ty::Tuple(array![].span()))), ] .span() } @@ -68,7 +68,7 @@ impl DirectionIntoFelt252 of Into { } } -#[derive(Component, Copy, Drop, Serde)] +#[derive(Model, Copy, Drop, Serde)] struct Moves { #[key] player: ContractAddress, @@ -82,7 +82,7 @@ struct Vec2 { y: u32 } -#[derive(Component, Copy, Drop, Print, Serde)] +#[derive(Model, Copy, Drop, Print, Serde)] struct Position { #[key] player: ContractAddress, diff --git a/src/systems/raw_contract.cairo b/src/systems/raw_contract.cairo index 26e7513..02e6bc2 100644 --- a/src/systems/raw_contract.cairo +++ b/src/systems/raw_contract.cairo @@ -1,11 +1,11 @@ use dojo::world::{IWorldDispatcher, IWorldDispatcherTrait}; -use dojo_examples::components::{Direction}; +use dojo_examples::models::{Direction}; // trait: specify functions to implement #[starknet::interface] trait IPlayerActions { - fn spawn(self: @TContractState, world: IWorldDispatcher); - fn move(self: @TContractState, world: IWorldDispatcher, direction: Direction); + fn spawn(self: @TContractState); + fn move(self: @TContractState, direction: Direction); } // exact same functionality as examples/ecs/src/systems/with_decorator.cairo @@ -14,12 +14,14 @@ trait IPlayerActions { mod player_actions_external { use starknet::{ContractAddress, get_caller_address}; use dojo::world::{IWorldDispatcher, IWorldDispatcherTrait}; - use dojo_examples::components::{Position, Moves, Direction, Vec2}; + use dojo_examples::models::{Position, Moves, Direction, Vec2}; use dojo_examples::utils::next_position; use super::IPlayerActions; #[storage] - struct Storage {} + struct Storage { + world_dispatcher: IWorldDispatcher, + } #[event] #[derive(Drop, starknet::Event)] @@ -36,7 +38,8 @@ mod player_actions_external { // impl: implement functions specified in trait #[external(v0)] impl PlayerActionsImpl of IPlayerActions { - fn spawn(self: @ContractState, world: IWorldDispatcher) { + fn spawn(self: @ContractState) { + let world = self.world_dispatcher.read(); let player = get_caller_address(); let position = get!(world, player, (Position)); set!( @@ -48,7 +51,8 @@ mod player_actions_external { ); } - fn move(self: @ContractState, world: IWorldDispatcher, direction: Direction) { + fn move(self: @ContractState, direction: Direction) { + let world = self.world_dispatcher.read(); let player = get_caller_address(); let (mut position, mut moves) = get!(world, player, (Position, Moves)); moves.remaining -= 1; @@ -65,12 +69,13 @@ mod player_actions_external { mod tests { use core::traits::Into; use array::{ArrayTrait}; + use starknet::class_hash::Felt252TryIntoClassHash; use dojo::world::{IWorldDispatcher, IWorldDispatcherTrait}; use dojo::test_utils::{spawn_test_world, deploy_contract}; - use dojo_examples::components::{position, moves}; - use dojo_examples::components::{Position, Moves, Direction, Vec2}; + use dojo_examples::models::{position, moves}; + use dojo_examples::models::{Position, Moves, Direction, Vec2}; use super::{ IPlayerActionsDispatcher, IPlayerActionsDispatcherTrait, @@ -82,18 +87,19 @@ mod tests { fn test_move() { let caller = starknet::contract_address_const::<0x0>(); - // components - let mut components = array![position::TEST_CLASS_HASH, moves::TEST_CLASS_HASH,]; - // deploy world with components - let world = spawn_test_world(components); + // models + let mut models = array![position::TEST_CLASS_HASH, moves::TEST_CLASS_HASH,]; + // deploy world with models + let world = spawn_test_world(models); // deploy systems contract - let contract_address = deploy_contract(player_actions::TEST_CLASS_HASH, array![].span()); + let contract_address = world + .deploy_contract('salt', player_actions::TEST_CLASS_HASH.try_into().unwrap()); let player_actions_system = IPlayerActionsDispatcher { contract_address }; // System calls - player_actions_system.spawn(world); - player_actions_system.move(world, Direction::Right(())); + player_actions_system.spawn(); + player_actions_system.move(Direction::Right(())); let moves = get!(world, caller, Moves); let right_dir_felt: felt252 = Direction::Right(()).into(); diff --git a/src/systems/with_decorator.cairo b/src/systems/with_decorator.cairo index 0bad00a..f1e51f6 100644 --- a/src/systems/with_decorator.cairo +++ b/src/systems/with_decorator.cairo @@ -1,18 +1,18 @@ use dojo::world::{IWorldDispatcher, IWorldDispatcherTrait}; -use dojo_examples::components::{Position, Moves, Direction}; +use dojo_examples::models::{Position, Moves, Direction}; use starknet::{ContractAddress, ClassHash}; // trait: specify functions to implement #[starknet::interface] trait IPlayerActions { - fn spawn(self: @TContractState, world: IWorldDispatcher); - fn move(self: @TContractState, world: IWorldDispatcher, direction: Direction); + fn spawn(self: @TContractState); + fn move(self: @TContractState, direction: Direction); } #[system] mod player_actions { use starknet::{ContractAddress, get_caller_address}; - use dojo_examples::components::{Position, Moves, Direction, Vec2}; + use dojo_examples::models::{Position, Moves, Direction, Vec2}; use dojo_examples::utils::next_position; use super::IPlayerActions; @@ -32,7 +32,8 @@ mod player_actions { #[external(v0)] impl PlayerActionsImpl of IPlayerActions { // ContractState is defined by system decorator expansion - fn spawn(self: @ContractState, world: IWorldDispatcher) { + fn spawn(self: @ContractState) { + let world = self.world_dispatcher.read(); let player = get_caller_address(); let position = get!(world, player, (Position)); set!( @@ -44,7 +45,8 @@ mod player_actions { ); } - fn move(self: @ContractState, world: IWorldDispatcher, direction: Direction) { + fn move(self: @ContractState, direction: Direction) { + let world = self.world_dispatcher.read(); let player = get_caller_address(); let (mut position, mut moves) = get!(world, player, (Position, Moves)); moves.remaining -= 1; @@ -66,8 +68,8 @@ mod tests { use dojo::test_utils::{spawn_test_world, deploy_contract}; - use dojo_examples::components::{position, moves}; - use dojo_examples::components::{Position, Moves, Direction, Vec2}; + use dojo_examples::models::{position, moves}; + use dojo_examples::models::{Position, Moves, Direction, Vec2}; use super::{player_actions, IPlayerActionsDispatcher, IPlayerActionsDispatcherTrait}; #[test] @@ -75,18 +77,19 @@ mod tests { fn test_move() { let caller = starknet::contract_address_const::<0x0>(); - // components - let mut components = array![position::TEST_CLASS_HASH, moves::TEST_CLASS_HASH,]; - // deploy world with components - let world = spawn_test_world(components); + // models + let mut models = array![position::TEST_CLASS_HASH, moves::TEST_CLASS_HASH,]; + // deploy world with models + let world = spawn_test_world(models); // deploy systems contract - let contract_address = deploy_contract(player_actions::TEST_CLASS_HASH, array![].span()); + let contract_address = world + .deploy_contract('salt', player_actions::TEST_CLASS_HASH.try_into().unwrap()); let player_actions_system = IPlayerActionsDispatcher { contract_address }; // System calls - player_actions_system.spawn(world); - player_actions_system.move(world, Direction::Right(())); + player_actions_system.spawn(); + player_actions_system.move(Direction::Right(())); let moves = get!(world, caller, Moves); let right_dir_felt: felt252 = Direction::Right(()).into(); diff --git a/src/utils.cairo b/src/utils.cairo index 586cea4..ca5c571 100644 --- a/src/utils.cairo +++ b/src/utils.cairo @@ -1,4 +1,4 @@ -use dojo_examples::components::{Position, Direction}; +use dojo_examples::models::{Position, Direction}; fn next_position(mut position: Position, direction: Direction) -> Position { match direction {