277 lines
9.1 KiB
Python
277 lines
9.1 KiB
Python
"""attend_window 的可复用辅助函数。"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import time
|
|
from typing import Any, Callable, List, Tuple
|
|
|
|
from ._common import ReservationItem, format_reservation_context, log_reservation_error, safe_write_state
|
|
|
|
|
|
def pop_if_already_participated(
|
|
analyser: Any,
|
|
reservations: List[ReservationItem],
|
|
target: ReservationItem,
|
|
ft: float,
|
|
write_state_callback: Callable[[], None],
|
|
error_utils_available: bool,
|
|
) -> bool:
|
|
"""若目标预约已参与过,则出队并写状态,返回 True。"""
|
|
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 not already_participated:
|
|
return False
|
|
try:
|
|
analyser._log_kv(
|
|
"预约",
|
|
"[预约] process_reservation_attend_window:检测到已参与过该预约,从队列中移除",
|
|
target_future_ts=int(ft) if ft > 0 else 0,
|
|
)
|
|
except Exception:
|
|
pass
|
|
reservations.pop(0)
|
|
if error_utils_available:
|
|
safe_write_state(
|
|
write_state_callback,
|
|
analyser,
|
|
format_reservation_context(item=target, reservations=reservations, reason="已参与过"),
|
|
)
|
|
else:
|
|
try:
|
|
write_state_callback()
|
|
except Exception:
|
|
pass
|
|
return True
|
|
except Exception as e:
|
|
if error_utils_available:
|
|
log_reservation_error(
|
|
analyser,
|
|
e,
|
|
format_reservation_context(item=target, reservations=reservations),
|
|
level="WARNING",
|
|
stage="检查是否已参与",
|
|
)
|
|
return False
|
|
|
|
|
|
def log_target_snapshot(analyser: Any, target: ReservationItem, ft: float) -> None:
|
|
try:
|
|
from datetime import datetime
|
|
|
|
ft_str = datetime.fromtimestamp(ft).strftime("%Y-%m-%d %H:%M:%S") if ft > 0 else "无效"
|
|
analyser._log_kv(
|
|
"预约",
|
|
"[预约] process_reservation_attend_window 获取目标预约",
|
|
target_future_ts=int(ft) if ft > 0 else 0,
|
|
target_future_str=ft_str,
|
|
target_value=float(target.get("prize_value_yuan", 0)),
|
|
target_pos_seen=int(target.get("pos_seen", 0)),
|
|
target_content=str(target.get("content", ""))[:50],
|
|
)
|
|
except Exception:
|
|
pass
|
|
|
|
|
|
def pop_if_invalid_future_ts(
|
|
analyser: Any,
|
|
reservations: List[ReservationItem],
|
|
target: ReservationItem,
|
|
ft: float,
|
|
write_state_callback: Callable[[], None],
|
|
error_utils_available: bool,
|
|
) -> bool:
|
|
"""若 future_ts 无效,则出队并写状态,返回 True。"""
|
|
if ft > 0:
|
|
return False
|
|
try:
|
|
analyser._log_kv(
|
|
"预约",
|
|
"[预约] process_reservation_attend_window:预约 future_ts 无效,丢弃",
|
|
future_ts=ft,
|
|
target=target,
|
|
)
|
|
except Exception:
|
|
pass
|
|
reservations.pop(0)
|
|
if error_utils_available:
|
|
safe_write_state(
|
|
write_state_callback,
|
|
analyser,
|
|
format_reservation_context(item=target, reservations=reservations, reason="future_ts 无效"),
|
|
)
|
|
else:
|
|
try:
|
|
write_state_callback()
|
|
except Exception:
|
|
pass
|
|
return True
|
|
|
|
|
|
def compute_dynamic_ahead(
|
|
analyser: Any,
|
|
target: ReservationItem,
|
|
room_pos: int,
|
|
reservation_back_swipe_delay_s: float,
|
|
join_ahead: float,
|
|
) -> Tuple[int, int]:
|
|
"""返回动态提前量与估算回退步数。"""
|
|
dynamic_ahead = int(join_ahead)
|
|
est_back_times = 0
|
|
try:
|
|
target_pos = int(target.get("pos_seen") or 0)
|
|
est_back_times = max(0, int(room_pos) - target_pos)
|
|
try:
|
|
max_back = int(getattr(analyser, "high_value_fudai_max_swipe_back", 60) or 60)
|
|
except Exception:
|
|
max_back = 60
|
|
if max_back > 0:
|
|
est_back_times = min(est_back_times, max_back)
|
|
reservation_back_swipe_delay_s_val = float(reservation_back_swipe_delay_s or 0.6)
|
|
try:
|
|
screenshot_budget = float(
|
|
getattr(analyser, "reservation_screenshot_budget_seconds", 8.0) or 8.0
|
|
)
|
|
except Exception:
|
|
screenshot_budget = 8.0
|
|
try:
|
|
verify_wait = float(
|
|
getattr(analyser, "reservation_back_verify_wait_seconds", 5.0) or 5.0
|
|
)
|
|
except Exception:
|
|
verify_wait = 5.0
|
|
try:
|
|
direct_attend_budget = float(
|
|
getattr(analyser, "reservation_direct_attend_min_action_seconds", 8.0)
|
|
or 8.0
|
|
)
|
|
except Exception:
|
|
direct_attend_budget = 8.0
|
|
try:
|
|
safety_budget = float(
|
|
getattr(analyser, "reservation_back_safety_budget_seconds", 4.0) or 4.0
|
|
)
|
|
except Exception:
|
|
safety_budget = 4.0
|
|
runtime_budget = (
|
|
est_back_times * reservation_back_swipe_delay_s_val
|
|
+ screenshot_budget
|
|
+ verify_wait
|
|
+ direct_attend_budget
|
|
+ safety_budget
|
|
)
|
|
dynamic_ahead = int(max(join_ahead, runtime_budget))
|
|
except Exception:
|
|
dynamic_ahead = int(join_ahead)
|
|
return dynamic_ahead, est_back_times
|
|
|
|
|
|
def mark_reservation_focus_and_log_success(
|
|
analyser: Any,
|
|
ft: float,
|
|
poll_before: float,
|
|
now_ts: 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],
|
|
) -> None:
|
|
"""更新锁定期和待处理标志,并输出回退成功日志。"""
|
|
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
|
|
from .helpers import _now_ts
|
|
|
|
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
|
|
|
|
|
|
def log_back_failed_keep_queue(
|
|
analyser: Any,
|
|
ft: float,
|
|
now_ts: float,
|
|
target: ReservationItem,
|
|
room_pos: int,
|
|
) -> None:
|
|
"""记录回退失败且保留队列的日志。"""
|
|
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
|
|
|
|
|
|
def log_attend_window_result(
|
|
analyser: Any,
|
|
start_time: float,
|
|
result: str,
|
|
ft: float,
|
|
now_ts: float,
|
|
) -> None:
|
|
"""记录 process_reservation_attend_window 返回结果日志。"""
|
|
try:
|
|
elapsed = time.time() - start_time
|
|
analyser._log_kv(
|
|
"预约",
|
|
"[预约] process_reservation_attend_window 返回结果",
|
|
result=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
|