refactor(fudai): 抽 _handle_in_fudai_detail 步骤4 到 handle_detail_helpers (3174 → 2926)
新增 3 个 helper: - step4_make_join_decision:参与决策入口(detail=None 短路 + 调 basic_join_decision) - _handle_reservation_completion:参与成功是预约福袋时的锁定期处理 + 队列清理 - _close_detail_after_attend:根据 _detail_closed_by_attend 决定是否点 click_back 净减 251 行重复代码。新增 7 个 step4 单元测试覆盖: - detail=None 短路 - join_decision 失败不设标志 - countdown 有效设标志 + 关详情 - attend 已关详情时跳过 click_back - 预约福袋无其他预约 → 清锁定期 - 预约福袋有其他预约 → 设保护期 - countdown 已过期不设标志 Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
cea3aced73
commit
8a975f2bf6
3 files changed
+452
-251
No files matched your search
@@ -92,3 +92,268 @@ def handle_consecutive_failures_check(self: "fudai_analyse") -> bool:
|
||||
except Exception:
|
||||
pass
|
||||
return True
|
||||
|
||||
|
||||
def step4_make_join_decision(
|
||||
self: "fudai_analyse",
|
||||
detail,
|
||||
skip: bool,
|
||||
skip_reason: str,
|
||||
attend_before_ts: float,
|
||||
start_time: float,
|
||||
) -> None:
|
||||
"""步骤4:参与决策 + 等待开奖标志设置 + 预约清理 + 关闭详情页。
|
||||
|
||||
本函数总是消费完后返回(caller 即 _handle_in_fudai_detail 在调用本函数后
|
||||
也是方法末尾,不需要再判断返回值)。
|
||||
"""
|
||||
import time
|
||||
from typing import Any, Tuple, cast
|
||||
|
||||
from douyin.fudai import douyin_fudai_core
|
||||
from douyin.reservation import douyin_reservation_logic
|
||||
|
||||
step4_start = time.time()
|
||||
|
||||
if detail is None:
|
||||
try:
|
||||
self._log_kv("参与", "详情数据为 None,无法执行参与决策")
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
# 清除任务数量标志
|
||||
try:
|
||||
self._fudai_detail_task_count_from_stage = None
|
||||
try:
|
||||
self._log_kv(
|
||||
"福袋详情",
|
||||
"_handle_in_fudai_detail:清除任务数量标志",
|
||||
log_type="DEBUG",
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
elapsed = time.time() - start_time
|
||||
self._log_kv(
|
||||
"流程",
|
||||
"_handle_in_fudai_detail完成(详情为None)",
|
||||
elapsed_seconds=round(elapsed, 2),
|
||||
)
|
||||
return
|
||||
|
||||
join_result = douyin_fudai_core.basic_join_decision(
|
||||
self, detail, (skip, skip_reason)
|
||||
)
|
||||
|
||||
# 检查是否参与成功:如果 _last_successful_attend_ts 刚刚更新,说明参与成功
|
||||
attend_after_ts = getattr(self, "_last_successful_attend_ts", 0.0) or 0.0
|
||||
if not (join_result and attend_after_ts > attend_before_ts):
|
||||
return
|
||||
|
||||
# 参与成功,设置等待开奖标志
|
||||
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[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 remain_seconds > 0:
|
||||
# 设置等待开奖标志和预期开奖时间
|
||||
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),
|
||||
)
|
||||
|
||||
# 检查是否是预约福袋,如果是处理锁定期和移除预约
|
||||
_handle_reservation_completion(self, future_timestamp)
|
||||
|
||||
# 关闭详情页,返回直播间等待开奖
|
||||
_close_detail_after_attend(self)
|
||||
|
||||
# 清除第一次读取标志,下次打开新福袋时重新识别
|
||||
try:
|
||||
self._fudai_detail_first_read = False
|
||||
self._last_fudai_detail_tuple = None
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
# 清除任务数量标志
|
||||
try:
|
||||
self._fudai_detail_task_count_from_stage = None
|
||||
try:
|
||||
self._log_kv(
|
||||
"福袋详情",
|
||||
"_handle_in_fudai_detail:清除任务数量标志",
|
||||
log_type="DEBUG",
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
# 步骤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))
|
||||
|
||||
|
||||
def _handle_reservation_completion(
|
||||
self: "fudai_analyse", future_timestamp: float
|
||||
) -> None:
|
||||
"""参与成功是预约福袋时:处理锁定期 + 移除已完成的预约。"""
|
||||
import time
|
||||
|
||||
from douyin.reservation import douyin_reservation_logic
|
||||
|
||||
try:
|
||||
if not getattr(self, "reservation_after_action_pending", False):
|
||||
return
|
||||
|
||||
try:
|
||||
self._log_kv(
|
||||
"预约",
|
||||
"[预约] 参与成功,处理锁定期并移除预约",
|
||||
future_ts=int(future_timestamp),
|
||||
current_focus_until=int(
|
||||
getattr(self, "reservation_focus_until_ts", 0)
|
||||
),
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
# 检查是否还有其他预约
|
||||
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:
|
||||
# 还有其他预约,设置短暂保护期
|
||||
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:
|
||||
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
|
||||
|
||||
|
||||
def _close_detail_after_attend(self: "fudai_analyse") -> None:
|
||||
"""参与成功后关闭详情页(如果 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
|
||||
Reference in new issue
Block a user