main.py 3.8 KB

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