Guard fudai retry and reservation seek verification
This commit is contained in:
1 parent
a06218dd16
commit
48b0a32e24
8 files changed
+264
-14
No files matched your search
@@ -57,7 +57,15 @@ def _detail_probe_after_click(analyser: Any) -> dict[str, Any]:
|
||||
}
|
||||
|
||||
|
||||
def _close_wrong_fudai_target(analyser: Any, device_id: str, *, stage: str, reason: str) -> None:
|
||||
def _close_wrong_fudai_target(
|
||||
analyser: Any,
|
||||
device_id: str,
|
||||
*,
|
||||
stage: str,
|
||||
reason: str,
|
||||
click_x: int | None = None,
|
||||
click_y: int | None = None,
|
||||
) -> None:
|
||||
"""Close a non-detail page opened by a risky fudai candidate click."""
|
||||
try:
|
||||
analyser._log_kv(
|
||||
@@ -89,6 +97,29 @@ def _close_wrong_fudai_target(analyser: Any, device_id: str, *, stage: str, reas
|
||||
analyser.operation.delay(0.7)
|
||||
except Exception:
|
||||
time.sleep(0.7)
|
||||
try:
|
||||
setattr(
|
||||
analyser,
|
||||
"_allow_fudai_click_after_wrong_target_until_ts",
|
||||
time.time() + 3.0,
|
||||
)
|
||||
if click_x is not None:
|
||||
setattr(analyser, "_last_wrong_fudai_click_x", int(click_x))
|
||||
if click_y is not None:
|
||||
setattr(analyser, "_last_wrong_fudai_click_y", int(click_y))
|
||||
analyser._log_kv(
|
||||
"流程",
|
||||
"find_and_open_fudai_detail:误入口已关闭,允许同房重试绕过一次优惠券预检",
|
||||
click_x=click_x,
|
||||
click_y=click_y,
|
||||
decision="allow_retry_after_wrong_target",
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
try:
|
||||
analyser.operation.get_screenshot(device_id, use_service=False)
|
||||
except Exception:
|
||||
pass
|
||||
for attr, value in (
|
||||
("last_fudai_x", 0),
|
||||
("_last_fudai_confirm_failure", None),
|
||||
@@ -660,6 +691,8 @@ def find_and_open_fudai_detail(
|
||||
device_id,
|
||||
stage=str(stage or ""),
|
||||
reason=str(reason or ""),
|
||||
click_x=click_x,
|
||||
click_y=click_y,
|
||||
)
|
||||
try:
|
||||
setattr(analyser, "_fudai_wrong_target_retry_depth", retry_depth + 1)
|
||||
|
||||
@@ -50,8 +50,32 @@ def check_have_fudai_with_ready_screenshot_optimized(
|
||||
if pic is None:
|
||||
return 0
|
||||
|
||||
if is_coupon_popup_before_fudai_click(analyser, pic):
|
||||
return 0
|
||||
try:
|
||||
bypass_coupon_guard_until = float(
|
||||
getattr(
|
||||
analyser,
|
||||
"_allow_fudai_click_after_wrong_target_until_ts",
|
||||
0.0,
|
||||
)
|
||||
or 0.0
|
||||
)
|
||||
except Exception:
|
||||
bypass_coupon_guard_until = 0.0
|
||||
bypass_coupon_guard = bypass_coupon_guard_until > time.time()
|
||||
if not bypass_coupon_guard:
|
||||
if is_coupon_popup_before_fudai_click(analyser, pic):
|
||||
return 0
|
||||
else:
|
||||
try:
|
||||
analyser._log_kv(
|
||||
"福袋查找",
|
||||
"错误入口关闭后的同房重试:短窗口跳过优惠券预检",
|
||||
bypass_until=int(bypass_coupon_guard_until),
|
||||
decision="wrong_target_retry_bypass_coupon_guard",
|
||||
level="DEBUG",
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
detector = FudaiDetector(analyser)
|
||||
detected_x, _detect_method = detector.detect(pic)
|
||||
|
||||
@@ -397,6 +397,9 @@ class SwipeHandler:
|
||||
except Exception:
|
||||
analyser.room_pos = 1
|
||||
try:
|
||||
analyser._room_pos_untrusted = True
|
||||
analyser._room_pos_untrusted_reason = str(reason or "")
|
||||
analyser._room_pos_untrusted_ts = now_ts
|
||||
analyser._swipe_times = int(getattr(analyser, "_swipe_times", 0) or 0) + 1
|
||||
analyser._same_room_escape_last_ts = now_ts
|
||||
analyser._just_switched_room = True
|
||||
@@ -415,6 +418,7 @@ class SwipeHandler:
|
||||
"回顶后仍同房兜底完成:已返回并切到下一个直播间",
|
||||
reason=reason,
|
||||
new_room_pos=int(getattr(analyser, "room_pos", 0) or 0),
|
||||
room_pos_untrusted=True,
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
@@ -173,6 +173,20 @@ def handle_reservation_direct_attend(analyser: Any) -> None:
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
try:
|
||||
analyser.reservation_after_action_pending = False
|
||||
analyser._current_attending_reservation_fudai = False
|
||||
analyser.reservation_focus_until_ts = 0.0
|
||||
except Exception:
|
||||
pass
|
||||
try:
|
||||
analyser._log_kv( # type: ignore[attr-defined]
|
||||
"预约",
|
||||
"[预约] 预约已过期,已清除预约直参状态,返回正常找福袋流程",
|
||||
reason="reservation_expired_clear_direct_attend_flags",
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
return
|
||||
|
||||
|
||||
@@ -314,6 +314,38 @@ def _handle_verify_failure(
|
||||
)
|
||||
|
||||
if ok2:
|
||||
verify_ok2, verify_detail2 = _verify_after_second_seek(
|
||||
analyser, updated_target
|
||||
)
|
||||
if not verify_ok2:
|
||||
_clear_back_focus_refs(
|
||||
reservation_focus_until_ts_ref,
|
||||
reservation_after_action_pending_ref,
|
||||
)
|
||||
try:
|
||||
analyser._log_kv(
|
||||
"预约",
|
||||
"[预约] 第二次回退后校验失败:不设置预约直参状态",
|
||||
target_pos=int(updated_target.get("pos_seen", 0)),
|
||||
target_room_title=(
|
||||
target_room_title[:50] if target_room_title else ""
|
||||
),
|
||||
verify_detail_preview=(
|
||||
str(verify_detail2)[:300]
|
||||
if verify_detail2 is not None
|
||||
else ""
|
||||
),
|
||||
reason="second_seek_verify_failed",
|
||||
level="WARNING",
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
try:
|
||||
write_state_callback()
|
||||
except Exception:
|
||||
pass
|
||||
return False
|
||||
|
||||
# 第二次回退成功,设置锁定期并标记待处理
|
||||
focus_until_new = ft + max(0.0, poll_before)
|
||||
if reservation_focus_until_ts_ref:
|
||||
@@ -347,16 +379,10 @@ def _handle_verify_failure(
|
||||
return True
|
||||
else:
|
||||
# 第二次回退也失败,清理锁定相关标志
|
||||
try:
|
||||
if reservation_focus_until_ts_ref:
|
||||
reservation_focus_until_ts_ref[0] = 0.0
|
||||
except Exception:
|
||||
pass
|
||||
try:
|
||||
if reservation_after_action_pending_ref:
|
||||
reservation_after_action_pending_ref[0] = False
|
||||
except Exception:
|
||||
pass
|
||||
_clear_back_focus_refs(
|
||||
reservation_focus_until_ts_ref,
|
||||
reservation_after_action_pending_ref,
|
||||
)
|
||||
|
||||
try:
|
||||
analyser._log_kv(
|
||||
@@ -381,6 +407,52 @@ def _handle_verify_failure(
|
||||
return False
|
||||
|
||||
|
||||
def _verify_after_second_seek(
|
||||
analyser: Any,
|
||||
updated_target: ReservationItem,
|
||||
) -> tuple[bool, Any]:
|
||||
"""二次回退也必须复核,避免滑到底或同房时把发送过滑动误判为成功。"""
|
||||
try:
|
||||
import douyin.reservation.douyin_reservation_verify as douyin_reservation_verify
|
||||
|
||||
return douyin_reservation_verify.validate_back_to_reservation(
|
||||
analyser=analyser,
|
||||
item=updated_target,
|
||||
quick=True,
|
||||
max_checks=1,
|
||||
)
|
||||
except Exception as e:
|
||||
try:
|
||||
analyser._log_kv(
|
||||
"预约",
|
||||
"[预约] 第二次回退后校验异常:不设置预约直参状态",
|
||||
target_pos=int(updated_target.get("pos_seen", 0)),
|
||||
error=str(e),
|
||||
error_type=type(e).__name__,
|
||||
reason="second_seek_verify_exception",
|
||||
level="WARNING",
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
return False, {"error": str(e), "error_type": type(e).__name__}
|
||||
|
||||
|
||||
def _clear_back_focus_refs(
|
||||
reservation_focus_until_ts_ref: List[float],
|
||||
reservation_after_action_pending_ref: List[bool],
|
||||
) -> None:
|
||||
try:
|
||||
if reservation_focus_until_ts_ref:
|
||||
reservation_focus_until_ts_ref[0] = 0.0
|
||||
except Exception:
|
||||
pass
|
||||
try:
|
||||
if reservation_after_action_pending_ref:
|
||||
reservation_after_action_pending_ref[0] = False
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
|
||||
def _mark_seek_uncertain(
|
||||
analyser: Any,
|
||||
target: ReservationItem,
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from unittest.mock import MagicMock
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
|
||||
def _mk_analyser():
|
||||
@@ -82,3 +82,50 @@ def test_verify_failure_marks_room_pos_uncertain_before_retry():
|
||||
assert analyser._reservation_seek_failed_target_pos == 2
|
||||
assert analyser._reservation_seek_expected_title == "麦小登18"
|
||||
assert analyser._reservation_seek_current_title == "料音城ATK官抖音旗舰"
|
||||
|
||||
|
||||
def test_second_seek_must_verify_before_setting_direct_attend():
|
||||
from douyin.reservation.reservation_logic import attend_window_verify as mod
|
||||
|
||||
analyser = _mk_analyser()
|
||||
target = {
|
||||
"pos_seen": 2,
|
||||
"room_title": "麦小登18",
|
||||
"room_title_raw": "麦小登-18.",
|
||||
"_seek_start_room_pos": 7,
|
||||
"_seek_direction_hint": "down",
|
||||
}
|
||||
focus_ref = [0.0]
|
||||
pending_ref = [False]
|
||||
write_state = MagicMock(return_value=None)
|
||||
seek_callback = MagicMock(return_value=True)
|
||||
|
||||
with patch.object(mod, "_RESERVATION_ERROR_UTILS_AVAILABLE", False), patch.object(
|
||||
mod,
|
||||
"_remap_target_via_room_titles",
|
||||
return_value=(dict(target), 2, 7, 7),
|
||||
), patch(
|
||||
"douyin.reservation.douyin_reservation_verify.validate_back_to_reservation",
|
||||
return_value=(False, {"reason": "same_room_after_second_seek"}),
|
||||
) as verify:
|
||||
ok = mod._handle_verify_failure(
|
||||
analyser=analyser,
|
||||
target=target,
|
||||
reservations=[target],
|
||||
target_room_title="麦小登18",
|
||||
target_room_title_raw="麦小登-18.",
|
||||
ft=9999999999.0,
|
||||
poll_before=10.0,
|
||||
reservation_focus_until_ts_ref=focus_ref,
|
||||
reservation_after_action_pending_ref=pending_ref,
|
||||
seek_reservation_room_callback=seek_callback,
|
||||
write_state_callback=write_state,
|
||||
verify_detail=None,
|
||||
)
|
||||
|
||||
assert ok is False
|
||||
seek_callback.assert_called_once()
|
||||
verify.assert_called_once()
|
||||
assert pending_ref[0] is False
|
||||
assert focus_ref[0] == 0.0
|
||||
write_state.assert_called_once()
|
||||
@@ -530,6 +530,27 @@ class TestCheckHaveFudaiWithReadyScreenshotOptimized:
|
||||
analyser.operation.click_back.assert_called_once_with(analyser.device_id)
|
||||
detector_cls.assert_not_called()
|
||||
|
||||
def test_wrong_target_retry_bypasses_coupon_popup_guard_once(self):
|
||||
analyser = FakeAnalyser()
|
||||
analyser._allow_fudai_click_after_wrong_target_until_ts = time.time() + 5.0
|
||||
pic = Image.new("RGB", (1080, 2400), (20, 20, 20))
|
||||
|
||||
with patch(
|
||||
"douyin.fudai.douyin_fudai_finder_pkg.optimized_entry.FudaiImageAcquirer"
|
||||
) as acquirer_cls, patch(
|
||||
"douyin.fudai.douyin_fudai_finder_pkg.optimized_entry.FudaiDetector"
|
||||
) as detector_cls, patch(
|
||||
"douyin.fudai.douyin_fudai_finder_pkg.optimized_entry.is_coupon_popup_before_fudai_click",
|
||||
return_value=True,
|
||||
) as guard:
|
||||
acquirer_cls.return_value.acquire_screenshot.return_value = pic
|
||||
detector_cls.return_value.detect.return_value = (132, "unit")
|
||||
result = check_have_fudai_with_ready_screenshot_optimized(analyser)
|
||||
|
||||
assert result == 132
|
||||
guard.assert_not_called()
|
||||
detector_cls.assert_called_once()
|
||||
|
||||
|
||||
# ============================================================
|
||||
# detect_by_template 已拆出为 module-level 函数
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import time
|
||||
from types import SimpleNamespace
|
||||
from unittest.mock import MagicMock
|
||||
from unittest.mock import patch
|
||||
@@ -130,3 +131,37 @@ def test_direct_attend_empty_reservations_clears_flags_without_detail_handler():
|
||||
if len(call.args) >= 2 and str(call.args[1]) == "预约回退 direct_attend fallback"
|
||||
]
|
||||
assert marker_logs
|
||||
|
||||
|
||||
def test_direct_attend_expired_reservation_clears_flags_without_detail_handler():
|
||||
from douyin.reservation.douyin_reservation_handler_pkg.direct_attend import (
|
||||
handle_reservation_direct_attend,
|
||||
)
|
||||
|
||||
analyser = _mk_analyser()
|
||||
analyser.reservations = [
|
||||
{
|
||||
"future_ts": time.time() - 120.0,
|
||||
"prize_value_yuan": 1000.0,
|
||||
"content": "expired",
|
||||
}
|
||||
]
|
||||
analyser._current_attending_reservation_fudai = True
|
||||
analyser.reservation_focus_until_ts = time.time() + 60.0
|
||||
|
||||
with patch(
|
||||
"douyin.reservation.douyin_reservation_handler_pkg.direct_attend.douyin_reservation_logic.cleanup_expired_reservations"
|
||||
) as cleanup:
|
||||
handle_reservation_direct_attend(analyser)
|
||||
|
||||
analyser._handle_in_fudai_detail.assert_not_called()
|
||||
cleanup.assert_called_once()
|
||||
assert analyser.reservation_after_action_pending is False
|
||||
assert analyser._current_attending_reservation_fudai is False
|
||||
assert analyser.reservation_focus_until_ts == 0.0
|
||||
|
||||
marker_logs = [
|
||||
call for call in analyser._log_kv.call_args_list
|
||||
if len(call.args) >= 2 and str(call.args[1]) == "预约回退 direct_attend 失败"
|
||||
]
|
||||
assert marker_logs
|
||||
Reference in new issue
Block a user