main.py 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #! venv/bin/python
  2. import json
  3. import re
  4. import sys
  5. from pprint import pprint
  6. import model
  7. import view
  8. def extract_hospital_time(this_message_text, this_message_integer):
  9. in_hospital = False
  10. time_text = ''
  11. hours = 0
  12. minutes = 0
  13. if this_message_integer == 0:
  14. if 'hospital' in this_message_text[0]:
  15. in_hospital = True
  16. time_text = this_message_text[0][15:]
  17. print(time_text)
  18. m = re.match(regex, time_text)
  19. if m:
  20. if m.group('hours'):
  21. hours = m.group('hours')
  22. if m.group('minutes'):
  23. minutes = m.group('minutes')
  24. print(f'Extracted {hours} hrs and {minutes} mins')
  25. if __name__ == '__main__':
  26. regex = re.compile(r'( (?P<hours>\d+) hrs)? (?P<minutes>\d+) mins ')
  27. for player_id in [4, 15]:
  28. status = model.get_loot_level(player_id)
  29. # print(status)
  30. extract_hospital_time(status['text'], status['integer'])
  31. old_status = 0
  32. write_file_next = False
  33. try:
  34. old_status_file = open(f'status-{player_id}.json', 'r')
  35. if old_status_file.mode == 'r':
  36. old_status_raw = old_status_file.read()
  37. if old_status_raw:
  38. old_status_json = json.loads(old_status_raw)
  39. old_status = int(old_status_json['integer'])
  40. else:
  41. write_file_next = True
  42. old_status_file.close()
  43. except FileNotFoundError:
  44. empty_status_file = open(f'status-{player_id}.json', 'w+')
  45. print('Created new file ... Exiting')
  46. empty_status_file.close()
  47. sys.exit(1)
  48. # print(old_status)
  49. calculate_next_opportunity = False
  50. send_message = False
  51. # print('if old_status {} != status[1] {}'.format(old_status, status[1]))
  52. assert type(old_status) is int, f"old_status {old_status} is not an integer: {type(old_status)}"
  53. assert type(status['integer']) is int, \
  54. f"status['integer'] {status['integer']} is not an integer: {type(status['integer'])}"
  55. if old_status != status['integer']:
  56. calculate_next_opportunity = True
  57. write_file_next = True
  58. send_message = True
  59. # print('write_file_next: {}'.format(write_file_next))
  60. if calculate_next_opportunity:
  61. message_text = status['text']
  62. message_integer = status['integer']
  63. if write_file_next:
  64. # summary = {'text': status["status"], 'roman': status["roman"], 'integer': status["integer"]}
  65. pprint(status)
  66. summary_json = json.dumps(status, indent=2)
  67. print(summary_json)
  68. write_file = open(f'status-{player_id}.json', 'w')
  69. write_file.write(summary_json)
  70. write_file.close()
  71. write_file.close()
  72. if send_message:
  73. view.send_message(status)