main.py 4.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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' in temp_status_json:
  61. alerts = temp_status_json['alert']
  62. else:
  63. alerts = {'15': False, '10': False, '5': False, '1': False}
  64. if not alerts[str(minutes_to_go)]:
  65. # We have not sent this alert before
  66. name_alert = status['name']
  67. id_alert = status['id']
  68. url_alert = f'https://www.torn.com/profiles.php?XID={id_alert}'
  69. add_here = ''
  70. if minutes_to_go == 1:
  71. add_here = '@here '
  72. if 8 < minutes_to_go < 12:
  73. add_here = '@here '
  74. alert = add_here + \
  75. f'Alert: {minutes_to_go} mins to attack [{name_alert} [{id_alert}]]({url_alert}). ' + \
  76. f'Be there at {attack_time_time}!'
  77. print('Sending alert')
  78. view.send_alert(alert)
  79. # We just sent an alert
  80. temp_status_json['alert'][str(minutes_to_go)] = True
  81. print(temp_status_json)
  82. write_file = open(f'status-{player_id}.json', 'w')
  83. write_file.write(temp_status_json)
  84. write_file.close()
  85. write_file.close()
  86. if send_message:
  87. view.send_message(status)