Skip to content

Feature/buy asset#11

Merged
RomanCantu merged 16 commits into
RobbieKiwi:mainfrom
RomanCantu:feature/buy_asset
Jun 9, 2025
Merged

Feature/buy asset#11
RomanCantu merged 16 commits into
RobbieKiwi:mainfrom
RomanCantu:feature/buy_asset

Conversation

@RomanCantu

Copy link
Copy Markdown
Collaborator
  • Handle buy asset requests
  • new update method for AssetRepos: update_owner
  • add a message to justify the (un)successful BuyAssetRequest in BuyAssetResponse

Comment thread src/models/event.py Outdated
Comment thread src/models/assets.py Outdated
Comment thread src/engine/engine.py Outdated
Comment thread src/engine/engine.py Outdated
else:
success = True
message = f"Player {player.id} successfully bought asset {asset.id}."
game_state.players.add_money(player.id, -asset.purchase_cost)

@RobbieKiwi RobbieKiwi Jun 7, 2025

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it would be clearer to have a method like "subtract money".
Also it is always better to use keyword arguments rather than positional arguments in case somebody changes the orders of arguments in future

@RomanCantu RomanCantu requested a review from RobbieKiwi June 8, 2025 15:00
@RobbieKiwi

Copy link
Copy Markdown
Owner

Generally speaking, a function that takes an object as an argument and does some operation should either:

  • Return a new version of the object, leaving the original unchanged
  • Return None and modify the original (A la inplace=True for dataframes)
    We should be clear about which one of these is happening. Usually the type hinting should make it clear.

In PlayerRepo._adjust_money I made a mistake by forgetting to add '.copy()' so it is BOTH mutating the object and returning a new object.

In Engine.handle_buy_asset_message, you should not be mutating the game_state object that is passed to the function.

The new game state should be generated something like this.
new_game_state = replace(game_state, players=players.subtract_money(x), assets=assets.change_owner(x))

Also note that for "players" and 'assets" we are also using the return-value, not expected a mutation of the original object

Comment thread src/engine/engine.py Outdated
Comment thread src/models/assets.py Outdated
Comment thread src/engine/engine.py Outdated
@RobbieKiwi

Copy link
Copy Markdown
Owner

Generally speaking, when you pass an argument to a function that acts on that argument either:

  • It should return a new version of that object without changing the original copy (preferred option usually)
  • Or it should change the object "in place" and return None
    And we should be quite explicit about which one of these is happening. Usually the function name and type hints make it clear enough.

If you look at PlayerRepo._adjust_money you can see an example of where I did this wrong, I forgot to add ".copy()" after the df so this function is BOTH mutating the original object and returning a new one.

Comment thread src/engine/engine.py Outdated
@RomanCantu RomanCantu merged commit e5882d1 into RobbieKiwi:main Jun 9, 2025
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants