refactor(reservation): 抽 process_reservation_attend_window 中"成功回退后设置锁定期"块到 setup_lock_period_and_log_success

将 84 行的"成功回退后处理"块抽到 helper:
- focus_until_ts = ft + poll_before(设置锁定期)
- reservation_after_action_pending = True
- 写"已回退到目标直播间"运行日志和"回退成功"行为日志
- write_state_callback
- 写"已触发回退"完成日志

attend_window.py: 637 → 574 行 (-63)
attend_window_helpers.py: 699 → 804 行

attend_window.py 跌破 600 行(1437 → 574,本会话累计削减 863 行 = -60%)

验证:
- pytest 1013 passed
- compile OK

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 13:02:28 +08:00
1 parent 28dcbc75c7
commit 06c3e54d26
2 files changed
+122 -80

No files matched your search

@@ -541,87 +541,24 @@ def process_reservation_attend_window(
pass
return False
# 进入锁定期:在开奖附近一段时间内不再切换直播间
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)
# 进入锁定期 + 写完成日志
from .attend_window_helpers import setup_lock_period_and_log_success
if reservation_after_action_pending_ref:
reservation_after_action_pending_ref[0] = True
else:
reservation_after_action_pending_ref.append(True)
try:
from datetime import datetime
ft_str = (
datetime.fromtimestamp(ft).strftime("%Y-%m-%d %H:%M:%S")
if ft > 0
else "无效"
)
# 运行日志
analyser._log_kv(
"预约",
f"[预约] 已回退到目标直播间,等待详情识别与参与 - 直播间:'{target_room_title[:30] if target_room_title else ''}', 开奖时间:{ft_str}, 价值:{target_value}元",
future_ts=int(ft),
future_str=ft_str,
focus_until=int(focus_until_new),
lock_period_seconds=int(focus_until_new - _now_ts()),
remain_seconds_to_open=int(ft - _now_ts()),
target_room_title=target_room_title,
target_room_title_raw=target_room_title_raw,
target_content=target_content,
target_value_yuan=target_value,
)
# 行为日志
analyser._log_kv(
"行为",
"回退到预约直播间成功",
action="回退到预约直播间",
result="成功",
detail={
"目标直播间": (
target_room_title
if target_room_title
else target_room_title_raw
),
"目标直播间原始": target_room_title_raw,
"福袋内容": target_content,
"福袋价值": target_value,
"预计开奖时间": ft_str,
"预计开奖时间戳": int(ft),
"剩余秒数": int(ft - _now_ts()) if ft > _now_ts() else 0,
},
)
except Exception:
pass
# 不在这里弹出队列,等待真正"参与完毕"后由上层统一处理
try:
write_state_callback()
except Exception:
pass
# 添加函数完成日志
try:
from datetime import datetime
elapsed = time.time() - start_time
analyser._log_kv(
"预约",
"[预约] process_reservation_attend_window 返回结果",
result="已触发回退",
future_ts=int(ft),
now_ts=int(now_ts),
remain_seconds_to_open=int(ft - now_ts) if ft > now_ts else 0,
elapsed_seconds=round(elapsed, 3),
)
except Exception:
pass
setup_lock_period_and_log_success(
analyser,
target,
ft,
now_ts,
poll_before,
target_room_title,
target_room_title_raw,
target_content,
target_value,
reservation_focus_until_ts_ref,
reservation_after_action_pending_ref,
start_time,
write_state_callback,
)
return True
@@ -105,6 +105,111 @@ def log_attend_window_detail_check(
pass
def setup_lock_period_and_log_success(
analyser: Any,
target: ReservationItem,
ft: float,
now_ts: float,
poll_before: float,
target_room_title: str,
target_room_title_raw: str,
target_content: str,
target_value: float,
reservation_focus_until_ts_ref: List[float],
reservation_after_action_pending_ref: List[bool],
start_time: float,
write_state_callback: Callable[[], None],
) -> None:
"""成功回退到目标直播间后:设置锁定期 + 写日志 + write_state + 写完成日志。
流程:
1. focus_until_ts = ft + poll_before(设置锁定期)
2. reservation_after_action_pending = True
3. 写"已回退到目标直播间"运行日志和"回退成功"行为日志
4. write_state_callback
5. 写"已触发回退"完成日志
"""
import time as _time
# 进入锁定期:在开奖附近一段时间内不再切换直播间
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:
from datetime import datetime
ft_str = (
datetime.fromtimestamp(ft).strftime("%Y-%m-%d %H:%M:%S")
if ft > 0
else "无效"
)
# 运行日志
analyser._log_kv(
"预约",
f"[预约] 已回退到目标直播间,等待详情识别与参与 - 直播间:'{target_room_title[:30] if target_room_title else ''}', 开奖时间:{ft_str}, 价值:{target_value}元",
future_ts=int(ft),
future_str=ft_str,
focus_until=int(focus_until_new),
lock_period_seconds=int(focus_until_new - _time.time()),
remain_seconds_to_open=int(ft - _time.time()),
target_room_title=target_room_title,
target_room_title_raw=target_room_title_raw,
target_content=target_content,
target_value_yuan=target_value,
)
# 行为日志
analyser._log_kv(
"行为",
"回退到预约直播间成功",
action="回退到预约直播间",
result="成功",
detail={
"目标直播间": (
target_room_title if target_room_title else target_room_title_raw
),
"目标直播间原始": target_room_title_raw,
"福袋内容": target_content,
"福袋价值": target_value,
"预计开奖时间": ft_str,
"预计开奖时间戳": int(ft),
"剩余秒数": int(ft - _time.time()) if ft > _time.time() else 0,
},
)
except Exception:
pass
# 不在这里弹出队列,等待真正"参与完毕"后由上层统一处理
try:
write_state_callback()
except Exception:
pass
# 添加函数完成日志
try:
elapsed = _time.time() - start_time
analyser._log_kv(
"预约",
"[预约] process_reservation_attend_window 返回结果",
result="已触发回退",
future_ts=int(ft),
now_ts=int(now_ts),
remain_seconds_to_open=int(ft - now_ts) if ft > now_ts else 0,
elapsed_seconds=round(elapsed, 3),
)
except Exception:
pass
def wait_after_seek_and_verify(analyser: Any) -> float:
"""回退操作成功后:等待直播间加载完成(写日志 + delay),返回实际等待秒数。