integration.py 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. from __future__ import annotations
  2. import collections
  3. import re
  4. import sys
  5. import typing
  6. import cache
  7. if typing.TYPE_CHECKING:
  8. import roi
  9. def main() -> None:
  10. mat = sys.argv[1]
  11. recipes: list[roi.Recipe] = cache.get('https://api.prunplanner.org/data/recipes/')
  12. companies = pmmg_monthly_report()
  13. print(mat, '→')
  14. wrought = (recipe for recipe in recipes
  15. if mat in (i['material_ticker'] for i in recipe['inputs']) and len(recipe['outputs']) == 1)
  16. output_mats = {}
  17. for recipe in sorted(wrought, key=lambda r: r['outputs'][0]['material_ticker']):
  18. (output,) = recipe['outputs']
  19. (input,) = (i for i in recipe['inputs'] if i['material_ticker'] == mat)
  20. ratio = input['material_amount'] / output['material_amount']
  21. output_mats[output['material_ticker']] = ratio
  22. print(f'\t{output["material_ticker"]:3}:', ratio)
  23. companies_produced = companies_consumed = companies_consumed_80 = 0
  24. into = dict.fromkeys(output_mats.keys(), 0)
  25. for company in companies.values():
  26. if (co_production := company.get(mat)) is None:
  27. continue
  28. companies_produced += 1
  29. consumed = 0
  30. for output_mat, per_run_consumption in output_mats.items():
  31. if co_consumption := company.get(output_mat):
  32. total_consumption = co_consumption['amount'] * per_run_consumption
  33. consumed += total_consumption
  34. into[output_mat] += total_consumption
  35. if consumed > 0:
  36. companies_consumed += 1
  37. if consumed > 0.8 * co_production['amount']:
  38. companies_consumed_80 += 1
  39. print(f'{companies_produced} companies producing')
  40. print(f'{companies_consumed} companies consuming their own production')
  41. print(f'{companies_consumed_80} companies consuming 80%+ of their own production')
  42. recipes_per_output = collections.defaultdict(int)
  43. for recipe in recipes:
  44. for output in recipe['outputs']:
  45. if output['material_ticker'] in into:
  46. recipes_per_output[output['material_ticker']] += 1
  47. for output_mat, total in sorted(into.items(), key=lambda kv: kv[1], reverse=True):
  48. alt = ''
  49. if (recipe_count := recipes_per_output[output_mat]) != 1:
  50. alt = f' ({recipe_count} recipes)'
  51. print(f'{output_mat:3}: {total:8,.0f}{alt}')
  52. def pmmg_monthly_report() -> dict[str, dict[str, CompanyOutput]]:
  53. report_constants = cache.get(
  54. 'https://raw.githubusercontent.com/PMMG-Products/pmmg-products.github.io/main/reports/src/staticData/constants.ts', json=False)
  55. # export const months = ["mar25", "apr25", ..., "dec25", "jan26"];
  56. match = re.search(r'export const months = \[(.*?)\];', report_constants)
  57. assert match
  58. months_str = match.group(1)
  59. months = [m.strip().strip('"') for m in months_str.split(',')]
  60. last_month = months[-1]
  61. print('getting report for', last_month)
  62. return cache.get(f'https://pmmg-products.github.io/reports/data/company-data-{last_month}.json')['individual']
  63. class CompanyOutput(typing.TypedDict):
  64. amount: int
  65. if __name__ == '__main__':
  66. main()