Skip to content
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
3 changes: 3 additions & 0 deletions src/interfaces/IActions.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,13 @@ pub trait IActions<T> {
fn get_community_chest(self: @T, id: u8, game_id: u256) -> CommunityChest;
fn get_railroad(self: @T, id: u8, game_id: u256) -> RailRoad;
fn get_tax(self: @T, id: u8, game_id: u256) -> Tax;
fn use_getout_of_jail_chance(ref self: T, game_id: u256) -> bool;
fn use_getout_of_jail_community_chest(ref self: T, game_id: u256) -> bool;

// Dice & player movement
fn roll_dice(ref self: T) -> (u8, u8);
fn move_player(ref self: T, game_id: u256, steps: u8) -> u8;
fn pay_jail_fine(ref self: T, game_id: u256) -> bool;
// fn handle_chance(ref self: T, game_id: u256, random_index: u32) -> @ByteArray;

// Handling landings on board
Expand Down
2 changes: 2 additions & 0 deletions src/model/game_player_model.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ pub struct GamePlayer {
pub no_section8: u8,
pub is_bankrupt: bool,
pub is_active: bool,
pub jail_turns: u8,
}


Expand Down Expand Up @@ -78,6 +79,7 @@ impl GamePlayerImpl of GamePlayerTrait {
no_section6: 0,
no_section7: 0,
no_section8: 0,
jail_turns: 0,
}
}

Expand Down
100 changes: 93 additions & 7 deletions src/systems/actions.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -463,36 +463,122 @@ pub mod actions {
true
}

fn pay_jail_fine(ref self: ContractState, game_id: u256) -> bool {
let mut world = self.world_default();
let caller = get_caller_address();
let mut player: GamePlayer = world.read_model((caller, game_id));
let mut game: Game = world.read_model(game_id);

assert(game.status == GameStatus::Ongoing, 'Game not started');
assert(player.jailed, 'Not in jail');

// Pay the fine
let fine_amount: u256 = 50;
assert(player.balance >= fine_amount, 'Insufficient funds to pay fine');

player.balance -= fine_amount;
player.jailed = false;
player.jail_turns = 0;

world.write_model(@game);
world.write_model(@player);

true
}

fn use_getout_of_jail_chance(ref self: ContractState, game_id: u256) -> bool {
let mut world = self.world_default();
let caller = get_caller_address();
let mut player: GamePlayer = world.read_model((caller, game_id));
let mut game: Game = world.read_model(game_id);
assert(player.chance_jail_card, 'No chance card');
assert(player.jailed, 'Not in jail');
assert(game.status == GameStatus::Ongoing, 'Game not started');
// Use the card
player.chance_jail_card = false;
player.jailed = false;
player.jail_turns = 0;
world.write_model(@game);
world.write_model(@player);
true
}

fn use_getout_of_jail_community_chest(ref self: ContractState, game_id: u256) -> bool {
let mut world = self.world_default();
let caller = get_caller_address();
let mut player: GamePlayer = world.read_model((caller, game_id));
let mut game: Game = world.read_model(game_id);
assert(player.comm_free_card, 'No community chest card');
assert(player.jailed, 'Not in jail');
assert(game.status == GameStatus::Ongoing, 'Game not started');
// Use the card
player.comm_free_card = false;
player.jailed = false;
player.jail_turns = 0;
world.write_model(@game);
world.write_model(@player);
true
}


fn move_player(ref self: ContractState, game_id: u256, steps: u8) -> u8 {
let mut world = self.world_default();
let caller = get_caller_address();
let mut game_player: GamePlayer = world.read_model((caller, game_id));
let mut game: Game = world.read_model(game_id);

assert(game.next_player == caller, 'Not your turn');
assert(game.status == GameStatus::Ongoing, 'Game is not ongoing');
assert!(game.next_player == caller, "Not your turn");
assert!(game.status == GameStatus::Ongoing, "Game is not ongoing");

// Move player
game_player = GamePlayerTrait::move(game_player, steps);
// Handle jailed players
if game_player.jailed {
game_player.jail_turns += 1;

if game_player.jail_turns > 3 {
// Automatically release player after 3 turns
game_player.jailed = false;
game_player.jail_turns = 0;
} else {
// Still in jail, no move
world.write_model(@game_player);
return game_player.position;
}
}

// Now free to move
game_player = GamePlayerTrait::move(game_player, steps);
game_player.dice_rolled = steps;

// Passed or landed on Go
if game_player.position >= 40 {
game_player.position %= 40;
game_player.balance += 200;
}

// Landing on "Go To Jail" space
if game_player.position == 30 {
game_player.position = 10;
game_player.jailed = true;
game_player.jail_turns = 0;

world.write_model(@game_player);
world.write_model(@game);
return game_player.position;
}

// Handle landing on property
let mut property = self.get_property(game_player.position, game_id);
property = self.handle_property_landing(game_player.clone(), property.clone());
property = self.handle_property_landing(game_player.clone(), property);

// Update state
world.write_model(@game_player);
world.write_model(@game);
world.write_model(@property);

game_player.position
}


fn buy_property(ref self: ContractState, mut property: Property) -> bool {
// get the world
let mut world = self.world_default();
Expand Down Expand Up @@ -927,7 +1013,7 @@ pub mod actions {
property.property_type == PropertyType::CommunityChest, 'not on community chest',
);
if card == "Advance to Go (Collect $200)" {
player.position = 1;
player.position = 0;
player.balance += 200;
} else if card == "Bank error in your favor - Collect $200" {
player.balance += 200;
Expand All @@ -938,7 +1024,7 @@ pub mod actions {
} else if card == "Get Out of Jail Free" {
player.comm_free_card = true;
} else if card == "Go to Jail" {
player.position = 11; // jail position
player.position = 10; // jail position
player.jailed = true;
} else if card == "Grand Opera Night - collect $50 from every player" {
let mut i = 0;
Expand Down
Loading
Loading