Skip to content

Commit

Permalink
🎉 Start the contracts
Browse files Browse the repository at this point in the history
  • Loading branch information
bal7hazar committed Aug 16, 2024
0 parents commit 654226d
Show file tree
Hide file tree
Showing 40 changed files with 1,598 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
target
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -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.
62 changes: 62 additions & 0 deletions contracts/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<picture>
<source media="(prefers-color-scheme: dark)" srcset=".github/mark-dark.svg">
<img alt="Dojo logo" align="right" width="120" src=".github/mark-light.svg">
</picture>

<a href="https://twitter.com/dojostarknet">
<img src="https://img.shields.io/twitter/follow/dojostarknet?style=social"/>
</a>
<a href="https://github.com/dojoengine/dojo">
<img src="https://img.shields.io/github/stars/dojoengine/dojo?style=social"/>
</a>

[![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

# Dojo Starter: Official Guide

The official Dojo Starter guide, the quickest and most streamlined way to get your Dojo provable game 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/tutorial/dojo-starter).

## Running Locally

#### Terminal one (Make sure this is running)
```bash
# Run Katana
katana --disable-fee --allowed-origins "*"
```

#### Terminal two
```bash
# Build the example
sozo build

# Migrate the example
sozo migrate apply

# Start Torii
torii --world 0x70835f8344647b1e573fe7aeccbf044230089eb19624d3c7dea4080f5dcb025 --allowed-origins "*"
```

---

## 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.

Happy coding!
22 changes: 22 additions & 0 deletions contracts/Scarb.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Code generated by scarb DO NOT EDIT.
version = 1

[[package]]
name = "dojo"
version = "1.0.0-alpha.4"
source = "git+https://github.com/dojoengine/dojo?tag=v1.0.0-alpha.5#6878242e120d3135d3bc1bb94135d7135693069b"
dependencies = [
"dojo_plugin",
]

[[package]]
name = "dojo_plugin"
version = "1.0.0-alpha.4"
source = "git+https://github.com/dojoengine/dojo?rev=f15def33#f15def330c0d099e79351d11c197f63e8cc1ff36"

[[package]]
name = "rpg"
version = "0.1.0"
dependencies = [
"dojo",
]
15 changes: 15 additions & 0 deletions contracts/Scarb.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[package]
cairo-version = "=2.7.0"
name = "rpg"
version = "0.1.0"

[cairo]
sierra-replace-ids = true

[scripts]
migrate = "sozo build && sozo migrate apply"

[dependencies]
dojo = { git = "https://github.com/dojoengine/dojo", tag = "v1.0.0-alpha.5" }

[[target.dojo]]
Binary file added contracts/assets/cover.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added contracts/assets/icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
21 changes: 21 additions & 0 deletions contracts/dojo_dev.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
[world]
name = "Dojo starter RPG"
description = "A fully onchain RPG game."
cover_uri = "file://assets/cover.png"
icon_uri = "file://assets/icon.png"
website = "https://github.com/dojoengine/dojo-starter"
seed = "dojo_starter_rpg"

[world.socials]
x = "https://x.com/ohayo_dojo"
discord = "https://discord.gg/FB2wR6uF"
github = "https://github.com/dojoengine/starter-rpg"
telegram = "https://t.me/dojoengine"

[namespace]
default = "dojo_starter_rpg"

[env]
rpc_url = "http://localhost:5050/"
account_address = "0xb3ff441a68610b30fd5e2abbf3a1548eb6ba6f3559f2862bf2dc757e5828ca"
private_key = "0x2bbf4f9fd0bbb2e60b0316c1fe0b76cf7a4d0198bd493ced9b8df2a3a24d68a"
2 changes: 2 additions & 0 deletions contracts/overlays/dev/actions.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
tag = "dojo_starter-actions"
writes = ["dojo_starter_rpg-Player", "dojo_starter_rpg-Dungeon"]
133 changes: 133 additions & 0 deletions contracts/src/components/playable.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
#[starknet::component]
mod PlayableComponent {
// Core imports

use core::debug::PrintTrait;

// Starknet imports

use starknet::ContractAddress;
use starknet::info::{get_caller_address, get_block_timestamp};

// Dojo imports

use dojo::world::IWorldDispatcher;
use dojo::world::IWorldDispatcherTrait;

// Internal imports

use rpg::constants;
use rpg::store::{Store, StoreTrait};
use rpg::models::player::{Player, PlayerTrait, PlayerAssert};
use rpg::models::dungeon::{Dungeon, DungeonTrait, DungeonAssert};
use rpg::types::role::Role;
use rpg::types::mode::Mode;
use rpg::types::direction::Direction;

// Errors

mod errors {}

// Storage

#[storage]
struct Storage {}

// Events

#[event]
#[derive(Drop, starknet::Event)]
enum Event {}

#[generate_trait]
impl InternalImpl<
TContractState, +HasComponent<TContractState>
> of InternalTrait<TContractState> {
fn spawn(
self: @ComponentState<TContractState>,
world: IWorldDispatcher,
name: felt252,
role: u8,
mode: u8
) {
// [Setup] Datastore
let store: Store = StoreTrait::new(world);

// [Effect] Create player
let player_id: felt252 = get_caller_address().into();
let time: u64 = get_block_timestamp();
let mut player = PlayerTrait::new(player_id, name, time, mode.into());

// [Effect] Player role
player.enrole(role.into());

// [Effect] Set player
store.set_player(player);
}

fn move(self: @ComponentState<TContractState>, world: IWorldDispatcher, direction: u8) {
// [Setup] Datastore
let store: Store = StoreTrait::new(world);
let player_id: felt252 = get_caller_address().into();
let (mut player, dungeon) = store.get_state(player_id);

// [Check] Player is not dead
player.assert_not_dead();

// [Check] Current dungeon is done
dungeon.assert_is_done();

// [Effect] Move player
let (monster, role) = player.move(direction.into());
let new_dungeon: Dungeon = DungeonTrait::new(dungeon.id, monster, role);

// [Effect] Update state
store.set_state(player, new_dungeon);
}

fn attack(self: @ComponentState<TContractState>, world: IWorldDispatcher) {
// [Setup] Datastore
let store: Store = StoreTrait::new(world);
let player_id: felt252 = get_caller_address().into();
let (mut player, mut dungeon) = store.get_state(player_id);

// [Check] Player is not dead
player.assert_not_dead();

// [Check] Current dungeon is not done
dungeon.assert_not_done();

// [Effect] Attack
dungeon.take_damage(player.role.into(), player.damage);

// [Effect] Defend
if dungeon.is_done() {
player.reward(dungeon.treasury());
} else {
player.take_damage(dungeon.role.into(), dungeon.damage);
}

// [Effect] Update state
store.set_state(player, dungeon);
}

fn heal(self: @ComponentState<TContractState>, world: IWorldDispatcher, quantity: u8) {
// [Setup] Datastore
let store: Store = StoreTrait::new(world);
let player_id: felt252 = get_caller_address().into();
let (mut player, dungeon) = store.get_state(player_id);

// [Check] Player is not dead
player.assert_not_dead();

// [Check] Current dungeon is a shop
dungeon.assert_is_shop();

// [Effect] Heal
player.heal(quantity);

// [Effect] Update state
store.set_player(player);
}
}
}
16 changes: 16 additions & 0 deletions contracts/src/constants.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Game

const SEED_WEEK_SECONDS: u64 = 604800;
const SEED_OFFSET_SECONDS: u64 = 345600;

// Player

const DEFAULT_PLAYER_DAMAGE: u8 = 10;
const DEFAULT_PLAYER_HEALTH: u8 = 100;
const MAX_PLAYER_HEALTH: u8 = 200;
const DEFAULT_PLAYER_GOLD: u16 = 0;

// Shop

const DEFAULT_POTION_COST: u16 = 10;
const DEFAULT_POTION_HEAL: u8 = 20;
35 changes: 35 additions & 0 deletions contracts/src/elements/modes/easy.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Internal imports

use rpg::elements::modes::interface::{ModeTrait, Monster, Role, RoleTrait};

impl Easy of ModeTrait {
#[inline]
fn monster(seed: felt252) -> Monster {
let luck: u256 = seed.into() % 100;
if luck < 20 {
Monster::None
} else if luck < 25 {
Monster::Boss
} else if luck < 35 {
Monster::Elite
} else {
Monster::Common
}
}

#[inline]
fn role(seed: felt252, player_role: Role) -> Role {
let luck: u256 = seed.into() % 100;
if luck < 20 {
Role::Fire
} else if luck < 40 {
Role::Water
} else if luck < 60 {
Role::Earth
} else if luck < 80 {
Role::Air
} else {
player_role.strength()
}
}
}
35 changes: 35 additions & 0 deletions contracts/src/elements/modes/hard.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Internal imports

use rpg::elements::modes::interface::{ModeTrait, Monster, Role, RoleTrait};

impl Hard of ModeTrait {
#[inline]
fn monster(seed: felt252) -> Monster {
let luck: u256 = seed.into() % 100;
if luck < 5 {
Monster::None
} else if luck < 25 {
Monster::Boss
} else if luck < 60 {
Monster::Elite
} else {
Monster::Common
}
}

#[inline]
fn role(seed: felt252, player_role: Role) -> Role {
let luck: u256 = seed.into() % 100;
if luck < 20 {
Role::Fire
} else if luck < 40 {
Role::Water
} else if luck < 60 {
Role::Earth
} else if luck < 80 {
Role::Air
} else {
player_role.weakness()
}
}
}
9 changes: 9 additions & 0 deletions contracts/src/elements/modes/interface.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Internal imports

use rpg::types::monster::Monster;
use rpg::types::role::{Role, RoleTrait};

trait ModeTrait {
fn monster(seed: felt252) -> Monster;
fn role(seed: felt252, player_role: Role) -> Role;
}
Loading

0 comments on commit 654226d

Please sign in to comment.