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
30 changes: 6 additions & 24 deletions src/interfaces/IActions.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ pub trait IActions<T> {
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;

fn calculate_net_worth(ref self: T, player: GamePlayer) -> u256;

fn get_winner_by_net_worth(ref self: T, players: Array<GamePlayer>) -> ContractAddress;
fn end_game(ref self: T, game: Game) -> ContractAddress;

fn offer_trade(
ref self: T,
game_id: u256,
Expand Down Expand Up @@ -77,17 +82,7 @@ pub trait IActions<T> {
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
// fn draw_chance_card(ref self: T, game_id: u256) -> Chance;
// fn draw_community_chest_card(ref self: T, game_id: u256) -> CommunityChest;
// fn pay_tax(ref self: T, game_id: u256, tax_id: u8) -> bool;
// fn go_to_jail(ref self: T, game_id: u256) -> bool;

// Jail specific actions
// fn pay_jail_fee(ref self: T, game_id: u256) -> bool;
// fn use_jail_card(ref self: T, game_id: u256) -> bool;

// Property transactions
fn buy_property(ref self: T, property: Property) -> bool;
Expand All @@ -107,18 +102,6 @@ pub trait IActions<T> {
fn process_community_chest_card(
ref self: T, game: Game, player: GamePlayer, card: ByteArray,
) -> (Game, GamePlayer);
// Trading system
// fn offer_trade(
// ref self: T,
// game_id: u256,
// to: ContractAddress,
// offered_property_ids: Array<u8>,
// requested_property_ids: Array<u8>,
// cash_offer: u256,
// cash_request: u256
// );
// fn accept_trade(ref self: T, game_id: u256, trade_id: u256) -> bool;
// fn decline_trade(ref self: T, game_id: u256, trade_id: u256) -> bool;

// // Auctions
// fn start_auction(ref self: T, property_id: u8, game_id: u256);
Expand All @@ -132,6 +115,5 @@ pub trait IActions<T> {
fn mint(ref self: T, recepient: ContractAddress, game_id: u256, amount: u256);
// Bankruptcy & ending game
// fn declare_bankruptcy(ref self: T, game_id: u256) -> bool;
// fn check_winner(self: @T, game_id: u256) -> Option<ContractAddress>;
// fn end_game(ref self: T, game_id: u256) -> bool;

}
4 changes: 2 additions & 2 deletions src/model/game_model.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ pub struct Game {
pub status: GameStatus, // Status of the game
pub mode: GameType, // Mode of the game
pub ready_to_start: bool, // Indicate whether game can be started
pub winner: felt252, // First winner position
pub winner: ContractAddress, // First winner position
pub next_player: ContractAddress, // Address of the player to make the next move
pub number_of_players: u8, // Number of players in the game
pub rolls_count: u256, // Sum of all the numbers rolled by the dice
Expand Down Expand Up @@ -139,7 +139,7 @@ impl GameImpl of GameTrait {
player_boot,
player_wheelbarrow,
next_player: zero_address.into(),
winner: zero_address.into(),
winner: zero_address,
rolls_times: 0,
rolls_count: 0,
number_of_players,
Expand Down
123 changes: 123 additions & 0 deletions src/systems/actions.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -1426,6 +1426,129 @@ pub mod actions {

true
}
fn calculate_net_worth(ref self: ContractState, player: GamePlayer) -> u256 {
let mut world = self.world_default();

let mut total_property_value: u256 = 0;
let mut total_house_cost: u256 = 0;
let mut total_rent_value: u256 = 0;
let mut card_value: u256 = 0;
let mut i = 0;
let properties_len = player.properties_owned.len();

while i < properties_len {
let prop_id = *player.properties_owned.at(i);
let game_id = player.game_id;
let property: Property = self.get_property(prop_id, game_id);

// Property value (half if mortgaged)
if property.is_mortgaged {
total_property_value += property.cost_of_property / 2;
} else {
total_property_value += property.cost_of_property;
}

// House/hotel cost
if property.development < 5 {
total_house_cost += property.cost_of_house * property.development.into();
} else if property.development == 5 {
total_house_cost += property.cost_of_house * 5;
}

// Rent value (always add — mortgaged or not, since it's dev level based)
let rent = match property.development {
0 => property.rent_site_only,
1 => property.rent_one_house,
2 => property.rent_two_houses,
3 => property.rent_three_houses,
4 => property.rent_four_houses,
_ => property.rent_hotel,
};
total_rent_value += rent;

i += 1;
};

// Jail/Chance card value
if player.chance_jail_card {
card_value += 50;
}
if player.comm_free_card {
card_value += 50;
}

let net_worth = player.balance
+ total_property_value
+ total_house_cost
+ total_rent_value
+ card_value;

// Debug prints
println!("Balance: {}", player.balance);
println!("Total property value: {}", total_property_value);
println!("Total house cost: {}", total_house_cost);
println!("Total rent value: {}", total_rent_value);
println!("Card value: {}", card_value);
println!("NET WORTH: {}", net_worth);

net_worth
}
fn get_winner_by_net_worth(
ref self: ContractState, players: Array<GamePlayer>,
) -> ContractAddress {
let mut i = 0;
let mut max_net_worth: u256 = 0;
let mut winner_address: ContractAddress = contract_address_const::<'0'>();

let players_len = players.len();
while i < players_len {
let player = players.at(i);
let net_worth = self.calculate_net_worth(player.clone());

if net_worth > max_net_worth {
max_net_worth = net_worth;
winner_address = *player.address;
};

i += 1;
};

winner_address
}


fn end_game(ref self: ContractState, game: Game) -> ContractAddress {
let mut world = self.world_default();
let mut players: Array<GamePlayer> = ArrayTrait::new();

let total_players = game.game_players.len();
let mut i = 0;

// Indexed loop over game.players
while i < total_players {
let player_address = game.game_players.at(i);
let player_model: GamePlayer = world.read_model((*player_address, game.id));

players.append(player_model);
i += 1;
};

// Find the winner by net worth
let winner_address = self.get_winner_by_net_worth(players);
let winner: Player = world.read_model(winner_address);

// Set game status to ended
let mut updated_game = game;
updated_game.status = GameStatus::Ended;
updated_game.winner = winner.address;

// Write back the updated game state
world.write_model(@updated_game);

// Return the winner's address
winner.address
}


fn reject_trade(ref self: ContractState, trade_id: u256, game_id: u256) -> bool {
let mut world = self.world_default();
Expand Down
Loading
Loading