integration.py 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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()['individual']
  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_only = companies_consumed_own = companies_consumed_80_own = 0
  24. into = dict.fromkeys(output_mats.keys(), 0)
  25. for company in companies.values():
  26. if (co_production := company.get(mat)) is not None:
  27. companies_produced += 1
  28. consumed = 0
  29. for output_mat, per_run_consumption in output_mats.items():
  30. if co_production := company.get(output_mat):
  31. co_consumption = co_production['amount'] * per_run_consumption
  32. consumed += co_consumption
  33. into[output_mat] += co_consumption
  34. if consumed > 0:
  35. companies_consumed += 1
  36. if co_production is None:
  37. companies_consumed_only += 1
  38. else:
  39. companies_consumed_own += 1
  40. if consumed > 0.8 * co_production['amount']:
  41. companies_consumed_80_own += 1
  42. print(f'{companies_produced} companies producing')
  43. print(f'{companies_consumed} companies consuming')
  44. print(f'{companies_consumed_only} companies consuming without producing any')
  45. print(f'{companies_consumed_own} companies consuming their own production')
  46. print(f'{companies_consumed_80_own} companies consuming 80%+ of their own production')
  47. recipes_per_output = collections.defaultdict(int)
  48. for recipe in recipes:
  49. for output in recipe['outputs']:
  50. if output['material_ticker'] in into:
  51. recipes_per_output[output['material_ticker']] += 1
  52. total_consumed = 0
  53. for output_mat, consumed in sorted(into.items(), key=lambda kv: kv[1], reverse=True):
  54. alt = ''
  55. if (recipe_count := recipes_per_output[output_mat]) != 1:
  56. alt = f' ({recipe_count} recipes)'
  57. print(f'{output_mat:3}: {consumed:8,.0f}{alt}')
  58. total_consumed += consumed
  59. print('total consumed:', total_consumed)
  60. def pmmg_monthly_report() -> CompanyData:
  61. last_month = pmmg_month()
  62. print('getting report for', last_month)
  63. return cache.get(f'https://prun.raylu.net/stats/data/company-data-{last_month}.json', expiry=cache.ONE_DAY)
  64. def pmmg_month() -> str:
  65. report_constants = cache.get('https://git.raylu.net/raylu/prunstats/raw/main/src/staticData/constants.ts',
  66. json=False, expiry=cache.ONE_DAY)
  67. # export const months = ["mar25", "apr25", ..., "dec25", "jan26"];
  68. match = re.search(r'export const months = \[(.*?)\];', report_constants)
  69. assert match
  70. months_str = match.group(1)
  71. months = [m.strip().strip('"') for m in months_str.split(',')]
  72. return months[-1]
  73. class CompanyData(typing.TypedDict):
  74. totals: dict[str, CompanyTotal]
  75. individual: dict[str, dict[str, CompanyOutput]]
  76. class CompanyTotal(typing.TypedDict):
  77. volume: float
  78. class CompanyOutput(typing.TypedDict):
  79. amount: int
  80. if __name__ == '__main__':
  81. main()