@@ -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