浏览代码

vertical integration analyzer

raylu 3 周之前
父节点
当前提交
33c60219f1
共有 1 个文件被更改,包括 64 次插入0 次删除
  1. 64 0
      integration.py

+ 64 - 0
integration.py

@@ -0,0 +1,64 @@
+from __future__ import annotations
+
+import sys
+import typing
+
+import cache
+
+def main() -> None:
+	mat = sys.argv[1]
+
+	recipes: list[Recipe] = cache.get('https://api.prunplanner.org/data/recipes')
+	companies: dict[str, dict[str, CompanyOutput]] = cache.get(
+			'https://pmmg-products.github.io/reports/data/company-data-oct25.json')['individual']
+
+	print(mat, '→')
+	wrought = (recipe for recipe in recipes if mat in (i['Ticker'] for i in recipe['Inputs']))
+	output_mats = {}
+	for recipe in wrought:
+		if len(recipe['Outputs']) != 1:
+			continue
+		(output,) = recipe['Outputs']
+		(input,) = (i for i in recipe['Inputs'] if i['Ticker'] == mat)
+		ratio = input['Amount'] / output['Amount']
+		output_mats[output['Ticker']] = ratio
+		print(f'\t{output["Ticker"]}:', ratio)
+
+	companies_produced = companies_consumed = companies_consumed_80 = 0
+	into = dict.fromkeys(output_mats.keys(), 0)
+	for company in companies.values():
+		if (co_production := company.get(mat)) is None:
+			continue
+		companies_produced += 1
+		consumed = 0
+		for output_mat, per_run_consumption in output_mats.items():
+			if co_consumption := company.get(output_mat):
+				total_consumption = co_consumption['amount'] * per_run_consumption
+				consumed += total_consumption
+				into[output_mat] += total_consumption
+		if consumed > 0:
+			companies_consumed += 1
+			if consumed > 0.8 * co_production['amount']:
+				companies_consumed_80 += 1
+	print(f'{companies_produced} companies producing')
+	print(f'{companies_consumed} companies consuming their own production')
+	print(f'{companies_consumed_80} companies consuming 80%+ of their own production')
+	for output_mat, total in into.items():
+		print(f'{output_mat}:', total)
+
+class CompanyOutput(typing.TypedDict):
+	amount: int
+
+class Recipe(typing.TypedDict):
+	RecipeName: str
+	BuildingTicker: str
+	Inputs: list[RecipeMat]
+	Outputs: list[RecipeMat]
+	TimeMs: int
+
+class RecipeMat(typing.TypedDict):
+	Ticker: str
+	Amount: int
+
+if __name__ == '__main__':
+	main()