from __future__ import annotations """ Flow control main helpers (本地模块化实现,基础版) ================================================ 当前只实现与你"基础抢福袋、不启用预约"相关的核心流程: - handle_stage_after_diagnosis:记录阶段信息(暂不做复杂跳转) - find_and_open_fudai_detail:通过像素检测福袋图标并点击打开详情 - handle_no_fudai_situation:在没有福袋时做简单的等待/上滑切换 - filter_fudai_detail:根据 config.json 对福袋进行基础筛选 """ from typing import Any, List, Tuple import time from douyin_detection import check_have_fudai_with_ready_screenshot from douyin_fudai_core import should_skip_fudai_by_config import douyin_reservation_logic def handle_stage_after_diagnosis( analyser: Any, stage: str, seen: str, loop_count: int, ) -> bool: """记录阶段诊断结果,当前不改变主流程走向。""" try: analyser._log_kv( "流程", "handle_stage_after_diagnosis", stage=stage, seen=seen, loop_count=loop_count, ) except Exception: pass return False def find_and_open_fudai_detail( analyser: Any, room_pos_ref: List[int], ) -> bool: """在当前直播间查找福袋图标并尝试点击打开详情(基础版,不做预约)。""" device_id = getattr(analyser, "device_id", "") # 1. 利用像素检测福袋图标的 X 坐标(1080 基准) x = check_have_fudai_with_ready_screenshot(analyser, screenshot_ready=False) if not x: try: analyser._log_kv("流程", "find_and_open_fudai_detail:未检测到福袋图标", x=int(x or 0)) except Exception: pass return False # 点击坐标:用检测到的 X,加一个固定基准 Y(图标所在高度) try: rx = int(getattr(analyser, "resolution_ratio_x", 1080) or 1080) ry = int(getattr(analyser, "resolution_ratio_y", 2400) or 2400) except Exception: rx, ry = 1080, 2400 base_y = 403 # 与检测时使用的同一水平线附近 click_x = int(x * rx / 1080) click_y = int(base_y * ry / 2400) try: analyser._log_kv( "流程", "find_and_open_fudai_detail:检测到福袋图标并点击", fudai_x=int(x), click_x=click_x, click_y=click_y, ) except Exception: pass # 优先走底层 operation.click,失败再尝试 analyser.click try: analyser.operation.click(device_id, click_x, click_y) analyser.operation.delay(1.0) return True except Exception: try: analyser.click(x, base_y) return True except Exception: return False def handle_no_fudai_situation( analyser: Any, x: int, swipe_times: int, wait_times_ref: List[int], needswitch: bool, reservation_focus_until_ts: float, reservations: list, room_pos: int, post_switch_delay_s: float, swipe_up_switch_callback, swipe_down_back_callback, sort_reservations_callback, clear_reservations_callback, write_state_callback, back_to_list_callback, into_list_callback, reservation_focus_until_ts_ref: List[float], reset_to_list_after_reservations_ref: List[bool], swipe_times_ref: List[int], ) -> bool: """当前直播间没有福袋时的基础处理。 本地版实现: - 先让预约模块有机会“抢占”流程(例如到点需要回退到预约直播间); - 若未触发预约回退,则执行简单的等待 + 上滑切换。 """ try: analyser._log_kv( "流程", "handle_no_fudai_situation", x=x, swipe_times=swipe_times, room_pos=room_pos, reservations=len(reservations), ) except Exception: pass # --- 优先让“预约回退窗口”有机会执行 --- try: if getattr(analyser, "high_value_fudai_enabled", False) and reservations: # 构造回调:下滑回退一个直播间 def _swipe_down_once() -> None: try: if swipe_down_back_callback: swipe_down_back_callback() except Exception: pass def _sort_reservations() -> None: try: # 使用统一核心逻辑排序 douyin_reservation_logic._sort_reservations_inplace(reservations) # type: ignore[attr-defined] except Exception: pass def _write_state() -> None: try: if write_state_callback: write_state_callback() except Exception: pass # 调用预约窗口处理:若返回 True,表示已触发回退,本轮不再上滑 def _seek_reservation_room(item: dict) -> bool: """委托给 analyser._seek_reservation_room,避免简化为恒 True。""" try: fn = getattr(analyser, "_seek_reservation_room", None) if not fn: return False return bool(fn(item)) except Exception: return False preempted = douyin_reservation_logic.process_reservation_attend_window( analyser=analyser, reservations=reservations, room_pos=room_pos, reservation_focus_until_ts_ref=reservation_focus_until_ts_ref, reservation_after_action_pending_ref=[False], reservation_back_swipe_delay_s=float( getattr(analyser, "reservation_back_swipe_delay", 0.6) or 0.6 ), swipe_down_back_callback=_swipe_down_once, seek_reservation_room_callback=_seek_reservation_room, sort_reservations_callback=_sort_reservations, write_state_callback=_write_state, ) if preempted: return True except Exception: # 预约处理异常不影响基础切换逻辑 pass # 简单策略:轻微等待 + 上滑切换 try: analyser.operation.delay(1.0) except Exception: time.sleep(1.0) if needswitch and swipe_up_switch_callback: swipe_up_switch_callback() try: analyser.operation.delay(post_switch_delay_s or 1.2) except Exception: time.sleep(post_switch_delay_s or 1.2) return False def filter_fudai_detail( analyser: Any, detail: Tuple, x: int, needswitch: bool, post_switch_delay_s: float, close_detail_callback, swipe_up_switch_callback, add_reservation_callback, ) -> bool: """福袋详情过滤逻辑:根据 config.json 对福袋进行基础筛选。""" skip, reason = should_skip_fudai_by_config(analyser, detail) try: content = str(detail[0] or "") if isinstance(detail, tuple) and len(detail) >= 1 else "" analyser._log_kv( "流程", "filter_fudai_detail", x=x, needswitch=needswitch, skip=int(bool(skip)), reason=reason, content_preview=content[:60] if content else "", ) except Exception: pass if not skip: return False try: close_detail_callback(analyser) except Exception: try: analyser.operation.click_back(analyser.device_id) except Exception: pass if needswitch and swipe_up_switch_callback: try: swipe_up_switch_callback() analyser.operation.delay(post_switch_delay_s or 1.2) except Exception: try: time.sleep(post_switch_delay_s or 1.2) except Exception: pass return True return False