Selaa lähdekoodia

Refactoring moving code to model.py

Foppe Hemminga 6 vuotta sitten
vanhempi
commit
9fdf891178
4 muutettua tiedostoa jossa 88 lisäystä ja 55 poistoa
  1. 15 43
      main.py
  2. 59 0
      model.py
  3. 7 6
      status-15.json
  4. 7 6
      status-4.json

+ 15 - 43
main.py

@@ -1,62 +1,33 @@
 #! venv/bin/python
 import json
-import re
-import sys
+# import sys
 from pprint import pprint
 
 import model
-import view
-
-
-def extract_hospital_time(this_message_text, this_message_integer):
-    in_hospital = False
-    time_text = ''
-    hours = 0
-    minutes = 0
-    if this_message_integer == 0:
-        if 'hospital' in this_message_text[0]:
-            in_hospital = True
-            time_text = this_message_text[0][15:]
-            print(time_text)
-            m = re.match(regex, time_text)
-            if m:
-                if m.group('hours'):
-                    hours = m.group('hours')
-                if m.group('minutes'):
-                    minutes = m.group('minutes')
-        print(f'Extracted {hours} hrs and {minutes} mins')
+# import view
 
 
 if __name__ == '__main__':
-    regex = re.compile(r'( (?P<hours>\d+) hrs)? (?P<minutes>\d+) mins ')
     for player_id in [4, 15]:
         status = model.get_loot_level(player_id)
         # print(status)
-        extract_hospital_time(status['text'], status['integer'])
-        old_status = 0
+        # seconds = extract_hospital_time(status['text'], status['integer'])
         write_file_next = False
-        try:
-            old_status_file = open(f'status-{player_id}.json', 'r')
-            if old_status_file.mode == 'r':
-                old_status_raw = old_status_file.read()
-                if old_status_raw:
-                    old_status_json = json.loads(old_status_raw)
-                    old_status = int(old_status_json['integer'])
-                else:
-                    write_file_next = True
-            old_status_file.close()
-        except FileNotFoundError:
-            empty_status_file = open(f'status-{player_id}.json', 'w+')
-            print('Created new file ... Exiting')
-            empty_status_file.close()
-            sys.exit(1)
-        # print(old_status)
+        # 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[1] {}'.format(old_status, status[1]))
+        # 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'])
+            timestamp_when_okay += seconds_in_hosp
         if old_status != status['integer']:
             calculate_next_opportunity = True
             write_file_next = True
@@ -77,4 +48,5 @@ if __name__ == '__main__':
             write_file.close()
 
         if send_message:
-            view.send_message(status)
+            pass
+            # view.send_message(status)

+ 59 - 0
model.py

@@ -1,5 +1,6 @@
 import json
 # from pprint import pprint
+import re
 
 from dotenv import load_dotenv
 import os
@@ -79,3 +80,61 @@ def get_loot_level(this_id):
     this_status_dict = {"id": this_id, "name": this_status_name, "integer": this_integer, "roman": this_loot_level,
                         "text": this_status_status, "timestamp": this_status_timestamp}
     return this_status_dict
+
+
+def _read_json_file(this_player_id):
+    return_status = -1, -1
+    try:
+        this_status_file = open(f'status-{this_player_id}.json', 'r')
+        if this_status_file.mode == 'r':
+            this_status_raw = this_status_file.read()
+            if this_status_raw:
+                temp_status_json = json.loads(this_status_raw)
+                this_status = int(temp_status_json['integer'])
+                return_status = temp_status_json, this_status
+        this_status_file.close()
+    except FileNotFoundError:
+        new_status_file = open(f'status-{this_player_id}.json', 'w+')
+        print('Created new file ... Exiting')
+        new_status_file.close()
+    return return_status
+
+
+def get_json_file(this_player_id):
+    return _read_json_file(this_player_id)
+
+
+def extract_hospital_time(this_message_text):
+    regex = re.compile(r'( (?P<hours>\d+) hrs)?( (?P<minutes>\d+) mins)?( (?P<seconds>\d+) secs)? ')
+    hours = 0
+    minutes = 0
+    seconds = 0
+    hours_plus = 0
+    minutes_plus = 0
+    seconds_plus = 0
+    if 'hospital' in this_message_text[0]:
+        time_text = this_message_text[0][15:]
+        print(time_text)
+        m = re.match(regex, time_text)
+        if m:
+            if m.group('hours'):
+                hours_plus = m.group('hours')
+            if m.group('minutes'):
+                minutes_plus = m.group('minutes')
+            if m.group('seconds'):
+                seconds_plus = m.group('minutes')
+        print(f'Extracted {hours_plus} hrs and {minutes_plus} mins and {seconds_plus} secs')
+    try:
+        hours += int(hours_plus)
+        minutes += int(minutes_plus)
+        seconds += int(seconds_plus)
+    except ValueError as err:
+        print(f'Cannot cast {hours_plus} or {minutes_plus} or {seconds_plus} into int: {err}')
+    return hours * 60 * 60 + minutes * 60 + seconds
+
+
+"""
+def calculate_time_to_next_attack(this_player_id):
+    pass
+    # this_status_json, this_status = _read_json_file(this_player_id)
+"""

+ 7 - 6
status-15.json

@@ -1,10 +1,11 @@
 {
   "id": 15,
-  "name:": "Leslie",
-  "integer": 3,
-  "roman": "III",
+  "name": "Leslie",
+  "integer": 0,
+  "roman": 0,
   "text": [
-    "Okay",
-    "Loot level III"
-  ]
+    "In hospital for 1 hrs 4 mins ",
+    "Defeated by <a href=\"profiles.php?XID=681394\">WAGABUND</a>"
+  ],
+  "timestamp": 1570022087
 }

+ 7 - 6
status-4.json

@@ -1,10 +1,11 @@
 {
   "id": 4,
-  "name:": "Duke",
-  "integer": 0,
-  "roman": 0,
+  "name": "Duke",
+  "integer": 3,
+  "roman": "III",
   "text": [
-    "In hospital for 1 hrs 23 mins ",
-    "Defeated by <a href=\"profiles.php?XID=2157516\">AlexSosa</a>"
-  ]
+    "Okay",
+    "Loot level III"
+  ],
+  "timestamp": 1570019197
 }