From 24951b36be27acbb3a62093ed0fdb2a7f261cc0a Mon Sep 17 00:00:00 2001 From: Isaak Date: Mon, 20 Nov 2023 00:21:21 +0100 Subject: [PATCH] Still some really weird behaviour, but stock trading is working a bit better now than before. --- main.py | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/main.py b/main.py index 1879932..f95e1bc 100644 --- a/main.py +++ b/main.py @@ -193,7 +193,7 @@ class Market: self.unemployment_rate = max(0, min(self.unemployment_rate + fluctuation, 1)) def handle_market_events(self): - event = random.choices(self.events, weights=[e["probability"] for e in self.events], k=1)[0] + event = random.choices(self.events, weights=[e["probability"] for e in self.events])[0] self.last_event_name = event["name"] getattr(self, event["effect"])() if event["effect"] in dir(self) else None @@ -486,13 +486,12 @@ class Company: 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 - - # 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 + if company_id != self.player_id: + # Update buyer's stock_holdings for investments in other companies + self.stock_holdings[company_id] += amount + else: + # Update own_stock_ownership when buying own shares + self.own_stock_ownership[self.player_id] += amount return f"Bought {amount} stocks of {company_id}."