Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update example spawn-and-move #1413

Merged
merged 5 commits into from
Jan 17, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 38 additions & 9 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
sozo model get Moves 0x517ececd29116499f4a1b64b094da79ba08dfd54a3edaa316134c41f8160973 --world 0x33ac2f528bb97cc7b79148fd1756dc368be0e95d391d8c6d6473ecb60b4560e
> struct Moves {
> #[key]
> player: ContractAddress = 0x517ececd29116499f4a1b64b094da79ba08dfd54a3edaa316134c41f8160973,
> 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
sozo execute 0x152dcff993befafe5001975149d2c50bd9621da7cbaed74f68e7d5e54e65abc spawn

# Fetch the updated entity
sozo model get --world 0x26065106fa319c3981618e7567480a50132f23932226a51c219ffb8e47daa84 Moves 0x517ececd29116499f4a1b64b094da79ba08dfd54a3edaa316134c41f8160973
> 0xa
sozo model get Moves 0x517ececd29116499f4a1b64b094da79ba08dfd54a3edaa316134c41f8160973 --world 0x33ac2f528bb97cc7b79148fd1756dc368be0e95d391d8c6d6473ecb60b4560e
> struct Moves {
> #[key]
> player: ContractAddress = 0x517ececd29116499f4a1b64b094da79ba08dfd54a3edaa316134c41f8160973,
> remaining: u8 = 1,
> last_direction: Direction = None,
> }
>
> enum Direction {
> None
> Left
> Right
> Up
> Down
> }
```
6 changes: 1 addition & 5 deletions examples/spawn-and-move/Scarb.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@
cairo-version = "2.4.0"
name = "dojo_examples"
version = "0.4.4"
# Use the prelude with the less imports as possible
# from corelib.
edition = "2023_10"

[cairo]
sierra-replace-ids = true
Expand All @@ -23,6 +20,5 @@ name = "example"
rpc_url = "http://localhost:5050/"

# Default account for katana with seed = 0
account_address = "0x6162896d1d7ab204c7ccac6dd5f8e9e7c25ecd5ae4fcb4ad32e57786bb46e03"
account_address = "0x517ececd29116499f4a1b64b094da79ba08dfd54a3edaa316134c41f8160973"
glihm marked this conversation as resolved.
Show resolved Hide resolved
private_key = "0x1800000000300000180000000000030000000000003006001800006600"
world_address = "0x5010c31f127114c6198df8a5239e2b7a5151e1156fb43791e37e7385faa8138"
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
}
Loading