feat: implement grid alignment checks for shop item selection to prevent misreads after scrolling

This commit is contained in:
Nik Afiq 2026-09-21 19:21:22 +09:00
parent 19e39ec8b2
commit 80c1a645b8
3 changed files with 82 additions and 7 deletions

View File

@ -840,14 +840,31 @@ SHOP_NAME_OCR_OFFSET = (-75, -105, 160, -45)
# threshold, set well clear of both clusters.
SHOP_BUTTON_PROBE_OFFSET = (90, 210)
SHOP_BUTTON_PURCHASABLE_MIN_BLUE_TINT = 40
# One driver.scroll(..., clicks=N) call at this many clicks reliably
# advances the grid by exactly one row (confirmed live: the item at
# row_y[1] before the scroll reappears at row_y[0] after it, same name and
# price) -- so select_targets_by_name can scan two full rows per
# screenshot and scroll by exactly one row's worth between reads, without
# skipping or double-reading a row.
# One driver.scroll(..., clicks=N) call at this many clicks USED TO reliably
# advance the grid by exactly one row (confirmed live at the time: the item
# at row_y[1] before the scroll reappeared at row_y[0] after it, same name
# and price). **No longer trustworthy alone** (found live 2026-09-21, see
# script_error.md and shop_utils._grid_looks_aligned's own docstring): once
# the catalog grew to include raw-material items whose names wrap to two
# lines, row heights are no longer uniform, so this fixed click count can
# overshoot past a clean row boundary and land mid-row. Kept as the primary
# scroll step (still correct most of the time -- the catalog's stone/report
# rows are still single-line and uniform), but select_targets_by_name now
# verifies the landing frame is actually row-aligned before trusting any
# read from it, rather than assuming this value alone guarantees that.
SHOP_SCROLL_STEP_CLICKS = 3
SHOP_SCROLL_POINT = (1310, 485)
# Realignment nudge used when a scroll lands mid-row (see
# shop_utils._grid_looks_aligned/_ensure_grid_aligned): a single small
# scroll-up click, retried up to this many times, searching for a frame
# where at least one cell's buy button reads purchasable again. 1 click is
# deliberately much smaller than SHOP_SCROLL_STEP_CLICKS's 3, so repeated
# nudges can land somewhere near a row boundary that a single big step
# jumped straight over; 5 nudges (5px-scroll-equivalent clicks) gives
# comfortable headroom to reverse a single 3-click overshoot. Not yet tuned
# against a measured per-click pixel distance -- first live value to try.
SHOP_SCROLL_REALIGN_NUDGE_CLICKS = 1
SHOP_SCROLL_REALIGN_MAX_NUDGES = 5
# Hard cap on scroll steps for select_targets_by_name, independent of its
# own "screenshot unchanged after scrolling" bottom-of-list detection --
# the live catalog was ~6 rows deep (24 slots) when this was built, so this

File diff suppressed because one or more lines are too long

View File

@ -185,6 +185,61 @@ def _scan_current_view(driver, config, remaining):
return None
def _grid_looks_aligned(driver, config):
"""True if at least one of the 8 fixed grid cells currently reads
purchasable -- a cheap proxy for "the fixed SHOP_ITEM_ROW_Y/
SHOP_ITEM_COL_X crops are actually landing on real cards right now",
not a torn mid-scroll frame.
Real live bug (2026-09-21): a real cron run of Common Shop logged 4-5
of 8 configured targets as "not found available at its expected price"
-- '中級強化珠' among them -- even though a read-only diagnostic probe
against the very same live screen, moments apart, found it sitting
unscrolled at the top of the grid with its correct name, correct price,
and a purchasable buy button. Root-caused with a before/after screenshot
of a single select_targets_by_name scroll call: the grid visibly moved
by something closer to 1.5-2 rows, not the clean single row
SHOP_SCROLL_STEP_CLICKS's own docstring assumed -- landing on a frame
with card titles cut off above the viewport and buy buttons sitting
nowhere near their calibrated offsets. Every fixed-offset OCR/color read
against that frame came back empty or garbled, and _is_purchasable read
False for all 8 cells (vs. 7-8/8 True on a genuinely aligned frame, e.g.
the very first unscrolled screenshot of the same run) -- so the scan
silently treated a real, available item as gone rather than as "my own
crop coordinates are pointing at nothing right now".
Not foolproof: a row that is genuinely, simultaneously sold out across
all 8 visible cells would also read this way. Accepted as an unlikely
edge case, consistent with this module's other scan heuristics (e.g.
select_targets_by_name's own "screenshot unchanged" bottom-of-list
check has a similar blind spot).
"""
n_rows = len(config.SHOP_ITEM_ROW_Y)
n_cols = len(config.SHOP_ITEM_COL_X)
return any(
_is_purchasable(driver, config, row, col)
for row in range(n_rows)
for col in range(n_cols)
)
def _ensure_grid_aligned(driver, config):
"""After a scroll, confirm the grid landed row-aligned (see
_grid_looks_aligned); if not, nudge back up in small steps until it is,
or give up after config.SHOP_SCROLL_REALIGN_MAX_NUDGES attempts.
Returns True once aligned (including if it already was), False if every
nudge was exhausted without recovering.
"""
if _grid_looks_aligned(driver, config):
return True
for _ in range(config.SHOP_SCROLL_REALIGN_MAX_NUDGES):
driver.scroll(*config.SHOP_SCROLL_POINT, "up", clicks=config.SHOP_SCROLL_REALIGN_NUDGE_CLICKS)
driver.wait(0.3)
if _grid_looks_aligned(driver, config):
return True
return False
def select_targets_by_name(driver, config, targets):
"""Name-OCR-based sibling of select_targets, for grids where a bought
(or sold-out) item sinks to the bottom and shifts everything else's
@ -250,6 +305,9 @@ def select_targets_by_name(driver, config, targets):
if remaining:
driver.scroll(*config.SHOP_SCROLL_POINT, "down", clicks=config.SHOP_SCROLL_STEP_CLICKS)
driver.wait(0.3)
if not _ensure_grid_aligned(driver, config):
print("[shop] grid scroll landed on a misaligned frame and could not realign -- stopping scan early (remaining targets may still be genuinely available)")
break
skipped = []
for name in remaining:
print(f"[shop] '{name}' not found available at its expected price {remaining[name]} -- skipping (sold out this cycle, or catalog changed)")