Skip to content

Commit

Permalink
feat: mugging decision tree (#125)
Browse files Browse the repository at this point in the history
* chore(contracts): update events

* fix(web): invalid sig errors on tx

* chore(web): use hosted madara in prod

* back in business!

* update to latest dojo

* update read me

* inline methods

* feat(contracts): hustler actions and consequences

* rename to player_status

* more updates

* working game loop

* refactor
  • Loading branch information
broody authored Sep 11, 2023
1 parent f8d0879 commit 55fcb4d
Show file tree
Hide file tree
Showing 84 changed files with 34,248 additions and 2,012 deletions.
17 changes: 13 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,31 @@ Install the latest Dojo toolchain from [releases](https://github.com/dojoengine/

```bash
# Start Katana
katana --seed 0 --block-time 1
katana --block-time 200

# Build the game
sozo build

# Migrate the world, this will declare/deploy contracts to katana
sozo migrate --name ryo
# Migrate the world, this will declare/deploy contracts to katana and take note of the world address
sozo migrate

# Start indexer, graphql endpoint at http://localhost:8080
torii --manifest target/dev/manifest.json --world-address 0x788f5fd335d29ed5f8686982079cc3aa9c82aa41968f759b2c3d0be8d5fa0c4
torii --world {world_address}

# Setup default authorization
./scripts/default_auth.sh

# Start frontend, located at http://localhost:3000
cd web
yarn install && yarn dev
```

Note: If the world address your game is deployed to is different, you'll need to update it in three places currently

- Scarb.toml
- script/default_auth.sh
- web/src/constants.ts

### With Madara

TBD
Expand Down
10 changes: 5 additions & 5 deletions Scarb.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "rollyourown"
version = "0.1.0"
cairo-version = "2.0.0-rc4"
cairo-version = "2.2.0"

[cairo]
sierra-replace-ids = true
Expand All @@ -16,12 +16,12 @@ dojo = { git = "https://github.com/dojoengine/dojo.git" }

# Katana
rpc_url = "http://localhost:5050"
account_address = "0x03ee9e18edc71a6df30ac3aca2e0b02a198fbce19b7480a63a0d71cbd76652e0"
private_key = "0x0300001800000000300000180000000000030000000000003006001800006600"
account_address = "0x517ececd29116499f4a1b64b094da79ba08dfd54a3edaa316134c41f8160973"
private_key = "0x1800000000300000180000000000030000000000003006001800006600"

# Madara
#rpc_url = "https://rinnegan.madara.zone"
#rpc_url = "https://api.cartridge.gg/x/shinai/madara"
#account_address = "0x2"
#private_key = "0xc1cf1490de1352865301bb8705143f3ef938f97fdf892f1090dcb5ac7bcd1d"

#world_address = "0x5b328933afdbbfd44901fd69a2764a254edbb6e992ae87cf958c70493f2d201"
#world_address = "0x3c3dfeb374720dfd73554dc2b9e0583cb9668efb3055d07d1533afa5d219fd5"
13 changes: 11 additions & 2 deletions scripts/default_auth.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
set -euo pipefail
pushd $(dirname "$0")/..

export WORLD_ADDRESS="0x49eea56f14aebfad0a3f73c530f0673084cc9c4b352965e685e52939221c389";
export WORLD_ADDRESS="0x3c3dfeb374720dfd73554dc2b9e0583cb9668efb3055d07d1533afa5d219fd5";

# make sure all components/systems are deployed
COMPONENTS=("Game" "Market" "Name" "Player" "Risks")
SYSTEMS=("create_game" "join_game" "set_name" "travel" "buy" "sell")
SYSTEMS=("create_game" "join_game" "set_name" "travel" "buy" "sell" "decide")

# check components
for component in ${COMPONENTS[@]}; do
Expand All @@ -29,6 +29,7 @@ SET_NAME_COMPONENTS=("Name")
BUY_COMPONENTS=("Drug" "Market" "Name" "Player")
SELL_COMPONENTS=("Drug" "Market" "Name" "Player")
TRAVEL_COMPONENTS=("Player")
DECIDE_COMPONENTS=("Player" "Drug")

for component in ${CREATE_GAME_COMPONENTS[@]}; do
sozo auth writer $component create_game --world $WORLD_ADDRESS
Expand All @@ -54,4 +55,12 @@ for component in ${TRAVEL_COMPONENTS[@]}; do
sozo auth writer $component travel --world $WORLD_ADDRESS
done

for component in ${TRAVEL_COMPONENTS[@]}; do
sozo auth writer $component decide --world $WORLD_ADDRESS
done

for component in ${DECIDE_COMPONENTS[@]}; do
sozo auth writer $component decide --world $WORLD_ADDRESS
done

echo "Default authorizations have been successfully set."
5 changes: 1 addition & 4 deletions src/components/drug.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@ struct Drug {
quantity: usize,
}

trait DrugTrait {
fn all() -> Span<felt252>;
}

#[generate_trait]
impl DrugImpl of DrugTrait {
fn all() -> Span<felt252> {
let mut drugs = array::ArrayTrait::new();
Expand Down
15 changes: 6 additions & 9 deletions src/components/game.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use box::BoxTrait;
use traits::Into;
use starknet::ContractAddress;

#[derive(Component, Copy, Drop, Serde, SerdeLen)]
#[derive(Component, Copy, Drop, Serde)]
struct Game {
#[key]
game_id: u32,
Expand All @@ -14,19 +14,16 @@ struct Game {
creator: ContractAddress,
}


trait GameTrait {
fn tick(self: @Game) -> bool;
}

#[generate_trait]
impl GameImpl of GameTrait {
fn tick(self: @Game) -> bool {
#[inline(always)]
fn tick(self: Game) -> bool {
let info = starknet::get_block_info().unbox();

if info.block_timestamp < *self.start_time {
if info.block_timestamp < self.start_time {
return false;
}
if *self.is_finished {
if self.is_finished {
return false;
}

Expand Down
6 changes: 1 addition & 5 deletions src/components/location.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,7 @@ use starknet::ContractAddress;

struct Location {}

trait LocationTrait {
fn all() -> Span<felt252>;
fn random(seed: felt252) -> felt252;
}

#[generate_trait]
impl LocationImpl of LocationTrait {
fn all() -> Span<felt252> {
let mut locations = array::ArrayTrait::new();
Expand Down
28 changes: 13 additions & 15 deletions src/components/market.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use debug::PrintTrait;

use rollyourown::constants::SCALING_FACTOR;

#[derive(Component, Copy, Drop, Serde, SerdeLen)]
#[derive(Component, Copy, Drop, Serde)]
struct Market {
#[key]
game_id: u32,
Expand All @@ -16,39 +16,37 @@ struct Market {
quantity: usize,
}

trait MarketTrait {
fn buy(self: @Market, quantity: usize) -> u128;
fn sell(self: @Market, quantity: usize) -> u128;
}

#[generate_trait]
impl MarketImpl of MarketTrait {
fn buy(self: @Market, quantity: usize) -> u128 {
assert(quantity < *self.quantity, 'not enough liquidity');
#[inline(always)]
fn buy(ref self: Market, quantity: usize) -> u128 {
assert(quantity < self.quantity, 'not enough liquidity');
let (amount, available, cash) = normalize(quantity, self);
let k = cash * available;
let cost = (k / (available - amount)) - cash;
cost
}

fn sell(self: @Market, quantity: usize) -> u128 {
#[inline(always)]
fn sell(ref self: Market, quantity: usize) -> u128 {
let (amount, available, cash) = normalize(quantity, self);
let k = cash * available;
let payout = cash - (k / (available + amount));
payout
}
}

fn normalize(amount: usize, market: @Market) -> (u128, u128, u128) {
fn normalize(amount: usize, market: Market) -> (u128, u128, u128) {
let amount: u128 = amount.into() * SCALING_FACTOR;
let available: u128 = (*market.quantity).into() * SCALING_FACTOR;
(amount, available, *market.cash)
let available: u128 = (market.quantity).into() * SCALING_FACTOR;
(amount, available, market.cash)
}


#[test]
#[should_panic(expected: ('not enough liquidity', ))]
fn test_not_enough_quantity() {
let market = Market {
let mut market = Market {
game_id: 0, location_id: 0, drug_id: 0, cash: SCALING_FACTOR * 1, quantity: 1
}; // pool 1:1
let cost = market.buy(10);
Expand All @@ -57,7 +55,7 @@ fn test_not_enough_quantity() {
#[test]
#[available_gas(100000)]
fn test_market_buy() {
let market = Market {
let mut market = Market {
game_id: 0, location_id: 0, drug_id: 0, cash: SCALING_FACTOR * 1, quantity: 10
}; // pool 1:10
let cost = market.buy(5);
Expand All @@ -67,7 +65,7 @@ fn test_market_buy() {
#[test]
#[available_gas(100000)]
fn test_market_sell() {
let market = Market {
let mut market = Market {
game_id: 0, location_id: 0, drug_id: 0, cash: SCALING_FACTOR * 1, quantity: 10
}; // pool 1:10
let payout = market.sell(5);
Expand Down
2 changes: 1 addition & 1 deletion src/components/name.cairo
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use starknet::ContractAddress;

#[derive(Component, Copy, Drop, Serde, SerdeLen)]
#[derive(Component, Copy, Drop, Serde)]
struct Name {
#[key]
game_id: u32,
Expand Down
21 changes: 13 additions & 8 deletions src/components/player.cairo
Original file line number Diff line number Diff line change
@@ -1,27 +1,32 @@
use starknet::ContractAddress;
use rollyourown::PlayerStatus;

#[derive(Component, Copy, Drop, Serde, SerdeLen)]
#[derive(Component, Copy, Drop, Serde)]
struct Player {
#[key]
game_id: u32,
#[key]
player_id: ContractAddress,
status: PlayerStatus,
location_id: felt252,
cash: u128,
health: u8,
drug_count: usize,
bag_limit: usize,
turns_remaining: usize,
}

trait PlayerTrait {
fn can_continue(self: @Player) -> bool;
}

#[generate_trait]
impl PlayerImpl of PlayerTrait {
fn can_continue(self: @Player) -> bool {
if *self.health == 0 {
#[inline(always)]
fn can_continue(ref self: Player) -> bool {
if self.health == 0 {
return false;
}
if self.turns_remaining == 0 {
return false;
}
if *self.turns_remaining == 0 {
if self.status != PlayerStatus::Normal {
return false;
}

Expand Down
Loading

1 comment on commit 55fcb4d

@vercel
Copy link

@vercel vercel bot commented on 55fcb4d Sep 11, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

rollyourown – ./

rollyourown-git-main.preview.cartridge.gg
rollyourown.preview.cartridge.gg

Please sign in to comment.