refactor(fudai): 抽 run_once_basic 中"高价值预约整段处理"块到 process_high_value_reservation_check

将 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) <noreply@anthropic.com>
This commit is contained in:
CodexandClaude Opus 4.7 committed 2026-05-22 12:41:27 +08:00
1 parent b1f70aa94d
commit f5b603e39e
2 files changed
+88 -93

No files matched your search

+2 -93
View File
@@ -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
@@ -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:根据当前状态写"跳过预约回退检查"的原因日志(纯日志,无副作用)。