def interpret(self, instructions, ignore_start_from_step=False): # split instructions if not isinstance(instructions, list): lines = instructions.splitlines() else: lines = instructions # ignore instructions before start step if not ignore_start_from_step and self.start_from_step > 1 and self.start_from_step <= len( lines): self.debug('Start from step: %d' % self.start_from_step) step = self.start_from_step - 1 lines = lines[step:] game_screen = None for i, line in enumerate(lines, start=1): # check for pause or suspend self.pause_event.wait() if self.suspend: break # parse instruction self.debug('Instruction (%d): %s' % (i, line), DebugLevel.Low) instruction = parser.parse_instruction(line) self.debug('Parse result: ' + str(instruction), DebugLevel.High) # save game screen before those instructions if instruction['name'] in ['Click', 'PressKey']: game_screen = tools.screen_game(self.game_location) elif instruction['name'] != 'MonitorGameScreen': game_screen = None # begin interpretation if instruction['name'] == 'Move': self.move(instruction['value']) elif instruction['name'] == 'Enclos': self.check_enclos(instruction['location'], instruction['type']) elif instruction['name'] == 'Zaap': self.use_zaap(instruction['from'], instruction['to']) elif instruction['name'] == 'Zaapi': self.use_zaapi(instruction['from'], instruction['to']) elif instruction['name'] == 'Collect': self.collect(instruction['map'], instruction['store_path']) elif instruction['name'] == 'Click': coordinates = { 'x': int(instruction['x']), 'y': int(instruction['y']), 'width': int(instruction['width']), 'height': int(instruction['height']) } if instruction['twice'] == 'True': self.double_click(coordinates) else: self.click(coordinates) elif instruction['name'] == 'Scroll': times = int(instruction['times']) value = times if instruction['direction'] == 'up' else -times self.scroll(value) elif instruction['name'] == 'Pause': self.pause() elif instruction['name'] == 'MonitorGameScreen': self.monitor_game_screen(tolerance=2.5, screen=game_screen) elif instruction['name'] == 'Wait': duration = int(instruction['value']) self.sleep(duration) elif instruction['name'] == 'PressKey': self.press_key(instruction['value']) elif instruction['name'] == 'TypeText': self.type_text(instruction['value']) elif instruction['name'] == 'Connect': if instruction['account_id'].isdigit(): account_id = int(instruction['account_id']) else: account_id = instruction['account_id'] self.connect(account_id) elif instruction['name'] == 'Disconnect': self.disconnect(instruction['value']) else: self.debug('Unknown instruction', DebugLevel.Low)
def interpret(self, instructions): # split instructions lines = instructions.splitlines() # ignore instructions before start step if self.start_from_step > 1 and self.start_from_step <= len(lines): self.debug('Start from step: %d' % self.start_from_step) step = self.start_from_step - 1 lines = lines[step:] for i, line in enumerate(lines, start=1): # check for pause or suspend self.pause_event.wait() if self.suspend: break # parse instruction self.debug('Instruction (%d): %s' % (i, line), DebugLevel.Low) instruction = parser.parse_instruction(line) self.debug('Parse result: ' + str(instruction), DebugLevel.High) # begin interpretation if instruction['name'] == 'Move': self.move(instruction['value']) elif instruction['name'] == 'Enclos': self.check_enclos(instruction['location'], instruction['type']) elif instruction['name'] == 'Zaap': self.use_zaap(instruction['from'], instruction['to']) elif instruction['name'] == 'Zaapi': self.use_zaapi(instruction['from'], instruction['to']) elif instruction['name'] == 'Collect': self.collect(instruction['map'], instruction['store_path']) elif instruction['name'] == 'Click': coordinates = (int(instruction['x']), int(instruction['y']), int(instruction['width']), int(instruction['height'])) if instruction['twice'] == 'True': self.double_click(coordinates) else: self.click(coordinates) elif instruction['name'] == 'Wait': if instruction['pause'] == 'True': self. await () elif instruction['duration'].isdigit(): self.sleep(int(instruction['duration'])) elif instruction['name'] == 'PressKey': self.press_key(instruction['value']) elif instruction['name'] == 'TypeText': self.type_text(instruction['value']) elif instruction['name'] == 'Connect': if instruction['account_id'].isdigit(): account_id = int(instruction['account_id']) else: account_id = instruction['account_id'] self.connect(account_id) elif instruction['name'] == 'Disconnect': self.disconnect(instruction['value']) else: self.debug('Unknown instruction', DebugLevel.Low)
def interpret(self, instructions, ignore_start_from_step=False): # split instructions if not isinstance(instructions, list): lines = instructions.splitlines() else: lines = instructions # ignore instructions before start step if not ignore_start_from_step and self.start_from_step > 1 and self.start_from_step <= len(lines): self.debug('Start from step: %d' % self.start_from_step) step = self.start_from_step - 1 lines = lines[step:] game_screen = None for i, line in enumerate(lines, start=1): # check for pause or suspend self.pause_event.wait() if self.suspend: break if self.wait_for_box_appear(box_name='Fight Button Light', timeout=1): self.handle_fight() # parse instruction self.debug('Instruction (%d): %s' % (i, line), DebugLevel.Low) instruction = parser.parse_instruction(line) self.debug('Parse result: ' + str(instruction), DebugLevel.High) # save game screen before those instructions if instruction['name'] in ['Click', 'PressKey']: game_screen = tools.screen_game(self.game_location) elif instruction['name'] != 'MonitorGameScreen': game_screen = None # begin interpretation if instruction['name'] == 'Move': self.move(instruction['value']) elif instruction['name'] == 'Enclos': self.check_enclos(instruction['location'], instruction['type']) elif instruction['name'] == 'Zaap': self.use_zaap(instruction['from'], instruction['to']) elif instruction['name'] == 'Zaapi': self.use_zaapi(instruction['from'], instruction['to']) elif instruction['name'] == 'Collect': if self.collect(instruction['map'], instruction['store_path']): # If collect return 1, we need to go to the bank. We forward 1 to the caller return 1 elif instruction['name'] == 'Click': coordinates = { 'x': int(instruction['x']), 'y': int(instruction['y']), 'width': int(instruction['width']), 'height': int(instruction['height']) } # Handle the case when the click location need to match the stored color if 'r' in instruction: coordinates['color'] = f"({instruction['r']}, {instruction['g']}, {instruction['b']})" while not self.check_location_color(coordinates): self.log("Click location has a different color, waiting ...") self.pause_event.wait() if self.suspend: return self.sleep(0.01) if 'hotkey' in instruction: self.hot_click(instruction['hotkey'], coordinates, instruction['twice']) elif instruction['twice'] == 'True': self.double_click(coordinates) else: self.click(coordinates) elif instruction['name'] == 'GoToBank': self.go_to_bank() # Return to skip the end of the ongoing path return elif instruction['name'] == 'Scroll': times = int(instruction['times']) value = times if instruction['direction'] == 'up' else -times self.scroll(value) elif instruction['name'] == 'Pause': self.pause() elif instruction['name'] == 'MonitorGameScreen': self.monitor_game_screen(tolerance=2.5, screen=game_screen) elif instruction['name'] == 'Wait': duration = int(instruction['value']) self.sleep(duration) elif instruction['name'] == 'PressKey': self.press_key(instruction['value']) elif instruction['name'] == 'TypeText': self.type_text(instruction['value']) elif instruction['name'] == 'Connect': if instruction['account_id'].isdigit(): account_id = int(instruction['account_id']) else: account_id = instruction['account_id'] self.connect(account_id) elif instruction['name'] == 'Disconnect': self.disconnect(instruction['value']) else: self.debug('Unknown instruction', DebugLevel.Low)