diff --git a/config.json b/config.json index d0cf597..83bc9c4 100644 --- a/config.json +++ b/config.json @@ -15,7 +15,8 @@ "kills_open": 1, "info_open": 1, "info_close": 0.5, - "gov_close": 1 + "gov_close": 1, + "max_random": 0.5 }, "formats": { "xlsx": true, diff --git a/roktracker/alliance/scanner.py b/roktracker/alliance/scanner.py index ddacdae..6018ea7 100644 --- a/roktracker/alliance/scanner.py +++ b/roktracker/alliance/scanner.py @@ -40,6 +40,8 @@ def __init__(self, port, config): self.govs_per_screen = 6 self.screens_needed = 0 + self.max_random_delay = config["scan"]["timings"]["max_random"] + # TODO: Load paths from config self.root_dir = get_app_root() self.tesseract_path = Path(self.root_dir / "deps" / "tessdata") @@ -204,7 +206,7 @@ def start_scan(self, kingdom: str, amount: int, formats: OutputFormats): break else: self.adb_client.adb_send_events("Touch", AllianceUI.misc.script) - time.sleep(1 + random.random()) + wait_random_range(1, self.max_random_delay) data_handler.save() self.adb_client.kill_adb() # make sure to clean up adb server diff --git a/roktracker/honor/scanner.py b/roktracker/honor/scanner.py index e7aa2b5..8259af5 100644 --- a/roktracker/honor/scanner.py +++ b/roktracker/honor/scanner.py @@ -41,6 +41,8 @@ def __init__(self, port, config): self.govs_per_screen = 5 self.screens_needed = 0 + self.max_random_delay = config["scan"]["timings"]["max_random"] + # TODO: Load paths from config self.root_dir = get_app_root() self.tesseract_path = Path(self.root_dir / "deps" / "tessdata") @@ -166,7 +168,7 @@ def start_scan(self, kingdom: str, amount: int, formats: OutputFormats): break else: self.adb_client.adb_send_events("Touch", HonorUI.misc.script) - time.sleep(1 + random.random()) + wait_random_range(1, self.max_random_delay) data_handler.save() self.adb_client.kill_adb() # make sure to clean up adb server diff --git a/roktracker/kingdom/scanner.py b/roktracker/kingdom/scanner.py index 2da58c3..a07e9a4 100644 --- a/roktracker/kingdom/scanner.py +++ b/roktracker/kingdom/scanner.py @@ -48,6 +48,8 @@ def __init__(self, config, scan_options, port): self.config = config self.timings = config["scan"]["timings"] + self.max_random_delay = config["scan"]["timings"]["max_random"] + self.advanced_scroll = config["scan"]["advanced_scroll"] self.scan_options = scan_options self.abort = False @@ -199,7 +201,8 @@ def scan_governor( f"input tap 690 " + str(self.get_gov_position(current_player, self.inactive_players)) ) - time.sleep(self.timings["gov_open"] + random_delay()) + + wait_random_range(self.timings["gov_open"], self.max_random_delay) gov_info = False count = 0 @@ -258,7 +261,7 @@ def scan_governor( + str(self.get_gov_position(current_player, self.inactive_players)), ) count += 1 - time.sleep(self.timings["gov_open"] + random_delay()) + wait_random_range(self.timings["gov_open"], self.max_random_delay) if count == 10: cont = self.ask_continue("Could not find user, retry?") if cont: @@ -284,7 +287,9 @@ def scan_governor( self.adb_client.secure_adb_tap( rok_ui.tap_positions["name_copy"] ) - time.sleep(self.timings["copy_wait"]) + wait_random_range( + self.timings["copy_wait"], self.max_random_delay + ) tk_clipboard = tkinter.Tk() governor_data.name = tk_clipboard.clipboard_get() tk_clipboard.destroy() @@ -294,8 +299,6 @@ def scan_governor( logging.log(logging.INFO, "Name copy failed, retying") copy_try = copy_try + 1 - # time.sleep(1.5 + random_delay()) - # 1st image data (ID, Power, Killpoints, Alliance) with PyTessBaseAPI( path=str(self.tesseract_path), psm=PSM.SINGLE_WORD, oem=OEM.LSTM_ONLY @@ -339,7 +342,7 @@ def scan_governor( # kills tier self.adb_client.secure_adb_tap(rok_ui.tap_positions["open_kills"]) self.state_callback("Scanning kills page") - time.sleep(self.timings["kills_open"] + random_delay()) + wait_random_range(self.timings["kills_open"], self.max_random_delay) self.adb_client.secure_adb_screencap().save( self.img_path / "kills_tier.png" @@ -417,7 +420,7 @@ def scan_governor( # More info tab self.adb_client.secure_adb_tap(rok_ui.tap_positions["more_info"]) self.state_callback("Scanning more info page") - time.sleep(self.timings["info_open"] + random_delay()) + wait_random_range(self.timings["info_open"], self.max_random_delay) self.adb_client.secure_adb_screencap().save(self.img_path / "more_info.png") image3 = load_cv2_img(self.img_path / "more_info.png", cv2.IMREAD_UNCHANGED) @@ -452,11 +455,11 @@ def scan_governor( self.adb_client.secure_adb_tap( rok_ui.tap_positions["close_info"] ) # close more info - time.sleep(self.timings["info_close"] + random_delay()) + wait_random_range(self.timings["info_close"], self.max_random_delay) self.adb_client.secure_adb_tap( rok_ui.tap_positions["close_gov"] ) # close governor info - time.sleep(self.timings["gov_close"] + random_delay()) + wait_random_range(self.timings["gov_close"], self.max_random_delay) end_time = time.time() diff --git a/roktracker/seed/scanner.py b/roktracker/seed/scanner.py index d557493..0e751ad 100644 --- a/roktracker/seed/scanner.py +++ b/roktracker/seed/scanner.py @@ -40,6 +40,8 @@ def __init__(self, port, config): self.govs_per_screen = 6 self.screens_needed = 0 + self.max_random_delay = config["scan"]["timings"]["max_random"] + # TODO: Load paths from config self.root_dir = get_app_root() self.tesseract_path = Path(self.root_dir / "deps" / "tessdata") @@ -204,7 +206,7 @@ def start_scan(self, kingdom: str, amount: int, formats: OutputFormats): break else: self.adb_client.adb_send_events("Touch", KingdomUI.misc.script) - time.sleep(1 + random.random()) + wait_random_range(1, self.max_random_delay) data_handler.save(amount, True) self.adb_client.kill_adb() # make sure to clean up adb server diff --git a/roktracker/utils/general.py b/roktracker/utils/general.py index a9b1773..8a94cf4 100644 --- a/roktracker/utils/general.py +++ b/roktracker/utils/general.py @@ -3,6 +3,7 @@ from os import PathLike import random import string +import time import cv2 import numpy as np @@ -84,6 +85,10 @@ def random_delay() -> float: return random.random() * 0.1 +def wait_random_range(min_time: float, max_offset: float) -> None: + time.sleep(random.uniform(min_time, min_time + max_offset)) + + def format_timedelta_to_HHMMSS(td: datetime.timedelta) -> str: td_in_seconds = td.total_seconds() hours, remainder = divmod(td_in_seconds, 3600)