Skip to content

Commit 1c5d400

Browse files
authored
Merge pull request #20 from aji70/main
feat: updated property manipulation and end game
2 parents 5558a34 + 0fd25fc commit 1c5d400

4 files changed

Lines changed: 557 additions & 26 deletions

File tree

src/interfaces/IActions.cairo

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ pub trait IActions<T> {
4545
fn use_getout_of_jail_chance(ref self: T, game_id: u256) -> bool;
4646
fn use_getout_of_jail_community_chest(ref self: T, game_id: u256) -> bool;
4747

48+
fn calculate_net_worth(ref self: T, player: GamePlayer) -> u256;
49+
50+
fn get_winner_by_net_worth(ref self: T, players: Array<GamePlayer>) -> ContractAddress;
51+
fn end_game(ref self: T, game: Game) -> ContractAddress;
52+
4853
fn offer_trade(
4954
ref self: T,
5055
game_id: u256,
@@ -77,17 +82,7 @@ pub trait IActions<T> {
7782
fn roll_dice(ref self: T) -> (u8, u8);
7883
fn move_player(ref self: T, game_id: u256, steps: u8) -> u8;
7984
fn pay_jail_fine(ref self: T, game_id: u256) -> bool;
80-
// fn handle_chance(ref self: T, game_id: u256, random_index: u32) -> @ByteArray;
8185

82-
// Handling landings on board
83-
// fn draw_chance_card(ref self: T, game_id: u256) -> Chance;
84-
// fn draw_community_chest_card(ref self: T, game_id: u256) -> CommunityChest;
85-
// fn pay_tax(ref self: T, game_id: u256, tax_id: u8) -> bool;
86-
// fn go_to_jail(ref self: T, game_id: u256) -> bool;
87-
88-
// Jail specific actions
89-
// fn pay_jail_fee(ref self: T, game_id: u256) -> bool;
90-
// fn use_jail_card(ref self: T, game_id: u256) -> bool;
9186

9287
// Property transactions
9388
fn buy_property(ref self: T, property: Property) -> bool;
@@ -107,18 +102,6 @@ pub trait IActions<T> {
107102
fn process_community_chest_card(
108103
ref self: T, game: Game, player: GamePlayer, card: ByteArray,
109104
) -> (Game, GamePlayer);
110-
// Trading system
111-
// fn offer_trade(
112-
// ref self: T,
113-
// game_id: u256,
114-
// to: ContractAddress,
115-
// offered_property_ids: Array<u8>,
116-
// requested_property_ids: Array<u8>,
117-
// cash_offer: u256,
118-
// cash_request: u256
119-
// );
120-
// fn accept_trade(ref self: T, game_id: u256, trade_id: u256) -> bool;
121-
// fn decline_trade(ref self: T, game_id: u256, trade_id: u256) -> bool;
122105

123106
// // Auctions
124107
// fn start_auction(ref self: T, property_id: u8, game_id: u256);
@@ -132,6 +115,5 @@ pub trait IActions<T> {
132115
fn mint(ref self: T, recepient: ContractAddress, game_id: u256, amount: u256);
133116
// Bankruptcy & ending game
134117
// fn declare_bankruptcy(ref self: T, game_id: u256) -> bool;
135-
// fn check_winner(self: @T, game_id: u256) -> Option<ContractAddress>;
136-
// fn end_game(ref self: T, game_id: u256) -> bool;
118+
137119
}

src/model/game_model.cairo

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ pub struct Game {
3131
pub status: GameStatus, // Status of the game
3232
pub mode: GameType, // Mode of the game
3333
pub ready_to_start: bool, // Indicate whether game can be started
34-
pub winner: felt252, // First winner position
34+
pub winner: ContractAddress, // First winner position
3535
pub next_player: ContractAddress, // Address of the player to make the next move
3636
pub number_of_players: u8, // Number of players in the game
3737
pub rolls_count: u256, // Sum of all the numbers rolled by the dice
@@ -139,7 +139,7 @@ impl GameImpl of GameTrait {
139139
player_boot,
140140
player_wheelbarrow,
141141
next_player: zero_address.into(),
142-
winner: zero_address.into(),
142+
winner: zero_address,
143143
rolls_times: 0,
144144
rolls_count: 0,
145145
number_of_players,

src/systems/actions.cairo

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1426,6 +1426,129 @@ pub mod actions {
14261426

14271427
true
14281428
}
1429+
fn calculate_net_worth(ref self: ContractState, player: GamePlayer) -> u256 {
1430+
let mut world = self.world_default();
1431+
1432+
let mut total_property_value: u256 = 0;
1433+
let mut total_house_cost: u256 = 0;
1434+
let mut total_rent_value: u256 = 0;
1435+
let mut card_value: u256 = 0;
1436+
let mut i = 0;
1437+
let properties_len = player.properties_owned.len();
1438+
1439+
while i < properties_len {
1440+
let prop_id = *player.properties_owned.at(i);
1441+
let game_id = player.game_id;
1442+
let property: Property = self.get_property(prop_id, game_id);
1443+
1444+
// Property value (half if mortgaged)
1445+
if property.is_mortgaged {
1446+
total_property_value += property.cost_of_property / 2;
1447+
} else {
1448+
total_property_value += property.cost_of_property;
1449+
}
1450+
1451+
// House/hotel cost
1452+
if property.development < 5 {
1453+
total_house_cost += property.cost_of_house * property.development.into();
1454+
} else if property.development == 5 {
1455+
total_house_cost += property.cost_of_house * 5;
1456+
}
1457+
1458+
// Rent value (always add — mortgaged or not, since it's dev level based)
1459+
let rent = match property.development {
1460+
0 => property.rent_site_only,
1461+
1 => property.rent_one_house,
1462+
2 => property.rent_two_houses,
1463+
3 => property.rent_three_houses,
1464+
4 => property.rent_four_houses,
1465+
_ => property.rent_hotel,
1466+
};
1467+
total_rent_value += rent;
1468+
1469+
i += 1;
1470+
};
1471+
1472+
// Jail/Chance card value
1473+
if player.chance_jail_card {
1474+
card_value += 50;
1475+
}
1476+
if player.comm_free_card {
1477+
card_value += 50;
1478+
}
1479+
1480+
let net_worth = player.balance
1481+
+ total_property_value
1482+
+ total_house_cost
1483+
+ total_rent_value
1484+
+ card_value;
1485+
1486+
// Debug prints
1487+
println!("Balance: {}", player.balance);
1488+
println!("Total property value: {}", total_property_value);
1489+
println!("Total house cost: {}", total_house_cost);
1490+
println!("Total rent value: {}", total_rent_value);
1491+
println!("Card value: {}", card_value);
1492+
println!("NET WORTH: {}", net_worth);
1493+
1494+
net_worth
1495+
}
1496+
fn get_winner_by_net_worth(
1497+
ref self: ContractState, players: Array<GamePlayer>,
1498+
) -> ContractAddress {
1499+
let mut i = 0;
1500+
let mut max_net_worth: u256 = 0;
1501+
let mut winner_address: ContractAddress = contract_address_const::<'0'>();
1502+
1503+
let players_len = players.len();
1504+
while i < players_len {
1505+
let player = players.at(i);
1506+
let net_worth = self.calculate_net_worth(player.clone());
1507+
1508+
if net_worth > max_net_worth {
1509+
max_net_worth = net_worth;
1510+
winner_address = *player.address;
1511+
};
1512+
1513+
i += 1;
1514+
};
1515+
1516+
winner_address
1517+
}
1518+
1519+
1520+
fn end_game(ref self: ContractState, game: Game) -> ContractAddress {
1521+
let mut world = self.world_default();
1522+
let mut players: Array<GamePlayer> = ArrayTrait::new();
1523+
1524+
let total_players = game.game_players.len();
1525+
let mut i = 0;
1526+
1527+
// Indexed loop over game.players
1528+
while i < total_players {
1529+
let player_address = game.game_players.at(i);
1530+
let player_model: GamePlayer = world.read_model((*player_address, game.id));
1531+
1532+
players.append(player_model);
1533+
i += 1;
1534+
};
1535+
1536+
// Find the winner by net worth
1537+
let winner_address = self.get_winner_by_net_worth(players);
1538+
let winner: Player = world.read_model(winner_address);
1539+
1540+
// Set game status to ended
1541+
let mut updated_game = game;
1542+
updated_game.status = GameStatus::Ended;
1543+
updated_game.winner = winner.address;
1544+
1545+
// Write back the updated game state
1546+
world.write_model(@updated_game);
1547+
1548+
// Return the winner's address
1549+
winner.address
1550+
}
1551+
14291552

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

0 commit comments

Comments
 (0)