From 02b9ce142a453c9d1b7b532a33f065a3c5d68e54 Mon Sep 17 00:00:00 2001 From: Isaak Date: Sat, 18 Nov 2023 18:02:01 +0100 Subject: [PATCH] crafting fixed --- main.py | 102 ++++++++++++++++++++++++++++++++------------------------ 1 file changed, 58 insertions(+), 44 deletions(-) diff --git a/main.py b/main.py index 2827c81..5669177 100644 --- a/main.py +++ b/main.py @@ -29,7 +29,6 @@ from rich.console import Console from rich.table import Table from rich.prompt import Prompt from rich import box - from rich.panel import Panel from rich.text import Text @@ -51,7 +50,6 @@ def load_json(filename): class Market: """Manages market dynamics, including companies, products, stock transactions, and economic indicators.""" - def __init__(self): self.current_turn = 0 self.companies = {} @@ -175,10 +173,7 @@ class Company: self.inventory = {'A': 0, 'B': 0, 'C': 0} self.crafting_queue = [] self.own_stock_ownership = {cid: 51 if cid == player_id else 0 for cid in [player_id] + competitors_ids} - - all_company_ids = set([player_id] + competitors_ids + ["RationalAI", "RiskTakingAI"]) - self.other_stock_holdings = {cid: 0 for cid in all_company_ids} - + self.stock_holdings = {cid: 0 for cid in set([player_id] + competitors_ids + ["RationalAI", "RiskTakingAI"])} self.total_shares = 100 self._market = market @@ -187,15 +182,44 @@ class Company: """Calculates the total value of the company, combining cash and the market value of its inventory.""" return self.cash + sum(self.inventory[product] * price for product, price in self._market.products.items()) - def crafting_decision(self): - """Displays crafting options and handles the user's crafting choice.""" - print("\nCrafting Decision") - recipe_keys = list(crafting_recipes.keys()) - print("\nAvailable Recipes:") - for idx, recipe in enumerate(recipe_keys, 1): - print(f" {idx}: {recipe}") - recipe_choice = self.get_user_choice(len(recipe_keys), "Choose a recipe to craft: ") - self.craft_product(recipe_keys[recipe_choice - 1]) + def craft_product(self, recipe_key): + """Processes crafting of a product based on the chosen recipe.""" + print(f"Inventory before crafting: {self.inventory}") + recipe = crafting_recipes[recipe_key] + if all(self.inventory[product] >= quantity for product, quantity in recipe['input'].items()): + self.crafting_queue.append({'recipe': recipe, 'turns_remaining': recipe['turns']}) + self._update_inventory(recipe['input'], decrease=True) + print("Crafting order placed.") + else: + print("Not enough resources to craft.") + print(f"Inventory after crafting: {self.inventory}") + + def _update_inventory(self, items, decrease=False): + """Updates the inventory based on the given items.""" + print(f"Inventory before update: {self.inventory}") + for product, quantity in items.items(): + if decrease: + self.inventory[product] -= quantity + else: + self.inventory[product] += quantity + print(f"Inventory after update: {self.inventory}") + + def update_crafting(self): + """Updates the crafting queue, completing orders as their turns conclude.""" + print(f"Crafting queue before update: {self.crafting_queue}") + completed_orders = [] + for order in self.crafting_queue: + print(f"Processing order: {order}") + order['turns_remaining'] -= 1 + if order['turns_remaining'] == 0: + print(f"Completing order: {order}") + self._update_inventory(order['recipe']['output'], decrease=False) + print(f"Inventory after completing order: {self.inventory}") + completed_orders.append(order) + + for order in completed_orders: + self.crafting_queue.remove(order) + print(f"Crafting queue after update: {self.crafting_queue}") def trade_product(self, market, product, quantity, buying=True): """Executes a product trade transaction based on the specified parameters.""" @@ -207,25 +231,15 @@ class Company: self.cash += total_cost self.inventory[product] -= quantity - def craft_product(self, recipe_key): - """Processes crafting of a product based on the chosen recipe.""" - recipe = crafting_recipes[recipe_key] - if all(self.inventory[product] >= quantity for product, quantity in recipe['input'].items()): - self.crafting_queue.append({'recipe': recipe, 'turns_remaining': recipe['turns']}) - for product, quantity in recipe['input'].items(): - self.inventory[product] -= quantity - print("Crafting order placed.") - else: - print("Not enough resources to craft.") - - def update_crafting(self): - """Updates the crafting queue, completing orders as their turns conclude.""" - for order in self.crafting_queue[:]: - order['turns_remaining'] -= 1 - if order['turns_remaining'] == 0: - for product, quantity in order['recipe']['output'].items(): - self.inventory[product] += quantity - self.crafting_queue.remove(order) + def crafting_decision(self): + """Displays crafting options and handles the user's crafting choice.""" + print("\nCrafting Decision") + recipe_keys = list(crafting_recipes.keys()) + print("\nAvailable Recipes:") + for idx, recipe in enumerate(recipe_keys, 1): + print(f" {idx}: {recipe}") + recipe_choice = self.get_user_choice(len(recipe_keys), "Choose a recipe to craft: ") + self.craft_product(recipe_keys[recipe_choice - 1]) def trade_stock(self, action, market, company_id, amount, is_ai=False): """Executes a stock trade action, buying or selling as specified.""" @@ -277,13 +291,13 @@ class Company: if company_id == self.player_id: return self.total_shares - sum(self.own_stock_ownership.values()) + self.own_stock_ownership[self.player_id] else: - return self.total_shares - self.other_stock_holdings.get(company_id, 0) + return self.total_shares - self.stock_holdings.get(company_id, 0) def _get_stock_ownership(self, company_id): """Retrieves the stock ownership amount for a given company.""" if company_id == self.player_id: return self.own_stock_ownership[self.player_id] - return self.other_stock_holdings.get(company_id, 0) + return self.stock_holdings.get(company_id, 0) def _update_stock_ownership(self, company_id, amount, total_value, buying): """Updates the stock ownership details after a buy or sell action.""" @@ -294,9 +308,9 @@ class Company: self.own_stock_ownership[self.player_id] -= amount else: if buying: - self.other_stock_holdings[company_id] += amount + self.stock_holdings[company_id] += amount else: - self.other_stock_holdings[company_id] -= amount + self.stock_holdings[company_id] -= amount self.cash += -total_value if buying else total_value def make_decision(self, market, competitors): @@ -314,7 +328,7 @@ class Company: [f"[bold]{company}[/]: {ownership} shares" for company, ownership in self.own_stock_ownership.items()]) status_table.add_row("Your Shareholders", shareholders) investments = ', '.join( - [f"[bold]{company}[/]: {holding} shares" for company, holding in self.other_stock_holdings.items() if + [f"[bold]{company}[/]: {holding} shares" for company, holding in self.stock_holdings.items() if holding > 0]) status_table.add_row("Your Investments", investments) console.print(status_table) @@ -349,7 +363,7 @@ class Company: products = list(market.products.keys()) print("\nAvailable Products:") for idx, product in enumerate(products, 1): - print(f" {idx}: {product} - Price: {market.products[product]},00 €") + print(f" {idx}: {product} - Price: {market.products[product]:.2f} €") product_choice = self.get_user_choice(len(products), "Choose a product to trade: ") product = products[product_choice - 1] @@ -372,8 +386,8 @@ class Company: for idx, company in enumerate(companies, 1): company_id = company.player_id stock_info = f" {idx}: {company_id} - Current stock price: {market.get_stock_price(company_id)}" - if action == 'sell' and self.other_stock_holdings.get(company_id, 0) > 0: - stock_info += f", Owned: {self.other_stock_holdings[company_id]}" + if action == 'sell' and self.stock_holdings.get(company_id, 0) > 0: + stock_info += f", Owned: {self.stock_holdings[company_id]}" print(stock_info) company_choice = self.get_user_choice(len(companies), "Enter the company number to trade stocks: ") @@ -478,10 +492,10 @@ class AICompany(Company): def select_stock_to_sell(self): """Chooses a company's stock to sell from the AI's holdings.""" - owned_stocks = [(comp_id, amount) for comp_id, amount in self.other_stock_holdings.items() if amount > 0] + owned_stocks = [(comp_id, amount) for comp_id, amount in self.stock_holdings.items() if amount > 0] if owned_stocks: company_id, _ = random.choice(owned_stocks) - amount = random.randint(1, self.other_stock_holdings[company_id]) + amount = random.randint(1, self.stock_holdings[company_id]) return company_id, amount return None, 0