def solve(self, image, offset, sides_info, screenshot_helper, current_module_position): first_time = True for _ in range(NUM_TIMES_TO_SOLVE): if not first_time: # Wait for the screen to redraw. Takes surprisingly long time.sleep(4) first_time = False image, offset = screenshot_helper.get_current_module_screenshot_and_position() print "\n----- In game try %s -----" % self._debug_image cv2.imwrite(os.path.join(MODULE_SPECIFIC_DIR, "whos_on_first", "in_game_%i.png" % self._debug_image), image) screen_text = get_screen_content(image, self._tesseract, self._debug_image) buttons, positions = get_buttons_and_positions( image, self._button_classifier, self._debug_image) print screen_text print buttons print positions to_press = button_to_press(screen_text, buttons) print "Pressing", to_press x, y = apply_offset_to_locations(positions, offset)[to_press.value] click_pixels(MouseButton.left, x, y) post_click_delay() self._debug_image += 1
def solve(self, image, offset, sides_info, screenshot_helper, current_module_position): symbols, positions = get_symbols_and_positions(image, self._symbol_classifier) positions = apply_offset_to_locations(positions, offset) order = get_symbol_order(symbols) for idx in order: x, y = positions[idx] click_pixels(MouseButton.left, x, y) post_click_delay()
def solve(self, image, offset, sides_info, screenshot_helper, current_module_position): positions, colors = get_wire_positions_and_colors(image) index_to_cut = get_wire_index_to_cut(colors, sides_info) x, y = apply_offset_to_single_location(positions[index_to_cut], offset) click_pixels(MouseButton.left, x, y) post_click_delay()
def solve_modules_on_this_side(module_solvers, sides_info, screenshot_helper): for module_type, (x, y), position in screenshot_helper.determine_visible_modules(): print "module type", module_type, "(x, y)=", x, y if not is_solvable(module_type, module_solvers): continue click_pixels(MouseButton.left, x, y) open_close_delay() screenshot, top_left = screenshot_helper.get_current_module_screenshot_and_position() solve_module(module_solvers, module_type, screenshot, top_left, sides_info, screenshot_helper, position) close_once()
def solve_modules_on_this_side(module_solvers, sides_info, screenshot_helper): for module_type, ( x, y), position in screenshot_helper.determine_visible_modules(): print "module type", module_type, "(x, y)=", x, y if not is_solvable(module_type, module_solvers): continue click_pixels(MouseButton.left, x, y) open_close_delay() screenshot, top_left = screenshot_helper.get_current_module_screenshot_and_position( ) solve_module(module_solvers, module_type, screenshot, top_left, sides_info, screenshot_helper, position) close_once()
def solve(self, image, offset, sides_info, screenshot_helper, current_module_position): for led, wire, star in get_complicated_wire_info_for_module(image): if wire is None: continue colors, position = wire if not should_cut_wire(led, colors, star, sides_info): continue position = apply_offset_to_single_location(position, offset) click_pixels(MouseButton.left, position[0], position[1]) post_click_delay()
def solve(self, image, offset, sides_info, screenshot_helper, current_module_position): state = WireSequenceState() down_button_x, down_button_y = apply_offset_to_single_location(get_down_button(image), offset) for i in xrange(NUM_PANELS): if i != 0: # Wait for next panel to show up time.sleep(1.5) im, offset = screenshot_helper.get_current_module_screenshot_and_position() connections = get_connections(im) for color, destination, to_click in connections: if state.should_cut_next_wire(color, destination): x, y = apply_offset_to_single_location(to_click, offset) click_pixels(MouseButton.left, x, y) post_click_delay() click_pixels(MouseButton.left, down_button_x, down_button_y) post_click_delay()
def solve(self, image, offset, sides_info, screenshot_helper, current_module_position): lookup_key, start_coordinates, end_coordinates = get_maze_params(image) top, right, bottom, left = apply_offset_to_locations( get_button_locations(image), offset) moves = find_path_through_maze(lookup_key, start_coordinates, end_coordinates) move_to_button = { UP: top, RIGHT: right, DOWN: bottom, LEFT: left, } for move in moves: x_raw, y_raw = move_to_button[move] click_pixels(MouseButton.left, x_raw, y_raw) post_click_delay()
def solve(self, image, offset, sides_info, screenshot_helper, current_module_position): state = MemoryState() first_time = True while not state.is_done(): if not first_time: time.sleep(4) first_time = False image, offset = screenshot_helper.get_current_module_screenshot_and_position() screen = get_screen(image, self.screen_classifier) buttons, button_locations = get_buttons_and_locations(image, self.button_classifier) idx_to_click = state.get_button_idx_to_click(screen, buttons) button_locations = apply_offset_to_locations(button_locations, offset) x, y = button_locations[idx_to_click] click_pixels(MouseButton.left, x, y) post_click_delay()
def solve(self, image, offset, sides_info, screenshot_helper, current_module_position): color, label, position = get_button_color_label_and_location(image, self._tesseract) x, y = apply_offset_to_single_location(position, offset) if should_release_immediately(color, label, sides_info): click_pixels(MouseButton.left, x, y) post_click_delay() return # Otherwise need to hold and release button. mouse_pixels(MouseEvent.left_mouse_down, x, y) # Wait for the strip to light up. time.sleep(.7) # Get a screenshot. We don't want to move the mouse because we're clicking on the button. # We also don't want to wait for good lighting because the strip's light shouldn't # matter and it's timing sensitive. strip_screenshot = screenshot_helper.get_current_module_screenshot_and_position( allow_bad_lighting=True, suppress_mouse_movement=True )[0] strip_color = get_strip_color(strip_screenshot) full_screenshot, (before_time, after_time) = \ screenshot_helper.get_full_screenshot_with_time_bound(suppress_mouse_movement=True) # print "Screenshot start, end, current =", before_time, after_time, time.time() clock_minutes, clock_seconds = get_clock_time_from_full_screenshot( full_screenshot, current_module_position, screenshot_helper) # print "Parsed at", time.time() average_time_at_screenshot = (before_time + after_time) / 2 delay_from_clock_time_seconds = get_release_delay_from_clock_time_for_color( clock_minutes, clock_seconds, strip_color) target_time = average_time_at_screenshot + delay_from_clock_time_seconds # print "Delay =", delay_from_clock_time_seconds, "target time =", target_time # Sleeps are likely inaccurate, but whatever. We can change this (eg to busy wait) to be # more precise if we find it's needed. sleep_time = target_time - time.time() # print "Sleeping for", sleep_time time.sleep(sleep_time) # We should be at our target time, we can go ahead and release the button. mouse_pixels(MouseEvent.left_mouse_up, x, y)
def solve(self, image, offset, sides_info, screenshot_helper, current_module_position): color, label, position = get_button_color_label_and_location( image, self._tesseract) x, y = apply_offset_to_single_location(position, offset) if should_release_immediately(color, label, sides_info): click_pixels(MouseButton.left, x, y) post_click_delay() return # Otherwise need to hold and release button. mouse_pixels(MouseEvent.left_mouse_down, x, y) # Wait for the strip to light up. time.sleep(.7) # Get a screenshot. We don't want to move the mouse because we're clicking on the button. # We also don't want to wait for good lighting because the strip's light shouldn't # matter and it's timing sensitive. strip_screenshot = screenshot_helper.get_current_module_screenshot_and_position( allow_bad_lighting=True, suppress_mouse_movement=True)[0] strip_color = get_strip_color(strip_screenshot) full_screenshot, (before_time, after_time) = \ screenshot_helper.get_full_screenshot_with_time_bound(suppress_mouse_movement=True) # print "Screenshot start, end, current =", before_time, after_time, time.time() clock_minutes, clock_seconds = get_clock_time_from_full_screenshot( full_screenshot, current_module_position, screenshot_helper) # print "Parsed at", time.time() average_time_at_screenshot = (before_time + after_time) / 2 delay_from_clock_time_seconds = get_release_delay_from_clock_time_for_color( clock_minutes, clock_seconds, strip_color) target_time = average_time_at_screenshot + delay_from_clock_time_seconds # print "Delay =", delay_from_clock_time_seconds, "target time =", target_time # Sleeps are likely inaccurate, but whatever. We can change this (eg to busy wait) to be # more precise if we find it's needed. sleep_time = target_time - time.time() # print "Sleeping for", sleep_time time.sleep(sleep_time) # We should be at our target time, we can go ahead and release the button. mouse_pixels(MouseEvent.left_mouse_up, x, y)
def solve(self, image, offset, sides_info, screenshot_helper, current_module_position): state = WireSequenceState() down_button_x, down_button_y = apply_offset_to_single_location( get_down_button(image), offset) for i in xrange(NUM_PANELS): if i != 0: # Wait for next panel to show up time.sleep(1.5) im, offset = screenshot_helper.get_current_module_screenshot_and_position( ) connections = get_connections(im) for color, destination, to_click in connections: if state.should_cut_next_wire(color, destination): x, y = apply_offset_to_single_location(to_click, offset) click_pixels(MouseButton.left, x, y) post_click_delay() click_pixels(MouseButton.left, down_button_x, down_button_y) post_click_delay()
def solve(self, image, offset, sides_info, screenshot_helper, current_module_position): def screenshot(): return screenshot_helper.get_current_module_screenshot_and_position( allow_bad_lighting=True, suppress_debug_copy=True)[0] state = SimonSaysState(sides_info) color_to_position = get_square_positions(image, offset) last_seen_light_state = get_lit_square(screenshot()) last_seen_start_time = None while True: # Not doing this in the while condition so we only have to take one screenshot per loop. im = screenshot() if get_is_done(im): break move_sequence = state.get_move_sequence() if move_sequence is not None: # Enter the moves for color in move_sequence: x, y = color_to_position[color] click_pixels(MouseButton.left, x, y) post_click_delay() # Wait for what was clicked to unhighlight time.sleep(1) # Reset light state last_seen_light_state = LitSquare.NONE last_seen_start_time = None continue current_light_state = get_lit_square(im) if current_light_state == last_seen_light_state: # Not sleeping because it takes long enough to grab and process a screenshot. # Busy waiting FTW! continue current_time = time.time() if last_seen_start_time is not None: duration = current_time - last_seen_start_time state.ingest_timing(duration, last_seen_light_state) last_seen_light_state = current_light_state last_seen_start_time = current_time
def solve(self, image, offset, sides_info, screenshot_helper, current_module_position): top_buttons, bottom_buttons = find_column_buttons(image) top_buttons = apply_offset_to_locations(top_buttons, offset) bottom_buttons = apply_offset_to_locations(bottom_buttons, offset) submit_button = apply_offset_to_single_location( find_submit_button(image), offset) letter_rows = [get_letters(image, self._letter_classifier)] for i in range(NUM_LETTERS_PER_COLUMN - 1): for button_x, button_y in bottom_buttons: click_pixels(MouseButton.left, button_x, button_y) post_click_delay() image, offset = screenshot_helper.get_current_module_screenshot_and_position( ) letter_rows.append(get_letters(image, self._letter_classifier)) button_rows = { UP: top_buttons, DOWN: bottom_buttons, } buttons_to_click = get_buttons_to_click_to_solve(letter_rows) for i, (direction, amount) in enumerate(buttons_to_click): button_x, button_y = button_rows[direction][i] for _ in range(amount): click_pixels(MouseButton.left, button_x, button_y) post_click_delay() click_pixels(MouseButton.left, submit_button[0], submit_button[1]) post_click_delay()
def solve(self, image, offset, sides_info, screenshot_helper, current_module_position): top_buttons, bottom_buttons = find_column_buttons(image) top_buttons = apply_offset_to_locations(top_buttons, offset) bottom_buttons = apply_offset_to_locations(bottom_buttons, offset) submit_button = apply_offset_to_single_location(find_submit_button(image), offset) letter_rows = [get_letters(image, self._letter_classifier)] for i in range(NUM_LETTERS_PER_COLUMN - 1): for button_x, button_y in bottom_buttons: click_pixels(MouseButton.left, button_x, button_y) post_click_delay() image, offset = screenshot_helper.get_current_module_screenshot_and_position() letter_rows.append(get_letters(image, self._letter_classifier)) button_rows = { UP: top_buttons, DOWN: bottom_buttons, } buttons_to_click = get_buttons_to_click_to_solve(letter_rows) for i, (direction, amount) in enumerate(buttons_to_click): button_x, button_y = button_rows[direction][i] for _ in range(amount): click_pixels(MouseButton.left, button_x, button_y) post_click_delay() click_pixels(MouseButton.left, submit_button[0], submit_button[1]) post_click_delay()
def solve(self, image, offset, sides_info, screenshot_helper, current_module_position): state = MemoryState() first_time = True while not state.is_done(): if not first_time: time.sleep(4) first_time = False image, offset = screenshot_helper.get_current_module_screenshot_and_position( ) screen = get_screen(image, self.screen_classifier) buttons, button_locations = get_buttons_and_locations( image, self.button_classifier) idx_to_click = state.get_button_idx_to_click(screen, buttons) button_locations = apply_offset_to_locations( button_locations, offset) x, y = button_locations[idx_to_click] click_pixels(MouseButton.left, x, y) post_click_delay()
def solve(self, image, offset, sides_info, screenshot_helper, current_module_position): state = MorseCodeState() arrow_locations = apply_offset_to_locations(find_arrows(image), offset) right_arrow_location = arrow_locations[1] tx_button_location = apply_offset_to_single_location(find_tx_button(image), offset) def get_light_state(): # We're very timing dependent, so we turn off the mouse movement (which introduces a # .2 second delay for each screenshot) and the bad light detection (which would # totally screw us up and isn't relevant because the morse code light looks the same # regardless of if the lights are on). We also supress the debug copy because we take # these so frequently, there end up being way too many (and saving the copy takes a # bit of time). return is_light_on(screenshot_helper.get_current_module_screenshot_and_position( allow_bad_lighting=True, suppress_debug_copy=True, suppress_mouse_movement=True)[0]) last_seen_light_state = get_light_state() last_seen_start_time = None while not state.is_word_known(): current_light_state = get_light_state() if current_light_state == last_seen_light_state: # Not sleeping because it takes long enough to grab and process a screenshot. # Busy waiting FTW! continue current_time = time.time() if last_seen_start_time is not None: duration = current_time - last_seen_start_time state.ingest_timing(duration, last_seen_light_state) last_seen_light_state = current_light_state last_seen_start_time = current_time num_time_to_press_right = state.get_num_time_to_press_right_arrow() for _ in xrange(num_time_to_press_right): click_pixels(MouseButton.left, right_arrow_location[0], right_arrow_location[1]) post_click_delay() click_pixels(MouseButton.left, tx_button_location[0], tx_button_location[1]) post_click_delay()