Skip to content

Commit

Permalink
feat: initial location select on game start
Browse files Browse the repository at this point in the history
  • Loading branch information
broody committed Sep 19, 2023
1 parent 2d529c9 commit e65be3e
Show file tree
Hide file tree
Showing 16 changed files with 164 additions and 137 deletions.
4 changes: 2 additions & 2 deletions src/components/market.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ impl MarketImpl of MarketTrait {
}
} else {
panic(array!['invalid drug_id']);
PricingInfos { min_price: 0, max_price: 0, min_qty: 0, max_qty: 0, }
PricingInfos { min_price: 0, max_price: 0, min_qty: 0, max_qty: 0, }
}
}
}
Expand All @@ -102,7 +102,7 @@ fn normalize(amount: usize, market: Market) -> (u128, u128, u128) {


#[test]
#[should_panic(expected: ('not enough liquidity', ))]
#[should_panic(expected: ('not enough liquidity',))]
fn test_not_enough_quantity() {
let mut market = Market {
game_id: 0, location_id: 0, drug_id: 0, cash: SCALING_FACTOR * 1, quantity: 1
Expand Down
15 changes: 6 additions & 9 deletions src/systems/create.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -43,21 +43,19 @@ mod create_game {
#[derive(Drop, starknet::Event)]
struct PlayerJoined {
game_id: u32,
player_id: ContractAddress,
location_id: felt252,
player_id: ContractAddress
}

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

let player = Player {
game_id,
player_id: ctx.origin,
status: PlayerStatus::Normal(()),
location_id,
status: PlayerStatus::Normal,
location_id: 0,
cash: STARTING_CASH,
health: STARTING_HEALTH,
run_attempts: 0,
Expand Down Expand Up @@ -139,13 +137,12 @@ mod create_game {
};

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

// emit game created
emit!(
ctx.world, GameCreated {
game_id, creator: ctx.origin, start_time, max_players, max_turns
}
ctx.world,
GameCreated { game_id, creator: ctx.origin, start_time, max_players, max_turns }
);

(game_id, ctx.origin)
Expand Down
5 changes: 2 additions & 3 deletions src/systems/decide.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,8 @@ mod decide {

emit!(ctx.world, Decision { game_id, player_id, action });
emit!(
ctx.world, Consequence {
game_id, player_id, outcome, health_loss, drug_loss, cash_loss
}
ctx.world,
Consequence { game_id, player_id, outcome, health_loss, drug_loss, cash_loss }
);
}

Expand Down
11 changes: 4 additions & 7 deletions src/systems/join.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ mod join_game {
#[derive(Drop, starknet::Event)]
struct PlayerJoined {
game_id: u32,
player_id: ContractAddress,
location_id: felt252,
player_id: ContractAddress
}

fn execute(ctx: Context, game_id: u32) -> ContractAddress {
Expand All @@ -37,13 +36,11 @@ mod join_game {

game.num_players += 1;

let location_id = LocationTrait::random();

let player = Player {
game_id,
player_id,
status: PlayerStatus::Normal(()),
location_id,
status: PlayerStatus::Normal,
location_id: 0,
cash: STARTING_CASH,
health: STARTING_HEALTH,
run_attempts: 0,
Expand All @@ -54,7 +51,7 @@ mod join_game {
};

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

player_id
}
Expand Down
2 changes: 1 addition & 1 deletion src/systems/set_name.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ mod set_name {
use rollyourown::components::name::Name;

fn execute(ctx: Context, game_id: u32, player_name: felt252) {
set!(ctx.world, (Name { game_id, player_id: ctx.origin, short_string: player_name, }))
set!(ctx.world, (Name { game_id, player_id: ctx.origin, short_string: player_name, }))
}
}
31 changes: 18 additions & 13 deletions src/systems/travel.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -51,25 +51,30 @@ mod travel {
assert(player.can_continue(), 'player cannot travel');
assert(player.location_id != next_location_id, 'already at location');

let mut risks: Risks = get!(ctx.world, (game_id, next_location_id).into(), Risks);
let seed = starknet::get_tx_info().unbox().transaction_hash;
player.status = risks.travel(seed, player.cash, player.drug_count);
if player.status != PlayerStatus::Normal {
set!(ctx.world, (player));
emit!(ctx.world, AdverseEvent { game_id, player_id, player_status: player.status });

return true;
}
// initial travel when game starts has no risk or events
if player.location_id != 0 {
let mut risks: Risks = get!(ctx.world, (game_id, next_location_id).into(), Risks);
let seed = starknet::get_tx_info().unbox().transaction_hash;
player.status = risks.travel(seed, player.cash, player.drug_count);
if player.status != PlayerStatus::Normal {
set!(ctx.world, (player));
emit!(ctx.world, AdverseEvent { game_id, player_id, player_status: player.status });

return true;
}

//market price fluctuation
market_events(ctx, game_id);
//market price fluctuation
market_events(ctx, game_id);

player.turns_remaining -= 1;
}

player.location_id = next_location_id;
player.turns_remaining -= 1;
set!(ctx.world, (player));

emit!(
ctx.world, Traveled {
ctx.world,
Traveled {
game_id, player_id, from_location: player.location_id, to_location: next_location_id
}
);
Expand Down
64 changes: 32 additions & 32 deletions src/tests/trade.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -19,36 +19,36 @@ use rollyourown::constants::SCALING_FACTOR;

const WEED_ID: felt252 = 0x57656564; // weed
const QUANTITY: usize = 3;
// #[test]
// #[available_gas(100000000)]
// fn test_trade() {
// let (world_address, game_id, player_id) = spawn_game(); // creator auto joins
// let world = IWorldDispatcher { contract_address: world_address };

// let player = get!(world, (game_id, player_id).into(), (Player));

// // market buy 3 weed
// let mut buy_calldata = array::ArrayTrait::<felt252>::new();
// buy_calldata.append(game_id.into());
// buy_calldata.append(player.location_id);
// buy_calldata.append(WEED_ID);
// buy_calldata.append(QUANTITY.into());
// world.execute('buy'.into(), buy_calldata);

// let player = get!(world, (game_id, player_id).into(), (Player));
// let player_drug = get!(world, (game_id, player_id, WEED_ID).into(), (Drug));
// assert(player.drug_count == QUANTITY, 'wrong drug count');
// assert(player_drug.quantity == QUANTITY, 'wrong purchase amount');

// // market sell 1 weed
// let mut sell_calldata = array::ArrayTrait::<felt252>::new();
// sell_calldata.append(game_id.into());
// sell_calldata.append(player.location_id);
// sell_calldata.append(WEED_ID);
// sell_calldata.append(1);
// world.execute('sell'.into(), sell_calldata);

// let player = get!(world, (game_id, player_id).into(), (Player));
// assert(player.drug_count == QUANTITY - 1, 'wrong sell amount');
// }

#[test]
#[available_gas(100000000)]
fn test_trade() {
let (world_address, game_id, player_id) = spawn_game(); // creator auto joins
let world = IWorldDispatcher { contract_address: world_address };

let player = get!(world, (game_id, player_id).into(), (Player));

// market buy 3 weed
let mut buy_calldata = array::ArrayTrait::<felt252>::new();
buy_calldata.append(game_id.into());
buy_calldata.append(player.location_id);
buy_calldata.append(WEED_ID);
buy_calldata.append(QUANTITY.into());
world.execute('buy'.into(), buy_calldata);

let player = get!(world, (game_id, player_id).into(), (Player));
let player_drug = get!(world, (game_id, player_id, WEED_ID).into(), (Drug));
assert(player.drug_count == QUANTITY, 'wrong drug count');
assert(player_drug.quantity == QUANTITY, 'wrong purchase amount');

// market sell 1 weed
let mut sell_calldata = array::ArrayTrait::<felt252>::new();
sell_calldata.append(game_id.into());
sell_calldata.append(player.location_id);
sell_calldata.append(WEED_ID);
sell_calldata.append(1);
world.execute('sell'.into(), sell_calldata);

let player = get!(world, (game_id, player_id).into(), (Player));
assert(player.drug_count == QUANTITY - 1, 'wrong sell amount');
}
74 changes: 37 additions & 37 deletions src/tests/travel.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -17,41 +17,41 @@ use rollyourown::tests::create::{spawn_game, spawn_player};
use rollyourown::constants::{TRAVEL_RISK, COPS_DRUG_THRESHOLD};

const WEED_ID: felt252 = 0x57656564; // weed
// #[test]
// #[available_gas(110000000)]
// fn test_travel_and_decision() {
// let (world_address, game_id, player_id) = spawn_game(); // creator auto joins
// let world = IWorldDispatcher { contract_address: world_address };
// let player = get!(world, (game_id, player_id).into(), (Player));

// let mut buy_calldata = array::ArrayTrait::<felt252>::new();
// buy_calldata.append(game_id.into());
// buy_calldata.append(player.location_id);
// buy_calldata.append(WEED_ID);
// buy_calldata.append(COPS_DRUG_THRESHOLD.into());
// world.execute('buy'.into(), buy_calldata);

// let brooklyn_id = 'Brooklyn';
// let mut travel_calldata = array::ArrayTrait::<felt252>::new();
// travel_calldata.append(game_id.into());
// travel_calldata.append(brooklyn_id);

// starknet::testing::set_transaction_hash(TRAVEL_RISK.into());
// world.execute('travel', travel_calldata);

// let player = get!(world, (game_id, player_id).into(), (Player));
// assert(player.status != PlayerStatus::Normal, 'incorrect status');
// assert(player.location_id != brooklyn_id, 'should not have traveled');

// let queens_id = 'Queens';
// let mut decision_calldata = array::ArrayTrait::<felt252>::new();
// decision_calldata.append(game_id.into());
// decision_calldata.append(0.into()); // 0 = pay
// decision_calldata.append(queens_id);

// world.execute('decide', decision_calldata);

// let player = get!(world, (game_id, player_id).into(), (Player));
// assert(player.location_id == queens_id, 'should have traveled');
// }

#[test]
#[available_gas(110000000)]
fn test_travel_and_decision() {
let (world_address, game_id, player_id) = spawn_game(); // creator auto joins
let world = IWorldDispatcher { contract_address: world_address };
let player = get!(world, (game_id, player_id).into(), (Player));

let mut buy_calldata = array::ArrayTrait::<felt252>::new();
buy_calldata.append(game_id.into());
buy_calldata.append(player.location_id);
buy_calldata.append(WEED_ID);
buy_calldata.append(COPS_DRUG_THRESHOLD.into());
world.execute('buy'.into(), buy_calldata);

let brooklyn_id = 'Brooklyn';
let mut travel_calldata = array::ArrayTrait::<felt252>::new();
travel_calldata.append(game_id.into());
travel_calldata.append(brooklyn_id);

starknet::testing::set_transaction_hash(TRAVEL_RISK.into());
world.execute('travel', travel_calldata);

let player = get!(world, (game_id, player_id).into(), (Player));
assert(player.status != PlayerStatus::Normal, 'incorrect status');
assert(player.location_id != brooklyn_id, 'should not have traveled');

let queens_id = 'Queens';
let mut decision_calldata = array::ArrayTrait::<felt252>::new();
decision_calldata.append(game_id.into());
decision_calldata.append(0.into()); // 0 = pay
decision_calldata.append(queens_id);

world.execute('decide', decision_calldata);

let player = get!(world, (game_id, player_id).into(), (Player));
assert(player.location_id == queens_id, 'should have traveled');
}
2 changes: 1 addition & 1 deletion web/src/components/map/Callout.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Location } from "@/dojo/types";
import { Icon } from "@chakra-ui/react";

export const Callout = ({ location }: { location: Location }) => {
export const Callout = ({ location }: { location?: Location }) => {
return (
<Icon
layerStyle="fill"
Expand Down
4 changes: 2 additions & 2 deletions web/src/components/map/Outline.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export const Outline = ({
target?: Location;
current?: Location;
}) => {
if (target === undefined || current === undefined) {
if (target === undefined) {
return <></>;
}

Expand Down Expand Up @@ -58,7 +58,7 @@ const SvgHighlight = ({
location,
fill,
}: {
location: Location;
location?: Location;
fill: string;
}) => {
return (
Expand Down
5 changes: 3 additions & 2 deletions web/src/dojo/entities/usePlayerEntity.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export class PlayerEntity {
turnsRemainingOnDeath: number;
drugCount: number;
bagLimit: number;
locationId: string;
locationId?: string;
status: PlayerStatus;
drugs: Drug[];

Expand All @@ -31,7 +31,8 @@ export class PlayerEntity {
this.turnsRemainingOnDeath = player.turns_remaining_on_death;
this.drugCount = player.drug_count;
this.bagLimit = player.bag_limit;
this.locationId = player.location_id;
this.locationId =
player.location_id === "0x0" ? undefined : player.location_id;
this.status = player.status;
this.drugs = drugs;
}
Expand Down
1 change: 0 additions & 1 deletion web/src/dojo/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ export const parseEvent = (
return {
gameId: num.toHexString(raw.data[0]),
playerId: num.toHexString(raw.data[1]),
locationId: num.toHexString(raw.data[2]),
} as JoinedEventData;
case RyoEvents.Decision:
return {
Expand Down
2 changes: 1 addition & 1 deletion web/src/dojo/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ export function getLocationByType(type: Location) {
return findBy<LocationInfo>(locations, "type", type);
}

export function getLocationById(id: string) {
export function getLocationById(id?: string) {
return findBy<LocationInfo>(locations, "id", id);
}

Expand Down
Loading

0 comments on commit e65be3e

Please sign in to comment.