diff --git a/your_projects/HelloDojo b/your_projects/HelloDojo deleted file mode 160000 index c6dfe43..0000000 --- a/your_projects/HelloDojo +++ /dev/null @@ -1 +0,0 @@ -Subproject commit c6dfe435771e3aaabeacf829638441b7a7fe2acb diff --git a/your_projects/HelloDojo/.vscode/settings.json b/your_projects/HelloDojo/.vscode/settings.json new file mode 100644 index 0000000..33dc04f --- /dev/null +++ b/your_projects/HelloDojo/.vscode/settings.json @@ -0,0 +1,5 @@ +{ + "cairo1.languageServerPath": "~/.dojo/bin/dojo-language-server", + "cairo1.enableLanguageServer": true, + "cairo1.enableScarb": true +} \ No newline at end of file diff --git a/your_projects/HelloDojo/LICENSE b/your_projects/HelloDojo/LICENSE new file mode 100644 index 0000000..d29b859 --- /dev/null +++ b/your_projects/HelloDojo/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2023 Dojo + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/your_projects/HelloDojo/README.md b/your_projects/HelloDojo/README.md new file mode 100644 index 0000000..ac208a9 --- /dev/null +++ b/your_projects/HelloDojo/README.md @@ -0,0 +1,78 @@ + + + Dojo logo + + + + + + + + + +[![discord](https://img.shields.io/badge/join-dojo-green?logo=discord&logoColor=white)](https://discord.gg/PwDa2mKhR4) +[![Telegram Chat][tg-badge]][tg-url] + +[tg-badge]: https://img.shields.io/endpoint?color=neon&logo=telegram&label=chat&style=flat-square&url=https%3A%2F%2Ftg.sumanjay.workers.dev%2Fdojoengine +[tg-url]: https://t.me/dojoengine + + +# Hello Dojo : an Onchain Game +In this project, I tried to create an on-chain game, which means that the entire game logic, assets, and interactions are decentralized and stored on a **blockchain**. This approach offers transparency, security, and unique ownership of in-game items or assets, ultimately revolutionizing how we play and experience games in the digital world. + +## Basic Overview +We will be using **Dojo Game Engine**, Dojo is a provable game engine and toolchain for building onchain games and autonomous worlds. Dojo + +--- + +# How To Start +In this section, I will just give you a quick setup, I highly recommand reading the official documentations, you will find it at the last section of this ReadMe file. + +## Installing the requirements +1. **Install Scarb** + - First you need to have Scarb, Scarb is Cairo Package Manger it's heavily inspired by cargo so you get the idea (I hope XD), you can simply install it by running `curl --proto '=https' --tlsv1.2 -sSf https://docs.swmansion.com/scarb/install.sh | sh` + + - Scarb will set up everything you will need to run your project, If you will use vscode make sure to go to your settings > search for cairo > and enable Scarb + + - If you have any errors like + ``` + error: compiler plugin could not be loaded `dojo_plugin v0.3.0-rc8 (git+https://github.com/dojoengine/dojo) + ``` + + you can simply run `dojoup -v 0.2.3` and replace the old dependecies in `Scrab.toml` with + ``` + [dependencies] + dojo = { git = "https://github.com/dojoengine/dojo.git", tag = "v0.2.3" } + ``` + +## Conclusion +I think I mentioned everything you need to know before starting working on this project, now you can jump to the next section and start working on **Hello Dojo** ! + +--- + +# Dojo Starter: Official Guide + +The official Dojo Starter guide, the quickest and most streamlined way to get your Dojo Autonomous World up and running. This guide will assist you with the initial setup, from cloning the repository to deploying your world. + +Read the full tutorial [here](https://book.dojoengine.org/cairo/hello-dojo.html) + +--- + +## Contribution + +This starter project is a constant work in progress and contributions are greatly appreciated! + +1. **Report a Bug** + + - If you think you have encountered a bug, and we should know about it, feel free to report it [here](https://github.com/dojoengine/dojo-starter/issues) and we will take care of it. + +2. **Request a Feature** + + - You can also request for a feature [here](https://github.com/dojoengine/dojo-starter/issues), and if it's viable, it will be picked for development. + +3. **Create a Pull Request** + - It can't get better then this, your pull request will be appreciated by the community. + +For any other questions, feel free to reach out to us [here](https://dojoengine.org/contact). + +Happy coding! diff --git a/your_projects/HelloDojo/Scarb.toml b/your_projects/HelloDojo/Scarb.toml new file mode 100644 index 0000000..2a48e1b --- /dev/null +++ b/your_projects/HelloDojo/Scarb.toml @@ -0,0 +1,27 @@ +[package] +cairo-version = "2.2.0" +name = "dojo_examples" +version = "0.1.0" + +[cairo] +sierra-replace-ids = true + +[dependencies] +dojo = { git = "https://github.com/dojoengine/dojo.git", tag = "v0.2.3" } + +[[target.dojo]] + +[tool.dojo] +initializer_class_hash = "0xbeef" + +[tool.dojo.env] +rpc_url = "http://localhost:5050/" +# Default account for katana with seed = 0 +account_address = "0x517ececd29116499f4a1b64b094da79ba08dfd54a3edaa316134c41f8160973" +private_key = "0x1800000000300000180000000000030000000000003006001800006600" +# world_address = "0x7d1f066a910bd86f532fa9ca66766722c20d47462fb99fb2fb0e1030262f9c5" + +# Madara testnet +# rpc_url = "https://api.cartridge.gg/x/shinai/madara" +# account_address = "0x2" +# private_key = "0xc1cf1490de1352865301bb8705143f3ef938f97fdf892f1090dcb5ac7bcd1d" diff --git a/your_projects/HelloDojo/scripts/default_auth.sh b/your_projects/HelloDojo/scripts/default_auth.sh new file mode 100644 index 0000000..8982501 --- /dev/null +++ b/your_projects/HelloDojo/scripts/default_auth.sh @@ -0,0 +1,18 @@ +#!/bin/bash +set -euo pipefail +pushd $(dirname "$0")/.. + +export WORLD_ADDRESS="0x7d1f066a910bd86f532fa9ca66766722c20d47462fb99fb2fb0e1030262f9c5"; + +# enable system -> component authorizations +COMPONENTS=("Position" "Moves" ) + +for component in ${COMPONENTS[@]}; do + sozo auth writer $component spawn --world $WORLD_ADDRESS +done + +for component in ${COMPONENTS[@]}; do + sozo auth writer $component move --world $WORLD_ADDRESS +done + +echo "Default authorizations have been successfully set." \ No newline at end of file diff --git a/your_projects/HelloDojo/src/components.cairo b/your_projects/HelloDojo/src/components.cairo new file mode 100644 index 0000000..e2dd5a6 --- /dev/null +++ b/your_projects/HelloDojo/src/components.cairo @@ -0,0 +1,54 @@ +use starknet::ContractAddress; + +#[derive(Component, Copy, Drop, Serde, SerdeLen)] +struct Moves { + #[key] + player: ContractAddress, + remaining: u8, +} + +#[derive(Component, Copy, Drop, Serde, SerdeLen)] +struct Position { + #[key] + player: ContractAddress, + x: u32, + y: u32 +} + +trait PositionTrait { + fn is_zero(self: Position) -> bool; + fn is_equal(self: Position, b: Position) -> bool; +} + +impl PositionImpl of PositionTrait { + fn is_zero(self: Position) -> bool { + if self.x - self.y == 0 { + return true; + } + false + } + + fn is_equal(self: Position, b: Position) -> bool { + self.x == b.x && self.y == b.y + } +} + +#[cfg(test)] +mod tests { + use super::{Position, PositionTrait}; + + #[test] + #[available_gas(100000)] + fn test_position_is_zero() { + let player = starknet::contract_address_const::<0x0>(); + assert(PositionTrait::is_zero(Position { player, x: 0, y: 0 }), 'not zero'); + } + + #[test] + #[available_gas(100000)] + fn test_position_is_equal() { + let player = starknet::contract_address_const::<0x0>(); + let position = Position { player, x: 420, y: 0 }; + assert(PositionTrait::is_equal(position, Position { player, x: 420, y: 0 }), 'not equal'); + } +} diff --git a/your_projects/HelloDojo/src/constants.cairo b/your_projects/HelloDojo/src/constants.cairo new file mode 100644 index 0000000..d8003ee --- /dev/null +++ b/your_projects/HelloDojo/src/constants.cairo @@ -0,0 +1 @@ +const OFFSET: felt252 = 1000; diff --git a/your_projects/HelloDojo/src/events.cairo b/your_projects/HelloDojo/src/events.cairo new file mode 100644 index 0000000..f15088e --- /dev/null +++ b/your_projects/HelloDojo/src/events.cairo @@ -0,0 +1,13 @@ +use starknet::ContractAddress; + +#[derive(Drop, Clone, Serde, PartialEq, starknet::Event)] +struct Moved { + player: ContractAddress, + x: u32, + y: u32 +} + +#[derive(Drop, Clone, Serde, PartialEq, starknet::Event)] +enum Event { + Moved: Moved +} diff --git a/your_projects/HelloDojo/src/lib.cairo b/your_projects/HelloDojo/src/lib.cairo new file mode 100644 index 0000000..b89a5a9 --- /dev/null +++ b/your_projects/HelloDojo/src/lib.cairo @@ -0,0 +1,5 @@ +mod components; +mod systems; +mod tests; +mod constants; +mod events; \ No newline at end of file diff --git a/your_projects/HelloDojo/src/systems.cairo b/your_projects/HelloDojo/src/systems.cairo new file mode 100644 index 0000000..8d51cfc --- /dev/null +++ b/your_projects/HelloDojo/src/systems.cairo @@ -0,0 +1,90 @@ +#[system] +mod spawn { + use dojo::world::Context; + + use dojo_examples::components::Position; + use dojo_examples::components::Moves; + use dojo_examples::constants::OFFSET; + + #[event] + use dojo_examples::events::{Event, Moved}; + + + // so we don't go negative + + fn execute(ctx: Context) { + // cast the offset to a u32 + let offset: u32 = OFFSET.try_into().unwrap(); + + set!( + ctx.world, + ( + Moves { player: ctx.origin, remaining: 100 }, + Position { player: ctx.origin, x: offset, y: offset }, + ) + ); + + emit!(ctx.world, Moved { player: ctx.origin, x: offset, y: offset, }); + + return (); + } +} + +#[system] +mod move { + use dojo::world::Context; + + use dojo_examples::components::Position; + use dojo_examples::components::Moves; + + #[event] + use dojo_examples::events::{Event, Moved}; + + #[derive(Serde, Drop)] + enum Direction { + Left: (), + Right: (), + Up: (), + Down: (), + } + + impl DirectionIntoFelt252 of Into { + fn into(self: Direction) -> felt252 { + match self { + Direction::Left(()) => 0, + Direction::Right(()) => 1, + Direction::Up(()) => 2, + Direction::Down(()) => 3, + } + } + } + + fn execute(ctx: Context, direction: Direction) { + let (mut position, mut moves) = get!(ctx.world, ctx.origin, (Position, Moves)); + moves.remaining -= 1; + let next = next_position(position, direction); + set!(ctx.world, (moves, next)); + emit!(ctx.world, Moved { player: ctx.origin, x: next.x, y: next.y, }); + return (); + } + + fn next_position(mut position: Position, direction: Direction) -> Position { + match direction { + Direction::Left(()) => { + position.x -= 1; + }, + Direction::Right(()) => { + position.x += 1; + }, + Direction::Up(()) => { + position.y -= 1; + }, + Direction::Down(()) => { + position.y += 1; + }, + }; + + position + } +} + diff --git a/your_projects/HelloDojo/src/tests.cairo b/your_projects/HelloDojo/src/tests.cairo new file mode 100644 index 0000000..6f10b53 --- /dev/null +++ b/your_projects/HelloDojo/src/tests.cairo @@ -0,0 +1 @@ +mod move; diff --git a/your_projects/HelloDojo/src/tests/move.cairo b/your_projects/HelloDojo/src/tests/move.cairo new file mode 100644 index 0000000..530dffa --- /dev/null +++ b/your_projects/HelloDojo/src/tests/move.cairo @@ -0,0 +1,96 @@ +#[cfg(test)] +mod tests { + use dojo::world::{IWorldDispatcherTrait, IWorldDispatcher}; + use dojo::test_utils::spawn_test_world; + + // project imports + use dojo_examples::components::{position, Position}; + use dojo_examples::components::{moves, Moves}; + use dojo_examples::systems::spawn; + use dojo_examples::systems::move; + use dojo_examples::constants::OFFSET; + + #[event] + use dojo_examples::events::{Event, Moved}; + + + // helper setup function + // reuse this function for all tests + fn setup_world() -> IWorldDispatcher { + // components + let mut components = array![position::TEST_CLASS_HASH, moves::TEST_CLASS_HASH]; + + // systems + let mut systems = array![spawn::TEST_CLASS_HASH, move::TEST_CLASS_HASH]; + + // deploy executor, world and register components/systems + spawn_test_world(components, systems) + } + + #[test] + #[available_gas(300000000)] + fn test_move() { + let world = setup_world(); + + // spawn entity + world.execute('spawn', array![]); + + // move entity + world.execute('move', array![move::Direction::Right(()).into()]); + + // it is just the caller + let caller = starknet::contract_address_const::<0x0>(); + + // check moves + let moves = get!(world, caller, (Moves)); + assert(moves.remaining == 99, 'moves is wrong'); + + // check position + let new_position = get!(world, caller, (Position)); + assert(new_position.x == (OFFSET + 1).try_into().unwrap(), 'position x is wrong'); + assert(new_position.y == OFFSET.try_into().unwrap(), 'position y is wrong'); + + //check events + + // unpop world creation events + let mut events_to_unpop = 1; // WorldSpawned + events_to_unpop += 2; // 2x ComponentRegistered + events_to_unpop += 2; // 2x SystemRegistered + loop { + if events_to_unpop == 0 { + break; + }; + + starknet::testing::pop_log_raw(world.contract_address); + events_to_unpop -= 1; + }; + + starknet::testing::pop_log_raw(world.contract_address); // unpop StoreSetRecord Moves + starknet::testing::pop_log_raw(world.contract_address); // unpop StoreSetRecord Position + // player spawns at x:OFFSET, y:OFFSET + assert( + @starknet::testing::pop_log(world.contract_address) + .unwrap() == @Event::Moved( + Moved { + player: caller, x: OFFSET.try_into().unwrap(), y: OFFSET.try_into().unwrap() + } + ), + 'invalid Moved event 0' + ); + + starknet::testing::pop_log_raw(world.contract_address); // unpop StoreSetRecord Moves + starknet::testing::pop_log_raw(world.contract_address); // unpop StoreSetRecord Position + // player move at x:OFFSET+1, y:OFFSET + assert( + @starknet::testing::pop_log(world.contract_address) + .unwrap() == @Event::Moved( + Moved { + player: caller, + x: (OFFSET + 1).try_into().unwrap(), + y: OFFSET.try_into().unwrap() + } + ), + 'invalid Moved event 1' + ); + } +}