Skip to content

Commit

Permalink
inline methods
Browse files Browse the repository at this point in the history
  • Loading branch information
broody committed Sep 7, 2023
1 parent a4be2d0 commit 2a0d9c4
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 50 deletions.
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
13 changes: 5 additions & 8 deletions src/components/game.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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
26 changes: 12 additions & 14 deletions src/components/market.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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
12 changes: 5 additions & 7 deletions src/components/player.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,14 @@ struct Player {
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 {
if self.turns_remaining == 0 {
return false;
}

Expand Down
15 changes: 6 additions & 9 deletions src/components/risks.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,23 @@ struct Risks {
game_id: u32,
#[key]
location_id: felt252,
// travel risk probabilities
travel: u8,
hurt: u8,
mugged: u8,
arrested: u8,
}

trait RisksTrait {
fn travel(self: @Risks, seed: felt252) -> (bool, TravelResult);
}

#[generate_trait]
impl RisksImpl of RisksTrait {
fn travel(self: @Risks, seed: felt252) -> (bool, TravelResult) {
#[inline(always)]
fn travel(ref self: Risks, seed: felt252) -> (bool, TravelResult) {
let mut seed = seed;
let mut health_loss = 0;
let mut arrested = false;
let mut mugged = false;
let mut event_occured = false;

if occurs(seed, *self.travel) {
if occurs(seed, self.travel) {
seed = pedersen::pedersen(seed, seed);
event_occured = true;

Expand All @@ -58,7 +55,7 @@ fn occurs(seed: felt252, likelihood: u8) -> bool {
#[available_gas(1000000)]
fn test_never_occurs() {
let seed = pedersen::pedersen(1, 1);
let risks = Risks { game_id: 0, location_id: 0, travel: 0, hurt: 0, mugged: 0, arrested: 0, };
let mut risks = Risks { game_id: 0, location_id: 0, travel: 0, hurt: 0, mugged: 0, arrested: 0, };
let (event_occured, result) = risks.travel(seed);

assert(!event_occured, 'event occured');
Expand All @@ -71,7 +68,7 @@ fn test_never_occurs() {
#[available_gas(1000000)]
fn test_always_occurs() {
let seed = pedersen::pedersen(1, 1);
let risks = Risks {
let mut risks = Risks {
game_id: 0, location_id: 0, travel: 100, hurt: 100, mugged: 100, arrested: 100,
};
let (event_occured, result) = risks.travel(seed);
Expand Down
3 changes: 2 additions & 1 deletion src/systems/trade.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ mod buy {

use rollyourown::components::name::Name;
use rollyourown::components::drug::Drug;
use rollyourown::components::player::Player;
use rollyourown::components::player::{Player, PlayerTrait};
use rollyourown::components::location::Location;
use rollyourown::components::game::{Game, GameTrait};
use rollyourown::components::risks::{Risks, RisksTrait};
Expand Down Expand Up @@ -45,6 +45,7 @@ mod buy {

let mut player = get!(ctx.world, (game_id, player_id).into(), Player);
assert(player.location_id == location_id, 'player is not at location');
assert(player.can_continue(), 'player cannot trade');

let mut market = get!(ctx.world, (game_id, location_id, drug_id).into(), Market);

Expand Down
4 changes: 2 additions & 2 deletions src/systems/travel.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ mod travel {

let player_id = ctx.origin;
let mut player = get!(ctx.world, (game_id, player_id).into(), Player);
assert(player.can_continue(), 'player cannot continue');
assert(player.can_continue(), 'player cannot travel');
assert(player.location_id != next_location_id, 'already at location');

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

let (event_occured, result) = risks.travel(seed);
Expand Down

0 comments on commit 2a0d9c4

Please sign in to comment.