#! venv/bin/python import json # import sys import math # from pprint import pprint import arrow import model import view if __name__ == '__main__': for player_id in [4, 15]: status = model.get_loot_level(player_id) # print(status) # seconds = extract_hospital_time(status['text'], status['integer']) write_file_next = False # Get data from stored json file old_status_json, old_status = model.get_json_file(player_id) if old_status_json == -1 or old_status == -1: write_file_next = True calculate_next_opportunity = False send_message = False # print('if old_status {} != status[\'integer\'] {}'.format(old_status, status['integer'])) assert type(old_status) is int, f"old_status {old_status} is not an integer: {type(old_status)}" assert type(status['integer']) is int, \ f"status['integer'] {status['integer']} is not an integer: {type(status['integer'])}" timestamp_when_okay = int(status['timestamp']) if status['integer'] == 0: seconds_in_hosp = model.extract_hospital_time(status['text'], _debug=False) timestamp_when_okay += seconds_in_hosp if old_status != status['integer']: calculate_next_opportunity = True write_file_next = True send_message = True # print('write_file_next: {}'.format(write_file_next)) if calculate_next_opportunity: message_text = status['text'] message_integer = status['integer'] if write_file_next: # summary = {'text': status["status"], 'roman': status["roman"], 'integer': status["integer"]} if status['integer'] <= 2: status['alert'] = {'15': False, '10': False, '5': False, '0': False} summary_json = json.dumps(status, indent=2) print(summary_json) write_file = open(f'status-{player_id}.json', 'w') write_file.write(summary_json) write_file.close() write_file.close() seconds_to_go, attack_time = model.calculate_time_to_next_attack(player_id) player_name = status['name'] attack_time_object = arrow.get(attack_time) attack_time_time = attack_time_object.format('hh:mm A') minutes_to_go = math.floor(seconds_to_go / 60) t = f'In < {minutes_to_go} mins ({attack_time_time}) {player_name} [{player_id}] reaches level IV' # print(t) status['time_string'] = t if minutes_to_go < 20 and minutes_to_go % 5 == 0 and minutes_to_go != 0: temp_status_json, _ = model.get_json_file(player_id) if 'alert' in temp_status_json: alerts = old_status['alert'] else: alerts = {'15': False, '10': False, '5': False, '0': False} if not alerts[str(minutes_to_go)]: # We have not sent this alert before name_alert = status['name'] id_alert = status['id'] url_alert = f'https://www.torn.com/profiles.php?XID={id_alert}' add_here = '' if 8 < minutes_to_go < 12: add_here = '@here ' alert = add_here + \ f'Alert: {minutes_to_go} mins to attack [{name_alert} [{id_alert}]]({url_alert}). ' + \ f'Be there at {attack_time_time}!' print('Sending alert') view.send_alert(alert) # We just sent an alert temp_status_json['alert'][str(minutes_to_go)] = True print(temp_status_json) write_file = open(f'status-{player_id}.json', 'w') write_file.write(temp_status_json) write_file.close() write_file.close() if send_message: view.send_message(status)