Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
ponderingdemocritus committed Jan 5, 2024
1 parent 2b7fef1 commit 74d3e55
Show file tree
Hide file tree
Showing 10 changed files with 132 additions and 131 deletions.
8 changes: 0 additions & 8 deletions .github/dependabot.yml

This file was deleted.

12 changes: 6 additions & 6 deletions Scarb.lock
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ dependencies = [
]

[[package]]
name = "dojo_examples"
name = "dojo_plugin"
version = "0.3.11"
source = "git+https://github.com/dojoengine/dojo?tag=v0.3.11#1e651b5d4d3b79b14a7d8aa29a92062fcb9e6659"

[[package]]
name = "dojo_starter"
version = "0.4.4"
dependencies = [
"dojo",
]

[[package]]
name = "dojo_plugin"
version = "0.3.11"
source = "git+https://github.com/dojoengine/dojo?tag=v0.3.11#1e651b5d4d3b79b14a7d8aa29a92062fcb9e6659"
2 changes: 1 addition & 1 deletion Scarb.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
cairo-version = "2.4.0"
name = "dojo_examples"
name = "dojo_starter"
version = "0.4.4"

[cairo]
Expand Down
2 changes: 2 additions & 0 deletions scripts/default_auth.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ COMPONENTS=("Position" "Moves" )

for component in ${COMPONENTS[@]}; do
sozo auth writer $component $ACTIONS_ADDRESS --world $WORLD_ADDRESS --rpc-url $RPC_URL
# time out for 1 second to avoid rate limiting
sleep 1
done

echo "Default authorizations have been successfully set."
14 changes: 11 additions & 3 deletions src/lib.cairo
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
mod actions;
mod models;
mod utils;
mod systems {
mod actions;
}

mod models {
mod moves;
mod position;
}

mod tests {
mod test_world;
}
31 changes: 31 additions & 0 deletions src/models/moves.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use starknet::ContractAddress;

#[derive(Model, Drop, Serde)]
struct Moves {
#[key]
player: ContractAddress,
remaining: u8,
last_direction: Direction
}

#[derive(Serde, Copy, Drop, Introspect)]
enum Direction {
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,
}
}
}

35 changes: 3 additions & 32 deletions src/models.cairo → src/models/position.cairo
Original file line number Diff line number Diff line change
@@ -1,32 +1,10 @@
use starknet::ContractAddress;

#[derive(Serde, Copy, Drop, Introspect)]
enum Direction {
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,
}
}
}

#[derive(Model, Drop, Serde)]
struct Moves {
#[derive(Model, Copy, Drop, Serde)]
struct Position {
#[key]
player: ContractAddress,
remaining: u8,
last_direction: Direction
vec: Vec2,
}

#[derive(Copy, Drop, Serde, Introspect)]
Expand All @@ -35,13 +13,6 @@ struct Vec2 {
y: u32
}

#[derive(Model, Copy, Drop, Serde)]
struct Position {
#[key]
player: ContractAddress,
vec: Vec2,
}

trait Vec2Trait {
fn is_zero(self: Vec2) -> bool;
fn is_equal(self: Vec2, b: Vec2) -> bool;
Expand Down
85 changes: 16 additions & 69 deletions src/actions.cairo → src/systems/actions.cairo
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
use dojo_examples::models::{Direction};

// define the interface
#[starknet::interface]
trait IActions<TContractState> {
fn spawn(self: @TContractState);
fn move(self: @TContractState, direction: Direction);
fn move(self: @TContractState, direction: dojo_starter::models::moves::Direction);
}

// dojo decorator
#[dojo::contract]
mod actions {
use starknet::{ContractAddress, get_caller_address};
use dojo_examples::models::{Position, Moves, Direction, Vec2};
use dojo_examples::utils::next_position;
use super::IActions;

use starknet::{ContractAddress, get_caller_address};
use dojo_starter::models::{position::{Position, Vec2}, moves::{Moves, Direction}};

// declaring custom event struct
#[event]
#[derive(Drop, starknet::Event)]
Expand All @@ -29,6 +27,18 @@ mod actions {
direction: 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; },
};
position
}


// impl: implement functions specified in trait
#[external(v0)]
impl ActionsImpl of IActions<ContractState> {
Expand Down Expand Up @@ -86,66 +96,3 @@ mod actions {
}
}
}

#[cfg(test)]
mod tests {
use starknet::class_hash::Felt252TryIntoClassHash;

// import world dispatcher
use dojo::world::{IWorldDispatcher, IWorldDispatcherTrait};

// import test utils
use dojo::test_utils::{spawn_test_world, deploy_contract};

// import models
use dojo_examples::models::{position, moves};
use dojo_examples::models::{Position, Moves, Direction, Vec2};

// import actions
use super::{actions, IActionsDispatcher, IActionsDispatcherTrait};

#[test]
#[available_gas(30000000)]
fn test_move() {
// caller
let caller = starknet::contract_address_const::<0x0>();

// 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 = world
.deploy_contract('salt', actions::TEST_CLASS_HASH.try_into().unwrap());
let actions_system = IActionsDispatcher { contract_address };

// call spawn()
actions_system.spawn();

// call move with direction right
actions_system.move(Direction::Right);

// Check world state
let moves = get!(world, caller, Moves);

// casting right direction
let right_dir_felt: felt252 = Direction::Right.into();

// check moves
assert(moves.remaining == 99, 'moves is wrong');

// check last direction
assert(moves.last_direction.into() == right_dir_felt, 'last direction is wrong');

// get new_position
let new_position = get!(world, caller, Position);

// check new position x
assert(new_position.vec.x == 11, 'position x is wrong');

// check new position y
assert(new_position.vec.y == 10, 'position y is wrong');
}
}
62 changes: 62 additions & 0 deletions src/tests/test_world.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#[cfg(test)]
mod tests {
use starknet::class_hash::Felt252TryIntoClassHash;

// import world dispatcher
use dojo::world::{IWorldDispatcher, IWorldDispatcherTrait};

// import test utils
use dojo::test_utils::{spawn_test_world, deploy_contract};

// import test utils
use dojo_starter::{
systems::{actions::{actions, IActionsDispatcher, IActionsDispatcherTrait}},
models::{position::{Position, Vec2, position}, moves::{Moves, Direction, moves}}
};


#[test]
#[available_gas(30000000)]
fn test_move() {
// caller
let caller = starknet::contract_address_const::<0x0>();

// 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 = world
.deploy_contract('salt', actions::TEST_CLASS_HASH.try_into().unwrap());
let actions_system = IActionsDispatcher { contract_address };

// call spawn()
actions_system.spawn();

// call move with direction right
actions_system.move(Direction::Right);

// Check world state
let moves = get!(world, caller, Moves);

// casting right direction
let right_dir_felt: felt252 = Direction::Right.into();

// check moves
assert(moves.remaining == 99, 'moves is wrong');

// check last direction
assert(moves.last_direction.into() == right_dir_felt, 'last direction is wrong');

// get new_position
let new_position = get!(world, caller, Position);

// check new position x
assert(new_position.vec.x == 11, 'position x is wrong');

// check new position y
assert(new_position.vec.y == 10, 'position y is wrong');
}
}
12 changes: 0 additions & 12 deletions src/utils.cairo

This file was deleted.

0 comments on commit 74d3e55

Please sign in to comment.