From f5b603e39e7963d3618050241a2c1c2f695060d7 Mon Sep 17 00:00:00 2001 From: Codex Date: Fri, 22 May 2026 12:41:27 +0800 Subject: [PATCH] =?UTF-8?q?refactor(fudai):=20=E6=8A=BD=20run=5Fonce=5Fbas?= =?UTF-8?q?ic=20=E4=B8=AD"=E9=AB=98=E4=BB=B7=E5=80=BC=E9=A2=84=E7=BA=A6?= =?UTF-8?q?=E6=95=B4=E6=AE=B5=E5=A4=84=E7=90=86"=E5=9D=97=E5=88=B0=20proce?= =?UTF-8?q?ss=5Fhigh=5Fvalue=5Freservation=5Fcheck?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 将 high_value_enabled and reservations_list 分支整段(含 4 个回调闭包 + 6 步处理流程) 约 99 行抽到 process_high_value_reservation_check helper,内部组合: - try_handle_queue_full_wait - log_skip_preemption_reason - reset_to_first_room_after_all_reservations_done - log_first_reservation_check - process_reservation_multi_room_mode(多间模式) - cleanup_expired_reservations_single_room(单间模式) 返回值约定: - True:触发了早返回(队列满等待 / 所有预约完成重置),调用方应直接 return - False:未触发早返回,调用方继续 douyin_fudai.py: 1319 → 1228 行 (-91) fudai_detail_helpers.py: 2804 → 2890 行 douyin_fudai.py 跌破 1300 行(4121 → 1228,本会话累计削减 2893 行 = -70%) 验证: - pytest 1013 passed - compile OK Co-Authored-By: Claude Opus 4.7 (1M context) --- douyin/core/douyin_fudai.py | 95 +------------------ .../fudai_modules/fudai_detail_helpers.py | 86 +++++++++++++++++ 2 files changed, 88 insertions(+), 93 deletions(-) diff --git a/douyin/core/douyin_fudai.py b/douyin/core/douyin_fudai.py index 094f772..104911f 100644 --- a/douyin/core/douyin_fudai.py +++ b/douyin/core/douyin_fudai.py @@ -740,103 +740,12 @@ class fudai_analyse: log_reservation_check_status(self, high_value_enabled) if high_value_enabled and reservations_list: - - def _swipe_down_once() -> None: - self.swipe_down_back_from_live() - - def _sort_reservations() -> None: - douyin_reservation_logic._sort_reservations_inplace(self.reservations) # type: ignore[attr-defined] - - def _write_state() -> None: - """将预约队列写入状态文件(节流,避免高频写盘卡顿)。""" - from douyin.core.fudai_modules.fudai_detail_helpers import ( - write_reservations_state_throttled, - ) - - write_reservations_state_throttled(self) - - focus_ref = [float(self.reservation_focus_until_ts or 0.0)] - after_action_ref = [ - bool(self.reservation_after_action_pending or False) - ] - - def _seek_reservation_room(item: dict) -> bool: - try: - return bool( - self._seek_reservation_room(item, operation_type="预约") - ) - except Exception: - return False - - # 检查队列是否已满且需要等待 from douyin.core.fudai_modules.fudai_detail_helpers import ( - try_handle_queue_full_wait, + process_high_value_reservation_check, ) - if try_handle_queue_full_wait( - self, - focus_ref, - _swipe_down_once, - _seek_reservation_room, - _sort_reservations, - _write_state, - start_time, - ): + if process_high_value_reservation_check(self, start_time): return - - # 根据当前状态写"跳过预约回退检查"的原因日志(无副作用) - from douyin.core.fudai_modules.fudai_detail_helpers import ( - log_skip_preemption_reason, - ) - - log_skip_preemption_reason(self) - - # 检查是否所有预约已完成,需要重置到第一个直播间 - from douyin.core.fudai_modules.fudai_detail_helpers import ( - reset_to_first_room_after_all_reservations_done, - ) - - if reset_to_first_room_after_all_reservations_done( - self, _swipe_down_once - ): - return - - # 单间模式(needswitch=False)下不启用预约回退逻辑 - # 因为单间模式不会切换直播间,不需要回退,预约会在当前直播间自动识别和参与 - try: - needswitch_cfg = bool(getattr(self, "needswitch", True)) - except Exception: - needswitch_cfg = True - - # 添加详细的调试日志,确认 needswitch 的值和预约信息 - from douyin.core.fudai_modules.fudai_detail_helpers import ( - log_first_reservation_check, - ) - - log_first_reservation_check(self, needswitch_cfg) - - if needswitch_cfg: - # 多间模式:需要回退到预约直播间 - from douyin.core.fudai_modules.fudai_detail_helpers import ( - process_reservation_multi_room_mode, - ) - - process_reservation_multi_room_mode( - self, - focus_ref, - after_action_ref, - _swipe_down_once, - _seek_reservation_room, - _sort_reservations, - _write_state, - ) - else: - # 单间模式:不需要回退,预约会在当前直播间自动识别和参与 - from douyin.core.fudai_modules.fudai_detail_helpers import ( - cleanup_expired_reservations_single_room, - ) - - cleanup_expired_reservations_single_room(self, _write_state) except Exception: pass diff --git a/douyin/core/fudai_modules/fudai_detail_helpers.py b/douyin/core/fudai_modules/fudai_detail_helpers.py index 981908d..fd0e136 100644 --- a/douyin/core/fudai_modules/fudai_detail_helpers.py +++ b/douyin/core/fudai_modules/fudai_detail_helpers.py @@ -42,6 +42,92 @@ def log_screenshot_failed( pass +def process_high_value_reservation_check(analyser, start_time: float) -> bool: + """run_once_basic 步骤 0.1.5:高价值预约启用且队列非空时的完整预约回退处理。 + + 流程: + 1. 定义 4 个回调闭包(_swipe_down_once / _sort_reservations / _write_state / _seek_reservation_room) + 2. 构建 focus_ref 和 after_action_ref + 3. 检查队列是否已满 → try_handle_queue_full_wait + 4. 根据状态写"跳过预约回退检查"原因日志 + 5. 检查是否所有预约已完成 → reset_to_first_room_after_all_reservations_done + 6. 写第一个预约检查日志 + 7. needswitch=True:多间模式调用 process_reservation_multi_room_mode + needswitch=False:单间模式调用 cleanup_expired_reservations_single_room + + 返回: + True:触发了早返回(队列已满等待 / 所有预约完成重置),调用方应直接 return + False:未触发早返回,调用方继续后续流程 + """ + from douyin.reservation import reservation_logic as douyin_reservation_logic + + def _swipe_down_once() -> None: + analyser.swipe_down_back_from_live() + + def _sort_reservations() -> None: + douyin_reservation_logic._sort_reservations_inplace(analyser.reservations) # type: ignore[attr-defined] + + def _write_state() -> None: + """将预约队列写入状态文件(节流,避免高频写盘卡顿)。""" + write_reservations_state_throttled(analyser) + + focus_ref = [float(analyser.reservation_focus_until_ts or 0.0)] + after_action_ref = [bool(analyser.reservation_after_action_pending or False)] + + def _seek_reservation_room(item: dict) -> bool: + try: + return bool( + analyser._seek_reservation_room(item, operation_type="预约") + ) + except Exception: + return False + + # 检查队列是否已满且需要等待 + if try_handle_queue_full_wait( + analyser, + focus_ref, + _swipe_down_once, + _seek_reservation_room, + _sort_reservations, + _write_state, + start_time, + ): + return True + + # 根据当前状态写"跳过预约回退检查"的原因日志(无副作用) + log_skip_preemption_reason(analyser) + + # 检查是否所有预约已完成,需要重置到第一个直播间 + if reset_to_first_room_after_all_reservations_done(analyser, _swipe_down_once): + return True + + # 单间模式(needswitch=False)下不启用预约回退逻辑 + try: + needswitch_cfg = bool(getattr(analyser, "needswitch", True)) + except Exception: + needswitch_cfg = True + + # 添加详细的调试日志,确认 needswitch 的值和预约信息 + log_first_reservation_check(analyser, needswitch_cfg) + + if needswitch_cfg: + # 多间模式:需要回退到预约直播间 + process_reservation_multi_room_mode( + analyser, + focus_ref, + after_action_ref, + _swipe_down_once, + _seek_reservation_room, + _sort_reservations, + _write_state, + ) + else: + # 单间模式:不需要回退,预约会在当前直播间自动识别和参与 + cleanup_expired_reservations_single_room(analyser, _write_state) + + return False + + def log_skip_preemption_reason(analyser) -> None: """run_once_basic:根据当前状态写"跳过预约回退检查"的原因日志(纯日志,无副作用)。