diff --git a/src/components/drug.cairo b/src/components/drug.cairo index 503ed6575..678d547f7 100644 --- a/src/components/drug.cairo +++ b/src/components/drug.cairo @@ -13,10 +13,7 @@ struct Drug { quantity: usize, } -trait DrugTrait { - fn all() -> Span; -} - +#[generate_trait] impl DrugImpl of DrugTrait { fn all() -> Span { let mut drugs = array::ArrayTrait::new(); diff --git a/src/components/game.cairo b/src/components/game.cairo index 990bf977d..b13222e8d 100644 --- a/src/components/game.cairo +++ b/src/components/game.cairo @@ -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; } diff --git a/src/components/location.cairo b/src/components/location.cairo index c4d956b0a..dd6937ead 100644 --- a/src/components/location.cairo +++ b/src/components/location.cairo @@ -5,11 +5,7 @@ use starknet::ContractAddress; struct Location {} -trait LocationTrait { - fn all() -> Span; - fn random(seed: felt252) -> felt252; -} - +#[generate_trait] impl LocationImpl of LocationTrait { fn all() -> Span { let mut locations = array::ArrayTrait::new(); diff --git a/src/components/market.cairo b/src/components/market.cairo index 7874cb55b..4e436180b 100644 --- a/src/components/market.cairo +++ b/src/components/market.cairo @@ -16,21 +16,19 @@ 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)); @@ -38,17 +36,17 @@ impl MarketImpl of MarketTrait { } } -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); @@ -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); @@ -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); diff --git a/src/components/player.cairo b/src/components/player.cairo index 62a962aea..69b706411 100644 --- a/src/components/player.cairo +++ b/src/components/player.cairo @@ -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; } diff --git a/src/components/risks.cairo b/src/components/risks.cairo index 371494574..e58486940 100644 --- a/src/components/risks.cairo +++ b/src/components/risks.cairo @@ -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; @@ -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'); @@ -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); diff --git a/src/systems/trade.cairo b/src/systems/trade.cairo index 5c59b1ed3..e328b624f 100644 --- a/src/systems/trade.cairo +++ b/src/systems/trade.cairo @@ -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}; @@ -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); diff --git a/src/systems/travel.cairo b/src/systems/travel.cairo index 2bcb4c33f..5757f50c9 100644 --- a/src/systems/travel.cairo +++ b/src/systems/travel.cairo @@ -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);