def run_arena(max_force, type, *, loop, context): if max_force is None: max_force = config.get("arena:max-force") if type is None: type = config.get("arena:type") state = get_arena_state(timeout=2) if state == "arena/game/active": if choose_enemy_and_attack(max_force, type): context["played"] += 1 # start search and run bu elif state == "arena/game/waiting_next": logger.info("waiting for next stage", extra={"rate": 1/5}) elif state == "arena/game/waiting_finish": logger.info("waiting for arena finished", extra={"rate": 1/5}) elif state in ("arena/game/victory", "arena/game/defeat"): _stats["played"] += 1 if state == "arena/game/victory": _stats["win"] += 1 loop.click_and_check("arena/game/back", timeout=3) elif state == "arena/game/finished": time.sleep(5) if loop.click_and_check(["arena/game/close", "arena/game/close2"], timeout=3): return True else: return False loop.retry(False)
def set_run_count(self, ev): try: value = int(ev.GetString()) except (TypeError, ValueError): self.run_count_spin.SetValue(config.get("arena:run-count")) else: if value < 1 or value > 99: self.run_count_spin.SetValue(config.get("arena:run-count")) else: arena.set_arena_run_count(value)
def set_max_force(self, ev): try: value = int(ev.GetString()) except (TypeError, ValueError): self.max_force_spin.SetValue(config.get("arena:max-force")) else: if value < 0 or value > 50000000: self.max_force_spin.SetValue(config.get("arena:max-force")) else: arena.set_arena_max_force(value)
def start_stop_arena(self, ev): if self._arena_thread: self.start_arena_button.SetLabelText("Start arena") self.arena_counter.SetLabelText("") self._arena_thread.raise_exception() self._arena_thread = None else: run_count = config.get("arena:run-count") if run_count < 1: return num = 1 def run_arena(): self.start_arena_button.SetLabelText("Stop arena") self.arena_counter.SetLabelText("arena #%s" % num) self._arena_thread = self.run_in_thread(arena.start_arena_once, args=(num, ), after=restart) def restart(): nonlocal num num += 1 if self._arena_thread is None: return count = config.get("arena:run-count") if num > count: self.start_arena_button.SetLabelText("Start arena") self.arena_counter.SetLabelText("") self._arena_thread = None else: run_arena() run_arena()
def wait(targets, timeout=..., logger=None, threshold=None, can_trace=True, trace_frame=0): trace_frame += 1 if isinstance(targets, str): targets = (targets, ) if timeout is ...: timeout = config.get("utils:default-wait-timeout") target_names = ", ".join(targets) tm = time.time() while 1: sample = client.get_sample() for t in targets: match = templates[t].find(sample=sample, threshold=threshold) if match: if can_trace: trace.trace("<done>", sample, match, trace_frame=trace_frame) return match.set_logger(logger) if timeout is not None and (time.time() - tm) > timeout: match = NoMatch(targets).set_logger(logger) if can_trace: trace.trace("<timeout>", sample, match, trace_frame=trace_frame) return match if logger and tm - time.time() > 2.: logger.info("waiting [%s]", target_names, extra={"rate": 1 / 5}) client.new_sample()
def _exception_hook(exctype, value, traceback): if isinstance(value, ConsoleException): if value.logger and not config.get("traceback", False): logging.getLogger(value.logger).error(value.msg) else: logging.getLogger("console").error(value.msg) else: sys.__excepthook__(exctype, value, traceback)
def enable_tunnel(): return run_command([ "adb", "-s", config.get("adb:device"), "forward", f"tcp:{settings.SERVER_PORT}", f"localabstract:{settings.ADB_SOCKET_NAME}" ])
def push_server(): return run_command([ "adb", "-s", config.get("adb:device"), "push", settings.ADB_SERVER_FILENAME, settings.ADB_DEVICE_SERVER_PATH ])
def click(self, x, y, rand_x=None, rand_y=None): if rand_x: x += random.randint(-rand_x, rand_x) if rand_y: y += random.randint(-rand_y, rand_y) self.mouse_down(x, y) time.sleep(config.get("client:click-timeout")) self.mouse_up(x, y) return x, y
def move(self, x1, y1, x2, y2): c = 8 dx = (x2 - x1) / c dy = (y2 - y1) / c self.mouse_down(x1, y1) time.sleep(0.05) move_timeout = config.get("client:move-timeout") for n in range(c): self.mouse_move(int(x1 + dx * n), int(y1 + dy * n)) time.sleep(move_timeout) time.sleep(0.05) self.mouse_up(x2, y2)
def restart(): nonlocal num num += 1 if self._arena_thread is None: return count = config.get("arena:run-count") if num > count: self.start_arena_button.SetLabelText("Start arena") self.arena_counter.SetLabelText("") self._arena_thread = None else: run_arena()
def connect_to_device(): output = get_attached_devices() if output is None: logger.error("Can't get list of connected devices.") return False adb_device = config.get("adb:device") connected = adb_device in [x[0] for x in output] if not connected: success, output = run_command_ex(["adb", "connect", adb_device]) if not success or "failed" in output: logger.error("Can't connect to device (start Nox or Bluestacks).") return False return True
def goto_arena(kind=None, *, loop): if get_arena_state(timeout=0, can_trace=False): return if loop.find("arena/dialog/search"): logger.info("still searching", extra={"rate": 1/5}) loop.retry(False) if loop.find("arena/dialog/opened"): loop.click_and_check("arena/dialog/approve", timeout=3) loop.retry() loc = navigation.get_loc() if not loc: loop.retry() if loc != "arena": navigation.goto("arena") loc = navigation.get_loc() if loc != "arena": loop.retry() if get_arena_state(timeout=0, can_trace=False): return kind = kind or config.get("arena:kind") if kind is not None: mode = loop.find([ "arena/food/check", "arena/ticket/check" ]) if not mode: loop.retry() if kind == "ticket" and mode != "arena/ticket/check": loop.click("arena/food/ticket", timeout=3) loop.retry() elif kind == "food" and mode != "arena/food/check": loop.click("arena/ticket/food", timeout=3) loop.retry() loop.click_and_check("arena/start", timeout=3) loop.retry()
def execute_server(): args = [ "adb", "-s", config.get("adb:device"), "shell", f"CLASSPATH={settings.ADB_DEVICE_SERVER_PATH}", "app_process", "/", "com.genymobile.scrcpy.Server", "1.12.1", "0", # maxsize f"{settings.SERVER_BIT_RATE}", f"{settings.SERVER_MAX_FPS}", "true", # tunnel forwarding "-", # crop "true", # always send frame meta (packet boundaries + timestamp) "true" # controls ] process = execute_process(args) ProcessWatch(process=process, name="scrcpy.Server", daemon=True).start() return process
def wait_while(targets, timeout=..., logger=None, threshold=None, can_trace=True, trace_frame=0): trace_frame += 1 if isinstance(targets, str): targets = (targets, ) if timeout is ...: timeout = config.get("utils:default-wait-timeout") tm = time.time() attempt = 1 while 1: sample = client.get_sample() for t in targets: match = templates[t].find(sample=sample, threshold=threshold) if match: if logger and tm - time.time() > 2.: logger.info("still can find [%s]", match, extra={"rate": 1 / 5}) break else: if can_trace: match = NoMatch(targets) trace.trace("<done>", sample, match, trace_frame=trace_frame) return True if timeout is not None and (time.time() - tm) > timeout: if can_trace: trace.trace("<timeout>", sample, match, trace_frame=trace_frame) return False client.new_sample() attempt += 1
def init_ui(self): self.Bind(wx.EVT_CLOSE, self.on_close) panel = wx.Panel(self) font = wx.SystemSettings.GetFont(wx.SYS_DEFAULT_GUI_FONT) font.SetPointSize(12) vbox = wx.BoxSizer(wx.VERTICAL) hbox = wx.BoxSizer(wx.HORIZONTAL) vbox1 = wx.BoxSizer(wx.VERTICAL) self.reboot_button = wx.Button(panel, label="Reboot", size=(100, -1)) self.reboot_button.SetFont(font) self.reboot_button.Bind(wx.EVT_BUTTON, self.reboot) vbox1.Add(self.reboot_button) button = wx.Button(panel, label="Clear log", size=(100, -1)) button.SetFont(font) button.Bind(wx.EVT_BUTTON, lambda e: self.logger.Clear()) vbox1.Add(button, 0, wx.TOP, 5) hbox.Add(vbox1, 0, wx.EXPAND, 0) vbox1 = wx.BoxSizer(wx.VERTICAL) hbox1 = wx.BoxSizer(wx.HORIZONTAL) label = wx.StaticText(panel, label="Arena", size=(40, -1), style=wx.ALIGN_RIGHT) label.SetFont(font) hbox1.Add(label) choice = wx.Choice(panel, choices=["10", "15"], size=(100, -1)) choice.SetFont(font) choice.SetSelection(choice.FindString(str(config.get("arena:type")))) choice.Bind(wx.EVT_CHOICE, self.set_arena_type) hbox1.Add(choice, 1, wx.LEFT, 10) vbox1.Add(hbox1) hbox1 = wx.BoxSizer(wx.HORIZONTAL) label = wx.StaticText(panel, label="Kind", size=(40, -1), style=wx.ALIGN_RIGHT) label.SetFont(font) hbox1.Add(label) choice = wx.Choice(panel, choices=["food", "ticket"], size=(100, -1)) choice.SetFont(font) choice.SetSelection(choice.FindString(str(config.get("arena:kind")))) choice.Bind(wx.EVT_CHOICE, self.set_arena_kind) hbox1.Add(choice, 1, wx.LEFT, 10) vbox1.Add(hbox1, 0, wx.TOP, 7) hbox.Add(vbox1, 0, wx.LEFT, 20) vbox1 = wx.BoxSizer(wx.VERTICAL) hbox1 = wx.BoxSizer(wx.HORIZONTAL) label = wx.StaticText(panel, label="Max force", size=(80, -1), style=wx.ALIGN_RIGHT) label.SetFont(font) hbox1.Add(label) self.max_force_spin = wx.SpinCtrl(panel, size=(100, -1), min=0, max=50000000, value=str( config.get("arena:max-force"))) self.max_force_spin.SetFont(font.Bold()) self.max_force_spin.Bind(wx.EVT_TEXT, self.set_max_force) hbox1.Add(self.max_force_spin, 1, wx.LEFT, 10) vbox1.Add(hbox1) hbox1 = wx.BoxSizer(wx.HORIZONTAL) label = wx.StaticText(panel, label="Run count", size=(80, -1), style=wx.ALIGN_RIGHT) label.SetFont(font) hbox1.Add(label) self.run_count_spin = wx.SpinCtrl(panel, size=(100, -1), min=1, max=99, value=str( config.get("arena:run-count"))) self.run_count_spin.SetFont(font.Bold()) self.run_count_spin.Bind(wx.EVT_TEXT, self.set_run_count) hbox1.Add(self.run_count_spin, 1, wx.LEFT, 10) vbox1.Add(hbox1, 0, wx.TOP, 4) hbox.Add(vbox1, 0, wx.LEFT, 20) vbox1 = wx.BoxSizer(wx.VERTICAL) hbox1 = wx.BoxSizer(wx.HORIZONTAL) self.start_arena_button = wx.Button(panel, label="Start arena", size=(90, -1)) self.start_arena_button.SetFont(font) self.start_arena_button.Bind(wx.EVT_BUTTON, self.start_stop_arena) self.start_arena_button.Disable() hbox1.Add(self.start_arena_button) vbox1.Add(hbox1) hbox1 = wx.BoxSizer(wx.HORIZONTAL) self.arena_counter = wx.StaticText(panel, label="", size=(90, -1), style=wx.ALIGN_CENTER) self.arena_counter.SetFont(font) hbox1.Add(self.arena_counter, 0, wx.TOP, 7) vbox1.Add(hbox1) hbox.Add(vbox1, 0, wx.LEFT, 20) vbox1 = wx.BoxSizer(wx.VERTICAL) hbox1 = wx.BoxSizer(wx.HORIZONTAL) label = wx.StaticText(panel, label="FPS", style=wx.ALIGN_RIGHT) label.SetFont(font) hbox1.Add(label, 1, wx.EXPAND | wx.RIGHT, 10) self.fps_label = wx.StaticText(panel, label="OFFLINE") self.fps_label.SetFont(font.Bold()) hbox1.Add(self.fps_label) vbox1.Add(hbox1, 1, wx.EXPAND) hbox.Add(vbox1, 1, wx.EXPAND | wx.LEFT, 20) vbox.Add(hbox, 0, wx.EXPAND | wx.LEFT | wx.RIGHT | wx.TOP, 10) self.logger = wx.TextCtrl(panel, style=wx.TE_MULTILINE | wx.TE_READONLY) self.logger.SetFont(font) vbox.Add(self.logger, 1, wx.EXPAND | wx.ALL, 10) panel.SetSizer(vbox) pub.subscribe(self.logger_listener, "logging")