将 188 行的"seek 成功后整段后处理"块抽到 helper: - 写"回退操作成功"日志 - 检查 reservation_back_verify_enabled,启用时: - wait_after_seek_and_verify 等待 - 调用 validate_back_to_reservation - 校验失败 → handle_verify_failure_retry_via_mapping - 校验通过 → 写日志 - setup_lock_period_and_log_success 返回值约定: - True:处理成功(含校验通过 OR 校验失败重试成功) - False:校验失败且重试失败 attend_window.py: 574 → 405 行 (-169) attend_window_helpers.py: 804 → 994 行 attend_window.py 跌破 500 行(1437 → 405,本会话累计削减 1032 行 = -72%) process_reservation_attend_window 函数从 1218 → 376 行 验证: - pytest 1013 passed - compile OK Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
995 lines
33 KiB
Python
995 lines
33 KiB
Python
"""attend_window 内部辅助函数。
|
||
|
||
从 process_reservation_attend_window 中抽出的相对独立的早返回块。
|
||
- check_total_timeout:检查总超时是否达到,达到则记日志返回 True
|
||
- handle_already_participated:检测到本预约 future_ts 已被参与,移除队头并写状态
|
||
- handle_invalid_future_ts:future_ts 无效,丢弃队头并写状态
|
||
"""
|
||
from __future__ import annotations
|
||
|
||
import time
|
||
from typing import Any, Callable, List
|
||
|
||
from ._common import (
|
||
ReservationItem,
|
||
_RESERVATION_ERROR_UTILS_AVAILABLE,
|
||
format_reservation_context,
|
||
safe_write_state,
|
||
)
|
||
|
||
|
||
def check_total_timeout(
|
||
analyser: Any, start_time: float, total_timeout_s: float, stage_label: str
|
||
) -> bool:
|
||
"""检查从 start_time 到现在是否超过 total_timeout_s。
|
||
|
||
超时则写入日志并返回 True;否则返回 False。
|
||
任何内部异常都被吞噬(与原代码一致),返回 False。
|
||
"""
|
||
try:
|
||
if (time.time() - start_time) > total_timeout_s:
|
||
try:
|
||
analyser._log_kv(
|
||
"预约",
|
||
f"[预约] process_reservation_attend_window:超时退出({stage_label})",
|
||
total_timeout_seconds=round(total_timeout_s, 3),
|
||
)
|
||
except Exception:
|
||
pass
|
||
return True
|
||
except Exception:
|
||
pass
|
||
return False
|
||
|
||
|
||
def log_attend_window_detail_check(
|
||
analyser: Any,
|
||
now_ts: float,
|
||
focus_until: float,
|
||
ft: float,
|
||
dynamic_ahead: int,
|
||
join_ahead: float,
|
||
poll_before: float,
|
||
est_back_times: int,
|
||
) -> None:
|
||
"""记录"详细检查"日志(锁定期状态 + 时间窗口)。纯日志,无副作用。"""
|
||
try:
|
||
from datetime import datetime
|
||
|
||
now_str = datetime.fromtimestamp(now_ts).strftime("%Y-%m-%d %H:%M:%S")
|
||
time_window_start_ts = ft - dynamic_ahead
|
||
time_window_start_str = (
|
||
datetime.fromtimestamp(time_window_start_ts).strftime("%Y-%m-%d %H:%M:%S")
|
||
if time_window_start_ts > 0
|
||
else "无效"
|
||
)
|
||
time_window_end_ts = ft + max(0.0, poll_before)
|
||
time_window_end_str = (
|
||
datetime.fromtimestamp(time_window_end_ts).strftime("%Y-%m-%d %H:%M:%S")
|
||
if time_window_end_ts > 0
|
||
else "无效"
|
||
)
|
||
ft_str = (
|
||
datetime.fromtimestamp(ft).strftime("%Y-%m-%d %H:%M:%S")
|
||
if ft > 0
|
||
else "无效"
|
||
)
|
||
|
||
analyser._log_kv(
|
||
"预约",
|
||
"[预约] process_reservation_attend_window 详细检查",
|
||
now_ts=int(now_ts),
|
||
now_str=now_str,
|
||
focus_until=int(focus_until) if focus_until > 0 else 0,
|
||
target_future_ts=int(ft),
|
||
target_future_str=ft_str,
|
||
dynamic_ahead=dynamic_ahead,
|
||
join_ahead=join_ahead,
|
||
poll_before=poll_before,
|
||
est_back_times=est_back_times,
|
||
time_window_start=int(time_window_start_ts),
|
||
time_window_start_str=time_window_start_str,
|
||
time_window_end=int(time_window_end_ts),
|
||
time_window_end_str=time_window_end_str,
|
||
remain_seconds_to_open=int(ft - now_ts) if ft > now_ts else 0,
|
||
remain_seconds_to_window=(
|
||
int(time_window_start_ts - now_ts)
|
||
if time_window_start_ts > now_ts
|
||
else 0
|
||
),
|
||
in_lock_period=bool(focus_until > 0 and now_ts < focus_until),
|
||
in_time_window=bool(now_ts >= time_window_start_ts),
|
||
passed_time_window=bool(now_ts > time_window_end_ts),
|
||
)
|
||
except Exception:
|
||
pass
|
||
|
||
|
||
def process_seek_success(
|
||
analyser: Any,
|
||
target: ReservationItem,
|
||
reservations: List[ReservationItem],
|
||
ft: float,
|
||
now_ts: float,
|
||
room_pos: int,
|
||
poll_before: float,
|
||
reservation_focus_until_ts_ref: List[float],
|
||
reservation_after_action_pending_ref: List[bool],
|
||
seek_reservation_room_callback: Callable[[ReservationItem], bool],
|
||
write_state_callback: Callable[[], None],
|
||
start_time: float,
|
||
) -> bool:
|
||
"""seek_reservation_room_callback 返回 True 后的整段后处理:
|
||
|
||
1. 写"回退操作成功,准备进行回退校验"日志
|
||
2. 如果启用 reservation_back_verify_enabled:
|
||
- 等待直播间加载(wait_after_seek_and_verify)
|
||
- 调用 validate_back_to_reservation 校验
|
||
- 校验失败 → 调用 handle_verify_failure_retry_via_mapping,返回其结果
|
||
- 校验通过 → 写日志
|
||
3. 设置锁定期 + 写完成日志(setup_lock_period_and_log_success)
|
||
4. 返回 True
|
||
|
||
返回:
|
||
True:处理成功(含校验通过 + 锁定期设置 OR 校验失败重试成功)
|
||
False:校验失败且重试失败(调用方应直接 return False)
|
||
"""
|
||
from .attend_window_verify_retry import handle_verify_failure_retry_via_mapping
|
||
|
||
# 获取目标直播间名字和福袋详情
|
||
target_room_title = str(target.get("room_title") or "").strip()
|
||
target_room_title_raw = str(target.get("room_title_raw") or "").strip()
|
||
target_content = str(target.get("content") or "").strip()
|
||
target_value = float(target.get("prize_value_yuan") or 0.0)
|
||
|
||
try:
|
||
analyser._log_kv(
|
||
"预约",
|
||
"[预约] process_reservation_attend_window:回退操作成功,准备进行回退校验",
|
||
target_pos=int(target.get("pos_seen", 0)),
|
||
target_room_title=target_room_title[:50] if target_room_title else "",
|
||
target_room_title_raw=(
|
||
target_room_title_raw[:50] if target_room_title_raw else ""
|
||
),
|
||
target_content=target_content[:50] if target_content else "",
|
||
target_value_yuan=target_value,
|
||
current_room_pos=int(room_pos),
|
||
reservation_back_verify_enabled=bool(
|
||
getattr(analyser, "reservation_back_verify_enabled", True)
|
||
),
|
||
level="DEBUG",
|
||
)
|
||
except Exception:
|
||
pass
|
||
|
||
# 回退校验
|
||
try:
|
||
verify_enabled = bool(
|
||
getattr(analyser, "reservation_back_verify_enabled", True)
|
||
)
|
||
try:
|
||
analyser._log_kv(
|
||
"预约",
|
||
"[预约] process_reservation_attend_window:检查回退校验开关",
|
||
reservation_back_verify_enabled=verify_enabled,
|
||
level="DEBUG",
|
||
)
|
||
except Exception:
|
||
pass
|
||
|
||
if verify_enabled:
|
||
wait_after_seek_and_verify(analyser)
|
||
|
||
try:
|
||
try:
|
||
analyser._log_kv(
|
||
"预约",
|
||
"[预约] process_reservation_attend_window:开始调用回退校验函数",
|
||
target_pos=int(target.get("pos_seen", 0)),
|
||
target_room_title=(
|
||
target_room_title[:50] if target_room_title else ""
|
||
),
|
||
level="DEBUG",
|
||
)
|
||
except Exception:
|
||
pass
|
||
|
||
import douyin.reservation.douyin_reservation_verify as douyin_reservation_verify
|
||
|
||
verify_ok, verify_detail = (
|
||
douyin_reservation_verify.validate_back_to_reservation(
|
||
analyser=analyser,
|
||
item=target,
|
||
quick=False,
|
||
max_checks=3,
|
||
)
|
||
)
|
||
|
||
try:
|
||
analyser._log_kv(
|
||
"预约",
|
||
"[预约] process_reservation_attend_window:回退校验函数返回",
|
||
verify_ok=verify_ok,
|
||
verify_detail_exists=verify_detail is not None,
|
||
level="DEBUG",
|
||
)
|
||
except Exception:
|
||
pass
|
||
|
||
if not verify_ok:
|
||
if handle_verify_failure_retry_via_mapping(
|
||
analyser,
|
||
target,
|
||
reservations,
|
||
verify_detail,
|
||
target_room_title,
|
||
target_room_title_raw,
|
||
ft,
|
||
poll_before,
|
||
reservation_focus_until_ts_ref,
|
||
reservation_after_action_pending_ref,
|
||
seek_reservation_room_callback,
|
||
write_state_callback,
|
||
):
|
||
return True
|
||
return False
|
||
else:
|
||
try:
|
||
analyser._log_kv(
|
||
"预约",
|
||
f"[预约] 回退校验通过:已成功回退到目标直播间并验证福袋详情 - 目标位置:{int(target.get('pos_seen', 0))}, 直播间:'{target_room_title[:30] if target_room_title else ''}'",
|
||
target_pos=int(target.get("pos_seen", 0)),
|
||
target_room_title=(
|
||
target_room_title[:50] if target_room_title else ""
|
||
),
|
||
)
|
||
except Exception:
|
||
pass
|
||
except Exception as e:
|
||
try:
|
||
analyser._log_kv(
|
||
"预约",
|
||
"[预约] 回退校验执行时发生异常,继续执行",
|
||
error=str(e),
|
||
error_type=type(e).__name__,
|
||
)
|
||
except Exception:
|
||
pass
|
||
# 校验异常不影响主流程,继续执行
|
||
else:
|
||
try:
|
||
analyser._log_kv(
|
||
"预约",
|
||
"[预约] 回退校验已禁用,跳过校验",
|
||
)
|
||
except Exception:
|
||
pass
|
||
except Exception as e:
|
||
try:
|
||
analyser._log_kv(
|
||
"预约",
|
||
"[预约] 检查回退校验开关时发生异常,继续执行",
|
||
error=str(e),
|
||
error_type=type(e).__name__,
|
||
)
|
||
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
|
||
|
||
|
||
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),返回实际等待秒数。
|
||
|
||
流程:
|
||
- 读 reservation_back_verify_wait_seconds(默认 5.0,限制 0.1-10.0)
|
||
- 写"开始等待"日志
|
||
- 调用 operation.delay 或 time.sleep
|
||
- 写"等待完成"日志
|
||
"""
|
||
import time as _time
|
||
|
||
try:
|
||
verify_wait_seconds = float(
|
||
getattr(analyser, "reservation_back_verify_wait_seconds", 5.0) or 5.0
|
||
)
|
||
if verify_wait_seconds < 0.1:
|
||
verify_wait_seconds = 0.1
|
||
if verify_wait_seconds > 10.0:
|
||
verify_wait_seconds = 10.0
|
||
except Exception:
|
||
verify_wait_seconds = 3.0
|
||
|
||
try:
|
||
analyser._log_kv(
|
||
"预约",
|
||
"[预约] process_reservation_attend_window:回退后等待直播间加载完成(开始等待)",
|
||
verify_wait_seconds=verify_wait_seconds,
|
||
level="INFO",
|
||
)
|
||
except Exception:
|
||
pass
|
||
|
||
wait_start_ts = _time.time()
|
||
try:
|
||
try:
|
||
analyser.operation.delay(verify_wait_seconds)
|
||
except Exception:
|
||
_time.sleep(verify_wait_seconds)
|
||
finally:
|
||
waited = _time.time() - wait_start_ts
|
||
try:
|
||
analyser._log_kv(
|
||
"预约",
|
||
"[预约] process_reservation_attend_window:回退等待完成",
|
||
expected_wait_seconds=verify_wait_seconds,
|
||
waited_seconds=round(waited, 3),
|
||
level="INFO",
|
||
)
|
||
except Exception:
|
||
pass
|
||
|
||
return verify_wait_seconds
|
||
|
||
|
||
def log_seek_failure_and_finish(
|
||
analyser: Any,
|
||
target: ReservationItem,
|
||
ft: float,
|
||
now_ts: float,
|
||
room_pos: int,
|
||
start_time: float,
|
||
write_state_callback: Callable[[], None],
|
||
) -> None:
|
||
"""process_reservation_attend_window 末尾"回退失败"块。
|
||
|
||
流程:
|
||
1. 写"回退到预约直播间失败"日志
|
||
2. 调用 write_state_callback
|
||
3. 写"未触发回退"完成日志
|
||
"""
|
||
import time as _time
|
||
|
||
try:
|
||
analyser._log_kv(
|
||
"预约",
|
||
"[预约] 回退到预约直播间失败,保留队列以便后续重试或清理",
|
||
future_ts=int(ft),
|
||
now_ts=int(now_ts),
|
||
remain_seconds_to_open=int(ft - now_ts),
|
||
target_pos=int(target.get("pos_seen", 0)),
|
||
current_room_pos=int(room_pos),
|
||
)
|
||
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) if ft > 0 else 0,
|
||
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 execute_seek_reservation_with_logging(
|
||
analyser: Any,
|
||
target: ReservationItem,
|
||
reservations: List[ReservationItem],
|
||
room_pos: int,
|
||
ft: float,
|
||
now_ts: float,
|
||
dynamic_ahead: int,
|
||
est_back_times: int,
|
||
poll_before: float,
|
||
seek_reservation_room_callback: Callable[[ReservationItem], bool],
|
||
):
|
||
"""执行回退动作前的日志 + 调用 seek_reservation_room_callback。
|
||
|
||
流程:
|
||
1. 写"进入预约参与窗口"日志
|
||
2. 计算 current_room_pos / target_pos / pos_diff
|
||
3. 写"准备执行回退到预约直播间"日志
|
||
4. 调用 seek_reservation_room_callback(target)
|
||
5. 异常时记录错误日志(log_reservation_error 或 _log_kv)
|
||
|
||
返回:
|
||
(ok: bool, current_room_pos: int, target_pos: int, pos_diff: int)
|
||
"""
|
||
try:
|
||
analyser._log_kv(
|
||
"预约",
|
||
"[预约] 进入预约参与窗口,准备回退到目标直播间",
|
||
future_ts=int(ft),
|
||
now_ts=int(now_ts),
|
||
room_pos=int(room_pos),
|
||
queue_len=len(reservations),
|
||
dynamic_ahead=int(dynamic_ahead),
|
||
est_back_times=int(est_back_times),
|
||
remain_seconds_to_open=int(ft - now_ts),
|
||
time_window_start=int(ft - dynamic_ahead),
|
||
time_window_end=int(ft + max(0.0, poll_before)),
|
||
)
|
||
except Exception:
|
||
pass
|
||
|
||
try:
|
||
current_room_pos = int(getattr(analyser, "room_pos", room_pos) or room_pos)
|
||
except Exception:
|
||
current_room_pos = int(room_pos)
|
||
|
||
try:
|
||
target_pos = int(target.get("pos_seen") or 0)
|
||
except Exception:
|
||
target_pos = 0
|
||
pos_diff = current_room_pos - target_pos
|
||
|
||
try:
|
||
analyser._log_kv(
|
||
"预约",
|
||
"[预约] 准备执行回退到预约直播间",
|
||
target_pos_seen=target_pos,
|
||
current_room_pos=current_room_pos,
|
||
pos_diff=pos_diff,
|
||
)
|
||
except Exception:
|
||
pass
|
||
|
||
ok = False
|
||
try:
|
||
ok = bool(seek_reservation_room_callback(target))
|
||
except Exception as e:
|
||
if _RESERVATION_ERROR_UTILS_AVAILABLE:
|
||
from ._common import log_reservation_error
|
||
|
||
log_reservation_error(
|
||
analyser,
|
||
e,
|
||
format_reservation_context(
|
||
item=target,
|
||
reservations=reservations,
|
||
current_room_pos=current_room_pos,
|
||
target_pos=target_pos,
|
||
pos_diff=pos_diff,
|
||
),
|
||
level="ERROR",
|
||
stage="执行回退操作",
|
||
)
|
||
else:
|
||
try:
|
||
analyser._log_kv(
|
||
"预约",
|
||
"[预约] 执行回退时发生异常",
|
||
error=str(e),
|
||
error_type=type(e).__name__,
|
||
)
|
||
except Exception:
|
||
pass
|
||
ok = False
|
||
|
||
return ok, current_room_pos, target_pos, pos_diff
|
||
|
||
|
||
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,
|
||
ft: float,
|
||
dynamic_ahead: int,
|
||
) -> None:
|
||
"""记录"时间窗口未到,跳过回退"日志。纯日志,无副作用。"""
|
||
try:
|
||
from datetime import datetime
|
||
|
||
time_window_start_ts = ft - dynamic_ahead
|
||
time_window_start_str = (
|
||
datetime.fromtimestamp(time_window_start_ts).strftime(
|
||
"%Y-%m-%d %H:%M:%S"
|
||
)
|
||
if time_window_start_ts > 0
|
||
else "无效"
|
||
)
|
||
analyser._log_kv(
|
||
"预约",
|
||
"[预约] 时间窗口未到,跳过回退",
|
||
now_ts=int(now_ts),
|
||
now_str=datetime.fromtimestamp(now_ts).strftime("%Y-%m-%d %H:%M:%S"),
|
||
time_window_start=int(time_window_start_ts),
|
||
time_window_start_str=time_window_start_str,
|
||
remain_seconds=int((ft - dynamic_ahead) - now_ts),
|
||
target_future_ts=int(ft),
|
||
dynamic_ahead=dynamic_ahead,
|
||
)
|
||
except Exception:
|
||
pass
|
||
|
||
|
||
def log_missed_window(
|
||
analyser: Any,
|
||
now_ts: float,
|
||
ft: float,
|
||
poll_before: float,
|
||
) -> None:
|
||
"""记录"已明显错过开奖时间"日志。纯日志,无副作用。"""
|
||
try:
|
||
analyser._log_kv(
|
||
"预约",
|
||
"[预约] 已明显错过开奖时间,清理过期预约",
|
||
future_ts=int(ft),
|
||
now_ts=int(now_ts),
|
||
poll_before=poll_before,
|
||
missed_seconds=int(now_ts - (ft + max(0.0, poll_before))),
|
||
)
|
||
except Exception:
|
||
pass
|
||
|
||
|
||
|
||
def _write_state_unified(
|
||
write_state_callback: Callable[[], None],
|
||
analyser: Any,
|
||
target: ReservationItem,
|
||
reservations: List[ReservationItem],
|
||
reason: str,
|
||
) -> None:
|
||
"""统一的"写状态"封装:优先走 safe_write_state,失败回退到裸 callback。"""
|
||
if _RESERVATION_ERROR_UTILS_AVAILABLE:
|
||
safe_write_state(
|
||
write_state_callback,
|
||
analyser,
|
||
format_reservation_context(
|
||
item=target,
|
||
reservations=reservations,
|
||
reason=reason,
|
||
),
|
||
)
|
||
else:
|
||
try:
|
||
write_state_callback()
|
||
except Exception:
|
||
pass
|
||
|
||
|
||
def handle_already_participated(
|
||
analyser: Any,
|
||
reservations: List[ReservationItem],
|
||
target: ReservationItem,
|
||
ft: float,
|
||
write_state_callback: Callable[[], None],
|
||
) -> bool:
|
||
"""检查 ft 是否已经参与过;若已参与则移除队头并写状态,返回 True。
|
||
|
||
判定规则:_participated_fudai_future_ts 中存在与 ft 误差 ≤ 5 秒的时间戳。
|
||
"""
|
||
try:
|
||
participated_fudai_ts = getattr(
|
||
analyser, "_participated_fudai_future_ts", set()
|
||
)
|
||
if not isinstance(participated_fudai_ts, set):
|
||
participated_fudai_ts = set()
|
||
|
||
already_participated = any(
|
||
abs(ft - participated_ts) <= 5.0
|
||
for participated_ts in participated_fudai_ts
|
||
)
|
||
|
||
if already_participated:
|
||
try:
|
||
analyser._log_kv(
|
||
"预约",
|
||
"[预约] process_reservation_attend_window:检测到已参与过该预约,从队列中移除",
|
||
target_future_ts=int(ft) if ft > 0 else 0,
|
||
)
|
||
except Exception:
|
||
pass
|
||
reservations.pop(0)
|
||
_write_state_unified(
|
||
write_state_callback, analyser, target, reservations, "已参与过"
|
||
)
|
||
return True
|
||
except Exception as e:
|
||
if _RESERVATION_ERROR_UTILS_AVAILABLE:
|
||
from ._common import log_reservation_error
|
||
|
||
log_reservation_error(
|
||
analyser,
|
||
e,
|
||
format_reservation_context(item=target, reservations=reservations),
|
||
level="WARNING",
|
||
stage="检查是否已参与",
|
||
)
|
||
|
||
return False
|
||
|
||
|
||
def handle_invalid_future_ts(
|
||
analyser: Any,
|
||
reservations: List[ReservationItem],
|
||
target: ReservationItem,
|
||
ft: float,
|
||
write_state_callback: Callable[[], None],
|
||
) -> None:
|
||
"""future_ts 无效(≤0),丢弃队头并写状态。调用方调用后应直接 return False。"""
|
||
try:
|
||
analyser._log_kv(
|
||
"预约",
|
||
"[预约] process_reservation_attend_window:预约 future_ts 无效,丢弃",
|
||
future_ts=ft,
|
||
target=target,
|
||
)
|
||
except Exception:
|
||
pass
|
||
reservations.pop(0)
|
||
_write_state_unified(
|
||
write_state_callback, analyser, target, reservations, "future_ts 无效"
|
||
)
|
||
|
||
|
||
def process_lock_period(
|
||
analyser: Any,
|
||
reservations: List[ReservationItem],
|
||
focus_until: float,
|
||
now_ts: float,
|
||
poll_before: float,
|
||
reservation_focus_until_ts_ref: List[float],
|
||
) -> float:
|
||
"""锁定期内:优先处理"锁定的预约",避免被队头其它预约打断。
|
||
|
||
- 找到与 focus_until 对应的预约(误差 ≤ 5 秒);
|
||
- 找不到 → 视为锁定失效,清除 focus_until_ts_ref;
|
||
- 找到但不在队头 → 把该项移到队头;
|
||
- 已在队头 → 不动。
|
||
|
||
返回(可能更新过的)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
|