From dcc5c595bd9163ae35764c22c6aae894a8f0be60 Mon Sep 17 00:00:00 2001 From: Valentin Dosimont Date: Sat, 4 Jan 2025 12:22:34 +0100 Subject: [PATCH] feat: improve direction type + add check on 'can_move' --- .github/workflows/test.yaml | 2 +- manifest_dev.json | 27 ++++++++++++++++----------- src/models.cairo | 14 ++++++++++---- src/systems/actions.cairo | 25 ++++++++++++++++--------- src/tests/test_world.cairo | 6 +++--- 5 files changed, 46 insertions(+), 28 deletions(-) diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index 85da260..32838d0 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -10,7 +10,7 @@ jobs: sozo-test: runs-on: ubuntu-latest env: - DOJO_VERSION: v1.0.8 + DOJO_VERSION: v1.0.9 steps: - uses: actions/checkout@v3 - run: curl -L https://install.dojoengine.org | bash diff --git a/manifest_dev.json b/manifest_dev.json index 0b74764..27d5d24 100644 --- a/manifest_dev.json +++ b/manifest_dev.json @@ -1,7 +1,7 @@ { "world": { - "class_hash": "0x79d9ce84b97bcc2a631996c3100d57966fc2f5b061fb1ec4dfd0040976bcac6", - "address": "0xe2ea9b5dd9804d13903edf712998943b7d5d606c139dd0f13eeb8f5b84da8d", + "class_hash": "0x45575a88cc5cef1e444c77ce60b7b4c9e73a01cbbe20926d5a4c72a94011410", + "address": "0x525177c8afe8680d7ad1da30ca183e482cfcd6404c1e09d83fd3fa2994fd4b8", "seed": "dojo_starter", "name": "Dojo starter", "entrypoints": [ @@ -92,6 +92,10 @@ { "name": "metadata_uri", "type": "core::byte_array::ByteArray" + }, + { + "name": "metadata_hash", + "type": "core::felt252" } ] }, @@ -1002,6 +1006,11 @@ "name": "uri", "type": "core::byte_array::ByteArray", "kind": "data" + }, + { + "name": "hash", + "type": "core::felt252", + "kind": "data" } ] }, @@ -1243,8 +1252,8 @@ }, "contracts": [ { - "address": "0x58b83bea84766c5725c12e239c1ec9e1fde679ddf709b772fe7a8fdfd3cda27", - "class_hash": "0x3f87eccb39c9dc7bb70d372350f4360ae1ff9da43bd72c89a96e336402a569b", + "address": "0x73a20bf1f5ebe9caf16bc08fb07434618300a55a764395a22f4fdde8237a547", + "class_hash": "0x6cbb5ab40ad7b48dff70edeacc4902a9eeae63a9e36456f9f908aedb904fbd2", "abi": [ { "type": "impl", @@ -1305,10 +1314,6 @@ "type": "enum", "name": "dojo_starter::models::Direction", "variants": [ - { - "name": "None", - "type": "()" - }, { "name": "Left", "type": "()" @@ -1480,13 +1485,13 @@ "models": [ { "members": [], - "class_hash": "0x7deb48ccf95cc441a0489cfefdae54aeb6f8ec462ba13ff25e23f080e66cc2f", + "class_hash": "0x3f52c6cf17db224f4e7f88629adf7e907ff110baa0616c7e8ef17c5aecf1b1b", "tag": "dojo_starter-DirectionsAvailable", "selector": "0x77844f1facb51e60e546a9832d56c6bd04fa23be4fd5b57290caae5e9a3c1e4" }, { "members": [], - "class_hash": "0x70edf8f3be0b118e78f856f3ea9ebb652cba3684abaf7f299bfa6f93bf907c9", + "class_hash": "0x5f79e21312b142fc289d3bd1e0f4c65ac3c4e532252c6fb1ad48b718d892dc", "tag": "dojo_starter-Moves", "selector": "0x2a29373f1af8348bd366a990eb3a342ef2cbe5e85160539eaca3441a673f468" }, @@ -1500,7 +1505,7 @@ "events": [ { "members": [], - "class_hash": "0x5be0a05a5df3bd3b4fc17f8b1feb395cb463ced20ea41d4fbb9b86a4d7efc66", + "class_hash": "0x3dca5564f84050b20e66a2e5e7619201502d241b223ea73cfc05f29e6e20ea1", "tag": "dojo_starter-Moved", "selector": "0x504403e5c02b6442527721fc464d9ea5fc8f1ee600ab5ccd5c0845d36fd45f1" } diff --git a/src/models.cairo b/src/models.cairo index e979c6d..06b0ebb 100644 --- a/src/models.cairo +++ b/src/models.cairo @@ -1,4 +1,4 @@ -use starknet::ContractAddress; +use starknet::{ContractAddress}; #[derive(Copy, Drop, Serde, Debug)] #[dojo::model] @@ -6,7 +6,7 @@ pub struct Moves { #[key] pub player: ContractAddress, pub remaining: u8, - pub last_direction: Direction, + pub last_direction: Option, pub can_move: bool, } @@ -29,7 +29,6 @@ pub struct Position { #[derive(Serde, Copy, Drop, Introspect, PartialEq, Debug)] pub enum Direction { - None, Left, Right, Up, @@ -47,7 +46,6 @@ pub struct Vec2 { impl DirectionIntoFelt252 of Into { fn into(self: Direction) -> felt252 { match self { - Direction::None => 0, Direction::Left => 1, Direction::Right => 2, Direction::Up => 3, @@ -56,6 +54,14 @@ impl DirectionIntoFelt252 of Into { } } +impl OptionDirectionIntoFelt252 of Into, felt252> { + fn into(self: Option) -> felt252 { + match self { + Option::None => 0, + Option::Some(d) => d.into(), + } + } +} #[generate_trait] impl Vec2Impl of Vec2Trait { diff --git a/src/systems/actions.cairo b/src/systems/actions.cairo index eaad690..859fb43 100644 --- a/src/systems/actions.cairo +++ b/src/systems/actions.cairo @@ -48,7 +48,7 @@ pub mod actions { // 2. Set the player's remaining moves to 100. let moves = Moves { - player, remaining: 100, last_direction: Direction::None(()), can_move: true + player, remaining: 100, last_direction: Option::None, can_move: true }; // Write the new moves to the world. @@ -66,15 +66,20 @@ pub mod actions { // Retrieve the player's current position and moves data from the world. let position: Position = world.read_model(player); let mut moves: Moves = world.read_model(player); + // if player hasn't spawn, read returns model default values. This leads to sub overflow afterwards. + // Plus it's generally considered as a good pratice to fast-return on matching conditions. + if !moves.can_move { + return; + } // Deduct one from the player's remaining moves. moves.remaining -= 1; // Update the last direction the player moved in. - moves.last_direction = direction; + moves.last_direction = Option::Some(direction); // Calculate the player's next position based on the provided direction. - let next = next_position(position, direction); + let next = next_position(position, moves.last_direction); // Write the new position to the world. world.write_model(@next); @@ -98,13 +103,15 @@ pub mod actions { } // Define function like this: -fn next_position(mut position: Position, direction: Direction) -> Position { +fn next_position(mut position: Position, direction: Option) -> 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; }, + Option::None => { return position; }, + Option::Some(d) => match d { + 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 } diff --git a/src/tests/test_world.cairo b/src/tests/test_world.cairo index 4d3ad29..0282fa7 100644 --- a/src/tests/test_world.cairo +++ b/src/tests/test_world.cairo @@ -79,16 +79,16 @@ use dojo::model::{ModelStorage, ModelValueStorage, ModelStorageTest}; initial_position.vec.x == 10 && initial_position.vec.y == 10, 'wrong initial position' ); - actions_system.move(Direction::Right(())); + actions_system.move(Direction::Right(()).into()); let moves: Moves = world.read_model(caller); let right_dir_felt: felt252 = Direction::Right(()).into(); assert(moves.remaining == initial_moves.remaining - 1, 'moves is wrong'); - assert(moves.last_direction.into() == right_dir_felt, 'last direction is wrong'); + assert(moves.last_direction.unwrap().into() == right_dir_felt, 'last direction is wrong'); let new_position: Position = world.read_model(caller); assert(new_position.vec.x == initial_position.vec.x + 1, 'position x is wrong'); assert(new_position.vec.y == initial_position.vec.y, 'position y is wrong'); } -} \ No newline at end of file +}