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 2dc01b3
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 29 deletions.
10 changes: 4 additions & 6 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,7 +137,7 @@ 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!(
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
28 changes: 16 additions & 12 deletions src/systems/travel.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -51,21 +51,25 @@ 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 has no risk
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!(
Expand Down
6 changes: 2 additions & 4 deletions web/src/pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -85,16 +85,14 @@ export default function Home() {
NUM_TURNS,
);

const { gameId, locationId } = event as JoinedEventData;
const { gameId } = event as JoinedEventData;
toast(
"Created Game",
Alert,
`http://amazing_explorer/${hash}`,
);

router.push(
`/${gameId}/${getLocationById(locationId)?.slug}`,
);
router.push(`/${gameId}/travel}`);
}}
>
Hustle
Expand Down

0 comments on commit 2dc01b3

Please sign in to comment.