diff --git a/douyin/fudai/douyin_fudai_finder_pkg/finder.py b/douyin/fudai/douyin_fudai_finder_pkg/finder.py index 8233a06..77b8cc3 100644 --- a/douyin/fudai/douyin_fudai_finder_pkg/finder.py +++ b/douyin/fudai/douyin_fudai_finder_pkg/finder.py @@ -72,6 +72,13 @@ def is_coupon_popup_before_fudai_click(analyser: Any, pic: Image.Image) -> bool: except Exception: window_s = 8.0 allow_back = (now_ts - last_ts) >= max(1.0, window_s) + try: + reservation_direct_attend = bool( + getattr(analyser, "_current_attending_reservation_fudai", False) + or getattr(analyser, "reservation_after_action_pending", False) + ) + except Exception: + reservation_direct_attend = False try: if douyin_screenshot_manager is not None: @@ -84,6 +91,7 @@ def is_coupon_popup_before_fudai_click(analyser: Any, pic: Image.Image) -> bool: "reason": "coupon_popup_before_fudai_click", "coupon_info": info, "allow_back": allow_back, + "reservation_direct_attend": reservation_direct_attend, }, annotate=True, background=True, @@ -94,7 +102,37 @@ def is_coupon_popup_before_fudai_click(analyser: Any, pic: Image.Image) -> bool: close_sent = False skipped_reason = None - if allow_back: + if reservation_direct_attend: + try: + operation = getattr(analyser, "operation", None) + click = getattr(operation, "click", None) + delay = getattr(operation, "delay", None) + if callable(click): + try: + rx = int(getattr(analyser, "resolution_ratio_x", 1080) or 1080) + ry = int(getattr(analyser, "resolution_ratio_y", 2400) or 2400) + except Exception: + rx, ry = 1080, 2400 + click( + getattr(analyser, "device_id", ""), + int(540 * rx // 1080), + int(2006 * ry // 2400), + ) + close_sent = True + if callable(delay): + delay(0.2) + skipped_reason = "reservation_direct_attend_coupon_click_close" + except Exception as close_error: + try: + analyser._log_kv( + "[福袋查找] 优惠券关闭异常", + "预约直参期间优惠券预检命中后点击关闭失败", + reason="coupon_popup_before_fudai_click", + error=str(close_error), + ) + except Exception: + pass + elif allow_back: try: operation = getattr(analyser, "operation", None) click_back = getattr(operation, "click_back", None) @@ -133,6 +171,7 @@ def is_coupon_popup_before_fudai_click(analyser: Any, pic: Image.Image) -> bool: coupon_info=info, close_sent=close_sent, skipped_reason=skipped_reason, + reservation_direct_attend=reservation_direct_attend, since_last_back_seconds=round(now_ts - last_ts, 2) if last_ts > 0 else None, back_window_seconds=window_s, ) diff --git a/douyin/reservation/douyin_reservation_handler_pkg/direct_attend.py b/douyin/reservation/douyin_reservation_handler_pkg/direct_attend.py index 86ce6df..c96d114 100644 --- a/douyin/reservation/douyin_reservation_handler_pkg/direct_attend.py +++ b/douyin/reservation/douyin_reservation_handler_pkg/direct_attend.py @@ -203,15 +203,6 @@ def _reset_direct_attend_failure_count(analyser: Any, item: Dict[str, Any]) -> N pass -def _coupon_guard_closed_during_direct_attend(analyser: Any, since_ts: float) -> bool: - """Return True if fudai finder just closed a coupon popup during direct attend.""" - try: - last_ts = float(getattr(analyser, "_last_coupon_guard_back_ts", 0.0) or 0.0) - except Exception: - return False - return last_ts > 0 and last_ts >= float(since_ts or 0.0) - - def _write_reservations_state_if_possible(analyser: Any, reason: str) -> None: try: state_path = getattr(analyser, "_reservations_state_path", "") or path_config.get_reservations_path() @@ -669,27 +660,6 @@ def handle_reservation_direct_attend(analyser: Any) -> None: pass if not opened: - if _coupon_guard_closed_during_direct_attend(analyser, open_start_ts): - _log_direct_attend_marker( - analyser, - "recoverable", - reason="coupon_popup_closed_before_fudai_click", - future_ts=int(future_ts), - remain_seconds=( - int(max(0, future_ts - time.time())) if future_ts > 0 else 0 - ), - ) - try: - analyser._log_kv( - "预约", - "[预约] 预约直参期间关闭了优惠券弹窗,保留预约并等待下一轮稳定重试", - reason="coupon_popup_closed_before_fudai_click", - future_ts=int(future_ts), - ) - except Exception: - pass - _clear_direct_attend_flags(analyser) - return _log_direct_attend_marker(analyser, "fallback", reason="fudai_icon_not_found") failure_count = _record_direct_attend_failure( analyser, diff --git a/tests/test_fudai_finder_pkg.py b/tests/test_fudai_finder_pkg.py index 25858ea..07011e9 100644 --- a/tests/test_fudai_finder_pkg.py +++ b/tests/test_fudai_finder_pkg.py @@ -65,6 +65,7 @@ class FakeAnalyser: # FudaiClickExecutor 可能用 operation class _Op: def __init__(self): + self.click = MagicMock(return_value=None) self.click_back = MagicMock(return_value=None) self.delay = MagicMock(return_value=None) @@ -428,6 +429,29 @@ class TestFudaiDetector: assert d.detect_by_icon_fallback_template(Image.open(sample).convert("RGB")) is None +def test_coupon_guard_uses_click_close_during_reservation_direct_attend(): + from douyin.fudai.douyin_fudai_finder_pkg.finder import ( + is_coupon_popup_before_fudai_click, + ) + + analyser = FakeAnalyser() + analyser._current_attending_reservation_fudai = True + pic = Image.new("RGB", (1080, 2400), "white") + + with patch( + "douyin.detection.pixel.popups.detect_coupon_popup_background", + return_value=(True, {"method": "mock_coupon"}), + ): + detected = is_coupon_popup_before_fudai_click(analyser, pic) + + assert detected is True + analyser.operation.click_back.assert_not_called() + analyser.operation.click.assert_called_once_with("test_device", 540, 2006) + assert analyser._log_kv.call_args.kwargs["skipped_reason"] == ( + "reservation_direct_attend_coupon_click_close" + ) + + # ============================================================ # FudaiClickExecutor # ============================================================ diff --git a/tests/test_reservation_direct_attend_markers.py b/tests/test_reservation_direct_attend_markers.py index 391a1d8..f294de2 100644 --- a/tests/test_reservation_direct_attend_markers.py +++ b/tests/test_reservation_direct_attend_markers.py @@ -229,33 +229,6 @@ def test_direct_attend_icon_not_found_does_not_enter_detail_handler(): assert marker_logs -def test_direct_attend_keeps_reservation_after_coupon_guard_close(): - from douyin.reservation.douyin_reservation_handler_pkg.direct_attend import ( - handle_reservation_direct_attend, - ) - - analyser = _mk_analyser() - analyser._current_attending_reservation_fudai = True - analyser.reservation_direct_attend_max_failures = 1 - - def _finder(_analyser, *_args, **_kwargs): - _analyser._last_coupon_guard_back_ts = time.time() - return False - - with patch( - "douyin.core.douyin_flow_control.find_and_open_fudai_detail", - side_effect=_finder, - ): - handle_reservation_direct_attend(analyser) - - analyser._handle_in_fudai_detail.assert_not_called() - assert analyser.reservation_after_action_pending is False - assert analyser._current_attending_reservation_fudai is False - assert analyser.reservations - assert not hasattr(analyser, "_reservation_direct_attend_failures") - assert _has_log_reason(analyser, "coupon_popup_closed_before_fudai_click") - - def test_direct_attend_empty_reservations_clears_flags_without_detail_handler(): from douyin.reservation.douyin_reservation_handler_pkg.direct_attend import ( handle_reservation_direct_attend,