From c5a70d4a623c07610362ca9e3429a993c2779f97 Mon Sep 17 00:00:00 2001 From: Codex Date: Fri, 22 May 2026 11:54:02 +0800 Subject: [PATCH] =?UTF-8?q?refactor(fudai):=20=E6=8A=BD=20=5Fhandle=5Fin?= =?UTF-8?q?=5Ffudai=5Fdetail=20=E4=B8=AD"=E5=8F=82=E4=B8=8E=E6=88=90?= =?UTF-8?q?=E5=8A=9F=E5=90=8E=E9=A2=84=E7=BA=A6=E5=A4=84=E7=90=86"?= =?UTF-8?q?=E5=9D=97=E5=88=B0=20handle=5Freservation=5Fafter=5Fattend=5Fsu?= =?UTF-8?q?ccess?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 将参与成功后的预约清理逻辑(约 116 行)抽到 fudai_detail_helpers.handle_reservation_after_attend_success。逻辑: - 如果还有其他预约:设置 10 秒保护期确保依次参与 - 如果没有其他预约:清除锁定期 - 从队列中移除当前刚完成的预约(5 秒误差匹配) - 重新排序剩余队列 douyin_fudai.py: 2260 → 2144 行 (-116) fudai_detail_helpers.py: 1495 → 1614 行 douyin_fudai.py 跌破 2200 行(4121 → 2144,本会话累计削减 1977 行 = -48%) 验证: - pytest 1013 passed - compile OK Co-Authored-By: Claude Opus 4.7 (1M context) --- douyin/core/douyin_fudai.py | 124 +----------------- .../fudai_modules/fudai_detail_helpers.py | 119 +++++++++++++++++ 2 files changed, 123 insertions(+), 120 deletions(-) diff --git a/douyin/core/douyin_fudai.py b/douyin/core/douyin_fudai.py index 7ac87ec..4cf19d4 100644 --- a/douyin/core/douyin_fudai.py +++ b/douyin/core/douyin_fudai.py @@ -1954,127 +1954,11 @@ class fudai_analyse: ) # 检查是否是预约福袋,如果是,处理锁定期和移除预约 - try: - if getattr(self, "reservation_after_action_pending", False): - # 这是预约福袋,参与成功后处理锁定期和移除预约 - try: - self._log_kv( - "预约", - "[预约] 参与成功,处理锁定期并移除预约", - future_ts=int(future_timestamp), - current_focus_until=int( - getattr(self, "reservation_focus_until_ts", 0) - ), - ) - except Exception: - pass + from douyin.core.fudai_modules.fudai_detail_helpers import ( + handle_reservation_after_attend_success, + ) - # 检查是否还有其他预约,如果有则设置保护期,否则清除锁定期 - has_other_reservations = False - if hasattr(self, "reservations") and self.reservations: - # 检查是否有其他预约(排除当前刚完成的预约) - for r in self.reservations: - try: - r_ft = float(r.get("future_ts") or 0.0) - if ( - r_ft > 0 - and abs(r_ft - future_timestamp) >= 5.0 - ): # 排除当前预约 - has_other_reservations = True - break - except Exception: - continue - - if has_other_reservations: - # 还有其他预约,设置短暂保护期(10秒)确保依次参与 - protection_until = time.time() + 10.0 - self.reservation_focus_until_ts = max( - self.reservation_focus_until_ts, protection_until - ) - try: - self._log_kv( - "预约", - "[预约] 参与成功,设置保护期确保依次参与", - protection_seconds=10, - new_focus_until=int( - self.reservation_focus_until_ts - ), - remaining_reservations=len( - [ - r - for r in self.reservations - if float(r.get("future_ts", 0)) > 0 - ] - ), - ) - except Exception: - pass - else: - # 没有其他预约,清除锁定期 - self.reservation_focus_until_ts = 0.0 - try: - self._log_kv( - "预约", - "[预约] 参与成功,清除锁定期(无其他预约)", - future_ts=int(future_timestamp), - ) - except Exception: - pass - - self.reservation_after_action_pending = False - - # 从预约队列中移除已完成的预约(匹配 future_ts,允许5秒误差) - if hasattr(self, "reservations") and self.reservations: - # use module-level `douyin_reservation_logic` - reservations_to_remove = [] - for idx, r in enumerate(self.reservations): - try: - r_ft = float(r.get("future_ts") or 0.0) - if ( - r_ft > 0 - and abs(r_ft - future_timestamp) < 5.0 - ): - reservations_to_remove.append(idx) - except Exception: - continue - - # 从后往前删除,避免索引变化 - for idx in reversed(reservations_to_remove): - try: - removed = self.reservations.pop(idx) - try: - self._log_kv( - "预约", - "[预约] 已从队列中移除已完成的预约", - future_ts=int( - removed.get("future_ts", 0) - ), - content=str(removed.get("content", ""))[ - :50 - ], - ) - except Exception: - pass - except Exception: - pass - - # 重新排序预约队列 - if self.reservations: - try: - douyin_reservation_logic._sort_reservations_inplace( - self.reservations - ) - except Exception: - pass - except Exception as e: - try: - self._log_kv( - "预约", - "[预约] 清除锁定期和移除预约时出错", - error=str(e), - ) - except Exception: - pass + handle_reservation_after_attend_success(self, future_timestamp) # 关闭详情页,返回直播间等待开奖 # 检查 attend_choujiang 是否已经关闭了详情,如果已关闭就不应该再点击返回键 diff --git a/douyin/core/fudai_modules/fudai_detail_helpers.py b/douyin/core/fudai_modules/fudai_detail_helpers.py index e5373a8..0d2dd6c 100644 --- a/douyin/core/fudai_modules/fudai_detail_helpers.py +++ b/douyin/core/fudai_modules/fudai_detail_helpers.py @@ -42,6 +42,125 @@ def log_screenshot_failed( pass +def handle_reservation_after_attend_success(analyser, future_timestamp: float) -> None: + """参与成功后处理预约队列:清除锁定期 / 设置保护期 / 移除已完成预约。 + + 用于 _handle_in_fudai_detail 步骤4 参与成功后的清理。 + """ + try: + if not getattr(analyser, "reservation_after_action_pending", False): + return + + try: + analyser._log_kv( + "预约", + "[预约] 参与成功,处理锁定期并移除预约", + future_ts=int(future_timestamp), + current_focus_until=int( + getattr(analyser, "reservation_focus_until_ts", 0) + ), + ) + except Exception: + pass + + # 检查是否还有其他预约 + has_other_reservations = False + if hasattr(analyser, "reservations") and analyser.reservations: + for r in analyser.reservations: + try: + r_ft = float(r.get("future_ts") or 0.0) + if r_ft > 0 and abs(r_ft - future_timestamp) >= 5.0: + has_other_reservations = True + break + except Exception: + continue + + if has_other_reservations: + # 还有其他预约,设置短暂保护期(10秒)确保依次参与 + protection_until = time.time() + 10.0 + analyser.reservation_focus_until_ts = max( + analyser.reservation_focus_until_ts, protection_until + ) + try: + analyser._log_kv( + "预约", + "[预约] 参与成功,设置保护期确保依次参与", + protection_seconds=10, + new_focus_until=int(analyser.reservation_focus_until_ts), + remaining_reservations=len( + [ + r + for r in analyser.reservations + if float(r.get("future_ts", 0)) > 0 + ] + ), + ) + except Exception: + pass + else: + # 没有其他预约,清除锁定期 + analyser.reservation_focus_until_ts = 0.0 + try: + analyser._log_kv( + "预约", + "[预约] 参与成功,清除锁定期(无其他预约)", + future_ts=int(future_timestamp), + ) + except Exception: + pass + + analyser.reservation_after_action_pending = False + + # 从预约队列中移除已完成的预约(匹配 future_ts,允许 5 秒误差) + if hasattr(analyser, "reservations") and analyser.reservations: + from douyin.reservation import ( + reservation_logic as douyin_reservation_logic, + ) + + reservations_to_remove = [] + for idx, r in enumerate(analyser.reservations): + try: + r_ft = float(r.get("future_ts") or 0.0) + if r_ft > 0 and abs(r_ft - future_timestamp) < 5.0: + reservations_to_remove.append(idx) + except Exception: + continue + + # 从后往前删除,避免索引变化 + for idx in reversed(reservations_to_remove): + try: + removed = analyser.reservations.pop(idx) + try: + analyser._log_kv( + "预约", + "[预约] 已从队列中移除已完成的预约", + future_ts=int(removed.get("future_ts", 0)), + content=str(removed.get("content", ""))[:50], + ) + except Exception: + pass + except Exception: + pass + + # 重新排序预约队列 + if analyser.reservations: + try: + douyin_reservation_logic._sort_reservations_inplace( + analyser.reservations + ) + except Exception: + pass + except Exception as e: + try: + analyser._log_kv( + "预约", + "[预约] 清除锁定期和移除预约时出错", + error=str(e), + ) + except Exception: + pass + + def handle_simple_skip_status( analyser, start_time: float, log_message: str, completion_label: str ) -> None: