refactor(reservation): 拆出锁定期同预约位置/窗口复杂检查(1118→988,-130 行)

handle_lock_period_same_reservation_check:把"锁定期内 + 同一预约 + 是否在目标
位置 + 时间窗口"的 139 行复杂逻辑抽到 helper。返回 True/False/None 让主流程
精确控制后续行为:
- True:直接参与
- False:跳过回退
- None:继续走后续时间窗口检查

attend_window.py 跌破 1000 行(988)。

新增 6 个单元测试覆盖各分支组合,1296 → 1302 用例全过。

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
CodexandClaude Opus 4.7 committed 2026-05-22 04:39:39 +08:00
1 parent f46f9dc23e
commit 02e685ba20
3 files changed
+260 -147

No files matched your search

@@ -29,6 +29,7 @@ from .attend_window_helpers import (
check_waiting_for_reservation_lottery,
compute_dynamic_ahead,
discard_invalid_future_ts,
handle_lock_period_same_reservation_check,
handle_time_window_early_returns,
log_attend_window_check_details,
log_target_reservation_details,
@@ -188,153 +189,22 @@ def process_reservation_attend_window(
est_back_times=est_back_times,
)
# 优化锁定期检查:
# 1. 如果没有锁定期(focus_until = 0),直接允许检查时间窗口
# 2. 如果锁定期已过期(now_ts >= focus_until),允许检查时间窗口
# 3. 如果锁定期还没结束,但预约时间已经到了,检查是否是同一个预约
# - 如果是同一个预约,允许回退(通常已经在目标直播间了)
# - 如果不是同一个预约,不允许回退(需要等待当前预约完成)
# 4. 特殊情况:如果是队列满后立即参与模式(通过标志判断),允许检查时间窗口
# 先检查是否是队列满立即参与模式
is_queue_full_immediate_attend_check = False
try:
queue_full_waiting_check = bool(
getattr(analyser, "queue_full_waiting_at_earliest", False) or False
)
if queue_full_waiting_check:
is_queue_full_immediate_attend_check = True
except Exception:
pass
if (
focus_until > 0
and now_ts < focus_until
and abs(focus_until - now_ts) > 1.0
and not is_queue_full_immediate_attend_check
):
# 锁定期还没结束,检查是否是同一个预约(允许5秒误差)
expected_focus_until = ft + max(0.0, poll_before)
if abs(focus_until - expected_focus_until) > 5.0:
# 不是同一个预约,锁定期还没结束,不允许回退
try:
analyser._log_kv(
"预约",
"[预约] 锁定期未结束且不是当前预约,跳过回退",
focus_until=int(focus_until),
expected_focus_until=int(expected_focus_until),
target_future_ts=int(ft),
lock_period_remain_seconds=int(focus_until - now_ts),
)
except Exception:
pass
return False
# 是同一个预约,即使锁定期还没结束,如果时间窗口到了,也允许回退
# 但这里通常已经在目标直播间了,需要检查是否已经在目标位置
# 获取当前实际的 room_pos(可能已经被更新)
try:
current_room_pos_check = int(
getattr(analyser, "room_pos", room_pos) or room_pos
)
except Exception:
current_room_pos_check = int(room_pos)
# 计算位置差
try:
target_pos_check = int(target.get("pos_seen") or 0)
except Exception:
target_pos_check = 0
pos_diff_check = current_room_pos_check - target_pos_check
# 如果已经在目标位置(pos_diff == 0),说明已经在目标直播间,不需要再次回退
# 但是需要检查时间窗口是否到达,如果到达了,应该返回 True 表示需要参与
# 或者如果是队列满立即参与模式,直接返回 True
if pos_diff_check == 0:
# 检查是否是队列满立即参与模式
is_queue_full_immediate_attend_pos = False
try:
queue_full_waiting_pos = bool(
getattr(analyser, "queue_full_waiting_at_earliest", False) or False
)
if queue_full_waiting_pos:
is_queue_full_immediate_attend_pos = True
except Exception:
pass
# 检查时间窗口是否到达,或者是队列满立即参与模式
if is_queue_full_immediate_attend_pos or (
now_ts >= ft - dynamic_ahead and now_ts <= ft + max(0.0, poll_before)
):
# 时间窗口已到达或队列满立即参与模式,虽然已在目标位置,但需要参与,返回 True
try:
analyser._log_kv(
"预约",
"[预约] 锁定期未结束但是同一个预约,且已在目标位置,时间窗口已到达或队列满立即参与模式,需要参与",
focus_until=int(focus_until),
target_future_ts=int(ft),
current_room_pos=current_room_pos_check,
target_pos=target_pos_check,
now_ts=int(now_ts),
time_window_start=int(ft - dynamic_ahead),
time_window_end=int(ft + max(0.0, poll_before)),
is_queue_full_immediate_attend=is_queue_full_immediate_attend_pos,
)
except Exception:
pass
# 更新锁定期和待处理标志(如果不是队列满立即参与模式,才设置锁定期)
if not is_queue_full_immediate_attend_pos:
focus_until_new = ft + max(0.0, poll_before)
if reservation_focus_until_ts_ref:
reservation_focus_until_ts_ref[0] = focus_until_new
else:
reservation_focus_until_ts_ref.append(focus_until_new)
if reservation_after_action_pending_ref:
reservation_after_action_pending_ref[0] = True
else:
reservation_after_action_pending_ref.append(True)
try:
write_state_callback()
except Exception:
pass
return True
else:
# 时间窗口未到达,跳过
# 注意:如果已经在目标位置但时间窗口未到,不应该设置 reservation_after_action_pending
# 因为这会导致反复等待,应该清除这个标志,让主循环正常处理
try:
analyser._log_kv(
"预约",
"[预约] 锁定期未结束但是同一个预约,且已在目标位置,但时间窗口未到达,跳过回退并清除待处理标志",
focus_until=int(focus_until),
target_future_ts=int(ft),
current_room_pos=current_room_pos_check,
target_pos=target_pos_check,
now_ts=int(now_ts),
time_window_start=int(ft - dynamic_ahead),
time_window_end=int(ft + max(0.0, poll_before)),
)
except Exception:
pass
# 清除待处理标志,避免反复等待
if reservation_after_action_pending_ref:
reservation_after_action_pending_ref[0] = False
return False
# 如果不在目标位置,继续检查时间窗口
try:
analyser._log_kv(
"预约",
"[预约] 锁定期未结束但是同一个预约,继续检查时间窗口",
focus_until=int(focus_until),
target_future_ts=int(ft),
current_room_pos=current_room_pos_check,
target_pos=target_pos_check,
pos_diff=pos_diff_check,
)
except Exception:
pass
# 锁定期内:复杂的"是否同一个预约 + 是否已在目标位置 + 时间窗口"检查
lock_check_result = handle_lock_period_same_reservation_check(
analyser=analyser,
target=target,
ft=ft,
now_ts=now_ts,
focus_until=focus_until,
poll_before=poll_before,
dynamic_ahead=dynamic_ahead,
room_pos=room_pos,
reservation_focus_until_ts_ref=reservation_focus_until_ts_ref,
reservation_after_action_pending_ref=reservation_after_action_pending_ref,
write_state_callback=write_state_callback,
)
if lock_check_result is not None:
return lock_check_result
# 检查是否是队列满后立即参与的情况(使用标志判断,而不是锁定期)
is_queue_full_immediate_attend = check_queue_full_immediate_attend(analyser)
@@ -11,6 +11,146 @@ from datetime import datetime
from typing import Any, Dict, Tuple
def handle_lock_period_same_reservation_check(
analyser: Any,
target: Any,
ft: float,
now_ts: float,
focus_until: float,
poll_before: float,
dynamic_ahead: int,
room_pos: int,
reservation_focus_until_ts_ref: list,
reservation_after_action_pending_ref: list,
write_state_callback: Any,
) -> bool | None:
"""锁定期内的复杂检查:
- 不在锁定期 / 队列满立即参与模式 → 返回 None(让主流程继续)
- 锁定期内但不是当前预约 → 返回 False(拒绝回退)
- 锁定期内是当前预约 + 已在目标位置 + 在窗口内 → 返回 True(直接参与)
- 锁定期内是当前预约 + 已在目标位置 + 时间未到 → 返回 False(清除标志)
- 锁定期内是当前预约 + 不在目标位置 → 返回 None(继续走时间窗口检查)
返回 True/False 时主流程应当 return 该值;返回 None 时继续后续逻辑。
"""
is_queue_full_immediate_attend_check = check_queue_full_immediate_attend(analyser)
# 不在锁定期 / 锁定期已过 / 队列满模式 → 跳过本检查
if not (
focus_until > 0
and now_ts < focus_until
and abs(focus_until - now_ts) > 1.0
and not is_queue_full_immediate_attend_check
):
return None
# 锁定期内,检查是否是同一个预约(5 秒误差)
expected_focus_until = ft + max(0.0, poll_before)
if abs(focus_until - expected_focus_until) > 5.0:
try:
analyser._log_kv(
"预约",
"[预约] 锁定期未结束且不是当前预约,跳过回退",
focus_until=int(focus_until),
expected_focus_until=int(expected_focus_until),
target_future_ts=int(ft),
lock_period_remain_seconds=int(focus_until - now_ts),
)
except Exception:
pass
return False
# 是同一个预约,检查是否已在目标位置
try:
current_room_pos_check = int(
getattr(analyser, "room_pos", room_pos) or room_pos
)
except Exception:
current_room_pos_check = int(room_pos)
try:
target_pos_check = int(target.get("pos_seen") or 0)
except Exception:
target_pos_check = 0
pos_diff_check = current_room_pos_check - target_pos_check
# 不在目标位置 → 让主流程继续走时间窗口检查
if pos_diff_check != 0:
try:
analyser._log_kv(
"预约",
"[预约] 锁定期未结束但是同一个预约,继续检查时间窗口",
focus_until=int(focus_until),
target_future_ts=int(ft),
current_room_pos=current_room_pos_check,
target_pos=target_pos_check,
pos_diff=pos_diff_check,
)
except Exception:
pass
return None
# 已在目标位置:检查时间窗口或队列满立即参与
is_queue_full_immediate_attend_pos = check_queue_full_immediate_attend(analyser)
if is_queue_full_immediate_attend_pos or (
now_ts >= ft - dynamic_ahead and now_ts <= ft + max(0.0, poll_before)
):
try:
analyser._log_kv(
"预约",
"[预约] 锁定期未结束但是同一个预约,且已在目标位置,时间窗口已到达或队列满立即参与模式,需要参与",
focus_until=int(focus_until),
target_future_ts=int(ft),
current_room_pos=current_room_pos_check,
target_pos=target_pos_check,
now_ts=int(now_ts),
time_window_start=int(ft - dynamic_ahead),
time_window_end=int(ft + max(0.0, poll_before)),
is_queue_full_immediate_attend=is_queue_full_immediate_attend_pos,
)
except Exception:
pass
# 更新锁定期和待处理标志(队列满模式下不重设锁定期)
if not is_queue_full_immediate_attend_pos:
focus_until_new = ft + max(0.0, poll_before)
if reservation_focus_until_ts_ref:
reservation_focus_until_ts_ref[0] = focus_until_new
else:
reservation_focus_until_ts_ref.append(focus_until_new)
if reservation_after_action_pending_ref:
reservation_after_action_pending_ref[0] = True
else:
reservation_after_action_pending_ref.append(True)
try:
write_state_callback()
except Exception:
pass
return True
# 时间窗口未到达,跳过并清除待处理标志
try:
analyser._log_kv(
"预约",
"[预约] 锁定期未结束但是同一个预约,且已在目标位置,但时间窗口未到达,跳过回退并清除待处理标志",
focus_until=int(focus_until),
target_future_ts=int(ft),
current_room_pos=current_room_pos_check,
target_pos=target_pos_check,
now_ts=int(now_ts),
time_window_start=int(ft - dynamic_ahead),
time_window_end=int(ft + max(0.0, poll_before)),
)
except Exception:
pass
if reservation_after_action_pending_ref:
reservation_after_action_pending_ref[0] = False
return False
def handle_time_window_early_returns(
analyser: Any,
reservations: list,
+103
View File
@@ -495,3 +495,106 @@ def test_time_window_queue_full_skips_not_yet_check():
)
# 队列满模式下时间未到也算"在窗口内"
assert result is None
# ---- handle_lock_period_same_reservation_check ---- #
def _make_lock_check_args(**overrides):
"""构造 handle_lock_period_same_reservation_check 的默认参数。"""
a = MagicMock()
a.queue_full_waiting_at_earliest = False
a.room_pos = 5
target = {"pos_seen": 5, "future_ts": 1700000100}
base = {
"analyser": a,
"target": target,
"ft": 1700000100.0,
"now_ts": 1700000000.0,
"focus_until": 0.0,
"poll_before": 10.0,
"dynamic_ahead": 100,
"room_pos": 5,
"reservation_focus_until_ts_ref": [0.0],
"reservation_after_action_pending_ref": [False],
"write_state_callback": MagicMock(),
}
base.update(overrides)
return base
def test_lock_check_no_lock_period_returns_none():
args = _make_lock_check_args(focus_until=0.0)
assert h.handle_lock_period_same_reservation_check(**args) is None
def test_lock_check_queue_full_mode_returns_none():
a = MagicMock()
a.queue_full_waiting_at_earliest = True
args = _make_lock_check_args(
focus_until=1700000050.0, # 锁定期内
analyser=a,
)
args["analyser"].room_pos = 5
assert h.handle_lock_period_same_reservation_check(**args) is None
def test_lock_check_different_reservation_returns_false():
"""锁定期内但不是当前预约 → False"""
args = _make_lock_check_args(
focus_until=1700000050.0, # 远不是 1700000100 + 10 = 1700000110
ft=1700000100.0,
poll_before=10.0,
)
assert h.handle_lock_period_same_reservation_check(**args) is False
def test_lock_check_same_reservation_at_target_in_window_returns_true():
"""同一预约 + 已在目标位置 + 时间窗口内 → True"""
a = MagicMock()
a.queue_full_waiting_at_earliest = False
a.room_pos = 5
write_cb = MagicMock()
args = _make_lock_check_args(
analyser=a,
focus_until=1700000110.0, # = ft + poll_before = 1700000100 + 10
ft=1700000100.0,
now_ts=1700000050.0, # 在窗口内 [ft-100=1700000000, ft+10=1700000110]
write_state_callback=write_cb,
)
result = h.handle_lock_period_same_reservation_check(**args)
assert result is True
write_cb.assert_called_once()
assert args["reservation_focus_until_ts_ref"][0] == 1700000110.0
assert args["reservation_after_action_pending_ref"][0] is True
def test_lock_check_same_reservation_at_target_window_not_yet_returns_false():
"""同一预约 + 已在目标位置 + 时间窗口未到 → False(清除待处理标志)"""
a = MagicMock()
a.queue_full_waiting_at_earliest = False
a.room_pos = 5
args = _make_lock_check_args(
analyser=a,
focus_until=1700000110.0,
ft=1700000100.0,
now_ts=1699999800.0, # 在窗口前 [1700000000-100, 1700000110]
reservation_after_action_pending_ref=[True],
)
result = h.handle_lock_period_same_reservation_check(**args)
assert result is False
assert args["reservation_after_action_pending_ref"][0] is False
def test_lock_check_same_reservation_not_at_target_returns_none():
"""同一预约 + 不在目标位置 → None(让主流程继续走时间窗口检查)"""
a = MagicMock()
a.queue_full_waiting_at_earliest = False
a.room_pos = 10 # 不等于 target.pos_seen=5
args = _make_lock_check_args(
analyser=a,
focus_until=1700000110.0,
ft=1700000100.0,
room_pos=10,
)
result = h.handle_lock_period_same_reservation_check(**args)
assert result is None