Still some really weird behaviour, but stock trading is working a bit better now than before.
This commit is contained in:
parent
82b89a7045
commit
2e831adf3a
69
main.py
69
main.py
@ -470,50 +470,55 @@ class Company:
|
||||
return "Invalid stock action."
|
||||
|
||||
def _buy_stock(self, company_id, amount, total_value, is_ai):
|
||||
"""Handles the buying of stocks for the specified company."""
|
||||
available_shares = self._calculate_available_shares()
|
||||
if amount > available_shares:
|
||||
return f"Not enough available shares to buy. Available: {available_shares}"
|
||||
# Calculate the total number of shares currently owned
|
||||
total_shares_owned = sum(self._market.companies[company_id].own_stock_ownership.values())
|
||||
available_shares = self.total_shares - total_shares_owned
|
||||
|
||||
if self.cash < total_value:
|
||||
return "Insufficient funds to buy stocks."
|
||||
# Determine the maximum shares that can be bought based on available cash and available shares
|
||||
max_affordable_shares = int(self.cash / self._market.get_stock_price(company_id))
|
||||
amount = min(amount, available_shares, max_affordable_shares)
|
||||
|
||||
# Update stock ownership
|
||||
if company_id == self.player_id:
|
||||
# Buying own company's stock
|
||||
self.own_stock_ownership[self.player_id] += amount
|
||||
else:
|
||||
# Buying another company's stock
|
||||
if is_ai:
|
||||
# For AI
|
||||
self.stock_holdings[company_id] += amount
|
||||
else:
|
||||
# For player
|
||||
if amount == 0:
|
||||
return "Cannot buy any shares due to insufficient funds or no available shares."
|
||||
|
||||
total_value = self._market.get_stock_price(company_id) * amount
|
||||
|
||||
self.cash -= total_value
|
||||
self._market.update_stock_ledger(company_id, self.player_id, amount)
|
||||
|
||||
# Update buyer's stock_holdings
|
||||
self.stock_holdings[company_id] += amount
|
||||
|
||||
self.cash -= total_value # Deduct the cost from the buyer's cash
|
||||
# Update the target company's own_stock_ownership to reflect the buyer's new ownership
|
||||
target_company = self._market.companies[company_id]
|
||||
target_company.own_stock_ownership[self.player_id] = target_company.own_stock_ownership.get(self.player_id,
|
||||
0) + amount
|
||||
|
||||
return f"Bought {amount} stocks of {company_id}."
|
||||
|
||||
def _sell_stock(self, company_id, amount, total_value, is_ai):
|
||||
"""Handles the selling of stocks for the specified company."""
|
||||
# Check if the seller has enough stocks to sell
|
||||
stock_ownership = self._get_stock_ownership(company_id) if not is_ai else self.own_stock_ownership[company_id]
|
||||
if stock_ownership < amount:
|
||||
if not is_ai:
|
||||
if self.stock_holdings[company_id] < amount:
|
||||
return "Not enough stocks to sell."
|
||||
|
||||
# Update stock ownership
|
||||
if is_ai:
|
||||
if company_id == self.player_id:
|
||||
# AI selling its own stock
|
||||
self.own_stock_ownership[company_id] -= amount
|
||||
else:
|
||||
# AI selling other company's stock
|
||||
# Update buyer's stock_holdings for non-AI (like player)
|
||||
self.stock_holdings[company_id] -= amount
|
||||
else:
|
||||
# Player selling stock
|
||||
if self.own_stock_ownership[company_id] < amount:
|
||||
return "Not enough stocks to sell."
|
||||
# Update own_stock_ownership for AI
|
||||
self.own_stock_ownership[company_id] -= amount
|
||||
|
||||
self.cash += total_value # Add the proceeds to the seller's cash
|
||||
self.cash += total_value
|
||||
self._market.update_stock_ledger(company_id, self.player_id, -amount)
|
||||
|
||||
# If an AI company is selling its own shares, update the ownership for all shareholders
|
||||
if is_ai and company_id == self.player_id:
|
||||
for shareholder in self._market.companies[company_id].own_stock_ownership.keys():
|
||||
self._market.companies[company_id].own_stock_ownership[shareholder] -= amount * \
|
||||
(self._market.companies[
|
||||
company_id].own_stock_ownership[
|
||||
shareholder] / self.total_shares)
|
||||
|
||||
return f"Sold {amount} stocks of {company_id}."
|
||||
|
||||
def _calculate_available_shares(self):
|
||||
|
||||
Loading…
Reference in New Issue
Block a user