Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
broody committed Sep 10, 2023
1 parent 90ec176 commit 3084e79
Show file tree
Hide file tree
Showing 41 changed files with 560 additions and 613 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
18 changes: 10 additions & 8 deletions src/constants.cairo
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
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 = 100; // 20% chance of mugged
const RUN_CHANCE: u8 = 0; // 60% chance of successfully getting away

const RUN_PENALTY: u8 = 30; // 30% of cash lost
const PAY_PENALTY: u8 = 10; // 10% of cash lost
// fixed payment amount, this should be dynamic in thef future
const PAYMENT: u128 = 250_0000; // $250

// 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
const MAX_CASH: u128 = 6_000_000_0000; // $6Mil
const MIN_CASH: u128 = 100_000_0000; // $100k

// 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
69 changes: 58 additions & 11 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::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,34 +47,50 @@ 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 });
assert(player.cash >= PAYMENT, 'not enough cash kid');
player.cash -= PAYMENT;

player.cash -= 1;
emit !(ctx.world, Decision { game_id, player_id, action: Action::Pay });
emit !(ctx.world, CashLoss { game_id, player_id, amount: PAYMENT });
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);

emit !(ctx.world, Decision { game_id, player_id, action: Action::Run });
match got_away {
bool::False => {
player.cash -= 1;
player.cash /= 2;
halve_drugs(ctx, game_id, player_id);

emit !(ctx.world, CashLoss { game_id, player_id, amount: player.cash });
Outcome::Captured(())
},
bool::True => {
Expand All @@ -83,8 +103,35 @@ mod decide {
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 });
}

// bruh, u losing half ur stash
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 ();
}
};
};
}
}
58 changes: 23 additions & 35 deletions src/systems/join.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ mod join_game {
use rollyourown::components::game::Game;
use rollyourown::components::player::Player;
use rollyourown::components::location::{Location, LocationTrait};
use rollyourown::constants::{SCALING_FACTOR, STARTING_CASH};
use rollyourown::constants::{
SCALING_FACTOR, STARTING_CASH, STARTING_HEALTH, STARTING_BAG_LIMIT
};

#[event]
#[derive(Drop, starknet::Event)]
Expand All @@ -27,48 +29,34 @@ mod join_game {
}

fn execute(ctx: Context, game_id: u32) -> ContractAddress {
let player_id = ctx.origin;
let block_info = starknet::get_block_info().unbox();

let game = get !(ctx.world, game_id, (Game));
let mut game = get !(ctx.world, game_id, (Game));
assert(!game.is_finished, 'game is finished');
assert(game.max_players > game.num_players, 'game is full');
assert(game.start_time >= block_info.block_timestamp, 'already started');

game.num_players += 1;

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

// update num players joined
set !(
ctx.world,
(Game {
game_id,
start_time: game.start_time,
max_players: game.max_players,
num_players: game.num_players + 1,
max_turns: game.max_turns,
is_finished: false,
creator: game.creator,
})
);

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

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

set !(ctx.world, (game, player));
emit !(ctx.world, PlayerJoined { game_id, player_id, location_id });

player_id
}
}
Loading

0 comments on commit 3084e79

Please sign in to comment.