Skip to content

Commit

Permalink
feat: officer decision tree (#130)
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

* added officer tree

* Update landing (#131)

* feat: update landing

* fix: build issues

* add font

* more updates

* Market pricing (#135)

* fix: add sleep between sozo auth writer command (#128)

* feat: market prices & bug fixes

* chore: fmt & lint

* update to starnet.js 5.19.6

* set maxFee 0 and use official starknet.js

---------

Co-authored-by: broody <[email protected]>

* point prod env to hosted katana/torii

* temp fix travel

* fix wording

* new consqeuence images

* enable market price and update price range

* sort by price

* fix travel risk

* fix buttons cutoff on decision page

* update trade page

* btn stay depressed on create

* single column drugs on location page on mobile

* gray out btn if can't perform trade

* stack header on mobile

* indicate actual price

* sort price low to high

* fix gang/cop encounter bias

* ui fixes

* fix mobile menu

* fix buying when inventory full

* fix media player height and padding

* toggle buy/sell on mobile

* fix travel delay

* quantity selector fix

* fix decision screen disappearing too fast

* header buttons float top on mobile

* yellow when inventory > 0

* move days countdown around

* use button with clicky sounds

* fix map outline error

---------

Co-authored-by: notV4l <[email protected]>
  • Loading branch information
broody and notV4l authored Sep 15, 2023
1 parent 5cb68e9 commit c4db28c
Show file tree
Hide file tree
Showing 75 changed files with 1,513 additions and 32,752 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Install the latest Dojo toolchain from [releases](https://github.com/dojoengine/

```bash
# Start Katana
katana --block-time 200
katana --disable-fee

# Build the game
sozo build
Expand Down
1 change: 1 addition & 0 deletions Scarb.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ dojo = { git = "https://github.com/dojoengine/dojo.git" }


# Katana
#rpc_url = "https://api.cartridge.gg/x/rollyourown/katana"
rpc_url = "http://localhost:5050"
account_address = "0x517ececd29116499f4a1b64b094da79ba08dfd54a3edaa316134c41f8160973"
private_key = "0x1800000000300000180000000000030000000000003006001800006600"
Expand Down
38 changes: 11 additions & 27 deletions scripts/default_auth.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,72 +2,56 @@
set -euo pipefail
pushd $(dirname "$0")/..

#export RPC_URL="http://localhost:5050";
export RPC_URL="https://api.cartridge.gg/x/rollyourown/katana";
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" "decide")

# check components
for component in ${COMPONENTS[@]}; do
sozo component entity $component --world $WORLD_ADDRESS > /dev/null
done

# check systems
for system in ${SYSTEMS[@]}; do
SYSTEM_OUTPUT=$(sozo system get $system --world $WORLD_ADDRESS)
if [[ "$SYSTEM_OUTPUT" == "0x0" ]]; then
echo "Error: $system is not deployed"
exit 1
fi
done

# enable system -> component authorizations
CREATE_GAME_COMPONENTS=("Game" "Market" "Name" "Player" "Risks")
JOIN_GAME_COMPONENTS=("Game" "Player")
SET_NAME_COMPONENTS=("Name")
BUY_COMPONENTS=("Drug" "Market" "Name" "Player")
SELL_COMPONENTS=("Drug" "Market" "Name" "Player")
TRAVEL_COMPONENTS=("Player")
TRAVEL_COMPONENTS=("Player" "Market")
DECIDE_COMPONENTS=("Player" "Drug")

for component in ${CREATE_GAME_COMPONENTS[@]}; do
sozo auth writer $component create_game --world $WORLD_ADDRESS
sozo auth writer $component create_game --world $WORLD_ADDRESS --rpc-url $RPC_URL
sleep 0.1
done

for component in ${JOIN_GAME_COMPONENTS[@]}; do
sozo auth writer $component join_game --world $WORLD_ADDRESS
sozo auth writer $component join_game --world $WORLD_ADDRESS --rpc-url $RPC_URL
sleep 0.1
done

for component in ${SET_NAME_COMPONENTS[@]}; do
sozo auth writer $component set_name --world $WORLD_ADDRESS
sozo auth writer $component set_name --world $WORLD_ADDRESS --rpc-url $RPC_URL
sleep 0.1
done

for component in ${BUY_COMPONENTS[@]}; do
sozo auth writer $component buy --world $WORLD_ADDRESS
sozo auth writer $component buy --world $WORLD_ADDRESS --rpc-url $RPC_URL
sleep 0.1
done

for component in ${SELL_COMPONENTS[@]}; do
sozo auth writer $component sell --world $WORLD_ADDRESS
sozo auth writer $component sell --world $WORLD_ADDRESS --rpc-url $RPC_URL
sleep 0.1
done

for component in ${TRAVEL_COMPONENTS[@]}; do
sozo auth writer $component travel --world $WORLD_ADDRESS
sozo auth writer $component travel --world $WORLD_ADDRESS --rpc-url $RPC_URL
sleep 0.1
done

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

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

Expand Down
60 changes: 59 additions & 1 deletion src/components/market.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ struct Market {
quantity: usize,
}

#[derive(Copy, Drop)]
struct PricingInfos {
min_price: u128,
max_price: u128,
min_qty: u128,
max_qty: u128,
}

#[generate_trait]
impl MarketImpl of MarketTrait {
#[inline(always)]
Expand All @@ -34,6 +42,56 @@ impl MarketImpl of MarketTrait {
let payout = cash - (k / (available + amount));
payout
}

#[inline(always)]
fn get_pricing_info(drug_id: felt252) -> PricingInfos {
if drug_id == 'Acid' {
PricingInfos {
min_price: 500 * SCALING_FACTOR,
max_price: 1000 * SCALING_FACTOR,
min_qty: 400,
max_qty: 900,
}
} else if drug_id == 'Weed' {
PricingInfos {
min_price: 250 * SCALING_FACTOR,
max_price: 500 * SCALING_FACTOR,
min_qty: 500,
max_qty: 1000,
}
} else if drug_id == 'Ludes' {
PricingInfos {
min_price: 10 * SCALING_FACTOR,
max_price: 50 * SCALING_FACTOR,
min_qty: 800,
max_qty: 2000,
}
} else if drug_id == 'Speed' {
PricingInfos {
min_price: 50 * SCALING_FACTOR,
max_price: 250 * SCALING_FACTOR,
min_qty: 600,
max_qty: 1500,
}
} else if drug_id == 'Heroin' {
PricingInfos {
min_price: 1000 * SCALING_FACTOR,
max_price: 2000 * SCALING_FACTOR,
min_qty: 300,
max_qty: 700,
}
} else if drug_id == 'Cocaine' {
PricingInfos {
min_price: 2000 * SCALING_FACTOR,
max_price: 6000 * SCALING_FACTOR,
min_qty: 250,
max_qty: 600,
}
} else {
panic(array!['invalid drug_id']);
PricingInfos { min_price: 0, max_price: 0, min_qty: 0, max_qty: 0, }
}
}
}

fn normalize(amount: usize, market: Market) -> (u128, u128, u128) {
Expand All @@ -44,7 +102,7 @@ fn normalize(amount: usize, market: Market) -> (u128, u128, u128) {


#[test]
#[should_panic(expected: ('not enough liquidity', ))]
#[should_panic(expected: ('not enough liquidity',))]
fn test_not_enough_quantity() {
let mut market = Market {
game_id: 0, location_id: 0, drug_id: 0, cash: SCALING_FACTOR * 1, quantity: 1
Expand Down
25 changes: 19 additions & 6 deletions src/components/risks.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use option::OptionTrait;
use debug::PrintTrait;

use rollyourown::constants::SCALING_FACTOR;
use rollyourown::PlayerStatus;

#[derive(Component, Copy, Drop, Serde)]
struct Risks {
Expand All @@ -17,8 +18,20 @@ struct Risks {
#[generate_trait]
impl RisksImpl of RisksTrait {
#[inline(always)]
fn travel(ref self: Risks, seed: felt252) -> bool {
occurs(seed, self.travel)
fn travel(ref self: Risks, seed: felt252) -> PlayerStatus {
if occurs(seed, self.travel) {
let seed = pedersen::pedersen(seed, seed);
let entropy: u256 = seed.into();
let result: u128 = entropy.low % 100;

// more bias towards gang encounter
return match result <= 40 {
bool::False => PlayerStatus::BeingMugged(()),
bool::True => PlayerStatus::BeingArrested(()),
};
}

return PlayerStatus::Normal(());
}

#[inline(always)]
Expand All @@ -43,19 +56,19 @@ fn occurs(seed: felt252, likelihood: u8) -> bool {
fn test_never_occurs() {
let seed = pedersen::pedersen(1, 1);
let mut risks = Risks { game_id: 0, location_id: 0, travel: 0, run: 0 };
let event = risks.travel(seed);
let player_status = risks.travel(seed);

assert(event == bool::False, 'event occured');
assert(player_status == PlayerStatus::Normal(()), 'event occured');
}

#[test]
#[available_gas(1000000)]
fn test_always_occurs() {
let seed = pedersen::pedersen(1, 1);
let mut risks = Risks { game_id: 0, location_id: 0, travel: 100, run: 0 };
let event = risks.travel(seed);
let player_status = risks.travel(seed);

assert(event == bool::True, 'event did not occur');
assert(player_status != PlayerStatus::Normal(()), 'event did not occur');
}

#[test]
Expand Down
17 changes: 8 additions & 9 deletions src/constants.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,15 @@ const SCALING_FACTOR: u128 = 10_000;
const TRAVEL_RISK: u8 = 30; // 30% chance of mugged
const RUN_CHANCE: u8 = 50; // 50% chance of successfully getting away

const BASE_PAYMENT: u128 = 400_0000; // base payment is $400

// max drug price is $300 = MAX_CASH / MIN_QUANTITY
// min drug price is $50 = MIN_CASH / MAX_QUANTITY
const MAX_QUANTITY: usize = 500;
const MIN_QUANITTY: usize = 200;
const MAX_CASH: u128 = 100_000_0000; // $100k
const MIN_CASH: u128 = 25_000_0000; // $25k
const BASE_PAYMENT: u128 = 500_0000; // base payment is $500

// starting stats
const STARTING_CASH: u128 = 2000_0000; // $2000
const STARTING_CASH: u128 = 4000_0000; // $4000
const STARTING_BAG_LIMIT: usize = 100; // inventory size
const STARTING_HEALTH: u8 = 100;

// market eventsks
const PRICE_VAR: u8 = 3; // 3% chance
const MIN_PRICE_VAR: u8 = 20; // 20%
const MAX_PRICE_VAR: u8 = 50; // 50%
const MAX_EXTRA_PRICE_VAR: u8 = 42; // 42%
33 changes: 20 additions & 13 deletions src/systems/create.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@ mod create_game {
use rollyourown::components::market::Market;
use rollyourown::components::drug::{Drug, DrugTrait};
use rollyourown::components::location::{Location, LocationTrait};
use rollyourown::components::market::{MarketTrait};
use rollyourown::constants::{
SCALING_FACTOR, TRAVEL_RISK, RUN_CHANCE, MIN_CASH, MAX_CASH, MIN_QUANITTY, MAX_QUANTITY,
STARTING_CASH, STARTING_HEALTH, STARTING_BAG_LIMIT
SCALING_FACTOR, TRAVEL_RISK, RUN_CHANCE, STARTING_CASH, STARTING_HEALTH, STARTING_BAG_LIMIT
};
use rollyourown::utils::random;
use debug::PrintTrait;

#[event]
#[derive(Drop, starknet::Event)]
Expand Down Expand Up @@ -75,14 +76,14 @@ mod create_game {
creator: ctx.origin,
};

set !(ctx.world, (game, player));
set!(ctx.world, (game, player));

let mut locations = LocationTrait::all();
loop {
match locations.pop_front() {
Option::Some(location_id) => {
//set location entity
set !(
//set risks entity
set!(
ctx.world,
(Risks {
game_id, location_id: *location_id, travel: TRAVEL_RISK, run: RUN_CHANCE
Expand All @@ -97,12 +98,20 @@ mod create_game {
match drugs.pop_front() {
Option::Some(drug_id) => {
seed = pedersen::pedersen(seed, *drug_id);
let market_cash = random(seed, MIN_CASH, MAX_CASH);
let rand = random(seed, MIN_QUANITTY.into(), MAX_QUANTITY.into());
let market_quantity: usize = rand.try_into().unwrap();
let pricing_infos = MarketTrait::get_pricing_info(*drug_id);
let market_price = random(
seed, pricing_infos.min_price, pricing_infos.max_price
);
let market_quantity: usize = random(
seed, pricing_infos.min_qty, pricing_infos.max_qty
)
.try_into()
.unwrap();

let market_cash = market_quantity.into() * market_price;

//set market entity
set !(
set!(
ctx.world,
(Market {
game_id,
Expand All @@ -126,12 +135,10 @@ mod create_game {
};

// emit player joined
emit !(
ctx.world, PlayerJoined { game_id, player_id: ctx.origin, location_id: location_id }
);
emit!(ctx.world, PlayerJoined { game_id, player_id: ctx.origin, location_id: location_id });

// emit game created
emit !(
emit!(
ctx.world,
GameCreated { game_id, creator: ctx.origin, start_time, max_players, max_turns }
);
Expand Down
Loading

1 comment on commit c4db28c

@vercel
Copy link

@vercel vercel bot commented on c4db28c Sep 15, 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.