From da098fafdc701c62fdd261e48d091477a0974637 Mon Sep 17 00:00:00 2001 From: Your Name Date: Fri, 16 Jan 2026 12:53:59 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=9B=B4=E6=96=B0=E7=A6=8F=E8=A2=8B?= =?UTF-8?q?=E7=9B=B8=E5=85=B3=E9=85=8D=E7=BD=AE=EF=BC=8C=E4=BC=98=E5=8C=96?= =?UTF-8?q?=E4=BB=BB=E5=8A=A1=E6=95=B0=E9=87=8F=E5=A4=84=E7=90=86=E9=80=BB?= =?UTF-8?q?=E8=BE=91=EF=BC=8C=E5=A2=9E=E5=BC=BA=E6=98=A0=E5=B0=84=E8=A1=A8?= =?UTF-8?q?=E8=AE=B0=E5=BD=95=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config/config.json | 6 +- douyin/core/douyin_fudai.py | 253 +++++++++++++++++--- douyin/fudai/douyin_fudai_core.py | 114 +++++++-- douyin/stage/douyin_stage.py | 50 ++-- fudai_detail_modules/fudai_detail_reader.py | 81 +++++-- 5 files changed, 420 insertions(+), 84 deletions(-) diff --git a/config/config.json b/config/config.json index 5b6f9b1..55dcd17 100644 --- a/config/config.json +++ b/config/config.json @@ -12,7 +12,7 @@ "auto_join_fansclub": true, "fansclub_fail_action": "switch", "high_value_fudai_enabled": true, - "high_value_fudai_threshold_yuan": 500.0, + "high_value_fudai_threshold_yuan": 1000.0, "high_value_fudai_join_ahead_seconds": 100, "high_value_fudai_max_swipe_back": 50, "high_value_fudai_max_reservations": 9999, @@ -31,7 +31,7 @@ "fudai_icon_max_wait_seconds": 5.0, "fudai_icon_retry_delay_seconds": 0.1, "fudai_exit_after_misses": 10, - "enable_back_on_misses": true, + "enable_back_on_misses": false, "fudai_click_wait_seconds": 1.5, "fudai_detail_initial_wait_seconds": 0.8, "fudai_detail_verified_wait_seconds": 0.2, @@ -39,7 +39,7 @@ "fudai_detail_save_debug_images": true, "fudai_disable_content_recognition": true, "fudai_disable_task_recognition": true, - "yanghao_recommend_duration_seconds": 180, + "yanghao_recommend_duration_seconds": 99999, "yanghao_recommend_interval_seconds": 600, "yanghao_like_prob": 1.0, "yanghao_fav_prob": 1.0, diff --git a/douyin/core/douyin_fudai.py b/douyin/core/douyin_fudai.py index 3817e1f..25c5a85 100644 --- a/douyin/core/douyin_fudai.py +++ b/douyin/core/douyin_fudai.py @@ -2793,6 +2793,20 @@ class fudai_analyse: except Exception: pass + # 清除任务数量标志 + try: + self._fudai_detail_task_count_from_stage = None + try: + analyser._log_kv( + "福袋详情", + "_handle_in_fudai_detail:清除任务数量标志", + log_type="DEBUG", + ) + except Exception: + pass + except Exception: + pass + # 步骤1结束计时 step1_elapsed = (time.time() - step1_start) * 1000 self._log_kv( @@ -2856,6 +2870,20 @@ class fudai_analyse: 总耗时毫秒=step2_elapsed, ) + # 清除任务数量标志 + try: + self._fudai_detail_task_count_from_stage = None + try: + analyser._log_kv( + "福袋详情", + "_handle_in_fudai_detail:清除任务数量标志", + log_type="DEBUG", + ) + except Exception: + pass + except Exception: + pass + elapsed = time.time() - start_time self._log_kv( "流程", @@ -3093,6 +3121,20 @@ class fudai_analyse: except Exception: pass + # 清除任务数量标志 + try: + self._fudai_detail_task_count_from_stage = None + try: + analyser._log_kv( + "福袋详情", + "_handle_in_fudai_detail:清除任务数量标志", + log_type="DEBUG", + ) + except Exception: + pass + except Exception: + pass + elapsed = time.time() - start_time self._log_kv( "流程", @@ -3116,30 +3158,15 @@ class fudai_analyse: self._last_fudai_detail_tuple = detail self._fudai_detail_first_read = True - # 成功识别福袋详情,立即记录到映射表 - try: - from douyin.reservation import douyin_reservation_handler - - douyin_reservation_handler.ReservationHandler.record_room_title_and_pos( - self - ) - self._log_kv( - "流程", - "_handle_in_fudai_detail:福袋详情识别成功,已记录到映射表", - fudai_content=(detail[0] if len(detail) > 0 else "")[:50], - prize_value_yuan=float( - detail[4] if len(detail) > 4 else 0.0 - ), - ) - except Exception as e: - try: - self._log_kv( - "流程", - "_handle_in_fudai_detail:记录映射表失败", - error=str(e), - ) - except Exception: - pass + # 映射表已在并行OCR中记录,这里不再重复记录 + self._log_kv( + "流程", + "_handle_in_fudai_detail:福袋详情识别成功", + fudai_content=(detail[0] if len(detail) > 0 else "")[:50], + prize_value_yuan=float( + detail[4] if len(detail) > 4 else 0.0 + ), + ) except Exception: pass else: @@ -3164,6 +3191,21 @@ class fudai_analyse: ) except Exception: pass + + # 清除任务数量标志 + try: + self._fudai_detail_task_count_from_stage = None + try: + analyser._log_kv( + "福袋详情", + "_handle_in_fudai_detail:清除任务数量标志", + log_type="DEBUG", + ) + except Exception: + pass + except Exception: + pass + elapsed = time.time() - start_time self._log_kv( "流程", @@ -3179,6 +3221,21 @@ class fudai_analyse: ) except Exception: pass + + # 清除任务数量标志 + try: + self._fudai_detail_task_count_from_stage = None + try: + analyser._log_kv( + "福袋详情", + "_handle_in_fudai_detail:清除任务数量标志", + log_type="DEBUG", + ) + except Exception: + pass + except Exception: + pass + elapsed = time.time() - start_time self._log_kv( "流程", @@ -3194,6 +3251,21 @@ class fudai_analyse: ) except Exception: pass + + # 清除任务数量标志 + try: + self._fudai_detail_task_count_from_stage = None + try: + analyser._log_kv( + "福袋详情", + "_handle_in_fudai_detail:清除任务数量标志", + log_type="DEBUG", + ) + except Exception: + pass + except Exception: + pass + elapsed = time.time() - start_time self._log_kv( "流程", @@ -3301,6 +3373,21 @@ class fudai_analyse: self._fudai_detail_consecutive_failures = 0 except Exception: pass + + # 清除任务数量标志 + try: + self._fudai_detail_task_count_from_stage = None + try: + analyser._log_kv( + "福袋详情", + "_handle_in_fudai_detail:清除任务数量标志", + log_type="DEBUG", + ) + except Exception: + pass + except Exception: + pass + elapsed = time.time() - start_time self._log_kv( "流程", @@ -3317,6 +3404,21 @@ class fudai_analyse: ) except Exception: pass + + # 清除任务数量标志 + try: + self._fudai_detail_task_count_from_stage = None + try: + analyser._log_kv( + "福袋详情", + "_handle_in_fudai_detail:清除任务数量标志", + log_type="DEBUG", + ) + except Exception: + pass + except Exception: + pass + elapsed = time.time() - start_time self._log_kv( "流程", @@ -3357,6 +3459,21 @@ class fudai_analyse: except Exception: pass douyin_fudai_detail.close_fudai_detail(self) + + # 清除任务数量标志 + try: + self._fudai_detail_task_count_from_stage = None + try: + analyser._log_kv( + "福袋详情", + "_handle_in_fudai_detail:清除任务数量标志", + log_type="DEBUG", + ) + except Exception: + pass + except Exception: + pass + elapsed = time.time() - start_time self._log_kv( "流程", @@ -3382,6 +3499,21 @@ class fudai_analyse: ) except Exception: pass + + # 清除任务数量标志 + try: + self._fudai_detail_task_count_from_stage = None + try: + analyser._log_kv( + "福袋详情", + "_handle_in_fudai_detail:清除任务数量标志", + log_type="DEBUG", + ) + except Exception: + pass + except Exception: + pass + elapsed = time.time() - start_time self._log_kv( "流程", @@ -3425,6 +3557,21 @@ class fudai_analyse: self._log_kv("参与", "详情数据为 None,无法执行参与决策") except Exception: pass + + # 清除任务数量标志 + try: + self._fudai_detail_task_count_from_stage = None + try: + analyser._log_kv( + "福袋详情", + "_handle_in_fudai_detail:清除任务数量标志", + log_type="DEBUG", + ) + except Exception: + pass + except Exception: + pass + elapsed = time.time() - start_time self._log_kv( "流程", @@ -3576,6 +3723,20 @@ class fudai_analyse: except Exception: pass + # 清除任务数量标志 + try: + self._fudai_detail_task_count_from_stage = None + try: + analyser._log_kv( + "福袋详情", + "_handle_in_fudai_detail:清除任务数量标志", + log_type="DEBUG", + ) + except Exception: + pass + except Exception: + pass + # 返回,让主循环只检测开奖结果,不执行其他操作 # 步骤4结束计时 step4_elapsed = (time.time() - step4_start) * 1000 @@ -3672,13 +3833,47 @@ class fudai_analyse: room_pos_ref: List[int] = [0] opened = douyin_flow_control.find_and_open_fudai_detail(self, room_pos_ref) if not opened: - # 无福袋,交给 flow_control 的“无福袋处理”逻辑 + # 无福袋:在切换直播间前记录映射表(用于预约回退校验、高价值预约表和高价值回访) + try: + self._record_room_title_and_pos() + self._log_kv( + "流程", + "_handle_live_room_has_or_not_fudai:无福袋直播间,已记录到映射表", + room_pos=room_pos_ref[0], + ) + except Exception as e: + try: + self._log_kv( + "流程", + "_handle_live_room_has_or_not_fudai:记录映射表失败", + error=str(e), + ) + except Exception: + pass + + # 无福袋,交给 flow_control 的"无福袋处理"逻辑 + # 根据配置决定是否允许切换直播间(needswitch=False 时只等待不切房) self._record_room_title_and_pos() + self._log_kv( + "流程", + "_handle_live_room_has_or_not_fudai:无福袋直播间,已记录到映射表", + room_pos=room_pos_ref[0], + ) + except Exception as e: + try: + self._log_kv( + "流程", + "_handle_live_room_has_or_not_fudai:记录映射表失败", + error=str(e), + ) + except Exception: + pass + + # 无福袋,交给 flow_control 的"无福袋处理"逻辑 # 根据配置决定是否允许切换直播间(needswitch=False 时只等待不切房) try: - needswitch_cfg = bool(getattr(self, "needswitch", True)) + need_switch_config = bool(getattr(self, "needswitch", True)) except Exception: - needswitch_cfg = True - + need_switch_config = True wait_times_ref = [0] swipe_times_ref = [0] reservation_focus_until_ts_ref = [0.0] @@ -3688,7 +3883,7 @@ class fudai_analyse: x=0, swipe_times=0, wait_times_ref=wait_times_ref, - needswitch=needswitch_cfg, + needswitch=need_switch_config, reservation_focus_until_ts=0.0, reservations=[], room_pos=room_pos_ref[0], diff --git a/douyin/fudai/douyin_fudai_core.py b/douyin/fudai/douyin_fudai_core.py index 9650244..9dba8a3 100644 --- a/douyin/fudai/douyin_fudai_core.py +++ b/douyin/fudai/douyin_fudai_core.py @@ -5261,6 +5261,40 @@ def basic_join_decision( room_title = current_room_title room_title_raw = current_room_title_raw + # 优先使用映射表中刚刚记录的直播间标题(如果存在) + # 这样可以确保高价值预约使用的是当前直播间的正确标题 + try: + room_pos_to_title_map = getattr(analyser, "_room_pos_to_title_map", {}) + if room_pos in room_pos_to_title_map: + map_info = room_pos_to_title_map[room_pos] + if isinstance(map_info, dict): + mapped_title = str(map_info.get("title", "")).strip() + mapped_title_raw = str(map_info.get("title_raw", "")).strip() + if mapped_title: + room_title = mapped_title + room_title_raw = mapped_title_raw + try: + analyser._log_kv( + "预约", + "使用映射表中的直播间标题", + room_pos=room_pos, + room_title=room_title[:50], + room_title_raw=room_title_raw[:50], + ) + except Exception: + pass + except Exception as e: + # 如果从映射表获取失败,继续使用缓存的标题 + try: + analyser._log_kv( + "预约", + "从映射表获取标题失败,使用缓存标题", + error=str(e), + room_pos=room_pos, + ) + except Exception: + pass + # 记录高价值预约(仅负责入队和写状态,不立即改变当前直播间) # 使用全局导入的模块(已在文件顶部导入) # 定义写入状态文件的回调函数(格式与 douyin_reservation.py 中的 write_reservations_state 一致) @@ -5537,19 +5571,20 @@ def basic_join_decision( write_state_callback=_write_reservation_state, ) - # 获取任务数量和其他详情信息(从阶段检测结果获取) + # 获取任务数量和其他详情信息(优先从缓存的福袋详情获取) task_count = 2 # 默认2个任务 time_text = "" participants = 0 copies = 0 try: - # 从 detail tuple 中获取信息 - if isinstance(detail, tuple) and len(detail) >= 6: - time_text = str(detail[1] or "") - participants = int(detail[2] or 0) - copies = int(detail[3] or 0) + # 优先从缓存的福袋详情中获取信息 + cached_detail = getattr(analyser, "_last_fudai_detail_tuple", None) + if cached_detail and isinstance(cached_detail, tuple) and len(cached_detail) >= 6: + time_text = str(cached_detail[1] or "") + participants = int(cached_detail[2] or 0) + copies = int(cached_detail[3] or 0) - # 从阶段检测结果获取任务数量 + # 从阶段检测结果获取任务数量(如果还没有被清除) task_count_from_stage = getattr( analyser, "_fudai_detail_task_count_from_stage", None ) @@ -5576,18 +5611,61 @@ def basic_join_decision( except Exception: pass else: - # 警告:未获取到任务数量,使用默认值0 - task_count = 0 + # 如果阶段检测结果已被清除,尝试从缓存的详情中获取任务数量 try: - analyser._log_kv( - "福袋详情", - "douyin_fudai_core:警告:未从阶段检测获取任务数量,使用默认值0", - log_type="WARNING", - 可能原因="阶段检测未触发或像素检测失败", - 建议="检查点击验证阶段检测是否正常执行", - ) - except Exception: - pass + cached_extra = getattr(analyser, "_last_fudai_detail_extra", None) + if cached_extra and isinstance(cached_extra, dict): + cached_task_count = cached_extra.get("task_count") + if cached_task_count is not None: + task_count = int(cached_task_count or 0) + try: + analyser._log_kv( + "福袋详情", + "douyin_fudai_core:从缓存详情获取任务数量", + log_type="OP", + 任务数量=task_count, + 来源="缓存详情", + ) + except Exception: + pass + else: + # 警告:未获取到任务数量,使用默认值0 + task_count = 0 + try: + analyser._log_kv( + "福袋详情", + "douyin_fudai_core:警告:未从缓存获取任务数量,使用默认值0", + log_type="WARNING", + 可能原因="缓存详情不完整或任务数量缺失", + 建议="检查福袋详情识别是否正常", + ) + except Exception: + pass + else: + # 警告:未获取到任务数量,使用默认值0 + task_count = 0 + try: + analyser._log_kv( + "福袋详情", + "douyin_fudai_core:警告:未从阶段检测获取任务数量,使用默认值0", + log_type="WARNING", + 可能原因="阶段检测未触发或像素检测失败", + 建议="检查点击验证阶段检测是否正常执行", + ) + except Exception: + pass + except Exception as e: + # 获取缓存失败,使用默认值 + task_count = 0 + try: + analyser._log_kv( + "福袋详情", + "douyin_fudai_core:从缓存获取任务数量失败,使用默认值", + error=str(e), + 使用默认值=True, + ) + except Exception: + pass # 限制任务数量在合理范围内 if task_count not in (1, 2, 3): diff --git a/douyin/stage/douyin_stage.py b/douyin/stage/douyin_stage.py index ce44b85..68644fc 100644 --- a/douyin/stage/douyin_stage.py +++ b/douyin/stage/douyin_stage.py @@ -737,11 +737,33 @@ def diagnose_stage_from_current_screen(analyser, trigger_reason="unknown"): # t if "has_fudai_detail" in pixel_details: detail_task_count = pixel_details["has_fudai_detail"].get("task_count") # 保存任务数量到 analyser 对象,供后续读取详情时使用 - # 确保 detail_task_count 不是 None 且触发原因是点击验证时才保存 - if ( - detail_task_count is not None - and trigger_reason == "find_and_open_fudai_detail_click_verification" - ): + # 特殊处理:如果是点击验证触发的原因,总是保存检测结果(包括0和None) + if trigger_reason == "find_and_open_fudai_detail_click_verification": + try: + analyser._fudai_detail_task_count_from_stage = detail_task_count + try: + _log( # type: ignore[misc] + "阶段检测:设置任务数量到 analyser(点击验证)", + 任务数量=detail_task_count, + 触发原因=trigger_reason, + ) + except Exception: + pass + # 如果任务数量 > 0,标记截图可以复用,避免OCR时重复截图 + if detail_task_count is not None and detail_task_count > 0: + analyser._screenshot_ready_for_ocr = True + try: + _log( # type: ignore[misc] + "标记截图可复用(任务数量>0)", + 任务数量=detail_task_count, + screenshot_ready=True, + ) + except Exception: + pass + except Exception: + pass + elif detail_task_count is not None and detail_task_count > 0: + # 其他情况下,只有检测到有效的任务数量才保存 try: analyser._fudai_detail_task_count_from_stage = detail_task_count try: @@ -765,19 +787,6 @@ def diagnose_stage_from_current_screen(analyser, trigger_reason="unknown"): # t pass except Exception: pass - else: - # 如果未检测到任务数量或触发原因不匹配,设置为空 - try: - analyser._fudai_detail_task_count_from_stage = None - try: - _log( # type: ignore[misc] - "阶段检测:任务数量未检测到或触发原因不匹配,设置为空", - 检测到的任务数量=detail_task_count, - 触发原因=trigger_reason, - ) - except Exception: - pass - except Exception: pass try: _log( # type: ignore[misc] @@ -787,8 +796,9 @@ def diagnose_stage_from_current_screen(analyser, trigger_reason="unknown"): # t ) except Exception: pass - else: - # detail_task_count 为 None,记录警告日志 + + # 如果任务数量为 None,记录警告日志 + if detail_task_count is None: try: _log( # type: ignore[misc] "警告:任务数量为 None,无法保存到 analyser 对象", diff --git a/fudai_detail_modules/fudai_detail_reader.py b/fudai_detail_modules/fudai_detail_reader.py index 48305ba..13aa539 100644 --- a/fudai_detail_modules/fudai_detail_reader.py +++ b/fudai_detail_modules/fudai_detail_reader.py @@ -99,20 +99,22 @@ def read_fudai_detail_once(analyser: Any) -> Tuple[str, Tuple, dict]: # 使用默认值 task_count = 0 - # 清除任务数量标志,截图复用标志留给后续OCR阶段使用 - try: - analyser._fudai_detail_task_count_from_stage = None - try: - analyser._log_kv( - "福袋详情", - "read_fudai_detail_once:清除任务数量标志", - log_type="DEBUG", - ) - except Exception: - pass - except Exception: - # 忽略异常,继续处理 - pass + # 注意:任务数量标志不由 read_fudai_detail_once 清除 + # 应该由调用方在完成所有相关处理后清除,避免影响后续逻辑 + # 保留此代码块以便将来需要时恢复 + # try: + # analyser._fudai_detail_task_count_from_stage = None + # try: + # analyser._log_kv( + # "福袋详情", + # "read_fudai_detail_once:清除任务数量标志", + # log_type="DEBUG", + # ) + # except Exception: + # pass + # except Exception: + # # 忽略异常,继续处理 + # pass # 如果任务数量为0,说明不在福袋详情页(阶段检测应该已经过滤掉了,这里作为兜底) # 在获取截图之前就返回错误,避免不必要的截图 @@ -279,6 +281,57 @@ def read_fudai_detail_once(analyser: Any) -> Tuple[str, Tuple, dict]: if task_count not in (1, 2, 3): task_count = 2 + # 并行记录映射表(用于预约回退校验、高价值预约表和高价值回访) + # 在OCR开始前启动并行任务,不阻塞OCR过程 + import threading + mapping_record_thread = None + try: + def _record_mapping_async(): + try: + # 导入并记录映射表 + from douyin.reservation import douyin_reservation_handler + douyin_reservation_handler.ReservationHandler.record_room_title_and_pos(analyser) + try: + analyser._log_kv( + "福袋详情", + "read_fudai_detail_once:并行记录映射表完成", + log_type="DEBUG", + ) + except Exception: + pass + except Exception as e: + try: + analyser._log_kv( + "福袋详情", + "read_fudai_detail_once:并行记录映射表失败", + error=str(e), + log_type="WARNING", + ) + except Exception: + pass + + # 启动并行线程记录映射表 + mapping_record_thread = threading.Thread(target=_record_mapping_async, daemon=True) + mapping_record_thread.start() + try: + analyser._log_kv( + "福袋详情", + "read_fudai_detail_once:启动并行映射表记录", + log_type="DEBUG", + ) + except Exception: + pass + except Exception as e: + try: + analyser._log_kv( + "福袋详情", + "read_fudai_detail_once:启动并行映射表记录失败", + error=str(e), + log_type="WARNING", + ) + except Exception: + pass + # 识别福袋内容 / 倒计时 / 价值 # 在OCR密集操作期间临时禁用截图服务,避免后台截图干扰 _ocr_screenshot_service_disabled = False