From d1235914dcc7bb235692a16176d55eba781f4bf2 Mon Sep 17 00:00:00 2001 From: danielcdz Date: Thu, 22 Aug 2024 11:36:24 -0600 Subject: [PATCH 1/3] WIP: add spawn and move --- src/models.cairo | 22 ++++++++++++++++++++++ src/systems/actions.cairo | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+) diff --git a/src/models.cairo b/src/models.cairo index 104efc3..ad22d34 100644 --- a/src/models.cairo +++ b/src/models.cairo @@ -89,6 +89,28 @@ struct Potion { pub potion_effect: u32, } +#[derive(Copy, Drop, Serde)] +#[dojo::model] +struct Moves { + #[key] + player: Player, + remaining: u8, +} + +#[derive(Drop, Copy, Serde)] +#[dojo::model] +struct Position { + #[key] + player: Player, + coordinates: Coordinates, +} + +#[derive(Drop, Copy, Serde, Introspect)] +struct Coordinates { + x: u32, + y: u32 +} + #[generate_trait] impl BeastImpl of BeastTrait { fn exist(self: Beast) -> bool { diff --git a/src/systems/actions.cairo b/src/systems/actions.cairo index d922512..5c740c7 100644 --- a/src/systems/actions.cairo +++ b/src/systems/actions.cairo @@ -8,6 +8,8 @@ use bytebeasts::models::WorldElements; #[dojo::interface] trait IActions { fn setWorld(ref world: IWorldDispatcher); + fn spawn(ref world: IWorldDispatcher); + fn move(ref world: IWorldDispatcher, direction: Direction); } // dojo decorator @@ -195,5 +197,38 @@ mod actions { }) ); } + + fn spawn(ref world: IWorldDispatcher) { + let player = get_caller_address(); // How are we handling players if we are not using address? + + let position = get!(world, player, (Position)); + + set!( + world, + ( + Moves { player, remaining: 100, last_direction: Direction::None }, + Position { + player, vec: Vec2 { x: position.vec.x + 10, y: position.vec.y + 10 } + }, + ) + ); + } + + fn move(ref world: IWorldDispatcher, direction: Direction) { + let player = get_caller_address(); //TODO: How are we handling players if we are not using address? + + let position = get!(world, player, (Position)); + + set!( + world, + ( + Moves { player, remaining: 100, last_direction: Direction::None }, //TODO: How are setting the last_direction if Moves does not have that attribute + Position { + player, vec: Vec2 { x: position.vec.x + 10, y: position.vec.y + 10 } + }, + ) + ); + } + } } From 70e35148c19fc00dfd501a6f06809f3162fd301a Mon Sep 17 00:00:00 2001 From: danielcdz Date: Fri, 23 Aug 2024 23:17:27 -0600 Subject: [PATCH 2/3] Add unit tests --- src/models.cairo | 37 ++++++++++++++++++++++++++++--------- src/systems/actions.cairo | 22 ++++++++-------------- 2 files changed, 36 insertions(+), 23 deletions(-) diff --git a/src/models.cairo b/src/models.cairo index ad22d34..95a864e 100644 --- a/src/models.cairo +++ b/src/models.cairo @@ -89,14 +89,6 @@ struct Potion { pub potion_effect: u32, } -#[derive(Copy, Drop, Serde)] -#[dojo::model] -struct Moves { - #[key] - player: Player, - remaining: u8, -} - #[derive(Drop, Copy, Serde)] #[dojo::model] struct Position { @@ -120,7 +112,7 @@ impl BeastImpl of BeastTrait { #[cfg(test)] mod tests { - use super::{Battle, Beast, Player, Mt, Potion, BeastTrait, WorldElements}; + use super::{Battle, Beast, Player, Coordinates, Position, Mt, Potion, BeastTrait, WorldElements}; #[test] fn test_beast_exist() { @@ -161,6 +153,33 @@ mod tests { assert_eq!(player.potions, 5, "Player should have 5 potions"); } + #[test] + fn test_position_initialization() { + let player = Player { + player_id: 1, + player_name: 'Hero', + beast_1: 1, + beast_2: 2, + beast_3: 3, + beast_4: 4, + potions: 5, + }; + + let coordinates = Coordinates{ + x: 10, + y: 10, + }; + + let position = Position { + player: player, + coordinates: coordinates + }; + + assert_eq!(position.player.player_id, 1, "Player ID should be 1"); + assert_eq!(position.coordinates.x, 10, "Player X coordinate should be 10"); + assert_eq!(position.coordinates.y, 10, "Player Y coordinate should be 10"); + } + #[test] fn test_battle_initialization() { let battle = Battle { diff --git a/src/systems/actions.cairo b/src/systems/actions.cairo index 5c740c7..2bfdf74 100644 --- a/src/systems/actions.cairo +++ b/src/systems/actions.cairo @@ -8,8 +8,8 @@ use bytebeasts::models::WorldElements; #[dojo::interface] trait IActions { fn setWorld(ref world: IWorldDispatcher); - fn spawn(ref world: IWorldDispatcher); - fn move(ref world: IWorldDispatcher, direction: Direction); + fn spawn(ref world: IWorldDispatcher, player_id: u32); + fn move(ref world: IWorldDispatcher, player_id: u32, new_x: u32, new_y: u32); } // dojo decorator @@ -198,33 +198,27 @@ mod actions { ); } - fn spawn(ref world: IWorldDispatcher) { - let player = get_caller_address(); // How are we handling players if we are not using address? - - let position = get!(world, player, (Position)); + fn spawn(ref world: IWorldDispatcher, player_id: u32) { + let player = get!(world, player_id, (Player)); set!( world, ( - Moves { player, remaining: 100, last_direction: Direction::None }, Position { - player, vec: Vec2 { x: position.vec.x + 10, y: position.vec.y + 10 } + player, coordinates: Coordinates { x: 10, y: 10 } }, ) ); } - fn move(ref world: IWorldDispatcher, direction: Direction) { - let player = get_caller_address(); //TODO: How are we handling players if we are not using address? - - let position = get!(world, player, (Position)); + fn move(ref world: IWorldDispatcher, player_id: u32, new_x: u32, new_y:u32) { + let player = get!(world, player_id, (Player)); set!( world, ( - Moves { player, remaining: 100, last_direction: Direction::None }, //TODO: How are setting the last_direction if Moves does not have that attribute Position { - player, vec: Vec2 { x: position.vec.x + 10, y: position.vec.y + 10 } + player, coordinates: coordinates { x: new_x, y: new_y } }, ) ); From 38c62227711d088a3a1102aa4938333ea78bfd44 Mon Sep 17 00:00:00 2001 From: danielcdz Date: Sat, 24 Aug 2024 15:48:00 -0600 Subject: [PATCH 3/3] Add scripts and test transactions --- scripts/move.sh | 12 ++++++++++++ scripts/spawn.sh | 12 ++++++++++++ src/systems/actions.cairo | 20 +++++++++++--------- 3 files changed, 35 insertions(+), 9 deletions(-) create mode 100755 scripts/move.sh create mode 100755 scripts/spawn.sh diff --git a/scripts/move.sh b/scripts/move.sh new file mode 100755 index 0000000..ccc44d9 --- /dev/null +++ b/scripts/move.sh @@ -0,0 +1,12 @@ +#!/bin/bash +set -euo pipefail +pushd $(dirname "$0")/.. + +export RPC_URL="http://0.0.0.0:5050"; + +export WORLD_ADDRESS="$(cat ./manifests/dev/deployment/manifest.json | jq -r '.world.address')" + +echo $WORLD_ADDRESS + +# sozo execute --world +sozo execute --world $WORLD_ADDRESS actions move -c 1,15,15 --wait diff --git a/scripts/spawn.sh b/scripts/spawn.sh new file mode 100755 index 0000000..72f1d64 --- /dev/null +++ b/scripts/spawn.sh @@ -0,0 +1,12 @@ +#!/bin/bash +set -euo pipefail +pushd $(dirname "$0")/.. + +export RPC_URL="http://0.0.0.0:5050"; + +export WORLD_ADDRESS="$(cat ./manifests/dev/deployment/manifest.json | jq -r '.world.address')" + +echo $WORLD_ADDRESS + +# sozo execute --world +sozo execute --world $WORLD_ADDRESS actions spawn -c 1 --wait diff --git a/src/systems/actions.cairo b/src/systems/actions.cairo index 2bfdf74..12906c7 100644 --- a/src/systems/actions.cairo +++ b/src/systems/actions.cairo @@ -1,6 +1,8 @@ use bytebeasts::models::Beast; use bytebeasts::models::Mt; use bytebeasts::models::Player; +use bytebeasts::models::Coordinates; +use bytebeasts::models::Position; use bytebeasts::models::Potion; use bytebeasts::models::WorldElements; @@ -18,7 +20,7 @@ mod actions { use super::{IActions}; use starknet::{ContractAddress, get_caller_address}; - use bytebeasts::models::{Beast, Mt, Player, Potion, WorldElements}; + use bytebeasts::models::{Beast, Mt, Player, Coordinates, Position, Potion, WorldElements}; #[abi(embed_v0)] impl ActionsImpl of IActions { @@ -199,26 +201,26 @@ mod actions { } fn spawn(ref world: IWorldDispatcher, player_id: u32) { - let player = get!(world, player_id, (Player)); + let player_from_world = get!(world, player_id, (Player)); set!( world, - ( - Position { - player, coordinates: Coordinates { x: 10, y: 10 } + (Position { + player: player_from_world, + coordinates: Coordinates { x: 10, y: 10 } }, ) ); } fn move(ref world: IWorldDispatcher, player_id: u32, new_x: u32, new_y:u32) { - let player = get!(world, player_id, (Player)); + let player_from_world = get!(world, player_id, (Player)); set!( world, - ( - Position { - player, coordinates: coordinates { x: new_x, y: new_y } + (Position { + player: player_from_world, + coordinates: Coordinates { x: new_x, y: new_y } }, ) );