Procházet zdrojové kódy

supply: use exports from previous planets

raylu před 15 hodinami
rodič
revize
5b287db18e
1 změnil soubory, kde provedl 23 přidání a 5 odebrání
  1. 23 5
      supply.py

+ 23 - 5
supply.py

@@ -85,7 +85,7 @@ def main() -> None:
 
 	warehouse = warehouse_inventory()
 	total_cost = 0
-	for planet in planets:
+	for i, planet in enumerate(planets):
 		print('\n' + cyan(planet.name))
 		supply_config = config.supply_config(planet.name)
 		for consumption in planet.net_consumption:
@@ -98,11 +98,19 @@ def main() -> None:
 				if ticker in supply_config.ignore_materials:
 					print(f' | {need:5.0f} (ignored)')
 				else:
+					print(f' | {need:5.0f}', end='')
+					sources = []
+					for exporter in planets[:i]:
+						if ticker in exporter.exporting and (avail := exporter.inventory.get(ticker, 0)):
+							need -= min(need, avail)
+							exporter.inventory[ticker] -= max(avail - need, 0)
+							sources.append(f'{exporter.name}: {avail}')
+					if avail := warehouse.get(ticker, 0):
+						need -= min(need, avail)
+						warehouse[ticker] -= max(avail - need, 0)
+						sources.append(f'WH: {avail}')
 					cost = raw_prices[ticker]['Ask'] * need
-					print(f' | {need:5.0f} (${cost:6.0f})')
-					if have := warehouse.get(ticker, 0):
-						cost = raw_prices[ticker]['Ask'] * max(need - have, 0)
-						warehouse[ticker] = max(have - need, 0)
+					print(f' (${cost:6.0f}) ' + ' '.join(sources))
 					total_cost += cost
 			else:
 				print()
@@ -189,10 +197,13 @@ class Planet:
 	name: str
 	inventory: dict[str, int]
 	net_consumption: typing.Sequence[Amount]
+	exporting: typing.Set[str]
 
 	def __init__(self, fio_burn: FIOBurn) -> None:
 		self.name = fio_burn['PlanetName'] or fio_burn['PlanetNaturalId']
 		self.inventory = {item['MaterialTicker']: item['MaterialAmount'] for item in fio_burn['Inventory']}
+
+		# producing any amount (including less than consumption)
 		producing = {item['MaterialTicker']: item for item in fio_burn['OrderProduction']}
 		self.net_consumption = []
 		for c in fio_burn['OrderConsumption'] + fio_burn['WorkforceConsumption']:
@@ -204,6 +215,13 @@ class Planet:
 			c['net_consumption'] = net
 			self.net_consumption.append(c)
 
+		consuming = {item['MaterialTicker'] for item in self.net_consumption}
+		# producing more than consumption
+		self.exporting = set()
+		for item in fio_burn['OrderProduction']:
+			if item['MaterialTicker'] not in consuming:
+				self.exporting.add(item['MaterialTicker'])
+
 	def supply_for_days(self, target_days: float) -> dict[str, int]:
 		buy: dict[str, int] = {}
 		for consumption in self.net_consumption: