diff --git a/ba_auto/config.py b/ba_auto/config.py index d28126e..06ea21f 100644 --- a/ba_auto/config.py +++ b/ba_auto/config.py @@ -77,10 +77,17 @@ MISSION_DAILY_GEM_CLAIM_BUTTON = (1400, 1095) CAFE_ICON = (165, 1100) CAFE_ROOM_SWITCH = (190, 160) CAFE_INCOME = (1780, 1105) -# Max sparkle-detection attempts per room (hits and misses both count -- -# sparkles are on a per-student cooldown, so most checks legitimately find -# nothing and the loop keeps polling rather than giving up after one miss). -CAFE_MAX_CLICKS_PER_ROOM = 15 +# _pat_current_view (2026-09-19 redesign, per explicit user direction): +# screenshot once, pat every sparkle found in that one frame, screenshot +# again, repeat -- stopping once CAFE_EMPTY_ROUNDS_TO_STOP consecutive +# rounds each find zero sparkles, rather than the old fixed budget of +# individual click attempts (which re-screenshotted and re-matched once per +# pat even when several sparkles were already visible in the same frame). +# CAFE_MAX_SPARKLE_ROUNDS is a hard safety cap on top of that -- the real +# stopping condition is the consecutive-empty-rounds count, this just +# bounds worst case if that somehow never triggers. +CAFE_MAX_SPARKLE_ROUNDS = 10 +CAFE_EMPTY_ROUNDS_TO_STOP = 2 CAFE_SPARKLE_TEMPLATE = os.path.join(ASSET_DIR, "cafe_sparkle.png") # A pat that crosses an affection-rank threshold shows a full-screen "絆ラン # クアップ!" (Bond Rank Up!) cutscene with no cafe header visible at all. @@ -1409,7 +1416,7 @@ BOUNTY_SWEEP_CONFIRM_TEXT_RECT = (605, 505, 1320, 615) # visual differences (see cafe.py's CLAIM_DISABLED_RGB, stamina.py's # MISSION_CLAIM_PROBE) rather than building new template assets, since every # state below reduces to a clean, high-contrast flat color rather than a -# complex shape needing find_cafe_sparkle-style matching. +# complex shape needing find_cafe_sparkles-style matching. # # RECALIBRATED 2026-07-29 after a real Blue Archive client UI update changed # this screen's whole layout. Re-verified live on nik-gpu the same day (see diff --git a/ba_auto/detector.py b/ba_auto/detector.py index 39dc295..c12f13f 100644 --- a/ba_auto/detector.py +++ b/ba_auto/detector.py @@ -18,7 +18,7 @@ SPARKLE_SHOT_PATH = os.path.join(config.SCRATCHPAD_DIR, "cafe_live.png") # Real-usage bug (2026-07-16, reported live with a screenshot): when two # students stand close together, (75, 47) overshoots past the intended # target's own head and lands on a DIFFERENT, closer student's body instead -# -- the click doesn't register as a pat there, so find_cafe_sparkle() just +# -- the click doesn't register as a pat there, so find_cafe_sparkles() just # matches the same still-showing sparkle again next iteration, repeatedly # clicking nearly the same point for the rest of the room's budget (confirmed # live: 13 of 16 "pats" in one run clustered within a ~10px box, all @@ -60,32 +60,75 @@ def _masked_template(template): return cv2.merge([mask_plane, mask_plane, mask_plane]) -def find_cafe_sparkle(image=None): - """`image` optionally supplies an already-decoded frame instead of - taking a fresh screenshot -- for cafe.py's per-iteration capture, shared - with that same iteration's navigation.is_header_bar_visible rank-up - check (see cafe.py's _pat_current_view).""" +# How close two sparkle detections' click points need to be (in px, both +# axes) to be treated as the same real sparkle rather than two distinct +# ones -- the multi-scale sweep in find_cafe_sparkles commonly matches one +# real sparkle at several neighboring scales with slightly different +# computed centers, and those need collapsing into a single click. +# Conservative starting guess, not yet live-calibrated against two genuinely +# distinct sparkles sitting close together: comfortably smaller than the +# template's own on-screen size across the sampled scales (70x93 at 1.0x, +# so ~42x56 to ~84x112 across SPARKLE_SCALES), on the assumption that two +# different students' sparkles won't normally render closer together than +# that. Tighten if a live run is ever seen merging two real sparkles into +# one click, or loosen if the same sparkle still produces duplicate clicks. +SPARKLE_DEDUPE_DISTANCE = 50 + + +def find_cafe_sparkles(image=None): + """Every distinct sparkle currently visible in one frame, as + `(x, y, score)` tuples sorted by descending score -- not just the + single best match `find_cafe_sparkle` used to return. `image` + optionally supplies an already-decoded frame instead of taking a fresh + screenshot -- for cafe.py's per-round capture, shared with that same + round's navigation.is_header_bar_visible rank-up check (see cafe.py's + _pat_current_view). + + Finding every sparkle in one screenshot, instead of just the best one, + lets the pat loop click through all of them from a single capture + before re-screenshotting, rather than paying for a fresh scrot + + multi-scale template match per individual pat when several sparkles are + already visible at once. Replaces the original single-match + find_cafe_sparkle (2026-09-19, per explicit user direction) as part of + the same change that made _pat_current_view stop after two consecutive + empty screenshots instead of a fixed click-attempt budget -- see that + function's own docstring. + """ template_full = cv2.imread(config.CAFE_SPARKLE_TEMPLATE) img = image if image is not None else driver.read_screenshot(SPARKLE_SHOT_PATH) th0, tw0 = template_full.shape[:2] + ox, oy = SPARKLE_CLICK_OFFSET - best = None + candidates = [] for scale in SPARKLE_SCALES: tw, th = max(1, round(tw0 * scale)), max(1, round(th0 * scale)) template = cv2.resize(template_full, (tw, th)) mask = _masked_template(template) result = cv2.matchTemplate(img, template, cv2.TM_CCORR_NORMED, mask=mask) - _, max_val, _, max_loc = cv2.minMaxLoc(result) - if max_val >= SPARKLE_THRESHOLD and (best is None or max_val > best[0]): - best = (max_val, max_loc[0], max_loc[1], tw, th, scale) + ys, xs = np.where(result >= SPARKLE_THRESHOLD) + for x, y in zip(xs, ys): + cx = int(x) + tw // 2 + round(ox * scale) + cy = int(y) + th // 2 + round(oy * scale) + candidates.append((float(result[y, x]), cx, cy)) - if best is None: - return None + return _dedupe_sparkles(candidates) - score, x, y, tw, th, scale = best - ox, oy = SPARKLE_CLICK_OFFSET - return (x + tw // 2 + round(ox * scale), y + th // 2 + round(oy * scale), score) + +def _dedupe_sparkles(candidates): + """Collapse candidates within SPARKLE_DEDUPE_DISTANCE of each other + (the same real sparkle matched at several nearby scales/positions) into + a single detection, keeping the highest-scoring one each time.""" + candidates = sorted(candidates, key=lambda c: c[0], reverse=True) + kept = [] + for score, x, y in candidates: + if any( + abs(x - kx) < SPARKLE_DEDUPE_DISTANCE and abs(y - ky) < SPARKLE_DEDUPE_DISTANCE + for _, kx, ky in kept + ): + continue + kept.append((score, x, y)) + return [(x, y, score) for score, x, y in kept] def find_template(template_path, region=None, threshold=0.85): @@ -96,7 +139,7 @@ def find_template(template_path, region=None, threshold=0.85): -- used wherever the reference identifies a screen/state by comparing a fixed on-screen crop to a known template image, rather than by OCR (OCR reads stay on read_text/read_int; see CLAUDE.md's OCR policy for why - these two stay separate detection paths). Unlike find_cafe_sparkle, this + these two stay separate detection paths). Unlike find_cafe_sparkles, this project's UI chrome (nav icons, modal titles, result banners) renders at a fixed resolution with no camera zoom involved, so no multi-scale search or color masking is needed here -- a single plain match is enough. Add a diff --git a/ba_auto/navigation.py b/ba_auto/navigation.py index 797701c..22b5263 100644 --- a/ba_auto/navigation.py +++ b/ba_auto/navigation.py @@ -90,8 +90,8 @@ def is_header_bar_visible(driver, image=None): # from the same frame instead of drifting across ~8 sequential captures). # # `image` optionally supplies an already-decoded frame (e.g. cafe.py's - # per-iteration capture, shared with that same iteration's - # find_cafe_sparkle call) instead of taking a fresh screenshot here. + # per-round capture, shared with that same round's + # find_cafe_sparkles call) instead of taking a fresh screenshot here. points = [(x, HEADER_ROW_Y) for x in HEADER_ROW_X_OFFSETS] for r, g, b in driver.colors_at(points, image=image): if not (r > SUBSCREEN_HEADER_MIN_CHANNEL and g > SUBSCREEN_HEADER_MIN_CHANNEL and b > SUBSCREEN_HEADER_MIN_CHANNEL): diff --git a/ba_auto/reference_notes/mapping.md b/ba_auto/reference_notes/mapping.md index d65890e..26365ff 100644 --- a/ba_auto/reference_notes/mapping.md +++ b/ba_auto/reference_notes/mapping.md @@ -6,7 +6,7 @@ Maps each local feature to the corresponding `~/repo/baas-reference/module/...` |---|---|---|---|---|---| | Battle Pass | `module/collect_pass_reward.py` | `implement` (main flow), `main_page_to_pass_menu`, `to_page_pass_mission`, `to_page_pass_menu`, `collect_reward` -- `detect_statistics`/`detect_pass_level`/`detect_pass_next_level_point`/`detect_pass_weekly_point` (OCR level/point readout) deliberately not ported, nothing in this project's flow consumes those numbers | `ba_auto/tasks/battle_pass.py` | `core/picture.py::co_detect`'s img_possibles/img_ends (template-image matching against ~20 assets this project doesn't have) → local color probes (`config.BATTLE_PASS_INSIDE_PROBES`/`BATTLE_PASS_MISSION_TAB_PROBES`/`BATTLE_PASS_CLAIM_PROBE`); claim-all button reuses stamina.py's exact bright-yellow-vs-grey enabled/disabled signature and its Enter-then-Enter-to-dismiss-popup convention | Done, live-tested 2026-07-29: real mission-point claim (Lv.3→4) and real pass-level reward claim, both confirmed via screenshots, plus two real `./ba_dailies.sh battle_pass` CLI runs (the first surfaced a real Escape-overshoots-to-home navigation bug, fixed via a self-healing `_ensure_pass_menu` loop rather than a single fixed retry; the second, post-fix, run was clean). `navigation.is_on_subscreen`/`is_modal_open` both read false/false on both battle-pass screens (same false-negative pattern login.py's own pre-home states hit) -- this module carries its own local probes and explicitly Escapes back to home at the end rather than trusting `ba_daily.py`'s generic post-task `return_to_home` cleanup, which would otherwise no-op and leave the game stuck on the pass menu. Also hit live at the very start of this session (not new -- the same full-screen ambient/idle cutscene state Phase 24 already documents, no header/nav chrome at all, cleared by a single click/Enter anywhere): `_ensure_home()`'s pre-task check would misread it as home (same false/false pattern as every other state in that family), so a run starting while it's showing would need that first click/keypress to land correctly. Not specific to this feature, no action taken here. Pure reclaim, no AP/credit/ticket spend -- added to `DEFAULT_ORDER` and the `daily` preset (not `q4h`, matching gem_shop/circle's once-a-day-is-enough placement). | | Mailbox | `module/mail.py` | `to_mail`, `implement` | `ba_auto/tasks/mailbox.py` | tap/click via xdotool, screenshot via scrot, `color.rgb_in_range` → `driver.color_at` pixel-probe check | Migrated: real Python, state-verified via color probe (no legacy bridge) | -| Cafe | `module/cafe_reward.py` | `to_cafe` (its `relationship_rank_up` popup-handling now also ported, see below), `interaction_for_cafe_solve_method3`, `collect`, `invite_girl`/`invite_by_affection`/`checkConfirmInvite` (student invitation, added 2026-07-14) | `ba_auto/tasks/cafe.py` | `picture.co_detect`/`color.rgb_in_range` → `driver.color_at` pixel-probe checks; sparkle template match ported in-process into `ba_auto/detector.py` (`find_cafe_sparkle`, now multi-scale) | Migrated: real Python, state-verified via color probes (no legacy bridge). Pat loop now polls for the full attempt budget instead of stopping on the first miss (see `plan.md` Phase 6 follow-up) — not yet confirmed against a live sparkle since none was available during testing. `_dismiss_rank_up_if_shown` reuses `navigation.is_on_subscreen` to detect and clear the full-screen bond-rank-up cutscene after a pat (see `plan.md` Phase 6 follow-up: rank-up popups) — not yet live-confirmed against a real trigger. **Student invitation (2026-07-14)**: per explicit user direction, invite a student into each room before farming it, preferring highest affection, always skipping (never confirming) a candidate that would swap an already-seated student's costume or move one in from the other room — directly ports `invite_by_affection`/`checkConfirmInvite`'s own logic with the reference's default `cafe_reward_allow_exchange_student`/`cafe_reward_allow_duplicate_invite` both `False` (no local config exists to make either configurable). Live-calibrated against nik-gpu with **zero real tickets spent** — all 3 real dialog variants (normal confirm, same-room costume-swap warning, neighboring-room move warning) found and confirmed live, every one cancelled rather than confirmed during calibration. The heart-shaped affection badge OCR reuses `detector.read_int_on_heart_badge` directly (pixel-confirmed the same widget lesson.py's own badges use — digit pixels sampled RG, matching that function's exact masking assumption) rather than building a second OCR path. Dialog-type detection OCRs the title bar and checks for either warning's own distinctive substring (`衣装`/`隣`) rather than an exact match, mirroring `event_sweep.py`'s own `"終了"` substring-match reasoning. All 5 heart-badge reads and all 3 dialog classifications verified offline against the real saved calibration screenshots before deploying (exact match, zero mismatches). **Confirmed live with real tickets spent, both rooms**: room 1 correctly skipped one 衣装替え (costume-swap) candidate then invited row 1 cleanly; room 2 correctly skipped three consecutive 隣のカフェの生徒を招待 (neighboring-room-move) candidates (expected — room 1's own invite had just taken the account's highest-affection students) then invited row 3 cleanly. Both newly-invited students were immediately patted successfully in the same run (the "newly invited student can be farmed" requirement), income was claimed, and the task returned cleanly to the true home screen with no warnings anywhere in the log. **Horizontal camera panning (2026-07-14)**: per explicit user direction ("due to my screen size... move screen most right and most left then farm"), `_pat_room` now pans the room camera to its rightmost extreme via a new `driver.drag()` primitive (this project's first — distinct from `driver.scroll()`'s wheel-based gesture, which is for list widgets, not a room camera), farms there, pans to the leftmost extreme, farms there too — no vertical panning, per the user's own instruction. Ports the reference's `zoom_out`'s underlying intent (see the whole room regardless of width) via the user's own specified mechanism (panning) rather than the reference's (zoom). Live-confirmed drag-direction-to-reveal-side mapping, that HUD elements (status bar, ticket buttons) stay fixed regardless of pan (no camera reset needed afterward), and that one drag already reaches the true extreme (extra repeats are a confirmed no-op, kept as a safety margin). **Confirmed live with a real game-state change**: a full run patted a real sparkle in room 2 (score 0.997) specifically after panning to an extreme, while room 1 found nothing that run (expected — per-student cooldown). Clean end-to-end completion, no warnings. **Invite ticket cooldown bugfix (2026-07-14)**: real-usage report (with screenshot) that the invite step kept failing once the account's ticket went on cooldown — `_open_invite_list`'s original check (`not is_on_subscreen`) couldn't distinguish the real MomoTalk list opening from a "通知" cooldown notice ("待機時間が経過した後に、再度招待することができます。") opening directly instead, since both dim the header the same way; misread as "list opened", it sent `_ensure_invite_sort`/`_try_invite_row`'s fixed coordinates into a dialog that has none of them. Fixed by checking `navigation.is_modal_open` (the darker real-dialog reading) first — a dialog appearing before any row is clicked can only mean the ticket click raised one directly — and dismissing it via the shared `SWEEP_CONFIRM_BUTTON`, returning `False` so the existing "skip this room's invite" fallback handles it. **Confirmed live**: both rooms correctly detected and dismissed the cooldown notice with no cascading errors, task completed cleanly (exit 0, true home screen confirmed via screenshot). See `plan.md`'s Phase 6 follow-up #4. **Two further real-usage fixes (2026-07-15, `next_fix.md`)**: (1) the cooldown-notice dismiss above now presses Escape and verifies via `navigation.is_modal_open` with bounded retry, instead of a fixed-coordinate click that could miss and leave the notice open under the following pan drags; (2) `_dismiss_rank_up_if_shown` switched from `navigation.is_on_subscreen`'s single-pixel header probe (found live to misread some characters' rank-up cutscene art as bright) to a new `navigation.is_header_bar_visible`, requiring 8 spread-out header-row points to all read bright via one atomic multi-point capture (`driver.colors_at`, added alongside it). Both best-effort — not yet re-confirmed live post-fix. **A real rank-up happened naturally via cron (2026-07-16)**, reported live with a screenshot of the game left stuck on the cutscene — exposed a third bug, a timing gap rather than a threshold problem: `_dismiss_rank_up_if_shown` was only ever called once, immediately after a pat, with no wait beforehand, but the cutscene renders with its own client-side animation delay, so that single check could catch the tail end of the still-normal room view and conclude "clear" a beat before the actual cutscene appeared. Confirmed via a live pixel-check that the resulting stuck cutscene reads under `is_on_subscreen`/`is_modal_open`'s thresholds too (`SUBSCREEN_HEADER_PROBE` read `(180,227,244)`, r=180 < the 200 both checks need), so `navigation.return_to_home`'s generic cleanup also couldn't recover it — every "are we home" check downstream falsely agreed nothing was wrong, and the whole `q4h` cron run finished "successfully" with the game actually left stuck. Fixed by checking on every `_pat_current_view` poll iteration instead of only right after a pat, so a delayed cutscene appearance is caught (and dismissed) roughly a second later instead of never. **Confirmed live**: manually reproduced the exact stuck state left by the incident and confirmed a single Enter (the same action `_dismiss_rank_up_if_shown` already takes) cleared it back to the real cafe room view; the per-iteration check itself has not yet been re-exercised against a fresh live rank-up trigger end-to-end (same "could not force one on demand" caveat as the original fix). **`SPARKLE_CLICK_OFFSET` recalibrated (2026-07-17)**: reported live with a screenshot (`screenshots/cafe/bug_student_close_together.png`) — when two students stand close together, the original `(75, 47)` offset (ported byte-for-byte from `scripts/detect_and_click.py`, never independently verified) overshoots past the intended target's head and lands on a different, closer student instead; the click doesn't register, so `find_cafe_sparkle()` matches the same still-showing sparkle again next iteration, repeatedly clicking nearly the same point for the rest of the room's budget (confirmed live: 13 of 16 "pats" in one run clustered within a ~10px box). First recalibration attempt, `(0, 0)` (raw template-match center), was also wrong — reported live ("clicking the actual sparkle instead of the student head"): a coincidental ambient thought-bubble animation at the test position had been misread as a hover confirmation. Properly recalibrated via numbered candidate-point overlays (screenshots with several labeled offset options drawn on a real live sparkle; the user picked the correct one directly on-screen) against two independent real students in different poses (sitting at an arcade cabinet, lying on a couch) — both converged on the same small offset, `(51, 15)`. **Confirmed live** via the real `./ba_dailies.sh cafe` CLI path: 6 sparkles patted across both rooms at genuinely distinct coordinates with zero repeated-click clustering, clean `[cafe] Done.` and true-home return. **Room 2 now invites lowest affection, plus invited-student name logging (2026-08-13, see `plan.md` Phase 31)**: per explicit user direction, room 1 keeps inviting highest affection (`_invite_student(..., prefer_highest=True)`, the default) while room 2 now invites lowest (`prefer_highest=False`) by sorting the MomoTalk list ascending instead of descending — same row-walking/skip logic either way, no reference equivalent (`invite_by_affection` only ever applies one direction per call and never logs back a name). New `CAFE_INVITE_NAME_RECT_X`/`CAFE_INVITE_NAME_RECT_Y_OFFSET` OCR rect, live-calibrated against a real open invite list; psm=7 (this project's usual single-line default) badly garbled short 2-character names ("ミカ" → `"' ーー、テテー"`), fixed by switching to psm=8 (single word) plus a punctuation strip, re-verified live reading all 5 sampled rows exactly correct. | +| Cafe | `module/cafe_reward.py` | `to_cafe` (its `relationship_rank_up` popup-handling now also ported, see below), `interaction_for_cafe_solve_method3`, `collect`, `invite_girl`/`invite_by_affection`/`checkConfirmInvite` (student invitation, added 2026-07-14) | `ba_auto/tasks/cafe.py` | `picture.co_detect`/`color.rgb_in_range` → `driver.color_at` pixel-probe checks; sparkle template match ported in-process into `ba_auto/detector.py` (`find_cafe_sparkle`, now multi-scale) | Migrated: real Python, state-verified via color probes (no legacy bridge). Pat loop now polls for the full attempt budget instead of stopping on the first miss (see `plan.md` Phase 6 follow-up) — not yet confirmed against a live sparkle since none was available during testing. `_dismiss_rank_up_if_shown` reuses `navigation.is_on_subscreen` to detect and clear the full-screen bond-rank-up cutscene after a pat (see `plan.md` Phase 6 follow-up: rank-up popups) — not yet live-confirmed against a real trigger. **Student invitation (2026-07-14)**: per explicit user direction, invite a student into each room before farming it, preferring highest affection, always skipping (never confirming) a candidate that would swap an already-seated student's costume or move one in from the other room — directly ports `invite_by_affection`/`checkConfirmInvite`'s own logic with the reference's default `cafe_reward_allow_exchange_student`/`cafe_reward_allow_duplicate_invite` both `False` (no local config exists to make either configurable). Live-calibrated against nik-gpu with **zero real tickets spent** — all 3 real dialog variants (normal confirm, same-room costume-swap warning, neighboring-room move warning) found and confirmed live, every one cancelled rather than confirmed during calibration. The heart-shaped affection badge OCR reuses `detector.read_int_on_heart_badge` directly (pixel-confirmed the same widget lesson.py's own badges use — digit pixels sampled RG, matching that function's exact masking assumption) rather than building a second OCR path. Dialog-type detection OCRs the title bar and checks for either warning's own distinctive substring (`衣装`/`隣`) rather than an exact match, mirroring `event_sweep.py`'s own `"終了"` substring-match reasoning. All 5 heart-badge reads and all 3 dialog classifications verified offline against the real saved calibration screenshots before deploying (exact match, zero mismatches). **Confirmed live with real tickets spent, both rooms**: room 1 correctly skipped one 衣装替え (costume-swap) candidate then invited row 1 cleanly; room 2 correctly skipped three consecutive 隣のカフェの生徒を招待 (neighboring-room-move) candidates (expected — room 1's own invite had just taken the account's highest-affection students) then invited row 3 cleanly. Both newly-invited students were immediately patted successfully in the same run (the "newly invited student can be farmed" requirement), income was claimed, and the task returned cleanly to the true home screen with no warnings anywhere in the log. **Horizontal camera panning (2026-07-14)**: per explicit user direction ("due to my screen size... move screen most right and most left then farm"), `_pat_room` now pans the room camera to its rightmost extreme via a new `driver.drag()` primitive (this project's first — distinct from `driver.scroll()`'s wheel-based gesture, which is for list widgets, not a room camera), farms there, pans to the leftmost extreme, farms there too — no vertical panning, per the user's own instruction. Ports the reference's `zoom_out`'s underlying intent (see the whole room regardless of width) via the user's own specified mechanism (panning) rather than the reference's (zoom). Live-confirmed drag-direction-to-reveal-side mapping, that HUD elements (status bar, ticket buttons) stay fixed regardless of pan (no camera reset needed afterward), and that one drag already reaches the true extreme (extra repeats are a confirmed no-op, kept as a safety margin). **Confirmed live with a real game-state change**: a full run patted a real sparkle in room 2 (score 0.997) specifically after panning to an extreme, while room 1 found nothing that run (expected — per-student cooldown). Clean end-to-end completion, no warnings. **Invite ticket cooldown bugfix (2026-07-14)**: real-usage report (with screenshot) that the invite step kept failing once the account's ticket went on cooldown — `_open_invite_list`'s original check (`not is_on_subscreen`) couldn't distinguish the real MomoTalk list opening from a "通知" cooldown notice ("待機時間が経過した後に、再度招待することができます。") opening directly instead, since both dim the header the same way; misread as "list opened", it sent `_ensure_invite_sort`/`_try_invite_row`'s fixed coordinates into a dialog that has none of them. Fixed by checking `navigation.is_modal_open` (the darker real-dialog reading) first — a dialog appearing before any row is clicked can only mean the ticket click raised one directly — and dismissing it via the shared `SWEEP_CONFIRM_BUTTON`, returning `False` so the existing "skip this room's invite" fallback handles it. **Confirmed live**: both rooms correctly detected and dismissed the cooldown notice with no cascading errors, task completed cleanly (exit 0, true home screen confirmed via screenshot). See `plan.md`'s Phase 6 follow-up #4. **Two further real-usage fixes (2026-07-15, `next_fix.md`)**: (1) the cooldown-notice dismiss above now presses Escape and verifies via `navigation.is_modal_open` with bounded retry, instead of a fixed-coordinate click that could miss and leave the notice open under the following pan drags; (2) `_dismiss_rank_up_if_shown` switched from `navigation.is_on_subscreen`'s single-pixel header probe (found live to misread some characters' rank-up cutscene art as bright) to a new `navigation.is_header_bar_visible`, requiring 8 spread-out header-row points to all read bright via one atomic multi-point capture (`driver.colors_at`, added alongside it). Both best-effort — not yet re-confirmed live post-fix. **A real rank-up happened naturally via cron (2026-07-16)**, reported live with a screenshot of the game left stuck on the cutscene — exposed a third bug, a timing gap rather than a threshold problem: `_dismiss_rank_up_if_shown` was only ever called once, immediately after a pat, with no wait beforehand, but the cutscene renders with its own client-side animation delay, so that single check could catch the tail end of the still-normal room view and conclude "clear" a beat before the actual cutscene appeared. Confirmed via a live pixel-check that the resulting stuck cutscene reads under `is_on_subscreen`/`is_modal_open`'s thresholds too (`SUBSCREEN_HEADER_PROBE` read `(180,227,244)`, r=180 < the 200 both checks need), so `navigation.return_to_home`'s generic cleanup also couldn't recover it — every "are we home" check downstream falsely agreed nothing was wrong, and the whole `q4h` cron run finished "successfully" with the game actually left stuck. Fixed by checking on every `_pat_current_view` poll iteration instead of only right after a pat, so a delayed cutscene appearance is caught (and dismissed) roughly a second later instead of never. **Confirmed live**: manually reproduced the exact stuck state left by the incident and confirmed a single Enter (the same action `_dismiss_rank_up_if_shown` already takes) cleared it back to the real cafe room view; the per-iteration check itself has not yet been re-exercised against a fresh live rank-up trigger end-to-end (same "could not force one on demand" caveat as the original fix). **`SPARKLE_CLICK_OFFSET` recalibrated (2026-07-17)**: reported live with a screenshot (`screenshots/cafe/bug_student_close_together.png`) — when two students stand close together, the original `(75, 47)` offset (ported byte-for-byte from `scripts/detect_and_click.py`, never independently verified) overshoots past the intended target's head and lands on a different, closer student instead; the click doesn't register, so `find_cafe_sparkle()` matches the same still-showing sparkle again next iteration, repeatedly clicking nearly the same point for the rest of the room's budget (confirmed live: 13 of 16 "pats" in one run clustered within a ~10px box). First recalibration attempt, `(0, 0)` (raw template-match center), was also wrong — reported live ("clicking the actual sparkle instead of the student head"): a coincidental ambient thought-bubble animation at the test position had been misread as a hover confirmation. Properly recalibrated via numbered candidate-point overlays (screenshots with several labeled offset options drawn on a real live sparkle; the user picked the correct one directly on-screen) against two independent real students in different poses (sitting at an arcade cabinet, lying on a couch) — both converged on the same small offset, `(51, 15)`. **Confirmed live** via the real `./ba_dailies.sh cafe` CLI path: 6 sparkles patted across both rooms at genuinely distinct coordinates with zero repeated-click clustering, clean `[cafe] Done.` and true-home return. **Room 2 now invites lowest affection, plus invited-student name logging (2026-08-13, see `plan.md` Phase 31)**: per explicit user direction, room 1 keeps inviting highest affection (`_invite_student(..., prefer_highest=True)`, the default) while room 2 now invites lowest (`prefer_highest=False`) by sorting the MomoTalk list ascending instead of descending — same row-walking/skip logic either way, no reference equivalent (`invite_by_affection` only ever applies one direction per call and never logs back a name). New `CAFE_INVITE_NAME_RECT_X`/`CAFE_INVITE_NAME_RECT_Y_OFFSET` OCR rect, live-calibrated against a real open invite list; psm=7 (this project's usual single-line default) badly garbled short 2-character names ("ミカ" → `"' ーー、テテー"`), fixed by switching to psm=8 (single word) plus a punctuation strip, re-verified live reading all 5 sampled rows exactly correct. **Real live "screen not confirmed" failure investigated 2026-09-19** (reported by the user: "cafe currently full of resource, never claimed"): every real logged run across two days (4/4 `daily` fires, 10/11 `q4h` fires) hit `_dismiss_rank_up_if_shown`'s "cafe screen not confirmed (rank-up cutscene stuck?)" warning on the very first pat-loop iteration in both rooms, then `_enter_room`'s room-switch also failed, so income was never claimed. Extensive live debugging (superpowers:systematic-debugging — fresh game kill+relaunch, the full `login`→`cafe` sequence, the cooldown-dialog-dismiss path, and a real supervised `q4h` run through the actual cron wrapper, all included) could **not** reproduce the failure even once (3 manual reproductions plus one full live `q4h` run all completed cleanly) despite matching the real preset's task order as closely as possible. Ruled out: `DISPLAY`/`XAUTHORITY` env handling (already hardcoded correctly in `config.ENV`, unaffected by cron's minimal environment), and a "game always cold-launches" theory (both `daily` and `q4h` end with `exit_game`, so every real fire does start from a freshly-relaunched game, but forcing that same cold-launch manually still didn't reproduce it). Real, unrelated things found along the way: the `q4h` crontab's actual schedule (`0 1,10,13,17,21 * * *`) doesn't match its own documented `1,5,9,13,17,21` — confirmed **intentional** by the user (changed to avoid their own active play time), not a bug; and a real screenshot-capture crash (repeated libpng CRC errors, `RuntimeError` after 3 retries) traced to running a manual task while a scheduled `q4h` fire was concurrently active on the same game window — a real hazard of manual testing overlapping a cron fire, not a code bug, left unaddressed per explicit user direction ("No need to fix this, leave it as it be"). Since the exact interrupting state couldn't be captured live, `_dismiss_rank_up_if_shown` now saves a diagnostic screenshot to `scratchpad/` (`cafe_diag_not_confirmed_.png`) on its own final failure, so the next real occurrence leaves hard evidence instead of log text alone. **Sparkle-detection redesigned the same day, per explicit user direction**: the original design took one screenshot and matched only the single best sparkle per click attempt, polling up to a fixed `CAFE_MAX_CLICKS_PER_ROOM` (15) budget regardless of how many sparkles were actually visible at once — correct, but wasteful (confirmed live: ~50s per view, ~100s per room, when nothing was available to pat). `detector.find_cafe_sparkles` (replacing `find_cafe_sparkle`) now finds every sparkle above threshold in one frame via per-scale `np.where` plus a proximity-based dedupe (`SPARKLE_DEDUPE_DISTANCE`, not yet live-calibrated against two genuinely distinct close-together sparkles), and `_pat_current_view` pats every match found in one screenshot before recapturing, stopping once `CAFE_EMPTY_ROUNDS_TO_STOP` (2) consecutive rounds each find nothing rather than exhausting a fixed click budget. Patting a whole batch from one stale screenshot is safe here specifically because cafe sparkles sit on static room students, not a reflowing list like Common Shop's item grid (see that row's own 2026-09-17 incident for the contrasting case). **Confirmed live** via a real `./ba_dailies.sh cafe` run post-fix: both rooms completed correctly (no sparkles happened to be available that run, so the "pat multiple from one screenshot" path itself is implemented-but-not-yet-exercised against a real multi-sparkle frame), but the empty-room case that used to take ~100s per room now completes in ~14s — confirmed via real log timestamps, a >85% reduction for that case. The `q4h` run this same investigation used to try reproducing the original bug also completed with zero anomalies using the code as it stood *before* this redesign, so that run doesn't independently confirm the redesign itself — only the standalone post-deploy run does. | | Stamina/AP | `module/collect_daily_task_power.py` | `to_tasks`/`implement` | `ba_auto/tasks/stamina.py` | `color.rgb_in_range` → `driver.color_at`; reference's per-tab claim loop → live UI's single "一括受取" bulk-claim button + Enter | Migrated: Mission-panel claim done (see `plan.md` Phase 8). The gem shop's own Daily Free Power flow (`module/collect_daily_free_power.py`) is a separate task now — see the "Gem shop daily free package" row below. **Daily gem reward bugfix (2026-07-16)**: reported live by the user against a real account state — the reference's own `implement` checks TWO separate button regions (the main 一括受取 area, then a second, separate "claim daily pyroxenes" button checked independently after), but only the first was ported originally. Fixed by porting the second region too: a new `MISSION_DAILY_GEM_CLAIM_BUTTON` (config.py), reusing the same bright-yellow-vs-grey color-probe style as the existing `MISSION_CLAIM_PROBE`, clicked via `driver.click` (not Enter — unlike 一括受取, this button has no keybind shown on screen) with a click-then-verify retry loop matching this project's established discipline. **Confirmed live**: gem balance went from 12,384 → 12,404 (exactly the reward's stated +20), clean return to the true home screen, no warnings. | | Gem shop daily free package | `module/collect_daily_free_power.py` | `implement`, `to_purchase_pyroxenes_menu`, `to_purchase_type`, `detect_free_power_availability`, `collect_daily_free_power`, `return_to_main_page` | `ba_auto/tasks/gem_shop.py` | Reference detects every step via `core.picture.co_detect` + fixed-region OpenCV template matching (no OCR anywhere in this reference flow). Originally ported here as plain fixed-coordinate clicks + color probes, matching this project's own established equivalent for a simple flat-color state-A/state-B difference (see `cafe.py`'s `CLAIM_DISABLED_RGB`, `stamina.py`'s `MISSION_CLAIM_PROBE`). **Rewritten 2026-09-17**: the left sidebar and the free card's status/buy-button are now located dynamically via OCR (`detector.read_lines`, new primitive) instead of fixed coordinates/colors — see the Status column for why. | Done, rewritten. Original: live-calibrated against nik-gpu 2026-07-15, genuinely claimed the account's real free package for the day (0 yen, confirmed +10 AP / +10,000 credits). Recalibrated 2026-07-29 after a UI update replaced the old 3-top-tab overlay dialog with a real full subscreen (left sidebar + デイリー/ウィークリー/マンスリー sub-tabs under 一般商品) — every coordinate/color re-verified live at that point. **Real live failure, reported by the user 2026-09-17**: `./ba_dailies.sh gem_shop` logged "free card status not recognized after click" 3/3 attempts then aborted. Root-caused (superpowers:systematic-debugging — `scratchpad/probe_gem_shop_careful_nav.py`/`probe_gem_shop_general_tab_fixed.py`, screenshots pulled back for direct visual inspection) to the sidebar gaining a new 限定商品 (NEW) entry above 一般商品 sometime after 2026-07-29, silently shifting 一般商品 from the 3rd to the 4th slot and swapping order with 定額商品 — the old fixed `GEM_SHOP_GENERAL_PRODUCTS_TAB=(196,376)` coordinate landed on 定額商品 every single attempt, reading an unrelated card's content and never matching either status color, exactly matching the reported symptom (a genuine "pressing the wrong button" bug, not a flaky click). Per explicit user request ("make the button press dynamic. Read word then click the correct position of the word"), fixed by adding `detector.read_lines()` (OCR a region, return `(x, y, text)` per detected line) and rewriting the sidebar click, the status read, and the buy-button click to all locate their target text dynamically each run instead of trusting a fixed position — calibrated live against real screenshots (`scratchpad/probe_gem_shop_ocr_regions.py`): the sidebar and the card's own text (`一日にN回まで購入可能`, `毎日無料パッケージ`, `無料`) all OCR cleanly as vertically-stacked lines in their normal (unselected) styling; a selected/highlighted sidebar item's inverted white-on-blue-pill styling was confirmed live to OCR unreliably (garbage text, not just a clean failure) even after trying an inverted threshold, so the design avoids ever needing to re-read a label the task itself just selected (checking for a still-unselected marker instead — see `_on_general_products_tab`, which checks for ウィークリー rather than whichever subtab is selected). The デイリー/ウィークリー/マンスリー subtab row was confirmed NOT the root cause (that exact fixed coordinate landed correctly every time in live testing) and was confirmed live NOT to OCR reliably as one horizontal region (tesseract fragments side-by-side columns far worse than stacked lines), so it deliberately stays a fixed click. **Confirmed live end-to-end** via a real `./ba_dailies.sh gem_shop` run post-fix: OCR-found the sidebar tab, correctly read "available", OCR-clicked the real 無料 button, and genuinely claimed the day's package — credits went from 373,773,507 to 373,783,507 (exactly +10,000) and AP rose accordingly, confirmed via before/after screenshots. Still added to `DEFAULT_ORDER`, unchanged from before — a pure free reclaim with no choice to make. | | Normal/Hard story AP sweep | `module/explore_tasks/sweep_task.py`, `module/explore_tasks/task_utils.py` | `to_region` (ported: OCR region-number readout + delta-click), a scoped-down `swipe_search_target_str` (ported: OCR stage-row label matching), `start_sweep`'s named-outcome contract (ported via `navigation.wait_for_state`, this project's scoped `co_detect` port) | `ba_auto/tasks/story_sweep.py` | OCR region/stage-name matching, ported for real (Phase 10) — replaces Phase 9's "next-region arrow stops advancing, then random stage" heuristic; modal closed via its own X button (Escape doesn't close it; X-button position re-calibrated per stage-layout variant, see Phase 10) | Done (see `plan.md` Phase 10, supersedes Phase 9; rewritten Phase 22 to match story_sweep_hard.py's Phase 21 fixes and add its own campaign guard). Config-driven exact `(region, stage, count)` targets (`config.STORY_SWEEP_TARGETS`), not latest-region/random-stage. Opt-in only, not in default flow. **2026-07-21 rewrite (Phase 22)**: per explicit user direction ("match the hard implementation on best practices"), ported story_sweep_hard.py's fixes back into this module — an explicit Normal-tab guard (`config.NORMAL_TAB`/`_open_normal_tab`, needed now that story_sweep_hard can run earlier in the same `q4h` preset and leave Hard selected), a loop-level self-heal (`_ensure_task_screen`) replacing the old per-path "Escape if on subscreen" cleanup that would have backed out of the region browser after every target (never triggered in production since this module only ever swept one rotation target per run, but a real latent bug), MAX/+ clicks no longer requiring a visible stepper raise as proof of success (same low-AP false-negative class Hard hit), `_click_plus` now resets to a known baseline via a new `SWEEP_MIN_BUTTON` before raising (closing a latent version of bounty.py's real overspend bug that this module's original `_click_plus_and_verify` never guarded against), a newly-added `_confirm_dialog_is_sweep` OCR gate against the two-stacked-button hazard (confirmed live 2026-07-21 that Normal's own tabbed modal has the identical gold 任務開始 button beneath 掃討開始 that config.py's own `SWEEP_MAX_BUTTON` comment already documented but this module never actually guarded against), the same `clicked_any`-gated `_watch_sweep_result` fix, and a `REGION_READ_RETRIES` OCR-read retry. Also added a genuinely new campaign-active guard (`config.TASK_CAMPAIGN_BADGE_RECT`, renamed from `HARD_CAMPAIGN_BADGE_RECT` once shared) — confirmed live to be pixel-identical rect/color to Hard's own banner, just reading "任務Normalで獲得できる報酬量が2倍(+100%)になっています" instead of "Hard". Confirmed live that Normal mode has no per-stage daily clear cap (no "残り回数" indicator anywhere), so Hard's gem-refill hazard does not apply here. `story_sweep_force` added as the override command. **Live-tested for real the same day**: a real MAX sweep of the day's rotation target (30-2) succeeded cleanly end-to-end on the first attempt — campaign guard passed, OCR stage-row match succeeded despite the known leading-digit misread ("20-2" read but the "2" suffix still matched), sweep confirmed and correctly detected as `"swept"` (not the old cosmetic misreport), clean return home. AP dropped from ~139 to 1 and credits rose by 13,966, confirming a real substantial MAX sweep. | @@ -17,8 +17,8 @@ Maps each local feature to the corresponding `~/repo/baas-reference/module/...` | Commissions | `module/clear_special_task_power.py` | Need to inspect | `ba_auto/tasks/commission.py` | sweep/color adaptation | Not started | | Exit game | None -- searched `~/repo/baas-reference/` thoroughly, no counterpart exists. The closest match, `core/Baas_thread.py`'s `shutdown()`/`start_shutdown()` (line ~984), is an optional full Windows OS shutdown (`subprocess.run(["shutdown", "-s", "-t", "60"])`) gated by a user toggle, not a game-client exit -- out of scope (this project's game and X display run on `nik-gpu`, shutting the host down would kill the SSH session too, nothing like what was requested). The only other app-closing call, `connection.py`'s `close_current_app` (line 381, plain ADB `app_stop`), exists purely for `Baas_thread.py`'s own error-recovery restarts (`deal_with_package_incorrect`/`deal_with_func_call_timeout`) inside a persistent background scheduler that's designed to keep running indefinitely -- it never deliberately exits once daily tasks finish. This feature is new, desktop-specific convenience logic motivated by this project's different architecture (cron launches a fresh one-shot process per preset, so closing the game after a run has real value the reference's always-on scheduler never needed), not a port of anything. | `ba_auto/tasks/exit_game.py` | No new driver/detector primitives needed -- reuses `navigation.return_to_home` (must-be-true-home gate), `navigation.is_modal_open` (verifies the Escape-triggered "exit the game?" dialog actually opened, the same generic dim-probe reused across bounty/gem_shop/circle's own confirm dialogs), `driver.keypress("Escape"/"Return")`, and `driver.window_exists()` (verifies the game process/window is actually gone afterward, not just that Enter was sent) | Done. `navigation.return_to_home`'s own docstring already documents the mechanism this relies on: an Escape press on the confirmed true home screen (nowhere else) raises this exact dialog -- every other task treats that as a hazard to avoid triggering by accident; this is the one task that wants it, gated the same way (only fires from a verified true-home state). No force-kill fallback if the graceful Escape/Enter path doesn't verify -- matches this project's established "abort cleanly on unknown state rather than guess" convention (gem_shop/bounty), rather than reaching for `driver.kill_game()`. Opt-in only, appended to the end of the `daily`/`q4h` presets (not `DEFAULT_ORDER`), since it deliberately ends the session. **Confirmed live (2026-07-18)** via a standalone `./ba_dailies.sh exit_game` run, user-reported "works well" -- `is_modal_open`'s generic dim-probe correctly read the exit-confirmation dialog after Escape, and the game closed cleanly after Enter. Not yet exercised as the tail end of a full `daily`/`q4h` preset run (only standalone so far), though nothing in its own logic depends on which task ran before it. | | Arena | `module/arena.py` | `implement` (main flow), `to_tactical_challenge` (nav from main page), `get_tickets` (ticket-count OCR), `choose_enemy` (self/opponent level OCR + bounded refresh-reroll loop), `check_skip_button` (skip-toggle color probe), `fight` (click fight, wait for win/lose), `collect_tactical_challenge_reward` (two reward-slot color probes) | `ba_auto/tasks/arena.py` | Ticket count/self level/opponent level/rank are plain digit OCR (`detector.read_int`/`read_text`, plus a new `read_int_white_on_dark` for the profile card's bright-on-dark level text). Skip-toggle state and the two reward-slot claimed-vs-claimable colors are plain pixel-color probes (`driver.color_at`/`_color_in_range`). The post-fight WIN/LOSE result modal and an unrelated list-refresh-expired notice are NOT detected by precisely locating a button (a color-region search proved unreliable — see Status); they're dismissed via a bounded blind-Enter-press loop matching `lesson.py`'s own `_run_one_schedule` pattern, gated by a hard safety check against the one modal where Enter is dangerous (opponent-info's own attack-formation button, checked via its fixed-position gold button). `choose_enemy`'s refresh-reroll loop is direct Python control flow, bounded by `maxArenaRefreshTimes`. Config knobs carried over from reference defaults: `ArenaComponentNumber`=1, `ArenaLevelDiff`=0, `maxArenaRefreshTimes`=10, `ArenaStopFightWhenRank1`=False | Done. Live-tested for real across all 5 of the account's daily tickets (2 WIN, 1 LOSE, 2 spent debugging the result-modal detection — see `plan.md` Phase 13 for the full writeup). Real navigation differences confirmed live: Tactical Challenge is a Work-hub card, not a bottom-nav icon; the reference's separate opponent-info and formation-edit screens are merged into one modal here with a live ticket-preview; `navigation.is_modal_open`'s shared probe reads *inverted* on this screen (own `_is_modal_open` via `config.ARENA_MODAL_PROBE`). Three real bugs fixed: a level-OCR crop too small for tesseract despite looking legible (fixed by widening the crop, not the pipeline); level text being bright-on-dark unlike every other OCR read in this project (fixed via `read_int_white_on_dark`); and the result-modal detection cycling through two failed color-based designs (fixed by switching to bounded blind-Enter dismissal with a hard safety gate — a real near-miss of the same "mistimed keypress" hazard class `CLAUDE.md` already documents from story_sweep). Deliberately opt-in only, never in `DEFAULT_ORDER` — unlike every other opt-in task so far (which spend a known-safe resource on a config-driven target list), this one fights a real ranked PvP battle that can win or lose and moves the account's actual arena rank. Per explicit user decision: fights exactly one battle per invocation, matching the reference's own per-call pacing (its `next_time = 55` background-thread rescheduling has no equivalent in this project's one-shot CLI). Not yet exercised live: the "no ticket" mid-flow race, an actual reroll click (every opponent offered was already an acceptable level), and `ArenaStopFightWhenRank1`'s rank-1 stop condition — all implemented per the reference's logic, just not yet hit by real game state. `detector.find_template`/`template_visible` (generalized named-template matcher, built during scaffolding) ended up unused — state detection stayed OCR/color-probe-driven throughout, like every other task in this project. Follow-up (2026-07-11): live bug report — this task never returned home at the end, so a second consecutive invocation starting from wherever the first left the game (the Tactical Challenge screen itself) sent `_open_tactical_challenge`'s home-relative clicks to the wrong place and failed all 3 retries. Fixed by calling the shared `navigation.return_to_home(driver)` at the very start of `run()`, reusing the generic Escape-based recovery primitive built for `event_sweep.py`'s own wrong-page recovery. Confirmed live (2026-07-11): reproduced the stuck-on-arena-screen scenario manually, then a real `arena` invocation recovered and completed a full fight normally (rank 14位's opponent-list entry moved 10位→9位, ticket 2→1, credits +1,080), with no repeat of the original failure. Follow-up #2 (2026-07-12), per explicit user request: reversed the original "exactly one battle per invocation" decision — `_fight_one` now holds the single-battle logic and `run()` loops it (`while tickets > 0 and fights < config.ARENA_MAX_FIGHTS_PER_RUN`), re-reading the OCR'd ticket count after each fight and waiting `config.ARENA_POST_BATTLE_COOLDOWN` (30s, per the user's own info about the real in-game lockout between fights) before continuing. `ArenaStopFightWhenRank1` is now re-checked before every fight in the loop, not just once. See `plan.md`'s Phase 13 follow-up #2 — not yet live-tested (spends multiple real tickets, needs the user's go-ahead first). **This DID subsequently run live via cron and surfaced a real hazard (2026-07-17)**: the log showed "battle skip already on" for all 5 fights and a clean "fought 5 battle(s) this run", but the account was left showing a real "Battle Complete" screen with a ~3-minute combat timer -- battles had run in full, not skipped -- and every task for the rest of that `daily` invocation failed to open its own screen. Two compounding bugs: (1) `_ensure_skip_on`'s "on" detection was never actually verified to reject a real "off" state (`ARENA_SKIP_ON_RGB` was only ever confirmed against a session where skip happened to already be on); (2) `_wait_for_result` had no early-exit signal and always ran its full fixed 15s budget before unconditionally declaring success -- far too short for a real battle -- and `_fight_one` discarded `_wait_for_result`'s return value entirely, so even a correctly detected failure never stopped the fight loop. Fixed by widening `RESULT_MODAL_MAX_POLLS` substantially (tolerating a real battle's full duration regardless of whether the skip-mode detection gets fully root-caused) plus a new `_back_on_challenge_list` early exit (so the normal fast case doesn't slow down), and by making `_fight_one` actually stop on a `_wait_for_result` failure. The skip-toggle's own "off" detection was not independently re-verified (would need a real ticket at a moment the account had none left) -- deliberately structured so the fix is safe even if that root cause isn't fully resolved. | -| Common Shop | `module/shop/common_shop.py`, `module/shop/shop_utils.py` | `implement`, `to_common_shop`, `get_item_position`/`ensure_choose`/`buy` (shared, see Tactical Shop row) | `ba_auto/tasks/shop_common.py`, `ba_auto/tasks/shop_utils.py` | Originally ported as fixed grid-position targets (`config.COMMON_SHOP_TARGETS`) + price-digit OCR verify, matching the reference's own external-price-list-index design as closely as this repo's missing external table allows. **Revised 2026-08-01**: fixed position turned out not to hold live -- this shop sinks sold-out items to the bottom of its grid and shifts everything after them up, so `(row, col)` drifted the moment anything sold out (see plan.md Phase 25 follow-up #2). `select_targets_by_name` now identifies items by OCR'd name instead, scanning/scrolling the full (scrollable, ~24-slot) grid and matching on name + exact price + a purchasable-button color check -- arguably *closer* to the reference's item-identity-based design intent than the original position table was, just using a local OCR read in place of the reference's external price-list index. Tactical Shop stays on the original position-based path (never showed this symptom). Purchase-confirm dialog + reward-acquired banner still handled via a single overlay-darkness probe (`config.SHOP_OVERLAY_PROBE`). **Rewritten again 2026-09-17** after a real live under-purchase (reported by the user with their own cron log pasted in: a real `daily` run bought only 4 of 8 configured targets, logging the other 4 as "not found available" even though, hours later with nothing else touched, those exact 4 sat unbought at their exact expected prices right at the top of the unscrolled grid) -- see the Status column for the full root-cause writeup; fixed three distinct bugs, not one | Done, re-verified live post-2026-08-01-rewrite (superseded by the 2026-09-17 fixes below): real run correctly bought the one target that was actually available (`中級レポート`, 125,000 credits, matching a real credit-balance delta) and correctly logged the other 7 as unavailable this cycle rather than misdiagnosing them as a catalog change. **2026-09-17 live debugging** (superpowers:systematic-debugging, root-caused from the user's own pasted `~/repo/scratchpad/ba_logs/daily.log` excerpt cross-checked against live screenshots -- see `ba_auto/tasks/shop_utils.py`'s own docstrings for the full writeup) found and fixed THREE separate real bugs, confirmed one at a time via five consecutive real `./ba_dailies.sh shop_common` runs against the account's actual daily catalog: (1) `select_targets_by_name`'s old row/col loop kept iterating with stale screenshot-implied positions after a successful buy, even though buying an item collapses its grid slot and shifts every later item forward -- fixed by re-scanning the current viewport from scratch (`_scan_current_view`) after every single buy instead of continuing the old pass; (2) even with that fix, two more confirmed-available targets were still missed, because the checkbox's own "checked" confirmation settles well before the rest of the grid's reflow/collapse animation visually finishes -- fixed with `config.SHOP_REFLOW_SETTLE_WAIT` (1.5s, not yet tuned down from a measured minimum), an explicit wait before the post-buy rescan; (3) the deepest bug: Common Shop's own tier-naming convention nests a shorter tier name inside a longer one (`上級強化珠`/Advanced is a literal substring of `最上級強化珠`/Highest, same for the レポート pair), and the original substring-only name match had no preference for an exact match, so scanning a "最上級..." card while its "上級..." sibling was still also a remaining target could resolve to the WRONG (shorter) target, compare against the wrong price, and permanently skip a genuinely-available card every run -- confirmed live: `最上級強化珠` sat available at its exact listed price across three consecutive real runs, never once bought, until fixed via `_match_name`'s exact-match-first resolution (substring fallback now prefers the longest/most-specific candidate). All 8 of today's configured targets were confirmed successfully bought across the debugging session's real runs (credit balance deltas matched each purchase's exact expected price sum throughout), and a final clean run correctly reported all 8 as sold out for the day with no further false skips. Earlier finding, still true: no visible purchase-count indicator (unlike the 青輝石 tab's "あと1回購入可能" labels). Also encountered but unrelated to this task: a stray already-open MomoTalk panel intercepted a `SHOP_ICON` click mid-session (closed manually); `navigation.is_on_subscreen`/`is_modal_open` don't recognize MomoTalk as a blocking state, same class of blind spot already documented for the old gem_shop dialog and cafe's rank-up cutscene -- not fixed here, out of scope for this incident, flagged for future attention if it recurs | -| Tactical Shop | `module/shop/tactical_challenge_shop.py`, `module/shop/shop_utils.py` | `implement`, `goto_shop_by_name`, shared `get_item_position`/`ensure_choose`/`buy` | `ba_auto/tasks/shop_tactical.py`, `ba_auto/tasks/shop_utils.py` | `goto_shop_by_name`'s OCR swipe-search over the shop-tab list → fixed click (`config.SHOP_TAB_TACTICAL`): this account's tab list is only 7 entries and fits on screen with no scroll needed, confirmed live, so there's nothing to search for — not an OCR-avoidance shortcut. Same grid-position + price-OCR-verify + overlay-probe design as Common Shop, sharing `shop_utils.run_shop_tab` | Done. Live-tested with real purchases (both configured AP-recovery drinks bought; AP and tactical-coin balance changes matched exactly) | +| Common Shop | `module/shop/common_shop.py`, `module/shop/shop_utils.py` | `implement`, `to_common_shop`, `get_item_position`/`ensure_choose`/`buy` (shared, see Tactical Shop row) | `ba_auto/tasks/shop_common.py`, `ba_auto/tasks/shop_utils.py` | Originally ported as fixed grid-position targets (`config.COMMON_SHOP_TARGETS`) + price-digit OCR verify, matching the reference's own external-price-list-index design as closely as this repo's missing external table allows. **Revised 2026-08-01**: fixed position turned out not to hold live -- this shop sinks sold-out items to the bottom of its grid and shifts everything after them up, so `(row, col)` drifted the moment anything sold out (see plan.md Phase 25 follow-up #2). `select_targets_by_name` now identifies items by OCR'd name instead, scanning/scrolling the full (scrollable, ~24-slot) grid and matching on name + exact price + a purchasable-button color check -- arguably *closer* to the reference's item-identity-based design intent than the original position table was, just using a local OCR read in place of the reference's external price-list index. Tactical Shop stays on the original position-based path (never showed this symptom). Purchase-confirm dialog + reward-acquired banner still handled via a single overlay-darkness probe (`config.SHOP_OVERLAY_PROBE`). **Rewritten again 2026-09-17** after a real live under-purchase (reported by the user with their own cron log pasted in: a real `daily` run bought only 4 of 8 configured targets, logging the other 4 as "not found available" even though, hours later with nothing else touched, those exact 4 sat unbought at their exact expected prices right at the top of the unscrolled grid) -- see the Status column for the full root-cause writeup; fixed three distinct bugs, not one | Done, re-verified live post-2026-08-01-rewrite (superseded by the 2026-09-17 fixes below): real run correctly bought the one target that was actually available (`中級レポート`, 125,000 credits, matching a real credit-balance delta) and correctly logged the other 7 as unavailable this cycle rather than misdiagnosing them as a catalog change. **2026-09-17 live debugging** (superpowers:systematic-debugging, root-caused from the user's own pasted `~/repo/scratchpad/ba_logs/daily.log` excerpt cross-checked against live screenshots -- see `ba_auto/tasks/shop_utils.py`'s own docstrings for the full writeup) found and fixed THREE separate real bugs, confirmed one at a time via five consecutive real `./ba_dailies.sh shop_common` runs against the account's actual daily catalog: (1) `select_targets_by_name`'s old row/col loop kept iterating with stale screenshot-implied positions after a successful buy, even though buying an item collapses its grid slot and shifts every later item forward -- fixed by re-scanning the current viewport from scratch (`_scan_current_view`) after every single buy instead of continuing the old pass; (2) even with that fix, two more confirmed-available targets were still missed, because the checkbox's own "checked" confirmation settles well before the rest of the grid's reflow/collapse animation visually finishes -- fixed with `config.SHOP_REFLOW_SETTLE_WAIT` (1.5s, not yet tuned down from a measured minimum), an explicit wait before the post-buy rescan; (3) the deepest bug: Common Shop's own tier-naming convention nests a shorter tier name inside a longer one (`上級強化珠`/Advanced is a literal substring of `最上級強化珠`/Highest, same for the レポート pair), and the original substring-only name match had no preference for an exact match, so scanning a "最上級..." card while its "上級..." sibling was still also a remaining target could resolve to the WRONG (shorter) target, compare against the wrong price, and permanently skip a genuinely-available card every run -- confirmed live: `最上級強化珠` sat available at its exact listed price across three consecutive real runs, never once bought, until fixed via `_match_name`'s exact-match-first resolution (substring fallback now prefers the longest/most-specific candidate). All 8 of today's configured targets were confirmed successfully bought across the debugging session's real runs (credit balance deltas matched each purchase's exact expected price sum throughout), and a final clean run correctly reported all 8 as sold out for the day with no further false skips. Earlier finding, still true: no visible purchase-count indicator (unlike the 青輝石 tab's "あと1回購入可能" labels). Also encountered but unrelated to this task: a stray already-open MomoTalk panel intercepted a `SHOP_ICON` click mid-session (closed manually); `navigation.is_on_subscreen`/`is_modal_open` don't recognize MomoTalk as a blocking state, same class of blind spot already documented for the old gem_shop dialog and cafe's rank-up cutscene -- not fixed here, out of scope for this incident, flagged for future attention if it recurs. **A FOURTH real bug found 2026-09-19**, two days after the above: the user reported the account still only showing 4 bought items on a given check. Re-investigating with the user's own cron logs (`~/ba_logs/daily.log`/`q4h.log`) found that, taken across both real `daily` cron fires each day, all 8 targets genuinely were being bought (the two fires split the 8 into complementary halves, each catching whatever the other one's timing missed) -- so the "only 4" symptom was a same-day timing snapshot, not a standing bug, EXCEPT for one real, separately-confirmed defect: `shop_utils.confirm_purchase` checked the purchase-confirmation overlay exactly once after clicking Buy and returned success as soon as that single read was "idle" -- with nothing distinguishing "confirmed and closed" from "the Buy click missed and the confirm dialog never opened at all". Caught live: a real cron run's log claimed "bought 4 item(s) for 303000 credits", but those exact 4 items were still sitting unbought at their base price hours later; re-running the task then genuinely bought them (confirmed via an exact matching credit-balance delta). Fixed by requiring the currency balance to have actually dropped by the exact expected spend once the overlay reports idle (re-read with the same `SHOP_PRICE_OCR_RETRIES` stray-misread-digit tolerance already used elsewhere in this module), returning `False` -- not a false "purchased" -- if it never does. Logically verified and syntax-checked, but **not yet live-confirmed against a real purchase specifically**: by the time the fix was written, all 8 of today's targets were already bought (from the same day's own earlier debugging), so no fresh purchasable item was available to exercise the new balance-check end-to-end; needs re-confirming the next time a genuinely fresh target is available (tomorrow's catalog reset, or Tactical Shop once its own separate price-misread bug -- see that row -- is fixed). | +| Tactical Shop | `module/shop/tactical_challenge_shop.py`, `module/shop/shop_utils.py` | `implement`, `goto_shop_by_name`, shared `get_item_position`/`ensure_choose`/`buy` | `ba_auto/tasks/shop_tactical.py`, `ba_auto/tasks/shop_utils.py` | `goto_shop_by_name`'s OCR swipe-search over the shop-tab list → fixed click (`config.SHOP_TAB_TACTICAL`): this account's tab list is only 7 entries and fits on screen with no scroll needed, confirmed live, so there's nothing to search for — not an OCR-avoidance shortcut. Same grid-position + price-OCR-verify + overlay-probe design as Common Shop, sharing `shop_utils.run_shop_tab` | Done. Live-tested with real purchases (both configured AP-recovery drinks bought; AP and tactical-coin balance changes matched exactly). **Regression noticed 2026-09-19, not yet investigated or fixed**: both configured targets (`初級栄養ドリンク(AP30)`/`中級栄養ドリンク(AP60)`) now consistently OCR-read price 50 for BOTH items (real prices 15/30), across multiple separate real runs -- always skipped as `price_mismatch`, `confirm_purchase` never reached. Same "50" for two different real prices points at either a real catalog/UI change (e.g. a display format change, or these items hitting some daily cap and showing a placeholder) rather than ordinary OCR noise, but unconfirmed -- flagged for a future live investigation, out of scope for the 2026-09-19 cafe/shop_common fixes. | | Lesson/Schedule | `module/lesson.py` | `implement`, `to_lesson_location_select`/`to_select_location`/`to_all_locations` (nav state machine), `get_lesson_region_num`/`switch_lesson_region_page`/`to_lesson_region` (paged region nav), `get_lesson_each_region_status`+`check_region_availability` (per-cell status via isometric-parallelogram pixel scan), `get_lesson_relationship_counts` (per-cell affection pip count via color count), `choose_lesson` (selection policy), `execute_lesson`/`to_location_info`/`start_lesson` (click cell -> info panel -> start -> result) | `ba_auto/tasks/lesson.py` | `picture.co_detect` -> `navigation.wait_for_state`-style bounded Enter-press loop (see below); the reference's paged-arrow region nav (needing OCR to know current position) -> this client renders the 12 regions as a scrollable list instead, which only ever settles at two scroll positions (`config.LESSON_REGION_ROW_Y`), so navigation is direct index-based clicking with nothing to OCR-locate; the reference's isometric `Parallelogram`/`Triangle` per-cell scan (tuned to the reference's own screen layout) -> reading each portrait's heart-shaped affection badge via a dedicated OCR path (`detector.read_int_on_heart_badge`) needs no isometric geometry at all | Done. Config-driven scope only in the sense of the *policy* (affection-first selection, sweep every unlocked region until tickets/lessons run out, no ticket purchasing, no favor-student targeting) -- unlike shop, no user-specific target list was needed since the reference's own `lesson_region_name.JP` (embedded directly in its `default_config.py`, not externally fetched) already names all 12 regions, used here only for logging. Live-tested for real: 5 real tickets spent across 3 regions with correct outcomes (ticket count, cleanup navigation, home-screen return all verified). Two real bugs were found and fixed from that run -- see below and `plan.md`'s Lesson phase. **Regression fix (Phase 12 follow-up)**: `LESSON_TICKET_OCR_RECT`'s left edge clipped in a stray katakana fragment next to the first digit, making tesseract drop the whole leading digit (`"7/7"` -> `"/7"`) and aborting every run outright; fixed by tightening the rect, re-validated with a full real run (7 tickets spent, correct count re-read after every schedule, clean stop and home-screen return). **Regression fix (Phase 12 follow-up #2)**: clicking the schedule icon doesn't always land on the Location Select list -- the game can resume directly on whichever region's per-region isometric map was last open (confirmed live: a previous run's Ctrl-C interruption left it stuck there, breaking `_open_region_grid` for every region in the next run identically). Fixed via a new `lesson._ensure_location_select_list` recovery check (detects the per-region map's own "すべてのスケジュール" button already showing before any row's been clicked, and returns via the back button if so); validated by deliberately reproducing the stuck state and confirming a real run recovered, spent the account's real remaining ticket, and finished cleanly. **Selection-policy rewrite (Phase 12 follow-up #3, 2026-07-13)**: per explicit user direction, replaced "always pick the single highest affection value" with a tiered min/max-farming priority -- any 3-student cell anywhere first, then any 2-student cell anywhere, then single-student cells sorted lowest-affection-first. This needs the whole board's state before deciding, not just the current region's, so the flow is now scan-all-then-execute (`_scan_all_regions`/`_build_priority_queue`/`_run_queue`) rather than the old per-region sweep-and-pick-best loop (`_find_best_cell`/`_sweep_region`, both removed). Verified offline against a synthetic board (correct tier ordering) and live against the real board via a zero-cost scan-only call (69 real schedulable cells found across 12 regions -- 7 triples, 30 doubles, 32 singles -- correctly bucketed and the singles tail exactly ascending by real affection value), with zero tickets spent since the account was fully out that day. Real ticket-spending execution (`_run_queue` actually running schedules) is not yet live-tested -- deferred to the user once tickets regenerate. | | Hard story AP sweep | `module/explore_tasks/sweep_task.py`, `module/explore_tasks/task_utils.py` | `sweep_hard_task` (main flow, 20 AP/hit, 3x cap), `to_hard_event`/`to_region` (nav), `start_sweep`'s named-outcome contract (shared with story_sweep.py's, ported the same way via `navigation.wait_for_state`) | `ba_auto/tasks/story_sweep_hard.py` | Reference's `unfinished_hard_tasks` persisted config list -> a fixed, user-supplied priority-ordered list (`config.HARD_STORY_SWEEP_TARGETS`: 18-3, 30-3, 27-3, 28-3, 17-3, 13-3, 23-2, 16-1, 20-3, 17-2, 15-2, 14-3, 10-3, 14-2, 7-1, 5-3, 4-1), swept in that fixed order every run rather than popped/persisted across runs; each target uses MAX (capped at 3x by the game itself), matching `config.STORY_SWEEP_TARGETS`'s own MAX convention. Reuses story_sweep.py's region-nav/stage-row-OCR/modal/result-watching machinery where the Hard screen's layout matches (region browser, stage list, sweep modal) rather than re-deriving it. New: a campaign-active guard with no reference equivalent at all (not present anywhere in baas-reference) -- per explicit user direction, OCRs for the pink "キャンペーン中" text (confirmed live on both the home screen and the Hard region browser) before spending any AP, skipping the whole run if no campaign is active unless an explicit override is passed | Implemented, not yet live-tested. Live investigation on nik-gpu (2026-07-20) confirmed: the Hard tab (`config.HARD_TAB`) sits atop the same region-browser screen story_sweep.py already opens, and stays selected across region navigation (no re-click needed between regions, unlike the reference's own per-move to_hard_event re-assertion); Hard's stage list always shows exactly 3 fixed rows (missions 1-3, no "-A" bonus, no scrolling -- `config.HARD_STAGE_ROWS_Y`, replacing story_sweep's scroll+OCR row search entirely); the stage-info modal is pixel-identical to story_sweep's own (all of `SWEEP_MAX_BUTTON`/`SWEEP_START_BUTTON`/`STAGE_MODAL_CLOSE_BUTTON`/confirm-dialog constants reused directly, confirmed via a real zero-AP-spent dry run: opened the confirm dialog, screenshotted it, cancelled). The in-panel "キャンペーン中" campaign banner (`config.HARD_CAMPAIGN_BADGE_RECT`) was confirmed live on two different regions (30 and 29) with identical text ("任務Hardで獲得できる報酬量が2倍(+100%)になっています"), read as account-wide rather than per-region -- chosen over the home screen's own ambiguous "キャンペーン中" badge (which can point at unrelated concurrent campaigns) as the actual gate signal. **Also confirmed live**: this modal has the exact same two-stacked-action-button hazard bounty.py hit for real (cyan 掃討開始 "start sweep" directly above gold 任務開始 "start mission", a real manual battle, both showing an identical AP-cost preview) -- ported bounty.py's own `_confirm_dialog_is_sweep` OCR text-gate proactively here rather than waiting to discover the same hazard live a second time. **Live-tested for real across three rounds (2026-07-20/21) -- see `plan.md` Phase 21 for the full writeup.** Multiple genuine Hard sweeps confirmed for real (H18-3/H30-3 both fully exhausted their daily 3/3 clears; six more targets swept for real in the final run). Five real bugs found and fixed: (1) a navigation-cascade bug where every exit path's leftover "if on subscreen, press Escape" cleanup backed out of the region browser entirely after every single target, breaking all subsequent targets -- fixed via a new `_ensure_hard_screen` self-heal check per loop iteration, replacing the removed Escape presses; (2) a false-negative MAX-click verification that wrongly aborted 11 real, affordable targets once AP dropped below 40 (MAX legitimately caps at 1x there, indistinguishable from "click failed") -- fixed by dropping the verification gate and trusting the downstream confirm-dialog checks instead; (3) a genuinely new real-money hazard found live: a stage already at 0/3 remaining today still shows a clickable 入場 button, and sweeping it raises a real "spend 40 gems to refill today's clears?" dialog -- fixed with a two-layer guard (`_remaining_sweeps_today`'s pre-check OCR on the modal's own count field, `_is_challenge_count_alert`'s OCR fallback on the dialog text, both declining via Escape, never a positional click; deliberately reproduced twice with zero gems spent, confirmed by screenshot); (4) a cosmetic `"unrecognized_state"` misreport on every real sweep, root-caused to the same terminal-state gap event_sweep.py hit historically (post-sweep flow can return to the bare region browser instead of leaving the modal open) -- fixed by porting event_sweep.py's own `clicked_any`-gated second `ends` condition; (5) a still-not-fully-root-caused region-navigation flakiness (30->27 failed identically across two runs) mitigated with a short OCR-read retry (`REGION_READ_RETRIES`), not yet independently re-confirmed live since account AP was exhausted (down to 6) by that point in the session. | diff --git a/ba_auto/tasks/cafe.py b/ba_auto/tasks/cafe.py index 05f3197..f5887f6 100644 --- a/ba_auto/tasks/cafe.py +++ b/ba_auto/tasks/cafe.py @@ -93,6 +93,9 @@ _pat_current_view poll iteration instead of only right after a pat -- see that function's own comment. """ +import os +import time + from ba_auto import detector, navigation ROOM_OPEN_RETRIES = 3 @@ -113,6 +116,28 @@ def _claim_disabled(driver): ) +def _save_diagnostic_screenshot(driver, config, label): + # Added 2026-09-19, per explicit user request, after this task's own + # "screen not confirmed (rank-up cutscene stuck?)" warning turned out to + # fire on ~90% of real scheduled runs in the logs but could not be + # reproduced on demand in several separate live debugging sessions + # (fresh game relaunch, full login->cafe sequence, the cooldown-dialog- + # dismiss path all included) -- every attempt to force it live + # succeeded cleanly instead. Since the exact interrupting state + # couldn't be captured live, the next REAL occurrence needs to leave + # hard evidence of what the screen actually looked like on its own, + # rather than relying on log text alone to diagnose after the fact. + # Written to scratchpad/ per this project's own temporary-file + # convention -- not auto-cleaned, review/delete via clean_scratchpad.sh + # once a real occurrence has been captured and inspected. + path = os.path.join(config.SCRATCHPAD_DIR, f"cafe_diag_{label}_{int(time.time())}.png") + try: + driver.read_screenshot(path) + print(f"[cafe] saved diagnostic screenshot: {path}") + except RuntimeError as exc: + print(f"[cafe] could not save diagnostic screenshot: {exc}") + + def _enter_room(driver, coords): for attempt in range(1, ROOM_OPEN_RETRIES + 1): driver.click(*coords) @@ -157,14 +182,32 @@ def _dismiss_rank_up_if_shown(driver, config, image=None): first_check_image = None driver.keypress("Return") driver.wait(1.5) - return navigation.is_header_bar_visible(driver) + confirmed = navigation.is_header_bar_visible(driver) + if not confirmed: + _save_diagnostic_screenshot(driver, config, "not_confirmed") + return confirmed def _pat_current_view(driver, config): - # Sparkles appear on a per-student cooldown, so most single checks find - # nothing -- the old Bash loop (and an earlier version of this one) gave - # up on the very first miss, which meant it essentially never farmed. - # Keep polling for the full budget instead of bailing early. + # Sparkles appear on a per-student cooldown. Original design: one + # screenshot + one sparkle-match attempt per click, giving up after a + # fixed budget of individual click attempts -- correct, but wasteful + # whenever several sparkles were visible in the same frame, since each + # still paid for its own fresh scrot + multi-scale template match. + # Redesigned 2026-09-19, per explicit user direction: one screenshot, + # find and pat EVERY sparkle visible in it (detector.find_cafe_sparkles, + # not just the single best match), then take a new screenshot and + # repeat -- stopping once config.CAFE_EMPTY_ROUNDS_TO_STOP consecutive + # rounds each find nothing, rather than polling a fixed click-attempt + # budget regardless of how many were found per frame. + # + # Patting through a whole batch from one screenshot without + # re-capturing between individual pats is safe here specifically + # because cafe sparkles sit on static room furniture/students, not a + # reflowing list -- unlike Common Shop's item grid (see shop_utils.py's + # own 2026-09-17 incident, where reusing stale positions after an + # action that could reflow the grid was NOT safe). Patting one sparkle + # doesn't move where any other already-detected sparkle is sitting. # # Real-usage bug (2026-07-16): the rank-up check used to run only once, # immediately after a pat, with no wait beforehand -- but the rank-up @@ -172,7 +215,7 @@ def _pat_current_view(driver, config): # animation delay, so that single check could catch the tail end of the # still-normal room view (header genuinely still visible in that instant) # and conclude "clear" a beat before the actual cutscene appeared. Once - # that happened, nothing ever checked again: find_cafe_sparkle() can't + # that happened, nothing ever checked again: find_cafe_sparkles() can't # tell "stuck on an undetected cutscene" apart from the ordinary # "nothing to pat right now" case, so the loop just polled dead air for # the rest of its budget, the following camera pan dragged over a static @@ -180,43 +223,42 @@ def _pat_current_view(driver, config): # return_to_home's generic cleanup failed to recover afterward -- this # exact cutscene's background happens to read under is_on_subscreen/ # is_modal_open's thresholds too, so every "are we home" check downstream - # falsely agreed nothing was wrong. Fixed by checking on EVERY poll - # iteration, not just right after a pat, so a delayed appearance is - # caught (and dismissed, via _dismiss_rank_up_if_shown's own Enter-press - # loop) on the next iteration, roughly a second later, instead of never. - # - # One capture per iteration shared between the rank-up check and the - # sparkle match (previously two separate scrot calls back-to-back with - # nothing in between): the header-bar check only samples fixed chrome - # pixels unrelated to the room scene, and the sparkle match already - # reports "wherever it is in this one frame" regardless of whether that - # frame is shared -- the room animating (a student walking around) - # doesn't change that, since neither read depends on the other lagging - # or leading it. See plan.md's "Performance improvement plan". + # falsely agreed nothing was wrong. Fixed by checking before every + # individual pat, not just once per round, so a delayed appearance is + # caught mid-batch instead of only between rounds. patted = 0 - for _ in range(config.CAFE_MAX_CLICKS_PER_ROOM): + empty_rounds = 0 + for _ in range(config.CAFE_MAX_SPARKLE_ROUNDS): image = detector.capture_screen() if not _dismiss_rank_up_if_shown(driver, config, image=image): print("[cafe] warning: cafe screen not confirmed (rank-up cutscene stuck?) -- stopping this view's pat loop rather than clicking blindly") - break - match = detector.find_cafe_sparkle(image=image) - if match is None: + return patted + + matches = detector.find_cafe_sparkles(image=image) + if not matches: + empty_rounds += 1 + if empty_rounds >= config.CAFE_EMPTY_ROUNDS_TO_STOP: + break driver.wait(1) continue - x, y, score = match - driver.click(x, y) - driver.wait(1) - driver.keypress("Return") - # park the cursor away from the sparkle area so it can't occlude the - # next detection screenshot (see screenshots/cafe/sparkle/02_*_cursor_on_head.png) - driver.move_mouse(10, 1190) - # No image= here -- the click/Return/mouse-move above are real state - # changes since this iteration's capture, so this check must be fresh. - if not _dismiss_rank_up_if_shown(driver, config): - print("[cafe] warning: cafe screen not confirmed after a pat (rank-up cutscene stuck?) -- stopping this view's pat loop rather than clicking blindly") - break - patted += 1 - print(f"[cafe] patted sparkle at ({x}, {y}), score={score:.3f}") + empty_rounds = 0 + + for x, y, score in matches: + driver.click(x, y) + driver.wait(1) + driver.keypress("Return") + # park the cursor away from the sparkle area so it can't occlude + # the next detection screenshot (see + # screenshots/cafe/sparkle/02_*_cursor_on_head.png) + driver.move_mouse(10, 1190) + # No image= here -- the click/Return/mouse-move above are real + # state changes since this batch's own capture, so this check + # must be fresh. + if not _dismiss_rank_up_if_shown(driver, config): + print("[cafe] warning: cafe screen not confirmed after a pat (rank-up cutscene stuck?) -- stopping this view's pat loop rather than clicking blindly") + return patted + patted += 1 + print(f"[cafe] patted sparkle at ({x}, {y}), score={score:.3f}") return patted diff --git a/ba_auto/tasks/shop_utils.py b/ba_auto/tasks/shop_utils.py index 640e8c2..4219840 100644 --- a/ba_auto/tasks/shop_utils.py +++ b/ba_auto/tasks/shop_utils.py @@ -257,24 +257,58 @@ def select_targets_by_name(driver, config, targets): return selected, skipped -def confirm_purchase(driver, config): +def confirm_purchase(driver, config, *, balance_rect, balance_before, expected_spend): """Click the bulk Buy button, then click through the purchase-confirm dialog and the reward-acquired banner. Both dim config.SHOP_OVERLAY_PROBE away from pure white; press Enter until it reads idle again, bounded by config.SHOP_PURCHASE_MAX_ENTER_PRESSES -- this project doesn't press keys past a bound against an unrecognized - state (see navigation.wait_for_state). Returns True once idle, False if - it never clears within the bound. + state (see navigation.wait_for_state). + + Real live false positive (2026-09-19): the overlay can already read + "idle" on the very first check -- there's nothing in a single + overlay-darkness read that distinguishes "confirmed and closed" from + "the Buy click missed and the confirm dialog never opened at all". + Confirmed live: a real cron run logged "bought 4 item(s)" for a real + credit amount, but those exact 4 items were still sitting unbought at + their base price hours later -- the overlay had simply never dimmed at + any point this function actually checked it. Fixed by also requiring + the currency balance to have actually dropped by `expected_spend` + once the overlay reports idle -- re-read with the same + stray-misread-digit retry tolerance SHOP_PRICE_OCR_RETRIES already uses + elsewhere in this module, so one bad OCR frame on the balance itself + can't sink an otherwise-real purchase. + + Returns True only once the overlay is idle AND the balance dropped by + exactly `expected_spend`; False otherwise -- whether the overlay never + cleared within the retry bound, or it cleared but the balance never + moved (or moved by some other amount), meaning nothing was really + bought despite looking "idle". """ driver.click(*config.SHOP_BUY_BUTTON) driver.wait(1.5) min_ch = config.SHOP_OVERLAY_IDLE_MIN_CHANNEL + idle = False for _ in range(config.SHOP_PURCHASE_MAX_ENTER_PRESSES): r, g, b = driver.color_at(*config.SHOP_OVERLAY_PROBE) if r > min_ch and g > min_ch and b > min_ch: - return True + idle = True + break driver.keypress("Return") driver.wait(1.5) + if not idle: + return False + + balance_after = None + for attempt in range(1, config.SHOP_PRICE_OCR_RETRIES + 1): + balance_after = detector.read_int(balance_rect) + if balance_after is not None and balance_before - balance_after == expected_spend: + return True + if attempt < config.SHOP_PRICE_OCR_RETRIES: + driver.wait(0.3) + + actual_spend = None if balance_after is None else balance_before - balance_after + print(f"[shop] purchase overlay cleared but balance changed by {actual_spend}, expected {expected_spend} -- treating as not confirmed") return False @@ -316,11 +350,11 @@ def run_shop_tab(driver, config, *, tab_button, targets, balance_rect, currency_ print("[shop] nothing selected (all targets skipped), cancelling") return "nothing_selected" - if not confirm_purchase(driver, config): - print("[shop] purchase confirmation did not resolve to an idle screen within the retry bound -- stopping, check the game manually") + spent = sum(price for _, price in selected) + if not confirm_purchase(driver, config, balance_rect=balance_rect, balance_before=balance, expected_spend=spent): + print("[shop] purchase confirmation did not resolve to a verified balance change within the retry bound -- stopping, check the game manually") return "unrecognized_state" - spent = sum(price for _, price in selected) msg = f"[shop] bought {len(selected)} item(s) for {spent} {currency_label}" if skipped: msg += f", skipped {len(skipped)}"