diff --git a/examples/spawn-and-move/README.md b/examples/spawn-and-move/README.md index 75e0ff346f..9293592c12 100644 --- a/examples/spawn-and-move/README.md +++ b/examples/spawn-and-move/README.md @@ -12,26 +12,55 @@ sozo build sozo migrate # Get the class hash of the Moves model by name -sozo model class-hash --world 0x26065106fa319c3981618e7567480a50132f23932226a51c219ffb8e47daa84 Moves -> 0x2b97f0b24be59ecf4504a27ac2301179be7df44c4c7d9482cd7b36137bc0fa4 +sozo model class-hash Moves --world 0x33ac2f528bb97cc7b79148fd1756dc368be0e95d391d8c6d6473ecb60b4560e +> 0x64495ca6dc1dc328972697b30468cea364bcb7452bbb6e4aaad3e4b3f190147 # Get the schema of the Moves model -sozo model schema --world 0x26065106fa319c3981618e7567480a50132f23932226a51c219ffb8e47daa84 Moves +sozo model schema Moves --world 0x33ac2f528bb97cc7b79148fd1756dc368be0e95d391d8c6d6473ecb60b4560e > struct Moves { -> remaining: u8 +> #[key] +> player: ContractAddress, +> remaining: u8, +> last_direction: Direction = Invalid Option, +> } +> +> enum Direction { +> None +> Left +> Right +> Up +> Down > } # Get the value of the Moves model for an entity. (in this example, -# 0x517ececd29116499f4a1b64b094da79ba08dfd54a3edaa316134c41f8160973 is -# the calling account. -sozo model get --world 0x26065106fa319c3981618e7567480a50132f23932226a51c219ffb8e47daa84 Moves 0x517ececd29116499f4a1b64b094da79ba08dfd54a3edaa316134c41f8160973 -> 0x0 +# 0x6162896d1d7ab204c7ccac6dd5f8e9e7c25ecd5ae4fcb4ad32e57786bb46e03, is +# the calling account which is also the key to retrieve a Moves model) +sozo model get Moves 0x6162896d1d7ab204c7ccac6dd5f8e9e7c25ecd5ae4fcb4ad32e57786bb46e03 --world 0x33ac2f528bb97cc7b79148fd1756dc368be0e95d391d8c6d6473ecb60b4560e +> struct Moves { +> #[key] +> player: ContractAddress = 0x6162896d1d7ab204c7ccac6dd5f8e9e7c25ecd5ae4fcb4ad32e57786bb46e03, +> remaining: u8 = 0, +> last_direction: Direction = None, +> } # The returned value is 0 since we haven't spawned yet. -# We can spawn a player using the actions contract address -sozo execute 0x31571485922572446df9e3198a891e10d3a48e544544317dbcbb667e15848cd spawn +# We can spawn a player using the actions contract address. +sozo execute 0x152dcff993befafe5001975149d2c50bd9621da7cbaed74f68e7d5e54e65abc spawn -# Fetch the updated entity -sozo model get --world 0x26065106fa319c3981618e7567480a50132f23932226a51c219ffb8e47daa84 Moves 0x517ececd29116499f4a1b64b094da79ba08dfd54a3edaa316134c41f8160973 -> 0xa +# Fetch the updated entity. +sozo model get Moves 0x6162896d1d7ab204c7ccac6dd5f8e9e7c25ecd5ae4fcb4ad32e57786bb46e03 --world 0x33ac2f528bb97cc7b79148fd1756dc368be0e95d391d8c6d6473ecb60b4560e +> struct Moves { +> #[key] +> player: ContractAddress = 0x6162896d1d7ab204c7ccac6dd5f8e9e7c25ecd5ae4fcb4ad32e57786bb46e03, +> remaining: u8 = 1, +> last_direction: Direction = None, +> } +> +> enum Direction { +> None +> Left +> Right +> Up +> Down +> } ``` diff --git a/examples/spawn-and-move/Scarb.toml b/examples/spawn-and-move/Scarb.toml index 558eed1b75..88e7c4153c 100644 --- a/examples/spawn-and-move/Scarb.toml +++ b/examples/spawn-and-move/Scarb.toml @@ -25,4 +25,4 @@ rpc_url = "http://localhost:5050/" # Default account for katana with seed = 0 account_address = "0x6162896d1d7ab204c7ccac6dd5f8e9e7c25ecd5ae4fcb4ad32e57786bb46e03" private_key = "0x1800000000300000180000000000030000000000003006001800006600" -world_address = "0x5010c31f127114c6198df8a5239e2b7a5151e1156fb43791e37e7385faa8138" +world_address = "0x33ac2f528bb97cc7b79148fd1756dc368be0e95d391d8c6d6473ecb60b4560e" diff --git a/examples/spawn-and-move/src/actions.cairo b/examples/spawn-and-move/src/actions.cairo index 6f56d1f928..ea94cde00f 100644 --- a/examples/spawn-and-move/src/actions.cairo +++ b/examples/spawn-and-move/src/actions.cairo @@ -1,19 +1,16 @@ -use dojo::world::{IWorldDispatcher, IWorldDispatcherTrait}; -use dojo_examples::models::{Position, Moves, Direction}; -use starknet::{ContractAddress, ClassHash}; - #[starknet::interface] trait IActions { fn spawn(self: @TContractState); - fn move(self: @TContractState, direction: Direction); + fn move(self: @TContractState, direction: dojo_examples::models::Direction); } #[dojo::contract] mod actions { + use super::IActions; + use starknet::{ContractAddress, get_caller_address}; use dojo_examples::models::{Position, Moves, Direction, Vec2}; use dojo_examples::utils::next_position; - use super::IActions; #[event] #[derive(Drop, starknet::Event)] @@ -97,9 +94,8 @@ mod tests { use dojo::test_utils::{spawn_test_world, deploy_contract}; - use dojo_examples::models::{position, moves}; - use dojo_examples::models::{Position, Moves, Direction, Vec2}; use super::{actions, IActionsDispatcher, IActionsDispatcherTrait}; + use dojo_examples::models::{Position, position, Moves, moves, Direction, Vec2}; #[test] #[available_gas(30000000)] diff --git a/examples/spawn-and-move/src/models.cairo b/examples/spawn-and-move/src/models.cairo index 8c8a275bed..c917342ece 100644 --- a/examples/spawn-and-move/src/models.cairo +++ b/examples/spawn-and-move/src/models.cairo @@ -1,24 +1,22 @@ -use core::array::ArrayTrait; -use core::debug::PrintTrait; use starknet::ContractAddress; #[derive(Serde, Copy, Drop, Introspect)] enum Direction { - None: (), - Left: (), - Right: (), - Up: (), - Down: (), + None, + Left, + Right, + Up, + Down, } impl DirectionIntoFelt252 of Into { fn into(self: Direction) -> felt252 { match self { - Direction::None(()) => 0, - Direction::Left(()) => 1, - Direction::Right(()) => 2, - Direction::Up(()) => 3, - Direction::Down(()) => 4, + Direction::None => 0, + Direction::Left => 1, + Direction::Right => 2, + Direction::Up => 3, + Direction::Down => 4, } } } @@ -31,13 +29,13 @@ struct Moves { last_direction: Direction } -#[derive(Copy, Drop, Serde, Print, Introspect)] +#[derive(Copy, Drop, Serde, Introspect)] struct Vec2 { x: u32, y: u32 } -#[derive(Model, Copy, Drop, Print, Serde)] +#[derive(Model, Copy, Drop, Serde)] struct Position { #[key] player: ContractAddress, @@ -64,7 +62,6 @@ impl Vec2Impl of Vec2Trait { #[cfg(test)] mod tests { - use core::debug::PrintTrait; use super::{Position, Vec2, Vec2Trait}; #[test] diff --git a/examples/spawn-and-move/src/utils.cairo b/examples/spawn-and-move/src/utils.cairo index ca5c5714f1..e14be466d8 100644 --- a/examples/spawn-and-move/src/utils.cairo +++ b/examples/spawn-and-move/src/utils.cairo @@ -2,22 +2,11 @@ use dojo_examples::models::{Position, Direction}; fn next_position(mut position: Position, direction: Direction) -> Position { match direction { - Direction::None(()) => { - return position; - }, - Direction::Left(()) => { - position.vec.x -= 1; - }, - Direction::Right(()) => { - position.vec.x += 1; - }, - Direction::Up(()) => { - position.vec.y -= 1; - }, - Direction::Down(()) => { - position.vec.y += 1; - }, + Direction::None => { return position; }, + Direction::Left => { position.vec.x -= 1; }, + Direction::Right => { position.vec.x += 1; }, + Direction::Up => { position.vec.y -= 1; }, + Direction::Down => { position.vec.y += 1; }, }; - position }