Skip to content

Commit

Permalink
added configurable random delay and increased default randomness to 0…
Browse files Browse the repository at this point in the history
….5 seconds
  • Loading branch information
Cyrexxis committed Jan 1, 2025
1 parent b6e50b6 commit 214a685
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 13 deletions.
3 changes: 2 additions & 1 deletion config.json
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 3 additions & 1 deletion roktracker/alliance/scanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion roktracker/honor/scanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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
Expand Down
21 changes: 12 additions & 9 deletions roktracker/kingdom/scanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand All @@ -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()
Expand All @@ -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
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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()

Expand Down
4 changes: 3 additions & 1 deletion roktracker/seed/scanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions roktracker/utils/general.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from os import PathLike
import random
import string
import time
import cv2
import numpy as np

Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 214a685

Please sign in to comment.