From 0629906449c790da761dd8a0d7eab199ec0654e6 Mon Sep 17 00:00:00 2001 From: Codex Date: Fri, 22 May 2026 14:12:02 +0800 Subject: [PATCH] =?UTF-8?q?refactor(reservation):=20=E6=8A=BD=20process=5F?= =?UTF-8?q?reservation=5Fattend=5Fwindow=20=E4=B8=AD=203=20=E4=B8=AA?= =?UTF-8?q?=E7=8B=AC=E7=AB=8B=E5=9D=97=E5=88=B0=20helper?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - calculate_dynamic_ahead:计算动态提前量 + 估计回退步数(约 32 行 → 4 行调用) - log_target_reservation_info:记录目标预约的详细信息日志(约 19 行 → 1 行调用) - is_queue_full_immediate_attend_mode:检查并记录"队列满立即参与模式"状态(约 22 行 → 1 行调用) attend_window.py: 293 → 231 行 (-62) attend_window_helpers.py: 1143 → 1234 行 attend_window.py 跌破 250 行(1437 → 231,本会话累计削减 1206 行 = -84%) process_reservation_attend_window 函数从约 376 → 215 行 验证: - pytest 1013 passed - compile OK Co-Authored-By: Claude Opus 4.7 (1M context) --- .../reservation_logic/attend_window.py | 80 ++-------------- .../attend_window_helpers.py | 91 +++++++++++++++++++ 2 files changed, 100 insertions(+), 71 deletions(-) diff --git a/douyin/reservation/reservation_logic/attend_window.py b/douyin/reservation/reservation_logic/attend_window.py index 39bf0eb..0211a86 100644 --- a/douyin/reservation/reservation_logic/attend_window.py +++ b/douyin/reservation/reservation_logic/attend_window.py @@ -102,8 +102,11 @@ def process_reservation_attend_window( # 检查是否已经参与过该预约(通过 _participated_fudai_future_ts) from .attend_window_helpers import ( + calculate_dynamic_ahead, handle_already_participated, handle_invalid_future_ts, + is_queue_full_immediate_attend_mode, + log_target_reservation_info, ) if handle_already_participated( @@ -112,25 +115,7 @@ def process_reservation_attend_window( return False # 添加目标预约的详细信息日志 - 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 + log_target_reservation_info(analyser, target, ft) # 没有合法 future_ts,则直接丢弃 if ft <= 0: @@ -139,39 +124,10 @@ def process_reservation_attend_window( ) return False - # 仅在"即将开奖窗口"(包括已过一小段时间)内才触发回退 - try: - join_ahead = float( - getattr(analyser, "high_value_fudai_join_ahead_seconds", 100) or 100 - ) - except Exception: - join_ahead = 100.0 - - # poll_before 已在函数前部统一计算 - - # 动态提前量计算:考虑回退步数耗时,避免提前量太小导致回退后错过 - 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) - # 动态提前量 = max(基础提前量, 回退步数 * 延迟 + 额外缓冲) - reservation_back_swipe_delay_s_val = float( - reservation_back_swipe_delay_s or 0.6 - ) - dynamic_ahead = int( - max(join_ahead, est_back_times * reservation_back_swipe_delay_s_val + 18) - ) - except Exception: - dynamic_ahead = int(join_ahead) + # 仅在"即将开奖窗口"内才触发回退:计算动态提前量 + join_ahead, dynamic_ahead, est_back_times = calculate_dynamic_ahead( + analyser, target, room_pos, reservation_back_swipe_delay_s + ) # 添加详细的调试日志:记录锁定期状态和时间窗口检查结果 from .attend_window_helpers import log_attend_window_detail_check @@ -210,25 +166,7 @@ def process_reservation_attend_window( # 否则继续后续检查 # 检查是否是队列满后立即参与的情况(使用标志判断,而不是锁定期) - is_queue_full_immediate_attend = False - try: - # 检查队列满等待标志 - queue_full_waiting = bool( - getattr(analyser, "queue_full_waiting_at_earliest", False) or False - ) - if queue_full_waiting: - is_queue_full_immediate_attend = True - try: - analyser._log_kv( - "预约", - "[预约] 检测到队列满后立即参与模式(通过标志判断),跳过时间窗口检查,直接参与", - queue_full_waiting=queue_full_waiting, - target_future_ts=int(ft), - ) - except Exception: - pass - except Exception: - pass + is_queue_full_immediate_attend = is_queue_full_immediate_attend_mode(analyser, ft) if not is_queue_full_immediate_attend and now_ts < ft - dynamic_ahead: # 还没到窗口(非队列满立即参与模式) diff --git a/douyin/reservation/reservation_logic/attend_window_helpers.py b/douyin/reservation/reservation_logic/attend_window_helpers.py index bb39fc5..7ef49c7 100644 --- a/douyin/reservation/reservation_logic/attend_window_helpers.py +++ b/douyin/reservation/reservation_logic/attend_window_helpers.py @@ -106,6 +106,97 @@ def log_attend_window_detail_check( pass +def calculate_dynamic_ahead( + analyser: Any, + target: ReservationItem, + room_pos: int, + reservation_back_swipe_delay_s: float, +): + """计算动态提前量 + 估计回退步数。 + + 返回 (join_ahead: float, dynamic_ahead: int, est_back_times: int)。 + """ + try: + join_ahead = float( + getattr(analyser, "high_value_fudai_join_ahead_seconds", 100) or 100 + ) + except Exception: + join_ahead = 100.0 + + 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) + # 动态提前量 = max(基础提前量, 回退步数 * 延迟 + 额外缓冲) + reservation_back_swipe_delay_s_val = float( + reservation_back_swipe_delay_s or 0.6 + ) + dynamic_ahead = int( + max(join_ahead, est_back_times * reservation_back_swipe_delay_s_val + 18) + ) + except Exception: + dynamic_ahead = int(join_ahead) + + return join_ahead, dynamic_ahead, est_back_times + + +def log_target_reservation_info(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 is_queue_full_immediate_attend_mode(analyser: Any, ft: float) -> bool: + """检查并记录"队列满立即参与模式"的状态。 + + 返回 True 表示当前处于队列满等待模式,跳过时间窗口检查直接参与。 + """ + try: + queue_full_waiting = bool( + getattr(analyser, "queue_full_waiting_at_earliest", False) or False + ) + if queue_full_waiting: + try: + analyser._log_kv( + "预约", + "[预约] 检测到队列满后立即参与模式(通过标志判断),跳过时间窗口检查,直接参与", + queue_full_waiting=queue_full_waiting, + target_future_ts=int(ft), + ) + except Exception: + pass + return True + except Exception: + pass + return False + + def process_attend_window_entry_checks( analyser: Any, reservations: List[ReservationItem],