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
75
main.py
75
main.py
@ -470,50 +470,55 @@ class Company:
|
|||||||
return "Invalid stock action."
|
return "Invalid stock action."
|
||||||
|
|
||||||
def _buy_stock(self, company_id, amount, total_value, is_ai):
|
def _buy_stock(self, company_id, amount, total_value, is_ai):
|
||||||
"""Handles the buying of stocks for the specified company."""
|
# Calculate the total number of shares currently owned
|
||||||
available_shares = self._calculate_available_shares()
|
total_shares_owned = sum(self._market.companies[company_id].own_stock_ownership.values())
|
||||||
if amount > available_shares:
|
available_shares = self.total_shares - total_shares_owned
|
||||||
return f"Not enough available shares to buy. Available: {available_shares}"
|
|
||||||
|
|
||||||
if self.cash < total_value:
|
# Determine the maximum shares that can be bought based on available cash and available shares
|
||||||
return "Insufficient funds to buy stocks."
|
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 amount == 0:
|
||||||
if company_id == self.player_id:
|
return "Cannot buy any shares due to insufficient funds or no available shares."
|
||||||
# Buying own company's stock
|
|
||||||
self.own_stock_ownership[self.player_id] += amount
|
total_value = self._market.get_stock_price(company_id) * amount
|
||||||
else:
|
|
||||||
# Buying another company's stock
|
self.cash -= total_value
|
||||||
if is_ai:
|
self._market.update_stock_ledger(company_id, self.player_id, amount)
|
||||||
# For AI
|
|
||||||
self.stock_holdings[company_id] += amount
|
# Update buyer's stock_holdings
|
||||||
else:
|
self.stock_holdings[company_id] += amount
|
||||||
# For player
|
|
||||||
self.stock_holdings[company_id] += amount
|
# 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
|
||||||
|
|
||||||
self.cash -= total_value # Deduct the cost from the buyer's cash
|
|
||||||
return f"Bought {amount} stocks of {company_id}."
|
return f"Bought {amount} stocks of {company_id}."
|
||||||
|
|
||||||
def _sell_stock(self, company_id, amount, total_value, is_ai):
|
def _sell_stock(self, company_id, amount, total_value, is_ai):
|
||||||
"""Handles the selling of stocks for the specified company."""
|
if not is_ai:
|
||||||
# Check if the seller has enough stocks to sell
|
if self.stock_holdings[company_id] < amount:
|
||||||
stock_ownership = self._get_stock_ownership(company_id) if not is_ai else self.own_stock_ownership[company_id]
|
return "Not enough stocks to sell."
|
||||||
if stock_ownership < amount:
|
# Update buyer's stock_holdings for non-AI (like player)
|
||||||
return "Not enough stocks to sell."
|
self.stock_holdings[company_id] -= amount
|
||||||
|
|
||||||
# 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
|
|
||||||
self.stock_holdings[company_id] -= amount
|
|
||||||
else:
|
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.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}."
|
return f"Sold {amount} stocks of {company_id}."
|
||||||
|
|
||||||
def _calculate_available_shares(self):
|
def _calculate_available_shares(self):
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user