Browse Source

supply: refactor into buy_for_target

raylu 3 tuần trước cách đây
mục cha
commit
1751827ed6
1 tập tin đã thay đổi với 16 bổ sung12 xóa
  1. 16 12
      supply.py

+ 16 - 12
supply.py

@@ -38,18 +38,7 @@ def main() -> None:
 	print(f'consuming {weight_per_day:.1f}t/d')
 	target_days = round(target_days + 0.05, 1)
 	while True:
-		weight_used = volume_used = 0
-		buy: dict[str, float] = {}
-		for consumption in net_consumption:
-			ticker = consumption['MaterialTicker']
-			avail = inventory.get(ticker, 0)
-			daily_consumption = consumption['net_consumption']
-			days = avail / daily_consumption
-			if days < target_days:
-				buy[ticker] = (target_days - days) * daily_consumption
-				weight_used += buy[ticker] * materials[ticker]['Weight']
-				volume_used += buy[ticker] * materials[ticker]['Volume']
-
+		buy, weight_used, volume_used = buy_for_target(materials, net_consumption, inventory, target_days)
 		if weight_used > 500 or volume_used > 500:
 			break
 		optimal = buy
@@ -84,6 +73,21 @@ def fio_data(planet: str) -> tuple[PlanetData, typing.Sequence[Material]]:
 	materials: list[Material] = cache.get('https://rest.fnar.net/material/allmaterials')
 	return planet_data, materials
 
+def buy_for_target(materials: dict[str, Material],  net_consumption: typing.Sequence[Amount], inventory: dict[str, int],
+		target_days: float) -> tuple[dict[str, float], float, float]:
+	weight_used = volume_used = 0
+	buy: dict[str, float] = {}
+	for consumption in net_consumption:
+		ticker = consumption['MaterialTicker']
+		avail = inventory.get(ticker, 0)
+		daily_consumption = consumption['net_consumption']
+		days = avail / daily_consumption
+		if days < target_days:
+			buy[ticker] = (target_days - days) * daily_consumption
+			weight_used += buy[ticker] * materials[ticker]['Weight']
+			volume_used += buy[ticker] * materials[ticker]['Volume']
+	return buy, weight_used, volume_used
+
 class PlanetData(typing.TypedDict):
 	PlanetName: str
 	Error: typing.Any