Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: death screen #150

Merged
merged 4 commits into from
Sep 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
1 change: 1 addition & 0 deletions src/components/player.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ struct Player {
drug_count: usize,
bag_limit: usize,
turns_remaining: usize,
turns_remaining_on_death: usize
}

#[generate_trait]
Expand Down
8 changes: 4 additions & 4 deletions src/components/risks.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ struct Risks {
#[generate_trait]
impl RisksImpl of RisksTrait {
#[inline(always)]
fn travel(ref self: Risks, seed: felt252, cash: u128, drug_count: usize) -> PlayerStatus {
if occurs(seed, self.travel) {
fn travel(self: @Risks, seed: felt252, cash: u128, drug_count: usize) -> PlayerStatus {
if occurs(seed, *self.travel) {
let seed = pedersen::pedersen(seed, seed);
let entropy: u256 = seed.into();
let result: u128 = entropy.low % 100;
Expand All @@ -43,8 +43,8 @@ impl RisksImpl of RisksTrait {
}

#[inline(always)]
fn run(ref self: Risks, seed: felt252) -> bool {
occurs(seed, self.capture)
fn run(self: @Risks, seed: felt252) -> bool {
occurs(seed, *self.capture)
}
}

Expand Down
9 changes: 5 additions & 4 deletions src/systems/create.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ mod create_game {
use rollyourown::components::location::{Location, LocationTrait};
use rollyourown::components::market::{MarketTrait};
use rollyourown::constants::{
SCALING_FACTOR, TRAVEL_RISK, CAPTURE_RISK, STARTING_CASH, STARTING_HEALTH,
STARTING_BAG_LIMIT
TRAVEL_RISK, CAPTURE_RISK, STARTING_CASH, STARTING_HEALTH, STARTING_BAG_LIMIT
};
use rollyourown::utils::random;
use debug::PrintTrait;
Expand Down Expand Up @@ -65,6 +64,7 @@ mod create_game {
drug_count: 0,
bag_limit: STARTING_BAG_LIMIT,
turns_remaining: max_turns,
turns_remaining_on_death: 0
};

let game = Game {
Expand Down Expand Up @@ -143,8 +143,9 @@ mod create_game {

// 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
10 changes: 7 additions & 3 deletions src/systems/decide.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ mod decide {
use dojo::world::Context;

use rollyourown::PlayerStatus;
use rollyourown::constants::{GANGS_PAYMENT, HEALTH_IMPACT, COPS_DRUG_THRESHOLD};
use rollyourown::constants::{COPS_DRUG_THRESHOLD, HEALTH_IMPACT, GANGS_PAYMENT};
use rollyourown::components::game::{Game, GameTrait};
use rollyourown::components::risks::{Risks, RisksTrait};
use rollyourown::components::player::{Player, PlayerTrait};
Expand Down Expand Up @@ -100,6 +100,7 @@ mod decide {
if health_loss >= player.health {
player.health = 0;
player.turns_remaining = 0;
player.turns_remaining_on_death = player.turns_remaining;
outcome = Outcome::Died;
} else {
player.health -= health_loss
Expand All @@ -109,11 +110,13 @@ 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
}
);
}


fn cops_payment(drug_count: u32) -> u128 {
if drug_count < COPS_DRUG_THRESHOLD + 20 {
1000_0000 // $1000
Expand All @@ -126,6 +129,7 @@ mod decide {
}
}


fn take_drugs(
ctx: Context, game_id: u32, player_id: ContractAddress, percentage: usize
) -> usize {
Expand Down
5 changes: 2 additions & 3 deletions src/systems/join.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ 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, STARTING_HEALTH, STARTING_BAG_LIMIT
};
use rollyourown::constants::{STARTING_CASH, STARTING_HEALTH, STARTING_BAG_LIMIT};

#[event]
#[derive(Drop, starknet::Event)]
Expand Down Expand Up @@ -52,6 +50,7 @@ mod join_game {
drug_count: 0,
bag_limit: STARTING_BAG_LIMIT,
turns_remaining: game.max_turns,
turns_remaining_on_death: 0
};

set!(ctx.world, (game, player));
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, }))
}
}
7 changes: 3 additions & 4 deletions src/systems/travel.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@ mod travel {
assert(game.tick(), 'game cannot progress');

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

let mut risks = get!(ctx.world, (game_id, next_location_id).into(), Risks);
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 {
Expand All @@ -69,8 +69,7 @@ mod travel {
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
Binary file added web/public/sounds/GameOver.mp3
Binary file not shown.
9 changes: 9 additions & 0 deletions web/src/components/icons/Skull.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Icon, IconProps } from ".";

export const Skull = (props: IconProps) => {
return (
<Icon viewBox="0 0 24 24" {...props}>
<path d="M19.0001 7.99998V6.99987H18.0001V5.99994H17.0002V5H6.99998V5.99994H6.00005V6.99987H5.00011V7.99981L4 7.99998V12.9997H4.99994V14.9995H5.99987V15.9995H6.99981V18.9993H8.99969V16.9994H10.9996V18.9993H12.9994V16.9994H14.9993V18.9993H16.9992V15.9995H17.9991V14.9995H18.9991V12.9997H19.999V9.99985L20 8.99992V7.99998L19.0001 7.99998ZM17.0002 12.9997H15.0003V11.9997H14.0004V9.99986H17.0002V12.9997ZM11.0002 14.9997V12.9998H13.0001V14.9997H11.0002ZM7.00015 11.9997V9.99986H9.99997V11.9997H9.0002V12.9997H7.00015V11.9997Z"/>
</Icon>
);
};
1 change: 1 addition & 0 deletions web/src/components/icons/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export * from "./Roll";
export * from "./Close";
export * from "./ExternalLink";
export * from "./Heart";
export * from "./Skull";

// Template for adding new icons. When copying svg from figma, viewBox is assumed
// to be 36x36, otherwise override within individual icons.
Expand Down
2 changes: 2 additions & 0 deletions web/src/dojo/entities/usePlayerEntity.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export class PlayerEntity {
cash: number;
health: number;
turnsRemaining: number;
turnsRemainingOnDeath: number;
drugCount: number;
bagLimit: number;
locationId: string;
Expand All @@ -27,6 +28,7 @@ export class PlayerEntity {
this.cash = Number(player.cash) / SCALING_FACTOR;
this.health = player.health;
this.turnsRemaining = player.turns_remaining;
this.turnsRemainingOnDeath = player.turns_remaining_on_death;
this.drugCount = player.drug_count;
this.bagLimit = player.bag_limit;
this.locationId = player.location_id;
Expand Down
4 changes: 2 additions & 2 deletions web/src/dojo/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export enum RyoEvents {
Sold = "0x123e760cef925d0b4f685db5e1ac87aadaf1ad9f8069122a5bb03353444c386",
AdverseEvent = "0x3605d6af5b08d01a1b42fa16a5f4dc202724f1664912948dcdbe99f5c93d0a0",
Decision = "0xc9315f646a66dd126a564fa76bfdc00bdb47abe0d8187e464f69215dbf432a",
Consqeuence = "0x1335a57b72e0bcb464f40bf1f140f691ec93e4147b91d0760640c19999b841d",
Consequence = "0x1335a57b72e0bcb464f40bf1f140f691ec93e4147b91d0760640c19999b841d",
}

export interface BaseEventData {
Expand Down Expand Up @@ -108,7 +108,7 @@ export const parseEvent = (
playerId: num.toHexString(raw.data[1]),
action: Number(raw.data[2]),
} as DecisionEventData;
case RyoEvents.Consqeuence:
case RyoEvents.Consequence:
return {
gameId: num.toHexString(raw.data[0]),
playerId: num.toHexString(raw.data[1]),
Expand Down
28 changes: 24 additions & 4 deletions web/src/dojo/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,24 @@ export const outcomes: OutcomeInfo[] = [
getMuggerResponses(Outcome.Escaped, isInitial),
color: "neon.200",
},
{
name: "Got killed by the Gang",
type: Outcome.Died,
status: PlayerStatus.BeingMugged,
imageSrc: "/images/events/fought.png",
getResponse: (isInitial: boolean) =>
getMuggerResponses(Outcome.Died, isInitial),
color: "red",
},
{
name: "Got killed by the Cops",
type: Outcome.Died,
status: PlayerStatus.BeingArrested,
imageSrc: "/images/events/fought.png",
getResponse: (isInitial: boolean) =>
getMuggerResponses(Outcome.Died, isInitial),
color: "red",
},
];

function findBy<T>(array: T[], key: keyof T, value: any): T | undefined {
Expand Down Expand Up @@ -191,11 +209,13 @@ export function getOutcomeInfo(
status: PlayerStatus,
type: Outcome,
): OutcomeInfo {
return (
outcomes.find((item) => {
const found = outcomes.find((item) => {
return item.status === status && item.type === type;
}) || outcomes[0]
);
});
if(!found) {
console.log(`getOutcomeInfo outcome ${status} ${type} not found !`)
}
return found || outcomes[0]
}

export function sortDrugMarkets(drugMarkets?: DrugMarket[]): DrugMarket[] {
Expand Down
Loading
Loading