integration.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. from __future__ import annotations
  2. import collections
  3. import sys
  4. import typing
  5. import cache
  6. def main() -> None:
  7. mat = sys.argv[1]
  8. recipes: list[Recipe] = cache.get('https://api.prunplanner.org/data/recipes')
  9. companies: dict[str, dict[str, CompanyOutput]] = cache.get(
  10. 'https://pmmg-products.github.io/reports/data/company-data-dec25.json')['individual']
  11. print(mat, '→')
  12. wrought = (recipe for recipe in recipes if mat in (i['Ticker'] for i in recipe['Inputs']) and len(recipe['Outputs']) == 1)
  13. output_mats = {}
  14. for recipe in sorted(wrought, key=lambda r: r['Outputs'][0]['Ticker']):
  15. (output,) = recipe['Outputs']
  16. (input,) = (i for i in recipe['Inputs'] if i['Ticker'] == mat)
  17. ratio = input['Amount'] / output['Amount']
  18. output_mats[output['Ticker']] = ratio
  19. print(f'\t{output["Ticker"]:3}:', ratio)
  20. companies_produced = companies_consumed = companies_consumed_80 = 0
  21. into = dict.fromkeys(output_mats.keys(), 0)
  22. for company in companies.values():
  23. if (co_production := company.get(mat)) is None:
  24. continue
  25. companies_produced += 1
  26. consumed = 0
  27. for output_mat, per_run_consumption in output_mats.items():
  28. if co_consumption := company.get(output_mat):
  29. total_consumption = co_consumption['amount'] * per_run_consumption
  30. consumed += total_consumption
  31. into[output_mat] += total_consumption
  32. if consumed > 0:
  33. companies_consumed += 1
  34. if consumed > 0.8 * co_production['amount']:
  35. companies_consumed_80 += 1
  36. print(f'{companies_produced} companies producing')
  37. print(f'{companies_consumed} companies consuming their own production')
  38. print(f'{companies_consumed_80} companies consuming 80%+ of their own production')
  39. recipes_per_output = collections.defaultdict(int)
  40. for recipe in recipes:
  41. for output in recipe['Outputs']:
  42. if output['Ticker'] in into:
  43. recipes_per_output[output['Ticker']] += 1
  44. for output_mat, total in sorted(into.items(), key=lambda kv: kv[1], reverse=True):
  45. alt = ''
  46. if (recipe_count := recipes_per_output[output_mat]) != 1:
  47. alt = f' ({recipe_count} recipes)'
  48. print(f'{output_mat:3}: {total:8,.0f}{alt}')
  49. class CompanyOutput(typing.TypedDict):
  50. amount: int
  51. class Recipe(typing.TypedDict):
  52. RecipeName: str
  53. BuildingTicker: str
  54. Inputs: list[RecipeMat]
  55. Outputs: list[RecipeMat]
  56. TimeMs: int
  57. class RecipeMat(typing.TypedDict):
  58. Ticker: str
  59. Amount: int
  60. if __name__ == '__main__':
  61. main()