refactor(fudai): 抽 _handle_in_fudai_detail 步骤4"参与成功后处理"块到 handle_attend_success_post_processing
将参与成功后的清理逻辑(约 90 行)抽到 fudai_detail_helpers.handle_attend_success_post_processing。逻辑: - 检查 _last_successful_attend_ts 是否更新 - 从 detail 提取倒计时 - 设置 _waiting_for_lottery_result / _lottery_result_expected_ts - 调用 handle_reservation_after_attend_success - 关闭详情页(如未由 attend_choujiang 关闭) - 清除标志 + 写步骤4完成日志 顺带删除了 1984-1996 行重复 2 次的"清除第一次读取标志"块。 douyin_fudai.py: 2102 → 2020 行 (-82) fudai_detail_helpers.py: 1678 → 1769 行 douyin_fudai.py 跌破 2100 行(4121 → 2020,本会话累计削减 2101 行 = -51%) 验证: - pytest 1013 passed - compile OK Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
23e24a9f27
commit
4f042f95cd
2 files changed
+98
-89
No files matched your search
@@ -1919,96 +1919,14 @@ class fudai_analyse:
|
||||
)
|
||||
|
||||
# 检查是否参与成功:如果 _last_successful_attend_ts 刚刚更新,说明参与成功
|
||||
attend_after_ts = getattr(self, "_last_successful_attend_ts", 0.0) or 0.0
|
||||
if join_result and attend_after_ts > attend_before_ts:
|
||||
# 参与成功,设置等待开奖标志,停止其他行为
|
||||
try:
|
||||
# 从 detail 中提取倒计时信息
|
||||
countdown_tuple = None
|
||||
remain_seconds = 0
|
||||
future_timestamp = 0.0
|
||||
if isinstance(detail, tuple) and len(detail) >= 6:
|
||||
d_cast: Tuple[Any, ...] = cast(Tuple[Any, ...], detail)
|
||||
countdown_tuple = d_cast[5]
|
||||
if isinstance(countdown_tuple, tuple) and len(countdown_tuple) >= 2:
|
||||
# countdown_tuple[0] 是 countdown_start(开始时间戳)
|
||||
# countdown_tuple[1] 是 countdown_end(结束时间戳/开奖时间戳)
|
||||
future_timestamp = float(countdown_tuple[1] or 0.0)
|
||||
# 计算剩余秒数:结束时间戳 - 当前时间
|
||||
current_time = time.time()
|
||||
if future_timestamp > current_time:
|
||||
remain_seconds = int(future_timestamp - current_time)
|
||||
if join_result:
|
||||
from douyin.core.fudai_modules.fudai_detail_helpers import (
|
||||
handle_attend_success_post_processing,
|
||||
)
|
||||
|
||||
if remain_seconds > 0:
|
||||
# 设置等待开奖标志和预期开奖时间
|
||||
# 直接保存开奖时间戳,等待时直接用 future_timestamp - time.time() 计算剩余秒数
|
||||
self._waiting_for_lottery_result = True
|
||||
self._lottery_result_expected_ts = future_timestamp
|
||||
self._lottery_wait_start_ts = time.time()
|
||||
|
||||
self._log_kv(
|
||||
"参与",
|
||||
"参与成功,设置等待开奖标志,停止其他行为",
|
||||
remain_seconds=remain_seconds,
|
||||
future_ts=int(future_timestamp),
|
||||
)
|
||||
|
||||
# 检查是否是预约福袋,如果是,处理锁定期和移除预约
|
||||
from douyin.core.fudai_modules.fudai_detail_helpers import (
|
||||
handle_reservation_after_attend_success,
|
||||
)
|
||||
|
||||
handle_reservation_after_attend_success(self, future_timestamp)
|
||||
|
||||
# 关闭详情页,返回直播间等待开奖
|
||||
# 检查 attend_choujiang 是否已经关闭了详情,如果已关闭就不应该再点击返回键
|
||||
detail_already_closed = getattr(
|
||||
self, "_detail_closed_by_attend", False
|
||||
)
|
||||
if not detail_already_closed:
|
||||
try:
|
||||
self.operation.click_back(self.device_id)
|
||||
self.operation.delay(1.0)
|
||||
self._log_kv("参与", "已关闭详情页,返回直播间等待开奖")
|
||||
except Exception:
|
||||
pass
|
||||
else:
|
||||
try:
|
||||
self._log_kv(
|
||||
"参与",
|
||||
"详情页已由 attend_choujiang 关闭,跳过返回键点击",
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
# 清除第一次读取标志,下次打开新福袋时重新识别
|
||||
try:
|
||||
self._fudai_detail_first_read = False
|
||||
self._last_fudai_detail_tuple = None
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
# 清除第一次读取标志,下次打开新福袋时重新识别
|
||||
try:
|
||||
self._fudai_detail_first_read = False
|
||||
self._last_fudai_detail_tuple = None
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
# 清除任务数量标志
|
||||
clear_task_count_flag(self)
|
||||
|
||||
# 返回,让主循环只检测开奖结果,不执行其他操作
|
||||
# 步骤4结束计时
|
||||
step4_elapsed = (time.time() - step4_start) * 1000
|
||||
self._log_kv(
|
||||
"详情处理", "步骤4[参与决策]-完成", 总耗时毫秒=step4_elapsed
|
||||
)
|
||||
return
|
||||
else:
|
||||
self._log_kv("参与", "参与成功但无法获取倒计时,跳过等待")
|
||||
except Exception as e:
|
||||
self._log_kv("参与", "设置等待开奖标志时出错", error=str(e))
|
||||
handle_attend_success_post_processing(
|
||||
self, detail, start_time, step4_start, attend_before_ts
|
||||
)
|
||||
|
||||
def _handle_live_room_has_or_not_fudai(self) -> None:
|
||||
"""在直播间:寻找福袋图标;有则点开详情,无则交给 flow_control 决定"继续找/切换/养号"."""
|
||||
|
||||
@@ -42,6 +42,97 @@ def log_screenshot_failed(
|
||||
pass
|
||||
|
||||
|
||||
def handle_attend_success_post_processing(
|
||||
analyser, detail, start_time: float, step4_start: float, attend_before_ts: float
|
||||
) -> bool:
|
||||
"""_handle_in_fudai_detail 步骤4:参与成功后的清理逻辑。
|
||||
|
||||
检查参与是否成功(_last_successful_attend_ts 是否更新),如果成功:
|
||||
1. 从 detail 提取倒计时信息
|
||||
2. 设置 _waiting_for_lottery_result / _lottery_result_expected_ts
|
||||
3. 调用 handle_reservation_after_attend_success
|
||||
4. 关闭详情页(如未由 attend_choujiang 关闭)
|
||||
5. 清除 _fudai_detail_first_read 和 _last_fudai_detail_tuple
|
||||
6. 清除任务数量标志
|
||||
7. 写步骤4完成日志
|
||||
|
||||
返回:
|
||||
True:参与成功并已完成清理(调用方应直接 return)
|
||||
False:参与未成功或无倒计时,调用方继续后续逻辑
|
||||
"""
|
||||
attend_after_ts = getattr(analyser, "_last_successful_attend_ts", 0.0) or 0.0
|
||||
if not attend_after_ts > attend_before_ts:
|
||||
return False
|
||||
|
||||
# 参与成功,设置等待开奖标志,停止其他行为
|
||||
try:
|
||||
countdown_tuple = None
|
||||
remain_seconds = 0
|
||||
future_timestamp = 0.0
|
||||
if isinstance(detail, tuple) and len(detail) >= 6:
|
||||
countdown_tuple = detail[5]
|
||||
if isinstance(countdown_tuple, tuple) and len(countdown_tuple) >= 2:
|
||||
future_timestamp = float(countdown_tuple[1] or 0.0)
|
||||
current_time = time.time()
|
||||
if future_timestamp > current_time:
|
||||
remain_seconds = int(future_timestamp - current_time)
|
||||
|
||||
if remain_seconds <= 0:
|
||||
analyser._log_kv("参与", "参与成功但无法获取倒计时,跳过等待")
|
||||
return False
|
||||
|
||||
# 设置等待开奖标志和预期开奖时间
|
||||
analyser._waiting_for_lottery_result = True
|
||||
analyser._lottery_result_expected_ts = future_timestamp
|
||||
analyser._lottery_wait_start_ts = time.time()
|
||||
|
||||
analyser._log_kv(
|
||||
"参与",
|
||||
"参与成功,设置等待开奖标志,停止其他行为",
|
||||
remain_seconds=remain_seconds,
|
||||
future_ts=int(future_timestamp),
|
||||
)
|
||||
|
||||
handle_reservation_after_attend_success(analyser, future_timestamp)
|
||||
|
||||
# 关闭详情页(如未由 attend_choujiang 关闭)
|
||||
detail_already_closed = getattr(analyser, "_detail_closed_by_attend", False)
|
||||
if not detail_already_closed:
|
||||
try:
|
||||
analyser.operation.click_back(analyser.device_id)
|
||||
analyser.operation.delay(1.0)
|
||||
analyser._log_kv("参与", "已关闭详情页,返回直播间等待开奖")
|
||||
except Exception:
|
||||
pass
|
||||
else:
|
||||
try:
|
||||
analyser._log_kv(
|
||||
"参与",
|
||||
"详情页已由 attend_choujiang 关闭,跳过返回键点击",
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
# 清除第一次读取标志
|
||||
try:
|
||||
analyser._fudai_detail_first_read = False
|
||||
analyser._last_fudai_detail_tuple = None
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
clear_task_count_flag(analyser)
|
||||
|
||||
# 步骤4结束计时
|
||||
step4_elapsed = (time.time() - step4_start) * 1000
|
||||
analyser._log_kv(
|
||||
"详情处理", "步骤4[参与决策]-完成", 总耗时毫秒=step4_elapsed
|
||||
)
|
||||
return True
|
||||
except Exception as e:
|
||||
analyser._log_kv("参与", "设置等待开奖标志时出错", error=str(e))
|
||||
return False
|
||||
|
||||
|
||||
def should_skip_fudai_search_in_lock_period(analyser, start_time: float) -> bool:
|
||||
"""_handle_live_room_has_or_not_fudai 早返回:锁定期内时间窗口未到。
|
||||
|
||||
|
||||
Reference in new issue
Block a user