Skip to content

Commit

Permalink
Update example spawn-and-move (dojoengine#1413)
Browse files Browse the repository at this point in the history
* Update dojo example

* Update dojo v0.5.0

* docs: minor fixes on new address

---------

Co-authored-by: glihm <[email protected]>
  • Loading branch information
dpinones and glihm authored Jan 17, 2024
1 parent 87599f9 commit 66b16f8
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 53 deletions.
55 changes: 42 additions & 13 deletions examples/spawn-and-move/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
> }
```
2 changes: 1 addition & 1 deletion examples/spawn-and-move/Scarb.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
12 changes: 4 additions & 8 deletions examples/spawn-and-move/src/actions.cairo
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
use dojo::world::{IWorldDispatcher, IWorldDispatcherTrait};
use dojo_examples::models::{Position, Moves, Direction};
use starknet::{ContractAddress, ClassHash};

#[starknet::interface]
trait IActions<TContractState> {
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)]
Expand Down Expand Up @@ -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)]
Expand Down
27 changes: 12 additions & 15 deletions examples/spawn-and-move/src/models.cairo
Original file line number Diff line number Diff line change
@@ -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<Direction, felt252> {
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,
}
}
}
Expand All @@ -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,
Expand All @@ -64,7 +62,6 @@ impl Vec2Impl of Vec2Trait {

#[cfg(test)]
mod tests {
use core::debug::PrintTrait;
use super::{Position, Vec2, Vec2Trait};

#[test]
Expand Down
21 changes: 5 additions & 16 deletions examples/spawn-and-move/src/utils.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit 66b16f8

Please sign in to comment.