Skip to content

Game Player Model #14

@blurbeast

Description

@blurbeast

📌 Feature Request: Complete Implementation of GamePlayerTrait for GamePlayer Model


🧩 Description

Fully implement all functions in the GamePlayerTrait for the GamePlayer model inside the models module.

These functions encapsulate all the actions a player can take within a game, including movement, balance updates, property management, jail status, and bankruptcy declaration.


🧠 Model Definition

#[dojo::model]
pub struct GamePlayer {
    #[key]
    pub address: ContractAddress,
    #[key]
    pub game_id: u256,
    pub player_symbol: PlayerSymbol,
    pub position: u8,
    pub jailed: bool,
    pub balance: u256,
    pub properties_owned: Array<u8>,
    pub is_bankrupt: bool,
    pub is_active: bool,
}

✅ Acceptance Criteria

  • All trait functions must be fully and meaningfully implemented.
  • All logic must be correctly bound to the GamePlayer model fields.
  • Mutations should be reflected through reference updates (ref self).
  • All changes must build successfully with sozo build.
  • Each function should do exactly what its name and context imply.

🛠️ Expected Functionality (Final Implementation Example)

impl GamePlayerImpl of GamePlayerTrait {
    fn create_game_player(username: felt252, address: ContractAddress, game_id: u256, player_symbol: PlayerSymbol) -> GamePlayer {
        GamePlayer {
            address,
            game_id,
            player_symbol,
            balance: 0,
            position: 0,
            jailed: false,
            is_bankrupt: false,
            is_active: true,
            properties_owned: array![]
        }
    }

    fn move(mut player: GamePlayer, steps: u8) {
        player.position = (player.position + steps) % 40; // Wrap around board
    }

    fn pay_game_player(ref self: GamePlayer, amount: u256) -> bool {
        self.balance += amount;
        true
    }

    fn deduct_game_player(ref self: GamePlayer, amount: u256) -> bool {
        if self.balance < amount {
            return false;
        }
        self.balance -= amount;
        true
    }

    fn add_property_to_game_player(ref self: GamePlayer, property_id: u8) -> bool {
        if self.properties_owned.contains(property_id) {
            return false;
        }
        self.properties_owned.append(property_id);
        true
    }

    fn remove_property_from_game_player(ref self: GamePlayer, property_id: u8) -> bool {
        let index = self.properties_owned.index_of(property_id);
        if index.is_none() {
            return false;
        }
        self.properties_owned.remove(index.unwrap());
        true
    }

    fn declare_bankruptcy(ref self: GamePlayer) -> bool {
        self.is_bankrupt = true;
        self.is_active = false;
        self.balance = 0;
        self.properties_owned.clear();
        true
    }

    fn jail_game_player(ref self: GamePlayer) -> bool {
        self.jailed = true;
        true
    }
}

📝 Note: Ensure utility functions like contains, index_of, and remove are either implemented or replaced with appropriate logic available in your framework.


🧪 Testing Notes

  • Verify a player can move and wrap from position 39 to 0.
  • Add/remove property IDs correctly, preventing duplicates.
  • Deduct fails if balance is insufficient.
  • Jail flag and bankruptcy flags correctly toggle.

📘 Status

Stat: ✅ Done
Build: ✅ Builds Successfully
Validation: ✅ All Functions Do What They're Meant For


📎 Additional Notes

  • Add appropriate unit/integration tests where applicable.
  • Consider edge cases like overflow in movement or re-jailing already jailed player.
  • This forms the foundation for any in-game economic or board-based action.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions