main.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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=True)
  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. name_alert = status['name']
  57. id_alert = status['id']
  58. url_alert = f'https://www.torn.com/profiles.php?XID={id_alert}'
  59. add_here = ''
  60. if 8 < minutes_to_go < 12:
  61. add_here = '@here '
  62. alert = add_here + f'Alert: {minutes_to_go} mins to attack [{name_alert} [{id_alert}]]({url_alert}). ' + \
  63. f'Be there at {attack_time_time}!'
  64. print('Sending alert')
  65. view.send_alert(alert)
  66. if send_message:
  67. view.send_message(status)