main.py 4.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. #! venv/bin/python
  2. import json
  3. # import sys
  4. import math
  5. # from pprint import pprint
  6. import arrow
  7. from pprint import pprint
  8. import model
  9. import view
  10. if __name__ == '__main__':
  11. for player_id in [4, 15]:
  12. status = model.get_loot_level(player_id)
  13. # print(status)
  14. # seconds = extract_hospital_time(status['text'], status['integer'])
  15. write_file_next = False
  16. # Get data from stored json file
  17. old_status_json, old_status = model.get_json_file(player_id)
  18. if old_status_json == -1 or old_status == -1:
  19. write_file_next = True
  20. calculate_next_opportunity = False
  21. send_message = False
  22. # print('if old_status {} != status[\'integer\'] {}'.format(old_status, status['integer']))
  23. assert type(old_status) is int, f"old_status {old_status} is not an integer: {type(old_status)}"
  24. assert type(status['integer']) is int, \
  25. f"status['integer'] {status['integer']} is not an integer: {type(status['integer'])}"
  26. timestamp_when_okay = int(status['timestamp'])
  27. if status['integer'] == 0:
  28. seconds_in_hosp = model.extract_hospital_time(status['text'], _debug=False)
  29. timestamp_when_okay += seconds_in_hosp
  30. if old_status != status['integer']:
  31. calculate_next_opportunity = True
  32. write_file_next = True
  33. send_message = True
  34. # print('write_file_next: {}'.format(write_file_next))
  35. if calculate_next_opportunity:
  36. message_text = status['text']
  37. message_integer = status['integer']
  38. if write_file_next:
  39. # summary = {'text': status["status"], 'roman': status["roman"], 'integer': status["integer"]}
  40. if status['integer'] <= 2:
  41. status['alert'] = {'15': False, '10': False, '5': False, '1': False}
  42. summary_json = json.dumps(status, indent=2)
  43. # print(summary_json)
  44. write_file = open(f'status-{player_id}.json', 'w')
  45. write_file.write(summary_json)
  46. write_file.close()
  47. write_file.close()
  48. seconds_to_go, attack_time = model.calculate_time_to_next_attack(player_id)
  49. player_name = status['name']
  50. attack_time_object = arrow.get(attack_time)
  51. attack_time_time = attack_time_object.format('hh:mm A')
  52. minutes_to_go = math.floor(seconds_to_go / 60)
  53. t = f'In < {minutes_to_go} mins ({attack_time_time}) {player_name} [{player_id}] reaches level IV'
  54. # print(t)
  55. status['time_string'] = t
  56. if (minutes_to_go < 20 and minutes_to_go % 5 == 0 and minutes_to_go != 0) or minutes_to_go == 1:
  57. temp_status_json, _ = model.get_json_file(player_id)
  58. print(f'{minutes_to_go}: temp_status_json: ')
  59. pprint(temp_status_json)
  60. if 'alert' not in temp_status_json:
  61. temp_status_json['alert'] = {'15': False, '10': False, '5': False, '1': False}
  62. alerts = temp_status_json['alert']
  63. if not alerts[str(minutes_to_go)]:
  64. # We have not sent this alert before
  65. name_alert = status['name']
  66. id_alert = status['id']
  67. url_alert = f'https://www.torn.com/profiles.php?XID={id_alert}'
  68. add_here = ''
  69. """
  70. if minutes_to_go == 1:
  71. add_here = '@here '
  72. if 8 < minutes_to_go < 12:
  73. add_here = '@here '
  74. """
  75. alert = add_here + \
  76. f'Alert: {minutes_to_go} mins to attack [{name_alert} [{id_alert}]]({url_alert}). ' + \
  77. f'Be there at {attack_time_time}!'
  78. print('Sending alert')
  79. view.send_alert(alert)
  80. # We just sent an alert
  81. temp_status_json['alert'][str(minutes_to_go)] = True
  82. temp_status_json_to_write = json.dumps(temp_status_json, indent=2)
  83. pprint(temp_status_json_to_write)
  84. write_file = open(f'status-{player_id}.json', 'w')
  85. write_file.write(temp_status_json_to_write)
  86. write_file.close()
  87. write_file.close()
  88. if send_message:
  89. view.send_message(status)