idlechampbestiary.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. import json
  2. from pprint import pprint
  3. import re
  4. from collections import OrderedDict
  5. import math
  6. from decimal import Decimal
  7. from idlechampaccount import ICAccount
  8. import subprocess
  9. import filecmp
  10. import os
  11. COMPARE = True
  12. POST = False
  13. REDOWNLOAD = False
  14. SHOW_CHANGES = False
  15. _summary = None
  16. if POST:
  17. instance = ICAccount()
  18. instance.login()
  19. # filename = '/home/txtsd/.local/share/Steam/steamapps/common/IdleChampions/IdleDragons_Data/StreamingAssets/downloaded_files/cached_definitions.json'
  20. filename = '/tmp/cached_definitions.json'
  21. if REDOWNLOAD or not os.path.isfile(filename):
  22. result = requests.get('http://master.idlechampions.com/~idledragons/post.php?call=getdefinitions')
  23. with open(filename, 'w') as f:
  24. if result.status_code == 200:
  25. f.write(result.text)
  26. monster_filename = 'json/monster_defines.json'
  27. with open(filename) as f:
  28. file = f.read()
  29. with open(monster_filename) as f:
  30. monster_file = f.read()
  31. js = json.loads(file)
  32. js_mon = json.loads(monster_file)
  33. js_graphic = js['graphic_defines']
  34. js_attack = js['attack_defines']
  35. js_hero = js['hero_defines']
  36. js_hero_skin = js['hero_skin_defines']
  37. js_upgrade = js['upgrade_defines']
  38. js_premium_item = js['premium_item_defines']
  39. js_sound = js['sound_defines']
  40. js_buff = js['buff_defines']
  41. js_loot = js['loot_defines']
  42. js_achievement = js['achievement_defines']
  43. js_ability = js['ability_defines']
  44. js_effect = js['effect_defines']
  45. js_changelog = js['changelog_defines']
  46. js_text = js['text_defines']
  47. js_chest_type = js['chest_type_defines']
  48. js_effect_key = js['effect_key_defines']
  49. js_tutorial_state = js['tutorial_state_defines']
  50. js_game_rule = js['game_rule_defines']
  51. js_news = js['news_defines']
  52. js_language = js['language_defines']
  53. js_familiar = js['familiar_defines']
  54. page_text = '''
  55. The \'\'\'Bestiary\'\'\' for [[Idle Champions of the Forgotten Realms]].
  56. ==Description==
  57. Idle Champions features a wide array of races from the D&D universe as opponents, such as [[Orc]]s, [[Hobgoblin]]s and [[Gelatinous Cube]]s.
  58. ==List Of Creatures==
  59. {creature_list_text}
  60. ==List of Types==
  61. __type_list_text__
  62. ==See also==
  63. [[Bosses]]
  64. __Navbox-IdleChampions__'''
  65. for mon in js_mon:
  66. if mon['name'] == 'Jarlaxle':
  67. temp = mon
  68. js_mon.remove(mon)
  69. temp['name'] = 'Jarlaxle (Monster)'
  70. js_mon.append(temp)
  71. __js_mon = sorted(js_mon, key=lambda x: x['id'])
  72. type_list = set()
  73. taglist = {}
  74. types = ['boss',
  75. 'flying',
  76. 'item',
  77. 'melee',
  78. 'ranged',
  79. 'relentless',
  80. 'spawner',
  81. 'static']
  82. for monster in __js_mon:
  83. for tag in monster['tags']:
  84. if tag not in taglist:
  85. type_list.add(tag)
  86. for item in type_list:
  87. # if item not in types:
  88. taglist.update({item: set()})
  89. # print(taglist)
  90. for monster in __js_mon:
  91. for tag in monster['tags']:
  92. if tag in taglist:
  93. taglist[tag].add(monster['name'])
  94. # pprint(taglist)
  95. # pprint(type_list)
  96. creature_list = ''
  97. for mon_type in sorted(taglist):
  98. if mon_type not in types:
  99. creature_list += '[[{mon_type}]]\n'.format(mon_type=mon_type.capitalize())
  100. creatures = ''
  101. # print(mon_type)
  102. # pprint(sorted(taglist[mon_type], key=lambda x: x['name']))
  103. for mob in sorted(taglist[mon_type]):
  104. creatures += '[[{mob}]], '.format(mob=mob)
  105. creatures = ': ' + creatures
  106. creatures = creatures[:-2]
  107. creatures += '\n'
  108. creature_list += creatures
  109. page_text = page_text.format(creature_list_text=creature_list)
  110. page_text = page_text.replace('__type_list_text__', '{type_list_text}')
  111. # print(page_text)
  112. list_of_types = ''
  113. for mon_type in sorted(taglist):
  114. if mon_type in types:
  115. list_of_types += '[[{mon_type}]]\n'.format(mon_type=mon_type.capitalize())
  116. creatures = ''
  117. # print(mon_type)
  118. # pprint(sorted(taglist[mon_type], key=lambda x: x['name']))
  119. for mob in sorted(taglist[mon_type]):
  120. creatures += '[[{mob}]], '.format(mob=mob)
  121. creatures = ': ' + creatures
  122. creatures = creatures[:-2]
  123. creatures += '\n'
  124. list_of_types += creatures
  125. page_text = page_text.format(type_list_text=list_of_types)
  126. # print(page_text)
  127. page_text = page_text.replace('__Navbox-IdleChampions__', '{{Navbox-IdleChampions}}')
  128. # Stub status
  129. page_text = '{{stub}}' + page_text
  130. with open('output/bestiary.txt', 'w') as f:
  131. f.write(page_text)
  132. if COMPARE:
  133. dir_path = os.path.dirname(os.path.realpath(__file__))
  134. if not os.path.isdir(os.path.join(dir_path, 'output')):
  135. os.makedirs(os.path.join(dir_path, 'output'))
  136. main_file = os.path.join(dir_path, 'output/bestiary.txt')
  137. posted_file = os.path.join(dir_path, 'output/posted/bestiary.txt')
  138. try:
  139. result = filecmp.cmp(main_file, posted_file, shallow=False)
  140. except FileNotFoundError as e:
  141. print('No source found')
  142. result = None
  143. if result is not None:
  144. if result:
  145. print('No changes')
  146. else:
  147. print('Changes detected!')
  148. if SHOW_CHANGES:
  149. result = subprocess.run(['git', 'diff', '--no-index', posted_file, main_file])
  150. if POST:
  151. if _summary is None:
  152. _summary = input('Enter change summary: ')
  153. EDIT_PARAMS = {
  154. 'action': 'edit',
  155. 'title': 'Bestiary',
  156. 'text': page_text,
  157. 'bot': '1',
  158. 'nocreate': '1',
  159. 'summary': _summary
  160. }
  161. print('Posting...')
  162. R2 = instance.post(data=EDIT_PARAMS)
  163. if R2.status_code == 200:
  164. print('Success!')
  165. with open('output/posted/bestiary.txt', 'w') as g:
  166. g.write(page_text)
  167. else:
  168. print('FAIL!')