fix: Simplify reservation backtracking and enhance participation logic
- Streamlined the `_handle_reservation_direct_attend()` method to directly use reservation details, eliminating the need for OCR recognition. - Introduced a `_force_attend_after_reservation_back` flag to bypass countdown checks during participation after a backtrack. - Updated the `basic_join_decision()` function to respect the new flag, ensuring immediate participation when set. - Enhanced logging for better traceability of reservation handling and participation decisions.
This commit is contained in:
1 parent
499002d3b1
commit
bdff3e7ba6
4 files changed
+4159
-280
No files matched your search
+4047
-14
File diff suppressed because it is too large.
Load diff
+88
-261
@@ -2763,17 +2763,20 @@ class fudai_analyse:
|
||||
except Exception:
|
||||
pass
|
||||
if reservation_direct_attend:
|
||||
# 预约回退后直接参与:跳过详情识别,直接参与
|
||||
# 预约回退后直接参与:使用正常流程,但设置强制参与标志
|
||||
try:
|
||||
self._reservation_back_direct_attend = False # 清除标志
|
||||
# 设置强制参与标志,让 basic_join_decision 跳过倒计时检查
|
||||
self._force_attend_after_reservation_back = True
|
||||
self._log_kv(
|
||||
"预约",
|
||||
"[预约] 预约回退后直接参与:跳过详情识别,直接参与",
|
||||
"[预约] 预约回退后直接参与:使用正常流程,设置强制参与标志",
|
||||
flag_cleared=True,
|
||||
force_attend=True,
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
# 直接参与,跳过详情识别
|
||||
# 使用正常流程处理
|
||||
self._handle_reservation_direct_attend()
|
||||
elapsed = time.time() - start_time
|
||||
self._log_kv(
|
||||
@@ -2814,10 +2817,13 @@ class fudai_analyse:
|
||||
# 预约回退后直接参与:查找福袋并打开详情页,然后直接参与
|
||||
try:
|
||||
self._reservation_back_direct_attend = False # 清除标志
|
||||
# 设置强制参与标志,让 basic_join_decision 跳过倒计时检查
|
||||
self._force_attend_after_reservation_back = True
|
||||
self._log_kv(
|
||||
"预约",
|
||||
"[预约] 预约回退后直接参与:查找福袋并打开详情页",
|
||||
"[预约] 预约回退后直接参与:查找福袋并打开详情页,设置强制参与标志",
|
||||
flag_cleared=True,
|
||||
force_attend=True,
|
||||
stage=stage,
|
||||
)
|
||||
except Exception:
|
||||
@@ -3639,276 +3645,97 @@ class fudai_analyse:
|
||||
|
||||
# ========== 福袋相关 ==========
|
||||
def _handle_reservation_direct_attend(self) -> None:
|
||||
"""预约回退后直接参与:跳过详情识别,根据任务数量多次参与。"""
|
||||
"""预约回退后直接参与:直接使用预约记录中的详情,跳过OCR识别。"""
|
||||
start_time = time.time()
|
||||
try:
|
||||
self._log_kv(
|
||||
"预约",
|
||||
"[预约] 预约回退后直接参与:跳过详情识别,直接参与",
|
||||
"[预约] 预约回退后直接参与:使用预约记录中的详情,跳过OCR识别",
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
# 获取任务数量(优先从预约记录中获取,如果没有则从阶段检测结果获取)
|
||||
task_count = 2 # 默认2个任务
|
||||
try:
|
||||
# 优先从预约队列中获取任务数量
|
||||
if self.reservations and len(self.reservations) > 0:
|
||||
first_reservation = self.reservations[0]
|
||||
reservation_task_count = first_reservation.get("task_count")
|
||||
if reservation_task_count is not None:
|
||||
task_count = int(reservation_task_count or 2)
|
||||
try:
|
||||
self._log_kv(
|
||||
"预约",
|
||||
"[预约] 预约回退后直接参与:从预约记录获取任务数量",
|
||||
task_count=task_count,
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
else:
|
||||
# 如果预约队列为空,从阶段检测结果获取
|
||||
task_count_from_stage = getattr(self, "_fudai_detail_task_count_from_stage", None)
|
||||
if task_count_from_stage is not None:
|
||||
task_count = int(task_count_from_stage or 2)
|
||||
# 清除标志,避免影响后续调用
|
||||
self._fudai_detail_task_count_from_stage = None
|
||||
else:
|
||||
# 如果阶段检测未保存任务数量,尝试通过阶段检测获取
|
||||
try:
|
||||
import douyin_stage
|
||||
self.operation.get_screenshot(self.device_id)
|
||||
result = douyin_stage.diagnose_stage_from_current_screen(self)
|
||||
if result:
|
||||
stage, _ = result
|
||||
if stage == "FUDai_DETAIL":
|
||||
# 尝试通过像素检测获取任务数量
|
||||
try:
|
||||
import douyin_pixel
|
||||
pic_path = os.path.join(os.path.dirname(__file__), "pic", "screenshot.png")
|
||||
if os.path.exists(pic_path):
|
||||
from PIL import Image
|
||||
pic = Image.open(pic_path)
|
||||
pic_rgb = pic.convert("RGB")
|
||||
rx = int(getattr(self, "resolution_ratio_x", 1080) or 1080)
|
||||
ry = int(getattr(self, "resolution_ratio_y", 2400) or 2400)
|
||||
y_pianyi = int(getattr(self, "y_pianyi", 0) or 0)
|
||||
result = douyin_pixel.detect_fudai_detail_page(
|
||||
pic_rgb, rx, ry, y_pianyi,
|
||||
template_threshold=float(getattr(self, "fudai_detail_template_threshold", 0.7) or 0.7),
|
||||
return_info=True, analyser=self
|
||||
)
|
||||
if isinstance(result, tuple):
|
||||
_, detect_info = result
|
||||
detected_task_count = detect_info.get("task_count", 0)
|
||||
if detected_task_count in (1, 2, 3):
|
||||
task_count = detected_task_count
|
||||
except Exception:
|
||||
pass
|
||||
except Exception:
|
||||
pass
|
||||
except Exception:
|
||||
task_count = 2
|
||||
|
||||
# 限制任务数量在合理范围内
|
||||
if task_count not in (1, 2, 3):
|
||||
task_count = 2
|
||||
|
||||
try:
|
||||
self._log_kv(
|
||||
"预约",
|
||||
"[预约] 预约回退后直接参与:检测到任务数量",
|
||||
task_count=task_count,
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
# 根据任务数量多次参与
|
||||
max_attend_times = task_count # 任务数量就是需要参与的次数
|
||||
attend_success_count = 0
|
||||
|
||||
for attend_round in range(max_attend_times):
|
||||
# 从预约记录中获取详情信息
|
||||
if not self.reservations or len(self.reservations) == 0:
|
||||
try:
|
||||
self.operation.get_screenshot(self.device_id)
|
||||
self.cut_pic(306, 2030, 780, 2110, "", "参与按钮")
|
||||
|
||||
# OCR识别参与按钮
|
||||
attend_button_text = str(
|
||||
self.operation.analyse_pic_word("参与按钮", 0) or ""
|
||||
self._log_kv(
|
||||
"预约",
|
||||
"[预约] 预约回退后直接参与:预约队列为空,无法获取详情",
|
||||
)
|
||||
if not attend_button_text or not any(
|
||||
k in attend_button_text
|
||||
for k in (
|
||||
"参与成功",
|
||||
"还需看播",
|
||||
"评论",
|
||||
"参与抽奖",
|
||||
"开始观看",
|
||||
"参与",
|
||||
)
|
||||
):
|
||||
attend_button_text = str(
|
||||
self.operation.analyse_pic_word("参与按钮", 1) or ""
|
||||
)
|
||||
|
||||
try:
|
||||
self._log_kv(
|
||||
"参与",
|
||||
f"预约回退后直接参与:第{attend_round + 1}/{max_attend_times}次参与",
|
||||
text=attend_button_text,
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
# 如果已参与,记录成功并继续下一次
|
||||
if (
|
||||
"参与成功" in attend_button_text
|
||||
or "还需看播" in attend_button_text
|
||||
or "等待开奖" in attend_button_text
|
||||
):
|
||||
attend_success_count += 1
|
||||
try:
|
||||
self._log_kv(
|
||||
"参与",
|
||||
f"预约回退后直接参与:第{attend_round + 1}次已参与",
|
||||
result="已参与",
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
# 如果不是最后一次,等待一下再继续
|
||||
if attend_round < max_attend_times - 1:
|
||||
self.operation.delay(0.5)
|
||||
continue
|
||||
|
||||
# 如果还需要参与,直接调用参与函数
|
||||
try:
|
||||
# 从预约队列中获取信息
|
||||
if self.reservations and len(self.reservations) > 0:
|
||||
first_reservation = self.reservations[0]
|
||||
future_ts = float(first_reservation.get("future_ts") or 0.0)
|
||||
prize_value_yuan = float(first_reservation.get("prize_value_yuan") or 0.0)
|
||||
content = str(first_reservation.get("content") or "")
|
||||
|
||||
# 构造简化的 detail tuple
|
||||
now_ts = time.time()
|
||||
countdown_start = now_ts
|
||||
countdown_end = future_ts if future_ts > 0 else (now_ts + 60.0)
|
||||
detail = (
|
||||
content, # content
|
||||
"", # time_text (不需要)
|
||||
0, # participants (不需要)
|
||||
0, # copies (不需要)
|
||||
prize_value_yuan, # prize_value_yuan
|
||||
(countdown_start, countdown_end), # countdown tuple
|
||||
)
|
||||
|
||||
# 直接调用参与决策
|
||||
import douyin_fudai_core
|
||||
join_result = douyin_fudai_core.basic_join_decision(self, detail)
|
||||
|
||||
if join_result:
|
||||
attend_success_count += 1
|
||||
try:
|
||||
self._log_kv(
|
||||
"参与",
|
||||
f"预约回退后直接参与:第{attend_round + 1}次参与成功",
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
else:
|
||||
try:
|
||||
self._log_kv(
|
||||
"参与",
|
||||
f"预约回退后直接参与:第{attend_round + 1}次参与失败或已跳过",
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
# 如果不是最后一次,等待一下再继续
|
||||
if attend_round < max_attend_times - 1:
|
||||
self.operation.delay(1.0) # 等待1秒再继续下一次参与
|
||||
else:
|
||||
try:
|
||||
self._log_kv(
|
||||
"参与",
|
||||
"预约回退后直接参与:预约队列为空,无法获取详情信息",
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
break
|
||||
except Exception as e:
|
||||
try:
|
||||
self._log_kv(
|
||||
"参与",
|
||||
f"预约回退后直接参与:第{attend_round + 1}次参与时出错",
|
||||
error=str(e),
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
except Exception as e:
|
||||
try:
|
||||
self._log_kv(
|
||||
"参与",
|
||||
f"预约回退后直接参与:第{attend_round + 1}次验证参与按钮异常",
|
||||
error=str(e),
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
except Exception:
|
||||
pass
|
||||
# 如果预约队列为空,回退到正常流程
|
||||
self._handle_in_fudai_detail()
|
||||
return
|
||||
|
||||
# 参与完成后,设置等待开奖标志
|
||||
if attend_success_count > 0:
|
||||
try:
|
||||
if self.reservations and len(self.reservations) > 0:
|
||||
first_reservation = self.reservations[0]
|
||||
future_ts = float(first_reservation.get("future_ts") or 0.0)
|
||||
if future_ts > 0:
|
||||
current_time = time.time()
|
||||
remain_seconds = int(future_ts - current_time)
|
||||
if remain_seconds > 0:
|
||||
# 设置等待开奖标志和预期开奖时间
|
||||
# 提前13秒,修正倒计时误差
|
||||
self._waiting_for_lottery_result = True
|
||||
self._lottery_result_expected_ts = future_ts - 13.0
|
||||
|
||||
try:
|
||||
self._log_kv(
|
||||
"参与",
|
||||
"预约回退后直接参与:参与完成,设置等待开奖标志",
|
||||
attend_success_count=attend_success_count,
|
||||
max_attend_times=max_attend_times,
|
||||
remain_seconds=remain_seconds,
|
||||
future_ts=int(future_ts),
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
except Exception as e:
|
||||
try:
|
||||
self._log_kv(
|
||||
"参与",
|
||||
"预约回退后直接参与:设置等待开奖标志时出错",
|
||||
error=str(e),
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
# 关闭详情页
|
||||
first_reservation = self.reservations[0]
|
||||
try:
|
||||
self.click(500, 470)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
elapsed = time.time() - start_time
|
||||
try:
|
||||
self._log_kv(
|
||||
"流程",
|
||||
"_handle_reservation_direct_attend完成",
|
||||
elapsed_seconds=round(elapsed, 2),
|
||||
task_count=task_count,
|
||||
attend_success_count=attend_success_count,
|
||||
max_attend_times=max_attend_times,
|
||||
# 从预约记录中获取信息
|
||||
content = str(first_reservation.get("content", "") or "")
|
||||
prize_value_yuan = float(first_reservation.get("prize_value_yuan", 0.0) or 0.0)
|
||||
future_ts = float(first_reservation.get("future_ts", 0.0) or 0.0)
|
||||
time_text = str(first_reservation.get("time_text", "") or "")
|
||||
participants = int(first_reservation.get("participants", 0) or 0)
|
||||
copies = int(first_reservation.get("copies", 0) or 0)
|
||||
|
||||
# 构造倒计时元组
|
||||
now_ts = time.time()
|
||||
countdown_start = now_ts
|
||||
countdown_end = future_ts if future_ts > 0 else (now_ts + 60.0)
|
||||
|
||||
# 构造 detail tuple(格式与 read_fudai_detail_once 返回的格式一致)
|
||||
detail = (
|
||||
content, # 0: 内容
|
||||
time_text, # 1: 倒计时文本
|
||||
participants, # 2: 参与人数
|
||||
copies, # 3: 份数
|
||||
prize_value_yuan, # 4: 价值
|
||||
(countdown_start, countdown_end), # 5: 倒计时元组
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
try:
|
||||
self._log_kv(
|
||||
"预约",
|
||||
"[预约] 预约回退后直接参与:从预约记录构造详情",
|
||||
content=content[:50] if content else "",
|
||||
prize_value_yuan=prize_value_yuan,
|
||||
future_ts=int(future_ts) if future_ts > 0 else 0,
|
||||
time_text=time_text,
|
||||
participants=participants,
|
||||
copies=copies,
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
# 保存详情数据,标记为已读取(用于后续可能的第二次打开)
|
||||
self._last_fudai_detail_tuple = detail
|
||||
self._fudai_detail_first_read = True
|
||||
|
||||
# 直接调用参与决策,跳过详情识别
|
||||
import douyin_fudai_core
|
||||
join_result = douyin_fudai_core.basic_join_decision(self, detail)
|
||||
|
||||
elapsed = time.time() - start_time
|
||||
try:
|
||||
self._log_kv(
|
||||
"流程",
|
||||
"_handle_reservation_direct_attend完成",
|
||||
elapsed_seconds=round(elapsed, 2),
|
||||
join_result=join_result,
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
except Exception as e:
|
||||
try:
|
||||
self._log_kv(
|
||||
"预约",
|
||||
"[预约] 预约回退后直接参与:从预约记录获取详情时出错,回退到正常流程",
|
||||
error=str(e),
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
# 如果出错,回退到正常流程
|
||||
self._handle_in_fudai_detail()
|
||||
|
||||
def _handle_in_fudai_detail(self) -> None:
|
||||
"""已经在福袋详情页:只跑详情识别 + 是否参与的基础逻辑。"""
|
||||
|
||||
+23
-4
@@ -2503,23 +2503,42 @@ def basic_join_decision(analyser: Any, detail: Tuple) -> bool:
|
||||
except Exception:
|
||||
join_ahead_seconds = 120
|
||||
|
||||
# 检查是否是回退后强制参与
|
||||
force_attend = getattr(analyser, "_force_attend_after_reservation_back", False)
|
||||
|
||||
# 如果剩余秒数在"预约参与阈值"内,立即参与而不是预约
|
||||
if remain_seconds is not None and remain_seconds <= join_ahead_seconds:
|
||||
# 或者如果是回退后强制参与,也立即参与
|
||||
if force_attend or (
|
||||
remain_seconds is not None and remain_seconds <= join_ahead_seconds
|
||||
):
|
||||
try:
|
||||
analyser._log_kv(
|
||||
"参与",
|
||||
"basic_join_decision:倒计时大于阈值,但剩余秒数在预约参与阈值内,立即参与",
|
||||
"basic_join_decision:倒计时大于阈值,但剩余秒数在预约参与阈值内或回退后强制参与,立即参与",
|
||||
reason=reason,
|
||||
value_yuan=prize_value_yuan,
|
||||
threshold=high_value_threshold,
|
||||
remain_seconds=int(remain_seconds),
|
||||
remain_seconds=(
|
||||
int(remain_seconds) if remain_seconds is not None else None
|
||||
),
|
||||
join_ahead_threshold=join_ahead_seconds,
|
||||
force_attend=force_attend,
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
# 不关闭详情,继续走立即参与逻辑
|
||||
skip = False
|
||||
reason = "high_value_immediate_join"
|
||||
reason = (
|
||||
"high_value_immediate_join"
|
||||
if not force_attend
|
||||
else "reservation_back_force_attend"
|
||||
)
|
||||
# 清除强制参与标志
|
||||
if force_attend:
|
||||
try:
|
||||
analyser._force_attend_after_reservation_back = False
|
||||
except Exception:
|
||||
pass
|
||||
else:
|
||||
# 剩余秒数大于"预约参与阈值",记录预约
|
||||
try:
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
{
|
||||
"updated_ts": 1766757516.1312146,
|
||||
"updated_ts": 1766758218.9022133,
|
||||
"reason": "startup_clear",
|
||||
"room_pos": 0,
|
||||
"current_room_title": "",
|
||||
|
||||
Reference in new issue
Block a user