From 06c3e54d2676d210eae9978d3f89e4f3adffceff Mon Sep 17 00:00:00 2001 From: Codex Date: Fri, 22 May 2026 13:02:28 +0800 Subject: [PATCH] =?UTF-8?q?refactor(reservation):=20=E6=8A=BD=20process=5F?= =?UTF-8?q?reservation=5Fattend=5Fwindow=20=E4=B8=AD"=E6=88=90=E5=8A=9F?= =?UTF-8?q?=E5=9B=9E=E9=80=80=E5=90=8E=E8=AE=BE=E7=BD=AE=E9=94=81=E5=AE=9A?= =?UTF-8?q?=E6=9C=9F"=E5=9D=97=E5=88=B0=20setup=5Flock=5Fperiod=5Fand=5Flo?= =?UTF-8?q?g=5Fsuccess?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 将 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) --- .../reservation_logic/attend_window.py | 97 +++------------- .../attend_window_helpers.py | 105 ++++++++++++++++++ 2 files changed, 122 insertions(+), 80 deletions(-) diff --git a/douyin/reservation/reservation_logic/attend_window.py b/douyin/reservation/reservation_logic/attend_window.py index 6350398..9b94f3d 100644 --- a/douyin/reservation/reservation_logic/attend_window.py +++ b/douyin/reservation/reservation_logic/attend_window.py @@ -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 diff --git a/douyin/reservation/reservation_logic/attend_window_helpers.py b/douyin/reservation/reservation_logic/attend_window_helpers.py index d36fd63..bdb6bf6 100644 --- a/douyin/reservation/reservation_logic/attend_window_helpers.py +++ b/douyin/reservation/reservation_logic/attend_window_helpers.py @@ -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),返回实际等待秒数。