refactor(reservation): 抽 process_reservation_attend_window 中"锁定期同预约检查"块到 handle_lock_period_same_reservation_check
将 148 行的"锁定期内的复杂检查"独立块抽到 helper: - 队列满立即参与模式 → 跳过此检查 - 锁定期未结束 + 不是同一预约 → return False(不允许回退) - 锁定期未结束 + 同一预约 + 已在目标位置 + 时间窗口到达 → return True(需要参与) - 锁定期未结束 + 同一预约 + 已在目标位置 + 时间窗口未到 → return False(清除待处理标志) - 锁定期未结束 + 同一预约 + 不在目标位置 → 写日志后 return None(继续检查) 返回值约定(None/True/False 三态): - True:需要参与,调用方应直接 return True - False:跳过/不允许回退,调用方应直接 return False - None:检查通过,调用方继续后续逻辑 attend_window.py: 1247 → 1121 行 (-126) attend_window_helpers.py: 329 → 496 行 attend_window.py 跌破 1200 行(1437 → 1121,本会话累计削减 316 行 = -22%) 验证: - pytest 1013 passed - compile OK Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
8d426676f6
commit
f0150cb3f4
2 files changed
+187
-146
No files matched your search
@@ -299,153 +299,27 @@ def process_reservation_attend_window(
|
||||
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
|
||||
# 优化锁定期检查(详细规则见 helper docstring)
|
||||
from .attend_window_helpers import handle_lock_period_same_reservation_check
|
||||
|
||||
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,
|
||||
target,
|
||||
ft,
|
||||
now_ts,
|
||||
focus_until,
|
||||
room_pos,
|
||||
dynamic_ahead,
|
||||
poll_before,
|
||||
reservation_focus_until_ts_ref,
|
||||
reservation_after_action_pending_ref,
|
||||
write_state_callback,
|
||||
)
|
||||
if lock_check_result is True:
|
||||
return True
|
||||
if lock_check_result is False:
|
||||
return False
|
||||
# 否则继续后续检查
|
||||
|
||||
# 检查是否是队列满后立即参与的情况(使用标志判断,而不是锁定期)
|
||||
is_queue_full_immediate_attend = False
|
||||
|
||||
@@ -105,6 +105,173 @@ def log_attend_window_detail_check(
|
||||
pass
|
||||
|
||||
|
||||
def handle_lock_period_same_reservation_check(
|
||||
analyser: Any,
|
||||
target: ReservationItem,
|
||||
ft: float,
|
||||
now_ts: float,
|
||||
focus_until: float,
|
||||
room_pos: int,
|
||||
dynamic_ahead: int,
|
||||
poll_before: float,
|
||||
reservation_focus_until_ts_ref: List[float],
|
||||
reservation_after_action_pending_ref: List[bool],
|
||||
write_state_callback: Callable[[], None],
|
||||
):
|
||||
"""优化锁定期检查:
|
||||
|
||||
1. 如果没有锁定期(focus_until = 0),直接允许检查时间窗口(返回 None)
|
||||
2. 如果锁定期已过期,允许检查时间窗口(返回 None)
|
||||
3. 如果锁定期还没结束,但预约时间已经到了,检查是否是同一个预约
|
||||
- 如果不是同一个预约 → 返回 False(不允许回退)
|
||||
- 如果是同一个预约 + 已在目标位置 + 时间窗口到达 → 返回 True(需要参与)
|
||||
- 如果是同一个预约 + 已在目标位置 + 时间窗口未到 → 返回 False(清除待处理标志)
|
||||
- 如果是同一个预约 + 不在目标位置 → 写日志后返回 None(继续检查时间窗口)
|
||||
4. 队列满立即参与模式:跳过此检查,返回 None
|
||||
|
||||
返回:
|
||||
True:需要参与,调用方应直接 return True
|
||||
False:跳过回退/不允许回退,调用方应直接 return False
|
||||
None:检查通过,调用方继续后续逻辑
|
||||
"""
|
||||
# 先检查是否是队列满立即参与模式
|
||||
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 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 = 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)
|
||||
):
|
||||
# 时间窗口已到达或队满立即参与模式:需要参与
|
||||
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 log_time_window_not_yet(
|
||||
analyser: Any,
|
||||
now_ts: float,
|
||||
|
||||
Reference in new issue
Block a user