Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
broody committed Sep 11, 2023
1 parent 90ec176 commit 2a2c461
Show file tree
Hide file tree
Showing 52 changed files with 892 additions and 786 deletions.
5 changes: 5 additions & 0 deletions scripts/default_auth.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 Down Expand Up @@ -58,4 +59,8 @@ 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."
4 changes: 3 additions & 1 deletion src/components/player.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ struct Player {
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,
status: PlayerStatus,
}

#[generate_trait]
Expand Down
8 changes: 1 addition & 7 deletions src/components/risks.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,6 @@ use debug::PrintTrait;

use rollyourown::constants::SCALING_FACTOR;

#[derive(Drop, Copy)]
struct TravelResult {
arrested: bool,
mugged: bool,
health_loss: u8,
}

#[derive(Component, Copy, Drop, Serde)]
struct Risks {
#[key]
Expand All @@ -28,6 +21,7 @@ impl RisksImpl of RisksTrait {
occurs(seed, self.travel)
}

#[inline(always)]
fn run(ref self: Risks, seed: felt252) -> bool {
occurs(seed, self.run)
}
Expand Down
25 changes: 13 additions & 12 deletions src/constants.cairo
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
const SCALING_FACTOR: u128 = 10_000;

const TRAVEL_RISK: u8 = 100; // 30% chance of mugged
const RUN_CHANCE: u8 = 30; // 30% chance of successfully getting away
const TRAVEL_RISK: u8 = 30; // 30% chance of mugged
const RUN_CHANCE: u8 = 50; // 50% chance of successfully getting away

const RUN_PENALTY: u8 = 30; // 30% of cash lost
const PAY_PENALTY: u8 = 10; // 10% of cash lost
const BASE_PAYMENT: u128 = 400_0000; // base payment is $400

// max drug price is $300
// min drug price is $2
const MAX_QUANTITY: usize = 50_000;
const MIN_QUANITTY: usize = 20_000;
const MAX_CASH: u128 = 60_000_000_000; // $6Mil
const MIN_CASH: u128 = 1_000_000_000; // $100k
// 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

// cash players start with
const STARTING_CASH: u128 = 20_000_000; // $2000
// starting stats
const STARTING_CASH: u128 = 2000_0000; // $2000
const STARTING_BAG_LIMIT: usize = 100; // inventory size
const STARTING_HEALTH: u8 = 100;
59 changes: 25 additions & 34 deletions src/systems/create.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ mod create_game {
use rollyourown::components::location::{Location, LocationTrait};
use rollyourown::constants::{
SCALING_FACTOR, TRAVEL_RISK, RUN_CHANCE, MIN_CASH, MAX_CASH, MIN_QUANITTY, MAX_QUANTITY,
STARTING_CASH
STARTING_CASH, STARTING_HEALTH, STARTING_BAG_LIMIT
};
use rollyourown::utils::random;

Expand All @@ -46,46 +46,37 @@ mod create_game {
location_id: felt252,
}


fn execute(
ctx: Context, start_time: u64, max_players: usize, max_turns: usize
) -> (u32, ContractAddress) {
let game_id = ctx.world.uuid();

// game entity
set !(
ctx.world,
(Game {
game_id,
start_time,
max_players,
num_players: 1, // caller auto joins
max_turns,
is_finished: false,
creator: ctx.origin,
})
);

let seed = starknet::get_tx_info().unbox().transaction_hash;
let location_id = LocationTrait::random(seed);
// player entity
set !(
ctx.world,
(
Player {
game_id,
player_id: ctx.origin,
location_id,
cash: STARTING_CASH,
health: 100,
turns_remaining: max_turns,
status: PlayerStatus::Normal(()),
},
)
);

// TODO: spawn locations with risk profiles balanced
// with market pricing
let player = Player {
game_id,
player_id: ctx.origin,
location_id,
cash: STARTING_CASH,
health: STARTING_HEALTH,
drug_count: 0,
bag_limit: STARTING_BAG_LIMIT,
turns_remaining: max_turns,
status: PlayerStatus::Normal(()),
};

let game = Game {
game_id,
start_time,
max_players,
num_players: 1, // caller auto joins
max_turns,
is_finished: false,
creator: ctx.origin,
};

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

let mut locations = LocationTrait::all();
loop {
match locations.pop_front() {
Expand Down
121 changes: 91 additions & 30 deletions src/systems/decide.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ mod decide {
use starknet::ContractAddress;

use dojo::world::Context;

use rollyourown::PlayerStatus;
use rollyourown::constants::{RUN_PENALTY, PAY_PENALTY};
use rollyourown::constants::BASE_PAYMENT;
use rollyourown::components::game::{Game, GameTrait};
use rollyourown::components::risks::{Risks, RisksTrait};
use rollyourown::components::player::{Player, PlayerTrait};
use rollyourown::components::drug::{Drug, DrugTrait};

#[derive(Copy, Drop, Serde, PartialEq)]
enum Action {
Expand All @@ -30,6 +32,8 @@ mod decide {
enum Event {
Decision: Decision,
Consequence: Consequence,
CashLoss: CashLoss,
DrugLoss: DrugLoss,
}

#[derive(Drop, starknet::Event)]
Expand All @@ -43,48 +47,105 @@ mod decide {
struct Consequence {
game_id: u32,
player_id: ContractAddress,
outcome: Outcome
outcome: Outcome,
}

fn execute(ctx: Context, game_id: u32, action: Action, next_location_id: felt252) {
let game = get !(ctx.world, game_id, Game);
assert(game.tick(), 'game cannot progress');
#[derive(Drop, starknet::Event)]
struct CashLoss {
game_id: u32,
player_id: ContractAddress,
amount: u128
}

#[derive(Drop, starknet::Event)]
struct DrugLoss {
game_id: u32,
player_id: ContractAddress,
drug_id: felt252,
quantity: usize
}

fn execute(ctx: Context, game_id: u32, action: Action, next_location_id: felt252) {
let player_id = ctx.origin;
let mut player = get !(ctx.world, (game_id, player_id).into(), Player);
assert(player.status != PlayerStatus::Normal(()), 'player response not needed');

let outcome = match action {
Action::Pay => {
emit !(ctx.world, Decision { game_id, player_id, action: Action::Pay });

player.cash -= 1;
Outcome::Paid(())
},
Action::Run => {
emit !(ctx.world, Decision { game_id, player_id, action: Action::Run });

let mut risks = get !(ctx.world, (game_id, player.location_id).into(), Risks);
let seed = starknet::get_tx_info().unbox().transaction_hash;
let got_away = risks.run(seed);

match got_away {
bool::False => {
player.cash -= 1;
Outcome::Captured(())
},
bool::True => {
Outcome::Escaped(())
}
}
},
let (outcome, cash_loss) = match action {
Action::Pay => pay(ctx, game_id, player_id, player.cash),
Action::Run => run(ctx, game_id, player_id, player.location_id, player.cash),
};

player.cash -= cash_loss;
player.status = PlayerStatus::Normal(());
player.location_id = next_location_id;
player.turns_remaining -= 1;
set !(ctx.world, (player));

set !(ctx.world, (player));
emit !(ctx.world, Consequence { game_id, player_id, outcome });
}

// Player will hand over either 20% of their cash or $400, which ever is more
fn pay(
ctx: Context, game_id: u32, player_id: ContractAddress, player_cash: u128
) -> (Outcome, u128) {
assert(player_cash >= BASE_PAYMENT, 'not enough cash kid');
let cash_loss = cmp::max(player_cash / 5, BASE_PAYMENT);

emit !(ctx.world, Decision { game_id, player_id, action: Action::Pay });
emit !(ctx.world, CashLoss { game_id, player_id, amount: cash_loss });
(Outcome::Paid(()), cash_loss)
}

// Player will try to run and can escape. However, if they are captured they lose 50% of everything
fn run(
ctx: Context,
game_id: u32,
player_id: ContractAddress,
location_id: felt252,
player_cash: u128
) -> (Outcome, u128) {
let mut risks = get !(ctx.world, (game_id, location_id).into(), Risks);
let seed = starknet::get_tx_info().unbox().transaction_hash;
let got_away = risks.run(seed);

emit !(ctx.world, Decision { game_id, player_id, action: Action::Run });
match got_away {
bool::False => {
let cash_loss = player_cash / 2;
halve_drugs(ctx, game_id, player_id);

emit !(ctx.world, CashLoss { game_id, player_id, amount: cash_loss });
(Outcome::Captured(()), cash_loss)
},
bool::True => {
(Outcome::Escaped(()), 0)
}
}
}

// sorry fren, u jus lost half ur stash, ngmi
fn halve_drugs(ctx: Context, game_id: u32, player_id: ContractAddress) {
let mut drugs = DrugTrait::all();
loop {
match drugs.pop_front() {
Option::Some(drug_id) => {
let mut drug = get !(ctx.world, (game_id, player_id, *drug_id).into(), Drug);
if (drug.quantity != 0) {
drug.quantity /= 2;

emit !(
ctx.world,
DrugLoss {
game_id, player_id, drug_id: *drug_id, quantity: drug.quantity
}
);
set !(ctx.world, (drug));
}
},
Option::None(()) => {
break ();
}
};
};
}
}
Loading

0 comments on commit 2a2c461

Please sign in to comment.