Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: balance game #136

Merged
merged 25 commits into from
Sep 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,13 @@ flowchart TD
C --> |Buys / Sells drugs on local markets|D[Select next location to travel to]
D --> |Player travels without incident|END[Turn ends]
D --> F[Player is Mugged]
F --> F1[Fight]
F --> F1[Pay]
F --> F2[Run]
F2 --> F12[Win] --> END
F2 --> L[Lose]
L --> |Player loses their stash|END
D --> G[Chased by Cops]
G --> F1[Fight]
G --> F1[Pay]
G --> F2[Run]
F1 --> F12
F1 --> L
Expand Down
5 changes: 2 additions & 3 deletions scripts/default_auth.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@
set -euo pipefail
pushd $(dirname "$0")/..

#export RPC_URL="http://localhost:5050";
export RPC_URL="https://api.cartridge.gg/x/rollyourown/katana";

export RPC_URL="http://localhost:5050";
#export RPC_URL="https://api.cartridge.gg/x/rollyourown/katana";
export WORLD_ADDRESS="0x3c3dfeb374720dfd73554dc2b9e0583cb9668efb3055d07d1533afa5d219fd5";

# enable system -> component authorizations
Expand Down
3 changes: 2 additions & 1 deletion src/components/location.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ impl LocationImpl of LocationTrait {
locations.span()
}

fn random(seed: felt252) -> felt252 {
fn random() -> felt252 {
let seed = starknet::get_tx_info().unbox().transaction_hash;
let locations = LocationImpl::all();
let seed: u256 = seed.into();
let len: u128 = locations.len().into();
Expand Down
42 changes: 21 additions & 21 deletions src/components/market.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -45,45 +45,45 @@ impl MarketImpl of MarketTrait {

#[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' {
if drug_id == 'Ludes' {
PricingInfos {
min_price: 10 * SCALING_FACTOR,
max_price: 50 * SCALING_FACTOR,
max_price: 60 * SCALING_FACTOR,
min_qty: 800,
max_qty: 2000,
}
} else if drug_id == 'Speed' {
PricingInfos {
min_price: 50 * SCALING_FACTOR,
max_price: 250 * SCALING_FACTOR,
max_price: 300 * SCALING_FACTOR,
min_qty: 600,
max_qty: 1500,
}
} else if drug_id == 'Weed' {
PricingInfos {
min_price: 200 * SCALING_FACTOR,
max_price: 700 * SCALING_FACTOR,
min_qty: 500,
max_qty: 1000,
}
} else if drug_id == 'Acid' {
PricingInfos {
min_price: 500 * SCALING_FACTOR,
max_price: 1800 * SCALING_FACTOR,
min_qty: 400,
max_qty: 900,
}
} else if drug_id == 'Heroin' {
PricingInfos {
min_price: 1000 * SCALING_FACTOR,
max_price: 2000 * SCALING_FACTOR,
min_price: 1200 * SCALING_FACTOR,
max_price: 4000 * 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_price: 3000 * SCALING_FACTOR,
max_price: 8000 * SCALING_FACTOR,
min_qty: 250,
max_qty: 600,
}
Expand Down
1 change: 1 addition & 0 deletions src/components/player.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ struct Player {
location_id: felt252,
cash: u128,
health: u8,
run_attempts: u8,
drug_count: usize,
bag_limit: usize,
turns_remaining: usize,
Expand Down
32 changes: 20 additions & 12 deletions src/components/risks.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use traits::{Into, TryInto};
use option::OptionTrait;
use debug::PrintTrait;

use rollyourown::constants::SCALING_FACTOR;
use rollyourown::constants::{SCALING_FACTOR, COPS_DRUG_THRESHOLD, ENCOUNTER_BIAS_GANGS};
use rollyourown::PlayerStatus;

#[derive(Component, Copy, Drop, Serde)]
Expand All @@ -12,31 +12,39 @@ struct Risks {
#[key]
location_id: felt252,
travel: u8,
run: u8,
capture: u8,
}

#[generate_trait]
impl RisksImpl of RisksTrait {
#[inline(always)]
fn travel(ref self: Risks, seed: felt252) -> PlayerStatus {
fn travel(ref self: Risks, seed: felt252, cash: u128, drug_count: usize) -> 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 match result <= ENCOUNTER_BIAS_GANGS {
bool::False => {
if drug_count < COPS_DRUG_THRESHOLD {
return PlayerStatus::BeingMugged;
}

PlayerStatus::BeingArrested
},
bool::True => {
PlayerStatus::BeingMugged
}
};
}

return PlayerStatus::Normal(());
return PlayerStatus::Normal;
}

#[inline(always)]
fn run(ref self: Risks, seed: felt252) -> bool {
occurs(seed, self.run)
occurs(seed, self.capture)
}
}

Expand All @@ -55,8 +63,8 @@ fn occurs(seed: felt252, likelihood: u8) -> bool {
#[available_gas(1000000)]
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 player_status = risks.travel(seed);
let mut risks = Risks { game_id: 0, location_id: 0, travel: 0, capture: 0 };
let player_status = risks.travel(seed, 0, 0);

assert(player_status == PlayerStatus::Normal(()), 'event occured');
}
Expand All @@ -65,8 +73,8 @@ fn test_never_occurs() {
#[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 player_status = risks.travel(seed);
let mut risks = Risks { game_id: 0, location_id: 0, travel: 100, capture: 0 };
let player_status = risks.travel(seed, 1, COPS_DRUG_THRESHOLD);

assert(player_status != PlayerStatus::Normal(()), 'event did not occur');
}
Expand Down
13 changes: 8 additions & 5 deletions src/constants.cairo
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
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 TRAVEL_RISK: u8 = 60; // 60% chance of travel encounter
const CAPTURE_RISK: u8 = 60; // 60% chance of capture

const ENCOUNTER_BIAS_GANGS: u128 = 75;
const COPS_DRUG_THRESHOLD: usize = 5; // cops encounter threshold

const BASE_PAYMENT: u128 = 400_0000; // base payment is $400
const HEALTH_IMPACT: u8 = 10;
const GANGS_PAYMENT: usize = 20;

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

// market eventsks
// market events
const PRICE_VAR: u8 = 3; // 3% chance
const MIN_PRICE_VAR: u8 = 20; // 20%
const MAX_PRICE_VAR: u8 = 50; // 50%
Expand Down
14 changes: 9 additions & 5 deletions src/systems/create.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ mod create_game {
use rollyourown::components::location::{Location, LocationTrait};
use rollyourown::components::market::{MarketTrait};
use rollyourown::constants::{
SCALING_FACTOR, TRAVEL_RISK, RUN_CHANCE, STARTING_CASH, STARTING_HEALTH, STARTING_BAG_LIMIT
SCALING_FACTOR, TRAVEL_RISK, CAPTURE_RISK, STARTING_CASH, STARTING_HEALTH,
STARTING_BAG_LIMIT
};
use rollyourown::utils::random;
use debug::PrintTrait;
Expand Down Expand Up @@ -51,19 +52,19 @@ mod create_game {
ctx: Context, start_time: u64, max_players: usize, max_turns: usize
) -> (u32, ContractAddress) {
let game_id = ctx.world.uuid();
let seed = starknet::get_tx_info().unbox().transaction_hash;
let location_id = LocationTrait::random(seed);
let location_id = LocationTrait::random();

let player = Player {
game_id,
player_id: ctx.origin,
status: PlayerStatus::Normal(()),
location_id,
cash: STARTING_CASH,
health: STARTING_HEALTH,
run_attempts: 0,
drug_count: 0,
bag_limit: STARTING_BAG_LIMIT,
turns_remaining: max_turns,
status: PlayerStatus::Normal(()),
};

let game = Game {
Expand All @@ -86,7 +87,10 @@ mod create_game {
set!(
ctx.world,
(Risks {
game_id, location_id: *location_id, travel: TRAVEL_RISK, run: RUN_CHANCE
game_id,
location_id: *location_id,
travel: TRAVEL_RISK,
capture: CAPTURE_RISK
})
);

Expand Down
Loading
Loading