-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(contracts): hustler actions and consequences
- Loading branch information
Showing
14 changed files
with
233 additions
and
119 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,5 @@ mod create; | |
mod join; | ||
mod trade; | ||
mod travel; | ||
mod player; | ||
mod set_name; | ||
mod decide; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
#[system] | ||
mod decide { | ||
use array::ArrayTrait; | ||
use box::BoxTrait; | ||
use traits::Into; | ||
use starknet::ContractAddress; | ||
|
||
use dojo::world::Context; | ||
use rollyourown::PlayerState; | ||
use rollyourown::constants::{RUN_PENALTY, PAY_PENALTY}; | ||
use rollyourown::components::game::{Game, GameTrait}; | ||
use rollyourown::components::risks::{Risks, RisksTrait}; | ||
use rollyourown::components::player::{Player, PlayerTrait}; | ||
|
||
#[derive(Copy, Drop, Serde, PartialEq)] | ||
enum Action { | ||
Pay: (), | ||
Run: (), | ||
} | ||
|
||
#[derive(Copy, Drop, Serde, PartialEq)] | ||
enum Result { | ||
Paid: (), | ||
GotAway: (), | ||
Mugged: (), | ||
} | ||
|
||
#[event] | ||
#[derive(Drop, starknet::Event)] | ||
enum Event { | ||
Decision: Decision, | ||
Consequence: Consequence, | ||
} | ||
|
||
#[derive(Drop, starknet::Event)] | ||
struct Decision { | ||
game_id: u32, | ||
player_id: ContractAddress, | ||
action: Action, | ||
} | ||
|
||
#[derive(Drop, starknet::Event)] | ||
struct Consequence { | ||
game_id: u32, | ||
player_id: ContractAddress, | ||
result: Result | ||
} | ||
|
||
fn execute(ctx: Context, game_id: u32, action: Action, next_location_id: felt252) { | ||
let game = get !(ctx.world, game_id, Game); | ||
assert(game.tick(), 'game cannot progress'); | ||
|
||
let player_id = ctx.origin; | ||
let mut player = get !(ctx.world, (game_id, player_id).into(), Player); | ||
assert(player.state != PlayerState::Normal(()), 'player response not needed'); | ||
|
||
let result = match action { | ||
Action::Pay => { | ||
emit !(ctx.world, Decision { game_id, player_id, action: Action::Pay }); | ||
|
||
player.cash -= 1; | ||
Result::Paid(()) | ||
}, | ||
Action::Run => { | ||
emit !(ctx.world, Decision { game_id, player_id, action: Action::Run }); | ||
|
||
let mut risks = get !(ctx.world, (game_id, player.location_id).into(), Risks); | ||
let seed = starknet::get_tx_info().unbox().transaction_hash; | ||
let got_away = risks.run(seed); | ||
|
||
match got_away { | ||
bool::False => { | ||
player.cash -= 1; | ||
Result::Mugged(()) | ||
}, | ||
bool::True => { | ||
Result::GotAway(()) | ||
} | ||
} | ||
}, | ||
}; | ||
|
||
player.state = PlayerState::Normal(()); | ||
player.location_id = next_location_id; | ||
player.turns_remaining -= 1; | ||
set !(ctx.world, (player)); | ||
|
||
emit !(ctx.world, Consequence { game_id, player_id, result }); | ||
} | ||
} |
Oops, something went wrong.