diff --git a/douyin/reservation/reservation_logic/attend_window.py b/douyin/reservation/reservation_logic/attend_window.py index c4adaca..896f07a 100644 --- a/douyin/reservation/reservation_logic/attend_window.py +++ b/douyin/reservation/reservation_logic/attend_window.py @@ -23,8 +23,10 @@ from .helpers import ( _sort_reservations_inplace, ) from .attend_window_helpers import ( + check_waiting_for_reservation_lottery, compute_dynamic_ahead, log_attend_window_check_details, + maybe_promote_locked_reservation, ) @@ -77,35 +79,8 @@ def process_reservation_attend_window( pass # 检查是否正在等待开奖且当前参与的是预约福袋,如果是则不抢占 - try: - waiting_for_lottery = bool( - getattr(analyser, "_waiting_for_lottery_result", False) - ) - current_attending_reservation = bool( - getattr(analyser, "_current_attending_reservation_fudai", False) - ) - if waiting_for_lottery and current_attending_reservation: - try: - analyser._log_kv( - "预约", - "[预约] 正在等待预约福袋开奖,不抢占其他预约", - waiting_for_lottery=waiting_for_lottery, - current_attending_reservation=current_attending_reservation, - ) - except Exception: - pass - return False - except Exception as e: - if _RESERVATION_ERROR_UTILS_AVAILABLE: - log_reservation_error( - analyser, - e, - format_reservation_context(reservations=reservations), - level="WARNING", - stage="检查是否正在等待预约福袋开奖", - ) - else: - pass + if check_waiting_for_reservation_lottery(analyser): + return False if not reservations: try: @@ -199,57 +174,16 @@ def process_reservation_attend_window( except Exception: pass - # 锁定期内:优先处理“锁定的预约”,避免被队头其它预约打断。 - # 同时,如果锁定期对应的预约已不在队列(例如刚参与成功并移除),视为“锁定失效”,清除锁定期。 - if focus_until > 0 and now_ts < focus_until: - locked_idx = None - try: - for i, item in enumerate(reservations): - try: - item_ft = float(item.get("future_ts") or 0.0) - except Exception: - continue - if item_ft <= 0: - continue - expected_focus_until = item_ft + max(0.0, poll_before) - if abs(focus_until - expected_focus_until) <= 5.0: - locked_idx = i - break - except Exception: - locked_idx = None - - if locked_idx is None: - try: - analyser._log_kv( - "预约", - "[预约] 检测到锁定期已失效(锁定预约不在队列),清除锁定期", - focus_until=int(focus_until), - lock_period_remain_seconds=int(focus_until - now_ts), - reservations_count=len(reservations), - ) - except Exception: - pass - try: - if reservation_focus_until_ts_ref: - reservation_focus_until_ts_ref[0] = 0.0 - else: - reservation_focus_until_ts_ref.append(0.0) - except Exception: - pass - focus_until = 0.0 - elif locked_idx != 0: - try: - locked_item = reservations.pop(locked_idx) - reservations.insert(0, locked_item) - analyser._log_kv( - "预约", - "[预约] 锁定期内固定处理锁定预约(忽略队头其他预约)", - focus_until=int(focus_until), - locked_future_ts=int(float(locked_item.get("future_ts") or 0.0)), - moved_from_index=int(locked_idx), - ) - except Exception: - pass + # 锁定期内:优先处理"锁定的预约",避免被队头其它预约打断。 + # 同时,如果锁定期对应的预约已不在队列(例如刚参与成功并移除),视为"锁定失效",清除锁定期。 + focus_until = maybe_promote_locked_reservation( + analyser, + reservations, + reservation_focus_until_ts_ref, + focus_until, + now_ts, + poll_before, + ) # 超时检查 try: diff --git a/douyin/reservation/reservation_logic/attend_window_helpers.py b/douyin/reservation/reservation_logic/attend_window_helpers.py index 31b120f..cbe4f57 100644 --- a/douyin/reservation/reservation_logic/attend_window_helpers.py +++ b/douyin/reservation/reservation_logic/attend_window_helpers.py @@ -10,6 +10,103 @@ from datetime import datetime from typing import Any, Dict, Tuple +def check_waiting_for_reservation_lottery(analyser: Any) -> bool: + """如果正在等待预约福袋开奖,不抢占其他预约。 + + 返回 True 表示主流程应当 return False(即跳过抢占)。 + """ + try: + waiting_for_lottery = bool( + getattr(analyser, "_waiting_for_lottery_result", False) + ) + current_attending_reservation = bool( + getattr(analyser, "_current_attending_reservation_fudai", False) + ) + if waiting_for_lottery and current_attending_reservation: + try: + analyser._log_kv( + "预约", + "[预约] 正在等待预约福袋开奖,不抢占其他预约", + waiting_for_lottery=waiting_for_lottery, + current_attending_reservation=current_attending_reservation, + ) + except Exception: + pass + return True + except Exception: + pass + return False + + +def maybe_promote_locked_reservation( + analyser: Any, + reservations: list, + reservation_focus_until_ts_ref: list, + focus_until: float, + now_ts: float, + poll_before: float, +) -> float: + """锁定期内:优先处理"锁定的预约",必要时把锁定目标移到队头; + 若锁定的预约已不在队列,则清除锁定期。 + + 返回更新后的 focus_until(被清除时为 0.0)。 + """ + if not (focus_until > 0 and now_ts < focus_until): + return focus_until + + locked_idx = None + try: + for i, item in enumerate(reservations): + try: + item_ft = float(item.get("future_ts") or 0.0) + except Exception: + continue + if item_ft <= 0: + continue + expected_focus_until = item_ft + max(0.0, poll_before) + if abs(focus_until - expected_focus_until) <= 5.0: + locked_idx = i + break + except Exception: + locked_idx = None + + if locked_idx is None: + try: + analyser._log_kv( + "预约", + "[预约] 检测到锁定期已失效(锁定预约不在队列),清除锁定期", + focus_until=int(focus_until), + lock_period_remain_seconds=int(focus_until - now_ts), + reservations_count=len(reservations), + ) + except Exception: + pass + try: + if reservation_focus_until_ts_ref: + reservation_focus_until_ts_ref[0] = 0.0 + else: + reservation_focus_until_ts_ref.append(0.0) + except Exception: + pass + return 0.0 + + if locked_idx != 0: + try: + locked_item = reservations.pop(locked_idx) + reservations.insert(0, locked_item) + analyser._log_kv( + "预约", + "[预约] 锁定期内固定处理锁定预约(忽略队头其他预约)", + focus_until=int(focus_until), + locked_future_ts=int(float(locked_item.get("future_ts") or 0.0)), + moved_from_index=int(locked_idx), + ) + except Exception: + pass + + return focus_until + + def compute_dynamic_ahead( analyser: Any, target: Dict[str, Any], diff --git a/tests/test_attend_window_helpers.py b/tests/test_attend_window_helpers.py index 64d5dd1..566a2ef 100644 --- a/tests/test_attend_window_helpers.py +++ b/tests/test_attend_window_helpers.py @@ -149,3 +149,98 @@ def test_log_attend_window_check_details_swallows_exceptions(): bad, now_ts=0, focus_until=0, ft=0, dynamic_ahead=0, join_ahead=0.0, poll_before=0.0, est_back_times=0, ) + + +# ---- check_waiting_for_reservation_lottery ---- # + +def test_waiting_lottery_with_reservation_returns_true(): + a = MagicMock() + a._waiting_for_lottery_result = True + a._current_attending_reservation_fudai = True + assert h.check_waiting_for_reservation_lottery(a) is True + + +def test_waiting_lottery_only_one_flag_returns_false(): + a = MagicMock() + a._waiting_for_lottery_result = True + a._current_attending_reservation_fudai = False + assert h.check_waiting_for_reservation_lottery(a) is False + + a._waiting_for_lottery_result = False + a._current_attending_reservation_fudai = True + assert h.check_waiting_for_reservation_lottery(a) is False + + +def test_waiting_lottery_neither_flag_returns_false(): + a = MagicMock() + a._waiting_for_lottery_result = False + a._current_attending_reservation_fudai = False + assert h.check_waiting_for_reservation_lottery(a) is False + + +# ---- maybe_promote_locked_reservation ---- # + +def test_promote_locked_no_lock_period_returns_unchanged(): + a = MagicMock() + reservations = [{"future_ts": 100}] + ref = [0.0] + result = h.maybe_promote_locked_reservation( + a, reservations, ref, focus_until=0.0, now_ts=200.0, poll_before=10.0 + ) + assert result == 0.0 + + +def test_promote_locked_expired_returns_unchanged(): + """now_ts >= focus_until → 锁定期已过,不修改""" + a = MagicMock() + reservations = [{"future_ts": 100}] + ref = [50.0] + result = h.maybe_promote_locked_reservation( + a, reservations, ref, focus_until=50.0, now_ts=100.0, poll_before=10.0 + ) + assert result == 50.0 + + +def test_promote_locked_target_not_in_queue_clears_focus(): + """锁定期对应的预约已不在队列 → 清除锁定期""" + a = MagicMock() + reservations = [{"future_ts": 200}, {"future_ts": 300}] + # focus_until = 1000,但队列里没有 ft+10 = 1000 的预约 + ref = [1000.0] + now = 500.0 + result = h.maybe_promote_locked_reservation( + a, reservations, ref, focus_until=1000.0, now_ts=now, poll_before=10.0 + ) + assert result == 0.0 + assert ref[0] == 0.0 + + +def test_promote_locked_already_at_head_no_swap(): + """锁定预约已在队头 → 不调整顺序""" + a = MagicMock() + # focus_until = 200 + 10 = 210,正好是第一个预约的"预期 focus" + reservations = [{"future_ts": 200}, {"future_ts": 300}] + ref = [210.0] + result = h.maybe_promote_locked_reservation( + a, reservations, ref, focus_until=210.0, now_ts=100.0, poll_before=10.0 + ) + assert result == 210.0 + # 顺序未变 + assert reservations[0]["future_ts"] == 200 + + +def test_promote_locked_swaps_to_head(): + """锁定预约不在队头 → 移到队头""" + a = MagicMock() + # focus_until = 300 + 10 = 310,对应队列中第二个预约 + reservations = [ + {"future_ts": 200}, + {"future_ts": 300}, + ] + ref = [310.0] + result = h.maybe_promote_locked_reservation( + a, reservations, ref, focus_until=310.0, now_ts=100.0, poll_before=10.0 + ) + assert result == 310.0 + # 锁定预约(300)已被移到队头 + assert reservations[0]["future_ts"] == 300