Refactor event stage row detection and scrolling logic

- Replaced fixed y-position detection of stage rows with dynamic search using color blob detection and OCR for stage numbers.
- Implemented a new `_locate_stage_rows` function to find all visible stage rows and their corresponding buttons.
- Updated `_find_stage_row` to scroll through the stage list based on the detected stage numbers, improving reliability and adaptability to different event layouts.
- Enhanced modal detection logic to avoid false positives by checking the visibility of the header bar.
- Adjusted scanning logic to handle transient misreads and ensure stable readings before concluding the presence of a target stage.
- Improved logging for better debugging and understanding of the scanning process.
This commit is contained in:
Nik Afiq 2026-10-08 08:10:45 +09:00
parent a6e124d175
commit 0bdd1919c4
5 changed files with 351 additions and 136 deletions

View File

@ -645,59 +645,79 @@ EVENT_FINISHED_TEXT_RECT = (1080, 585, 1760, 635)
# nothing guarantees that for a future event). # nothing guarantees that for a future event).
EVENT_QUEST_TAB = (1416, 165) EVENT_QUEST_TAB = (1416, 165)
# Stage list, scrolled to its bottom extreme. Confirmed live this always # Stage list: located DYNAMICALLY every scan, not from fixed row positions.
# reveals the event's LAST 5 stages at these exact row positions regardless
# of starting scroll state -- unlike story_sweep's region stage list, row
# height here stayed constant across both 1-line and 2-line wrapped titles
# during calibration, so no separate top/bottom row-position sets were
# needed for this task's stages-9-12 target range.
EVENT_STAGE_LIST_SCROLL_POINT = (1400, 700)
EVENT_STAGE_LIST_SCROLL_CLICKS = 25
# **Recalibrated 2026-09-24**: these were calibrated against the event
# running at the time ("鉄道爆走事件"). A brand-new, unrelated event
# ("It's our Engine!", 2026.09.23 11:00-2026.10.07 10:59) replaced it and
# shifted this same bottom-scrolled row layout down by a consistent ~12-13px
# -- confirmed live via pixel-column scanning of the actual digit glyphs
# (not guessed): row centers measured at 379/550/722/893 (previously
# 366/538/710/883), a steady +12 to +13px offset across all four rows. The
# 5th slot (previously 1055) no longer lands on a stage row at all for this
# event -- only 4 stages (9-12) fit in the bottom-scrolled view now, with
# the event-points progress bar's own "4799/15000" digits sitting in
# roughly that vertical area instead; keeping a 5th slot there risked
# misreading those digits as a stage number, so it's dropped rather than
# guessed at. `_scan_stage_rows_once` loops over however many entries this
# tuple has, so 4 instead of 5 needs no other code change. Root-caused with
# `detector.read_int_bordered` itself returning None/garbage at the OLD
# positions on the live page (matching the user's own reported log
# verbatim) before re-measuring -- not assumed from the symptom alone. If a
# future event shifts this again, re-run the same pixel-column-scan
# approach (scratchpad probe, sample the actual glyph pixel extents) rather
# than guessing a new offset.
EVENT_STAGE_ROW_Y = (379, 550, 722, 893)
EVENT_STAGE_ENTER_X = 1698
# Stage-number OCR crop (e.g. "09", "12"), offset from a row's known center
# y -- rect = (x1, row_y - top_pad, x2, row_y + bottom_pad). Pixel-scanned
# live across all bottom-extreme row positions; tight enough to exclude the
# 3-star rating row rendered just below each number.
# #
# **Widened from (30, 5) to (20, 20) alongside the 2026-09-24 row-position # History: this used to be a fixed tuple of row-center y's for the list's
# recalibration above**: the old asymmetric pad assumed the digit glyph sat # bottom-scrolled extreme (EVENT_STAGE_ROW_Y), plus a fixed number-OCR rect
# mostly ABOVE row_y (true for the old event, digit center measured ~8px # offset from each (EVENT_STAGE_NUMBER_OCR_X/_Y_PAD) and a fixed click x
# above row_y); the new event's digit center sits ~16px BELOW row_y instead # (EVENT_STAGE_ENTER_X). Every new event shifted the bottom-scrolled rows by
# (measured band e.g. 363-395 around center 379, i.e. +-16px), so a # a different amount (the list's total content height differs per event):
# symmetric pad was chosen instead of just re-flipping the asymmetry -- # calibrated 366/538/710/883/1055 for "鉄道爆走事件" (2026-07), recalibrated
# comfortably covers the ~32px-tall digit glyph with a few px of margin on # to 379/550/722/893 for "It's our Engine!" (2026-09-24) -- and the very next
# both sides without reaching into the star-rating row (~13px further below # event, a rerun of an older one ("我らオカルト研究会!", 2026-10-07), went
# the digit's own bottom edge in every measured row). # right back to the ~2026-07 positions (digits measured at y 344-376 /
EVENT_STAGE_NUMBER_OCR_X = (1055, 1145) # 515-548 / 686-719 / 857-890 / 1029-1061), so every fixed crop missed its
EVENT_STAGE_NUMBER_OCR_Y_PAD = (20, 20) # digits and all 6 outer attempts of the 2026-10-08 01:00 q4h run gave up
# with "wrong_page" while sitting on the correct, fully-rendered list
# (scratchpad/event_sweep_no_rows_debug.png). Recalibrating the tuple a
# third time would only fix the current event.
#
# Now a port of the reference's own approach (core/image.py
# swipe_search_target_str, called from activity_utils.to_mission_task_info):
# find every visible 入場 (enter) button, OCR the stage number on that
# button's own row, and scroll toward the target by comparing it against the
# smallest/largest stage number currently visible. Row positions, row count
# and scroll extent can all differ per event without any recalibration.
#
# Search area for the 入場 buttons: the whole right-hand list panel below the
# Story/Quest/Challenge tab bar (bottom edge y~208). Deliberately much wider
# than the buttons' measured x-span (1605-1780) so a horizontally-shifted
# future layout still lands inside it.
EVENT_STAGE_LIST_REGION = (1000, 212, 1910, 1195)
# 入場 button fill, sampled live from the 2026-10-07 event's list:
# (141, 228, 255) near the top edge, (122, 219, 253) mid-button.
EVENT_STAGE_ENTER_BUTTON_RGB = ((90, 185, 225), (185, 245, 255))
# Every fully-visible button measured 175x117 (all 5 bottom-scrolled rows);
# the row half-hidden under the tab bar measured 165x45. Requiring >= 140x90
# keeps only fully-visible rows, whose stage number is also fully visible.
EVENT_STAGE_ENTER_BUTTON_MIN_SIZE = (140, 90)
# Horizontal band, relative to a button's LEFT edge, searched for that row's
# stage-number digits (vertically: the button's own top-to-bottom span,
# which covers the number but not the cards above/below). Measured digits
# at x 1075-1120 with the button's left edge at 1605 (i.e. -530..-485);
# widened to -570..-440 so the digits don't need to sit at one exact offset.
# The number crop itself is then fitted to the dark digit glyphs found in
# that band (detector.find_glyph_bbox) -- the yellow star-rating row below
# the number never reaches the glyph darkness threshold (measured: no dark
# components other than the digits inside any button's span), so it's
# excluded without a hand-tuned pad.
EVENT_STAGE_NUMBER_BAND_DX = (-570, -440)
# 任務情報 (stage info) modal. Corner probe is pixel-identical to # Scroll gesture for stepping through the list toward the target stage.
# story_sweep's STAGE_MODAL_PROBE (same dark-backdrop dialog component) but # Measured live (2026-10-08): one wheel click moves this list by exactly one
# kept as its own constant to avoid coupling the two tasks' config together. # row (~171px), with 5 full rows visible at once. The step must move the
EVENT_STAGE_MODAL_PROBE = (1870, 600) # list by LESS than the visible rows so no row can be skipped between two
EVENT_STAGE_MODAL_DIM_MAX_CHANNEL = 150 # scans -- 4 keeps a one-row overlap.
EVENT_STAGE_LIST_SCROLL_POINT = (1400, 700)
EVENT_STAGE_LIST_SCROLL_STEP_CLICKS = 4
# 任務情報 (stage info) modal -- no constant here any more. It used to be
# detected by EVENT_STAGE_MODAL_PROBE = (1870, 600) reading dark (<150 every
# channel) under the modal's dim backdrop, but that point is the EVENT'S OWN
# background art, which changes with every event: the 2026-10-07 rerun
# ("我らオカルト研究会!") has a dark night-castle background that reads
# (71, 65, 84) there with NO modal open (scratchpad/
# event_sweep_no_rows_debug.png). The 2026-10-08 02:09 run's
# _close_stage_modal therefore believed the modal was still open on the
# plain Quest list after a successful sweep and pressed Escape/the X
# position three times, knocking the game back toward home (recovered by
# return_to_home; bounty.py hit the same art-dependence on its own screen,
# see BOUNTY_STAGE_MODAL_PROBE). event_sweep._is_stage_modal_open now reads
# the white top header bar instead -- fixed UI chrome, identical on every
# event: ~(248, 250, 252) on the plain list, dimmed to ~(80, 81, 82) by the
# modal's backdrop (measured on a real stuck event stage modal, see
# navigation._not_home) -- with navigation.is_home_nav_bar_visible ruling
# out the home screen, whose own header art is dark too.
# Sweep sub-panel. Unlike story_sweep, this modal has ONE fixed layout -- # Sweep sub-panel. Unlike story_sweep, this modal has ONE fixed layout --
# confirmed identical across both stages tested live (09 and 12), no # confirmed identical across both stages tested live (09 and 12), no

View File

@ -233,6 +233,67 @@ def find_color_centroid(region, rgb_min, rgb_max, min_pixels=1):
return (x1 + int(xs.mean()), y1 + int(ys.mean())) return (x1 + int(xs.mean()), y1 + int(ys.mean()))
def find_color_blobs(region, rgb_min, rgb_max, min_size=(1, 1), image=None):
"""Bounding box `(x1, y1, x2, y2)` of every connected blob of pixels
within `region` falling in the given RGB range, at least `min_size`
`(width, height)` large, sorted top-to-bottom.
Unlike find_color_centroid (one centroid over ALL matching pixels), this
keeps several same-colored buttons apart -- built for event_sweep.py's
stage list, where every visible row has its own identical 入場 button.
The local equivalent of the reference's core/image.py
get_image_all_appear_position + merge_nearby_coordinates (every
on-screen instance of one button, deduplicated). `min_size` doubles as
the "only fully-visible instances count" filter: a button half-clipped
by a scroll viewport's edge comes out as a short blob and is dropped,
the same way a partial template fails the reference's match threshold.
"""
x1, y1, _, _ = region
mask = _color_mask(region, rgb_min, rgb_max, image=image).astype(np.uint8)
count, _, stats, _ = cv2.connectedComponentsWithStats(mask)
min_w, min_h = min_size
boxes = []
for i in range(1, count):
x, y, w, h, _ = stats[i]
if w >= min_w and h >= min_h:
boxes.append((x1 + int(x), y1 + int(y), x1 + int(x + w), y1 + int(y + h)))
return sorted(boxes, key=lambda box: (box[1], box[0]))
def find_glyph_bbox(region, max_gray=110, glyph_height=(20, 60), max_glyph_width=45, image=None):
"""Union bounding box `(x1, y1, x2, y2)` of the dark text glyphs inside
`region`, or None if there are none.
A "glyph" is a connected component of near-black pixels (grayscale <=
`max_gray`) whose height falls within `glyph_height` and whose width is
at most `max_glyph_width` -- tall enough to drop small specks/outline
fragments, narrow enough to drop horizontal rules or a dark background
strip. For locating a number whose exact position inside its card isn't
reliable (event_sweep.py's stage numbers: the same card's digits sat at
different offsets in different events), so the OCR crop can be fitted
to wherever the digits actually rendered instead of a hardcoded rect.
"""
x1, y1, x2, y2 = region
img = image if image is not None else driver.read_screenshot(OCR_SHOT_PATH)
gray = cv2.cvtColor(img[y1:y2, x1:x2], cv2.COLOR_BGR2GRAY)
mask = (gray <= max_gray).astype(np.uint8)
count, _, stats, _ = cv2.connectedComponentsWithStats(mask)
min_h, max_h = glyph_height
found = None
for i in range(1, count):
x, y, w, h, _ = stats[i]
if not (min_h <= h <= max_h and w <= max_glyph_width):
continue
box = (int(x), int(y), int(x + w), int(y + h))
if found is None:
found = box
else:
found = (min(found[0], box[0]), min(found[1], box[1]), max(found[2], box[2]), max(found[3], box[3]))
if found is None:
return None
return (x1 + found[0], y1 + found[1], x1 + found[2], y1 + found[3])
def _ocr_crop(region): def _ocr_crop(region):
x1, y1, x2, y2 = region x1, y1, x2, y2 = region
img = driver.read_screenshot(OCR_SHOT_PATH) img = driver.read_screenshot(OCR_SHOT_PATH)
@ -342,7 +403,7 @@ def read_int(region, psm=7):
return int(digits) if digits else None return int(digits) if digits else None
def read_int_bordered(region, psm=7, border=20): def read_int_bordered(region, psm=7, border=20, image=None):
"""read_int, but with a white margin added around the upscaled crop """read_int, but with a white margin added around the upscaled crop
before OCR. before OCR.
@ -362,9 +423,13 @@ def read_int_bordered(region, psm=7, border=20):
this one tight edge-to-edge crop shape has ever been confirmed to need this one tight edge-to-edge crop shape has ever been confirmed to need
it -- every other OCR read in this project already has enough natural it -- every other OCR read in this project already has enough natural
margin. margin.
`image` optionally supplies an already-decoded frame (from
capture_screen()) instead of taking a fresh screenshot -- see
region_contains_color's own `image` param for why.
""" """
x1, y1, x2, y2 = region x1, y1, x2, y2 = region
img = driver.read_screenshot(OCR_SHOT_PATH) img = image if image is not None else driver.read_screenshot(OCR_SHOT_PATH)
crop = img[y1:y2, x1:x2] crop = img[y1:y2, x1:x2]
gray = cv2.cvtColor(crop, cv2.COLOR_BGR2GRAY) gray = cv2.cvtColor(crop, cv2.COLOR_BGR2GRAY)
_, thresh = cv2.threshold(gray, 150, 255, cv2.THRESH_BINARY) _, thresh = cv2.threshold(gray, 150, 255, cv2.THRESH_BINARY)

View File

@ -117,8 +117,9 @@ HOME_NAV_BAR_MIN_CHANNEL = 220
HOME_NAV_BAR_MAX_SPREAD = 25 HOME_NAV_BAR_MAX_SPREAD = 25
def is_home_nav_bar_visible(driver): def is_home_nav_bar_visible(driver, image=None):
for r, g, b in driver.colors_at(HOME_NAV_BAR_PROBES): # `image`: optional already-decoded frame, same as is_header_bar_visible's.
for r, g, b in driver.colors_at(HOME_NAV_BAR_PROBES, image=image):
if min(r, g, b) < HOME_NAV_BAR_MIN_CHANNEL: if min(r, g, b) < HOME_NAV_BAR_MIN_CHANNEL:
return False return False
if max(r, g, b) - min(r, g, b) > HOME_NAV_BAR_MAX_SPREAD: if max(r, g, b) - min(r, g, b) > HOME_NAV_BAR_MAX_SPREAD:

File diff suppressed because one or more lines are too long

View File

@ -355,6 +355,58 @@ kind (the live investigation above spent its effort reproducing/fixing the
finished-text rect instead, since that page happened to be reachable at finished-text rect instead, since that page happened to be reachable at
the time) -- next real `stage_not_found`-triggering run should confirm the the time) -- next real `stage_not_found`-triggering run should confirm the
stability fix directly. stability fix directly.
**Dynamic stage-row search (2026-10-08).** Everything above still located
rows by a fixed tuple of y positions at the list's bottom-scrolled extreme
(config.EVENT_STAGE_ROW_Y), recalibrated per event: 366/538/710/883/1055
for the 2026-07 event, shifted to 379/550/722/893 on 2026-09-24 for "It's
our Engine!". The very next event, a rerun of an older one ("我らオカルト研
究会!", from 2026-10-07), rendered at the ORIGINAL 2026-07 positions again,
so every fixed crop missed its digits. The 2026-10-08 01:00 q4h run spent
all 6 outer attempts on "wrong_page" while sitting on the correct, fully
rendered list (its saved event_sweep_no_rows_debug.png shows rows 08-12
plainly). The bottom-scrolled position just depends on each event's list
content height, so no fixed tuple can cover every event.
Replaced with a port of the reference's own approach, core/image.py
swipe_search_target_str (called from activity_utils.to_mission_task_info),
instead of a fourth recalibration:
- _locate_stage_rows finds every fully-visible 入場 button by color blob
(detector.find_color_blobs; size filter rejects buttons clipped by the
viewport edge), then fits an OCR crop to the dark digit glyphs beside each
one (detector.find_glyph_bbox) and reads it with read_int_bordered. No y
position, row count or digit offset is hardcoded.
- _find_stage_row scrolls toward the target by comparing it with the
smallest/largest visible stage number, from wherever the list starts,
instead of jumping to the bottom extreme -- so any stage of any event
length is reachable, not just the last 5.
- A scan only counts if it reads 2+ strictly-ascending stage numbers. A
saved home-screen capture showed why: its お仕事 button is a big cyan blob
with a stray glyph that OCRs as "4".
- A target past the list's end (scrolled toward it, the list didn't move)
ends as "stage_not_in_event" with no further retries, since re-navigating
can't add a stage.
- The 入場 click now targets the detected button's own center.
Live-confirmed at zero AP on the rerun event: the old failing screenshot
reads 8/9/10/11/12 correctly; starting from the top, middle and bottom of
the list, targets 1, 5, 9 and 12 were all found by scrolling; target 13
came back "beyond_list" after one scroll that didn't move the list. One
wheel click measured as exactly one row (see
config.EVENT_STAGE_LIST_SCROLL_STEP_CLICKS). The first real q4h run on it
(2026-10-08 02:09) found stage 10 on its second attempt (the first landed
on the finished event's page, caught by _is_finished_event_page) and
swept it for real (AP 240 -> 10, "result: swept").
That same run exposed one more event-art-dependent probe:
_is_stage_modal_open read the event's own background at (1870, 600),
which this event paints dark, so the plain list read as "modal open" and
_close_stage_modal pressed Escape/the X position three times after the
sweep. Now reads the white header bar (fixed UI chrome) instead, with a
home-screen exclusion -- see config.py's "任務情報 (stage info) modal"
note. Live-confirmed at zero AP: home and plain list read closed, the real
stage-10 modal read open, and after _close_stage_modal it read closed with
the list intact.
""" """
import datetime import datetime
import os import os
@ -370,12 +422,26 @@ WRONG_PAGE_RETRIES = 6
STAGE_ENTER_RETRIES = 3 STAGE_ENTER_RETRIES = 3
STAGE_ROW_SCAN_ATTEMPTS = 3 STAGE_ROW_SCAN_ATTEMPTS = 3
STAGE_ROW_SCAN_RETRY_WAIT = 2.5 STAGE_ROW_SCAN_RETRY_WAIT = 2.5
# Scroll-and-rescan budget for walking the list toward the target -- the
# reference's own swipe_search_target_str call uses max_swipe_times=10.
STAGE_SEARCH_STEPS = 10
STAGE_MISREAD_RESCANS = 2
STAGE_MISREAD_RESCAN_WAIT = 1
STAGE_LIST_SCROLL_SETTLE = 1
SWEEP_START_RETRIES = 3 SWEEP_START_RETRIES = 3
def _is_stage_modal_open(driver, config): def _is_stage_modal_open(driver, config):
r, g, b = driver.color_at(*config.EVENT_STAGE_MODAL_PROBE) # The modal's dim backdrop darkens the event screen's white header bar;
return r < config.EVENT_STAGE_MODAL_DIM_MAX_CHANNEL and g < config.EVENT_STAGE_MODAL_DIM_MAX_CHANNEL and b < config.EVENT_STAGE_MODAL_DIM_MAX_CHANNEL # that bar is fixed UI chrome, unlike the event background art the old
# single-point probe read -- see config.py's "任務情報 (stage info)
# modal" note. The home check keeps a dark home header from ever
# reading as "modal open", so _close_stage_modal can't press Escape on
# the home screen (Blue Archive's exit-game prompt).
image = detector.capture_screen()
if navigation.is_header_bar_visible(driver, image=image):
return False
return not navigation.is_home_nav_bar_visible(driver, image=image)
def _color_in_range(rgb, rgb_range): def _color_in_range(rgb, rgb_range):
@ -435,22 +501,44 @@ def _open_event_screen(driver, config):
return True return True
def _row_number_rect(config, row_y): def _locate_stage_rows(config, image):
x1, x2 = config.EVENT_STAGE_NUMBER_OCR_X """Every fully-visible stage row in one frame, top to bottom, as
top_pad, bottom_pad = config.EVENT_STAGE_NUMBER_OCR_Y_PAD `(stage_number_or_None, enter_button_center, number_glyph_box_or_None)`.
return (x1, row_y - top_pad, x2, row_y + bottom_pad)
The local port of swipe_search_target_str's inner loop: find every 入場
button (get_image_all_appear_position), then OCR the stage number on
that button's own row (its ocr_region_offsets crop). Unlike the
reference's fixed offset crop, the number crop here is fitted to the
digit glyphs actually found beside the button -- see
config.EVENT_STAGE_NUMBER_BAND_DX.
"""
dx1, dx2 = config.EVENT_STAGE_NUMBER_BAND_DX
rows = []
for bx1, by1, bx2, by2 in detector.find_color_blobs(
config.EVENT_STAGE_LIST_REGION, *config.EVENT_STAGE_ENTER_BUTTON_RGB,
min_size=config.EVENT_STAGE_ENTER_BUTTON_MIN_SIZE, image=image,
):
center = ((bx1 + bx2) // 2, (by1 + by2) // 2)
number_box = detector.find_glyph_bbox((max(0, bx1 + dx1), by1, max(0, bx1 + dx2), by2), image=image)
label = None
if number_box is not None:
nx1, ny1, nx2, ny2 = number_box
label = detector.read_int_bordered((nx1 - 4, ny1 - 4, nx2 + 4, ny2 + 4), image=image)
# Stage numbers render as two digits ("01".."12"); anything
# longer is an OCR artifact, not a real stage.
if label is not None and not 1 <= label <= 99:
label = None
rows.append((label, center, number_box))
return rows
def _scan_stage_rows_once(driver, config, stage): def _scan_stage_rows_once(driver, config):
labels = [] rows = _locate_stage_rows(config, detector.capture_screen())
row_y_match = None if not rows:
for row_y in config.EVENT_STAGE_ROW_Y: print("[event_sweep] no fully-visible 入場 buttons found")
label = detector.read_int_bordered(_row_number_rect(config, row_y)) for label, (_, y), _ in rows:
print(f"[event_sweep] row @ {row_y}: read '{label}'") print(f"[event_sweep] row @ {y}: read '{label}'")
labels.append(label) return rows
if label == stage:
row_y_match = row_y
return row_y_match, labels
def _is_finished_event_page(driver, config): def _is_finished_event_page(driver, config):
@ -469,71 +557,110 @@ def _is_finished_event_page(driver, config):
def _find_stage_row(driver, config, stage): def _find_stage_row(driver, config, stage):
# This event's target range (9-12) always sits within the last 5 rows, """Scroll the Quest list until `stage`'s row is visible. Returns
# confirmed live regardless of starting scroll position -- so unlike `(enter_button_center_or_None, status)`, status one of:
# story_sweep._find_stage_row, only the bottom extreme is ever checked.
# - "found"
# Also tracks whether ANY row OCR'd a real number at all. Originally, - "finished_page": the finished-event "period ended" text is showing
# all-empty reads alone were treated as "wrong page" -- live testing - "no_rows": no stage number ever read -- still rendering, or a page
# found that produces false positives (a fresh navigation's list can this task doesn't recognize (debug screenshot saved)
# still be settling/rendering for far longer than expected, up to - "beyond_list": scrolled toward the target and the list didn't move
# ~20s+), and false negatives are cheap to avoid: an explicit check for -- `stage` lies past this event's last/first row
# the finished-event page's own "period ended" text (_is_finished_event_ - "not_found": rows read, but the target's own row never did
# page) is the AUTHORITATIVE signal now. All-empty reads alone just
# mean "keep waiting patiently", not "wrong page" -- see module Port of core/image.py swipe_search_target_str's outer loop: compare the
# docstring for the full history of this getting fixed twice. target against the smallest/largest stage number visible and scroll
# toward it (down if bigger than everything visible, up if smaller),
# A real run's log (2026-07-29, user report) surfaced a related false rather than jumping to one pre-calibrated scroll extreme and reading
# positive this all-empty handling didn't cover: scan 1 read all 5 rows fixed row positions -- see config.EVENT_STAGE_LIST_REGION for why the
# None (correctly treated as "still settling"), but scan 2 read a single fixed-position version kept breaking on every new event.
# stray/partial digit ('7', not matching any real row on this event) at
# just ONE row position while the other 4 were still None -- one non- Carried over from the previous fixed-row version (see module docstring):
# None read was enough to satisfy the old "saw ANY valid row -> trust - a freshly-opened list can take many seconds to render, so a scan with
# it" check, so the loop immediately concluded "genuinely on the right no readable numbers waits and rescans in place rather than concluding
# page, target stage just isn't in it" (stage_not_found) off a single "wrong page" at once; _is_finished_event_page is the only immediate,
# transient misread, instead of recognizing the list was still mid- authoritative wrong-page signal;
# render. Fixed the same way the all-empty case already was: don't - a single transient read is never trusted to conclude the target is
# trust a "some rows read, target not among them" conclusion the first absent: "beyond_list" requires a real scroll toward the target that
# time it appears either -- require the exact same set of labels to left the exact same labels on screen, and a target that should be
# read back identically on two consecutive scans first. A genuinely visible (between the smallest and largest number read) but wasn't is
# rendered (even wrong) page's OCR reads are stable frame to frame; a rescanned in place, not given up on.
# mid-render one isn't, which is exactly the signal that already fixed """
# the all-empty version of this same problem class.
x, y = config.EVENT_STAGE_LIST_SCROLL_POINT x, y = config.EVENT_STAGE_LIST_SCROLL_POINT
driver.scroll(x, y, "down", config.EVENT_STAGE_LIST_SCROLL_CLICKS) empty_scans = 0
driver.wait(0.5) misread_rescans = 0
saw_any_valid_row = False
last_direction = None
previous_labels = None previous_labels = None
for attempt in range(1, STAGE_ROW_SCAN_ATTEMPTS + 1):
row_y, labels = _scan_stage_rows_once(driver, config, stage)
if row_y is not None:
return row_y, True, False
saw_any_valid_row = any(label is not None for label in labels) for _ in range(STAGE_SEARCH_STEPS):
if saw_any_valid_row and labels == previous_labels: rows = _scan_stage_rows_once(driver, config)
return None, True, False labels = [label for label, _, _ in rows]
visible = [label for label in labels if label is not None]
# Only trust a scan that actually looks like this list: 2+ stage
# numbers, strictly ascending top to bottom. Confirmed against a
# saved home-screen capture: the お仕事 button there is a big cyan
# blob too, with a stray glyph beside it that OCRs as "4" -- one
# bogus row like that must never be clicked as the target or steer
# the scroll. A mid-render or misread list fails this the same way
# and is just rescanned.
if len(visible) >= 2 and all(a < b for a, b in zip(visible, visible[1:])):
for label, center, _ in rows:
if label == stage:
return center, "found"
else:
if visible:
print(f"[event_sweep] rows read {labels} don't look like a stage list yet (need 2+ ascending numbers)")
visible = []
if not visible:
if _is_finished_event_page(driver, config): if _is_finished_event_page(driver, config):
print("[event_sweep] finished-event page text detected -- confirmed wrong page") print("[event_sweep] finished-event page text detected -- confirmed wrong page")
return None, False, True return None, "finished_page"
empty_scans += 1
if empty_scans >= STAGE_ROW_SCAN_ATTEMPTS:
break
print(f"[event_sweep] no stage-row numbers recognized (empty scan {empty_scans}/{STAGE_ROW_SCAN_ATTEMPTS}) -- screen may still be settling")
driver.wait(STAGE_ROW_SCAN_RETRY_WAIT)
continue
saw_any_valid_row = True
if stage > max(visible):
direction = "down"
elif stage < min(visible):
direction = "up"
else:
misread_rescans += 1
if misread_rescans > STAGE_MISREAD_RESCANS:
print(f"[event_sweep] stage {stage} should be among the visible rows {labels} but its row never read as {stage}")
return None, "not_found"
print(f"[event_sweep] stage {stage} should be among the visible rows {labels} -- rescanning in place ({misread_rescans}/{STAGE_MISREAD_RESCANS})")
driver.wait(STAGE_MISREAD_RESCAN_WAIT)
continue
if direction == last_direction and labels == previous_labels:
print(f"[event_sweep] list didn't move scrolling {direction} (still {labels}) -- stage {stage} is past the end of this event's stage list")
return None, "beyond_list"
print(f"[event_sweep] stage {stage} is outside the visible rows {visible} -- scrolling {direction}")
driver.scroll(x, y, direction, config.EVENT_STAGE_LIST_SCROLL_STEP_CLICKS)
driver.wait(STAGE_LIST_SCROLL_SETTLE)
last_direction = direction
previous_labels = labels
if saw_any_valid_row: if saw_any_valid_row:
print(f"[event_sweep] some rows read but not yet stable across scans (scan {attempt}/{STAGE_ROW_SCAN_ATTEMPTS}): {labels}") print(f"[event_sweep] stage {stage} not reached within {STAGE_SEARCH_STEPS} scans")
else: return None, "not_found"
print(f"[event_sweep] no stage-row numbers recognized on scan {attempt}/{STAGE_ROW_SCAN_ATTEMPTS} -- screen may still be settling")
previous_labels = labels
if attempt < STAGE_ROW_SCAN_ATTEMPTS:
driver.wait(STAGE_ROW_SCAN_RETRY_WAIT)
# Exhausted the patience budget with no valid rows AND no confirmed # Exhausted the patience budget with no valid rows AND no confirmed
# finished-event text -- a genuinely inconclusive state. Save a debug # finished-event text -- a genuinely inconclusive state. Save a debug
# screenshot so a future recurrence can actually be diagnosed/used to # screenshot so a future recurrence can actually be diagnosed, per
# fix EVENT_FINISHED_TEXT_RECT's calibration, per CLAUDE.md's "write # CLAUDE.md's "write debug images to scratchpad/" convention.
# debug images to scratchpad/" convention.
debug_path = os.path.join(config.SCRATCHPAD_DIR, "event_sweep_no_rows_debug.png") debug_path = os.path.join(config.SCRATCHPAD_DIR, "event_sweep_no_rows_debug.png")
driver.screenshot(debug_path) driver.screenshot(debug_path)
print(f"[event_sweep] gave up waiting for stage rows without confirming finished-event text either -- saved {debug_path}") print(f"[event_sweep] gave up waiting for stage rows without confirming finished-event text either -- saved {debug_path}")
return None, False, False return None, "no_rows"
def _click_max_and_verify(driver, config): def _click_max_and_verify(driver, config):
@ -636,14 +763,14 @@ def _watch_sweep_result(driver, config):
return outcome or "unrecognized_state" return outcome or "unrecognized_state"
def _open_stage_modal(driver, config, row_y): def _open_stage_modal(driver, config, button_center):
# A single click-then-check here was found live to intermittently miss # A single click-then-check here was found live to intermittently miss
# (CLAUDE.md already documents this exact failure mode for the # (CLAUDE.md already documents this exact failure mode for the
# mailbox/cafe icons: "missed the first click and worked on retry") -- # mailbox/cafe icons: "missed the first click and worked on retry") --
# retry-with-verify like every other click-then-confirm step in this # retry-with-verify like every other click-then-confirm step in this
# module, rather than aborting on the first miss. # module, rather than aborting on the first miss.
for attempt in range(1, STAGE_ENTER_RETRIES + 1): for attempt in range(1, STAGE_ENTER_RETRIES + 1):
driver.click(config.EVENT_STAGE_ENTER_X, row_y) driver.click(*button_center)
driver.wait(2) driver.wait(2)
if _is_stage_modal_open(driver, config): if _is_stage_modal_open(driver, config):
return True return True
@ -669,17 +796,19 @@ def _click_sweep_start_and_verify(driver, config):
def _sweep_target(driver, config, stage, count): def _sweep_target(driver, config, stage, count):
print(f"[event_sweep] --- target stage {stage} x {count} ---") print(f"[event_sweep] --- target stage {stage} x {count} ---")
row_y, saw_any_valid_row, confirmed_wrong_page = _find_stage_row(driver, config, stage) button_center, status = _find_stage_row(driver, config, stage)
if row_y is None: if status == "finished_page":
if confirmed_wrong_page:
return "wrong_page" return "wrong_page"
if not saw_any_valid_row: if status == "no_rows":
print("[event_sweep] gave up waiting for stage rows without ever confirming finished-event text -- treating as wrong page anyway (see scratchpad debug screenshot)") print("[event_sweep] gave up waiting for stage rows without ever confirming finished-event text -- treating as wrong page anyway (see scratchpad debug screenshot)")
return "wrong_page" return "wrong_page"
print(f"[event_sweep] stage {stage} not found in the visible stage list") if status == "beyond_list":
return "stage_not_in_event"
if status != "found":
print(f"[event_sweep] stage {stage} not found in the stage list")
return "stage_not_found" return "stage_not_found"
if not _open_stage_modal(driver, config, row_y): if not _open_stage_modal(driver, config, button_center):
print("[event_sweep] stage info panel not detected, aborting") print("[event_sweep] stage info panel not detected, aborting")
return "unrecognized_state" return "unrecognized_state"