feat: 调整配置和逻辑,优化等待开奖和日志显示体验

This commit is contained in:
Your Name committed 2026-01-07 07:58:11 +08:00
1 parent c6eb76845f
commit a3175aaddb
6 files changed
+224 -218

No files matched your search

+1 -1
View File
@@ -7,7 +7,7 @@
"max_participants": 0,
"skip_if_countdown_gt_seconds": 180,
"reset_after_swipes_enabled": true,
"reset_after_swipes": 30,
"reset_after_swipes": 50,
"reset_after_swipes_action": "back",
"auto_join_fansclub": true,
"fansclub_fail_action": "switch",
+173 -193
View File
@@ -1031,227 +1031,156 @@ class fudai_analyse:
start_time = time.time()
# 直接参与标志已移除,相关旧逻辑已清理,继续按正常流程执行
# 0. 如果正在等待开奖,检查高价值预约是否需要抢占,否则只检测开奖结果
# 0. 如果正在等待开奖:不在此期间检查并抢占其他预约,避免放弃当前正在参加的直播间
if getattr(self, "_waiting_for_lottery_result", False):
try:
# 检查是否有高价值预约需要处理(除非遇到高价值预约,否则应该等待福袋开奖)
should_preempt = False
try:
if getattr(self, "high_value_fudai_enabled", False) and getattr(
self, "reservations", None
):
reservations = self.reservations
if reservations and len(reservations) > 0:
# 检查是否有预约需要立即处理
reservation_focus_until_ts_ref = [0.0]
reservation_after_action_pending_ref = [False]
room_pos_ref = [getattr(self, "room_pos", 0) or 0]
# 在等待开奖期间只检测开奖结果,不触发预约抢占逻辑,
# 以确保按顺序完成当前参与流程。
future_ts = getattr(self, "_lottery_result_expected_ts", 0.0) or 0.0
current_ts = time.time()
remain_seconds = max(0, future_ts - current_ts + 6.0)
# 检查是否有预约需要抢占
preempted = douyin_reservation_logic.process_reservation_attend_window(
self,
reservations,
room_pos_ref[0],
reservation_focus_until_ts_ref,
reservation_after_action_pending_ref,
float(
getattr(self, "reservation_back_swipe_delay_s", 0.5)
or 0.5
),
self.swipe_down_back_from_live,
self._seek_reservation_room,
lambda: (
douyin_reservation_logic._sort_reservations_inplace(
self.reservations
)
if self.reservations
else None
),
lambda: None, # write_state_callback
)
# 开奖时间 + 1秒缓冲后开始检测开奖结果
check_start_ts = future_ts + 1.0
timeout_ts = future_ts + 10.0
if preempted:
# 有高价值预约需要处理,清除等待开奖标志,中断等待
self._waiting_for_lottery_result = False
should_preempt = True
if current_ts >= timeout_ts:
self._waiting_for_lottery_result = False
timeout_seconds = int(current_ts - future_ts)
self._log_kv(
"参与",
f"等待开奖超时(超过开奖时间{timeout_seconds}秒),清除等待标志,继续执行其他操作",
超时秒数=timeout_seconds,
)
elif current_ts >= check_start_ts:
passed_seconds = int(current_ts - future_ts)
self._log_kv(
"参与",
f"等待开奖结果中,进入连续检测窗口(最多10s),已过开奖时间{passed_seconds}秒",
已过开奖时间秒数=passed_seconds,
)
deadline_ts = min(timeout_ts, check_start_ts + 10.0)
attempt = 0
consecutive_none = 0
interval = 0.5
while time.time() <= deadline_ts:
attempt += 1
loop_start = time.time()
try:
try:
self.operation.get_screenshot(self.device_id)
except Exception:
pass
result = self._check_lottery_result_by_ocr()
except Exception as e:
result = None
try:
self._log_kv(
"参与",
"等待开奖期间检测到高价值预约需要处理,中断等待",
)
except Exception as exc:
self._log_kv("参与", "检查高价值预约时出错", error=str(exc))
if should_preempt:
# 被高价值预约抢占,继续执行后续逻辑处理预约
pass
else:
# 没有预约抢占,继续等待开奖
# 直接用预计开奖时间减去当前时间计算剩余秒数,加上6秒延迟修正
future_ts = getattr(self, "_lottery_result_expected_ts", 0.0) or 0.0
current_ts = time.time()
remain_seconds = max(
0, future_ts - current_ts + 6.0
) # 加上6秒延迟,修正倒计时误差
# 开奖时间 + 1秒缓冲后开始检测开奖结果(提前4秒:从+5秒改为+1秒)
check_start_ts = future_ts + 1.0
# 超时时间:如果超过开奖时间10秒还没检测到结果,清除等待标志(福袋开奖后10秒会自动关闭)
timeout_ts = future_ts + 10.0
if current_ts >= timeout_ts:
# 超过超时时间,清除等待标志,避免卡死
self._waiting_for_lottery_result = False
timeout_seconds = int(current_ts - future_ts)
self._log_kv(
"参与",
f"等待开奖超时(超过开奖时间{timeout_seconds}秒),清除等待标志,继续执行其他操作",
超时秒数=timeout_seconds,
)
# 继续执行后续逻辑,不返回
elif current_ts >= check_start_ts:
# 已到检测窗口:在这里改为最多连续检测 10 秒(截图+OCR),
# 直到识别到结果或超时。保持原有 timeout_ts 上限( future_ts+10 )。
passed_seconds = int(current_ts - future_ts)
self._log_kv(
"参与",
f"等待开奖结果中,进入连续检测窗口(最多10s),已过开奖时间{passed_seconds}秒",
已过开奖时间秒数=passed_seconds,
)
# 计算循环结束时间:不超过已有 timeout_ts,同时保证最多检测10秒
deadline_ts = min(timeout_ts, check_start_ts + 10.0)
attempt = 0
consecutive_none = 0
interval = 0.5 # 每轮期望间隔,单位秒
while time.time() <= deadline_ts:
attempt += 1
loop_start = time.time()
try:
# 每次都截图以捕获弹窗/按钮的变化
try:
self.operation.get_screenshot(self.device_id)
except Exception:
pass
result = self._check_lottery_result_by_ocr()
except Exception as e:
result = None
try:
self._log_kv(
"参与",
"等待开奖期间OCR检测出错,继续重试",
error=str(e),
)
except Exception:
pass
if result:
# 检测到结果,清等待并处理
self._waiting_for_lottery_result = False
try:
self._log_kv(
"参与",
"等待期间检测到开奖结果,开始处理",
attempt=attempt,
)
except Exception:
pass
self._handle_lottery_result_by_ocr(result)
elapsed = time.time() - start_time
try:
self._log_kv(
"流程",
"run_once_basic完成(等待开奖期间处理结果)",
elapsed_seconds=round(elapsed, 2),
)
except Exception:
pass
return
# 未识别到结果
consecutive_none += 1
now_ts_inner = time.time()
remain_to_deadline = int(max(0, deadline_ts - now_ts_inner))
try:
# 节流日志:每次循环记录一次简短状态
self._log_kv(
"参与",
"等待开奖连续检测:未识别到结果",
attempt=attempt,
remain_seconds=remain_to_deadline,
"等待开奖期间OCR检测出错,继续重试",
error=str(e),
)
except Exception:
pass
# 计算耗时并睡眠(避免过密)
spent = time.time() - loop_start
to_sleep = max(0.0, interval - spent)
if to_sleep > 0:
# 若剩余时间不足则直接继续循环判断超时
if time.time() + to_sleep > deadline_ts:
break
self.operation.delay(to_sleep)
if result:
self._waiting_for_lottery_result = False
try:
self._log_kv(
"参与",
"等待期间检测到开奖结果,开始处理",
attempt=attempt,
)
except Exception:
pass
self._handle_lottery_result_by_ocr(result)
elapsed = time.time() - start_time
try:
self._log_kv(
"流程",
"run_once_basic完成(等待开奖期间处理结果)",
elapsed_seconds=round(elapsed, 2),
)
except Exception:
pass
return
# 循环结束:未识别到结果或到达 deadline
self._waiting_for_lottery_result = False
consecutive_none += 1
now_ts_inner = time.time()
remain_to_deadline = int(max(0, deadline_ts - now_ts_inner))
try:
self._log_kv(
"参与",
"等待开奖连续检测结束:未识别到开奖结果,清除等待标志",
attempts=attempt,
consecutive_none=consecutive_none,
"等待开奖连续检测:未识别到结果",
attempt=attempt,
remain_seconds=remain_to_deadline,
)
except Exception:
pass
# 切换下一个直播间(保持原有行为)
try:
needswitch = bool(getattr(self, "needswitch", True))
except Exception:
needswitch = True
spent = time.time() - loop_start
to_sleep = max(0.0, interval - spent)
if to_sleep > 0:
if time.time() + to_sleep > deadline_ts:
break
self.operation.delay(to_sleep)
if needswitch:
try:
self._log_kv(
"开奖结果", "处理完开奖结果,切换直播间找新福袋"
)
except Exception:
pass
self.swipe_up_to_next_live()
elapsed = time.time() - start_time
try:
self._log_kv(
"流程",
"run_once_basic完成(等待开奖期间处理结果)",
elapsed_seconds=round(elapsed, 2),
)
except Exception:
pass
return
else:
# 还未到开奖时间,短暂等待后继续检测
wait_s = min(2.0, remain_seconds)
remain_seconds_int = int(remain_seconds)
# 在日志消息中显示倒计时
self._waiting_for_lottery_result = False
try:
self._log_kv(
"参与",
f"等待开奖中,剩余{remain_seconds_int}秒",
剩余秒数=remain_seconds_int,
"等待开奖连续检测结束:未识别到开奖结果,清除等待标志",
attempts=attempt,
consecutive_none=consecutive_none,
)
self.operation.delay(wait_s)
elapsed = time.time() - start_time
except Exception:
pass
try:
needswitch = bool(getattr(self, "needswitch", True))
except Exception:
needswitch = True
if needswitch:
try:
self._log_kv(
"开奖结果", "处理完开奖结果,切换直播间找新福袋"
)
except Exception:
pass
self.swipe_up_to_next_live()
elapsed = time.time() - start_time
try:
self._log_kv(
"流程",
f"run_once_basic完成(等待开奖中,剩余{remain_seconds_int}秒)",
"run_once_basic完成(等待开奖期间处理结果)",
elapsed_seconds=round(elapsed, 2),
)
return # 返回,不执行其他操作
except Exception:
pass
return
else:
wait_s = min(2.0, remain_seconds)
remain_seconds_int = int(remain_seconds)
self._log_kv(
"参与",
f"等待开奖中,剩余{remain_seconds_int}秒",
剩余秒数=remain_seconds_int,
)
self.operation.delay(wait_s)
elapsed = time.time() - start_time
self._log_kv(
"流程",
f"run_once_basic完成(等待开奖中,剩余{remain_seconds_int}秒)",
elapsed_seconds=round(elapsed, 2),
)
return
except Exception as exc:
self._log_kv("参与", "等待开奖检测时出错", error=str(exc))
# 出错时清除等待标志,避免卡死
self._waiting_for_lottery_result = False
# 0. 检查30分钟超时:如果30分钟没有成功参与福袋,则返回推荐页面并重新进入直播间
@@ -1839,6 +1768,30 @@ class fudai_analyse:
)
except Exception:
pass
# 在回退后尝试立即推进一次识别/参与流程,避免依赖下一轮循环。
# 仅在当前不处于等待开奖状态时执行(避免打断等待流程)。
try:
if not getattr(self, "_waiting_for_lottery_result", False):
try:
self._log_kv(
"预约",
"[预约] 回退后立即尝试处理当前直播间以推进识别/参与",
)
except Exception:
pass
try:
self._handle_live_room_has_or_not_fudai()
except Exception as e:
try:
self._log_kv(
"预约",
"[预约] 回退后立即处理当前直播间出错",
error=str(e),
)
except Exception:
pass
except Exception:
pass
else:
# 单间模式:不需要回退,预约会在当前直播间自动识别和参与
# 只需要清理过期的预约
@@ -1919,6 +1872,16 @@ class fudai_analyse:
retries=max_screenshot_retries,
elapsed_seconds=round(elapsed, 2),
screenshot_elapsed_seconds=round(screenshot_elapsed, 2),
reservation_after_action_pending=bool(
getattr(self, "reservation_after_action_pending", False)
),
reservation_focus_until_ts=float(
getattr(self, "reservation_focus_until_ts", 0.0) or 0.0
),
waiting_for_lottery=bool(
getattr(self, "_waiting_for_lottery_result", False)
),
room_pos=int(getattr(self, "room_pos", 0) or 0),
suggestion="检查设备连接状态和ADB服务",
)
return
@@ -2473,6 +2436,23 @@ class fudai_analyse:
def _handle_in_fudai_detail(self) -> None:
"""已经在福袋详情页:只跑详情识别 + 是否参与的基础逻辑。"""
start_time = time.time()
try:
self._log_kv(
"诊断",
"进入 _handle_in_fudai_detail:记录当前关键 flags",
reservation_after_action_pending=bool(
getattr(self, "reservation_after_action_pending", False)
),
reservation_focus_until_ts=float(
getattr(self, "reservation_focus_until_ts", 0.0) or 0.0
),
waiting_for_lottery=bool(
getattr(self, "_waiting_for_lottery_result", False)
),
room_pos=int(getattr(self, "room_pos", 0) or 0),
)
except Exception:
pass
# 注意:点击参与抽奖后会自动关闭详情页,下次循环会跳过阶段检测直接查找福袋
# 检查是否是第二次打开福袋详情(已经读取过详情)
+13 -14
View File
@@ -30,7 +30,7 @@ class MainFlowController:
device_id: str = "",
y_pianyi: int = 0,
needswitch: bool = False,
wait_minutes: int = 15,
wait_minutes: int = 0,
) -> None:
"""兼容原 fudai1.fudai_choujiang 的主入口(当前实现为精简版:仅基础抢福袋)。
@@ -105,19 +105,18 @@ class MainFlowController:
# 执行单次循环
MainFlowController.run_once_basic(analyser)
# 等待指定时间(如果配置了等待时间)
if wait_minutes > 0:
wait_seconds = wait_minutes * 60
try:
analyser.operation.delay(wait_seconds)
except Exception:
time.sleep(wait_seconds)
else:
# 如果没有配置等待时间,短暂延迟避免CPU占用过高
try:
analyser.operation.delay(1.0)
except Exception:
time.sleep(1.0)
# 主循环短延迟:移除长时间休眠以实现实时轮询
# 可通过 analyser.main_loop_sleep_seconds 配置短延迟(秒),默认 1.0
try:
wait_seconds = float(
getattr(analyser, "main_loop_sleep_seconds", 1.0) or 1.0
)
except Exception:
wait_seconds = 1.0
try:
analyser.operation.delay(wait_seconds)
except Exception:
time.sleep(wait_seconds)
@staticmethod # type: ignore[misc]
def run_once_basic(analyser: Any) -> None:
@@ -459,6 +459,23 @@ class ReservationHandler:
)
except Exception:
pass
try:
analyser._log_kv( # type: ignore[attr-defined]
"诊断",
"handle_reservation_direct_attend:当前关键 flags",
reservation_after_action_pending=bool(
getattr(analyser, "reservation_after_action_pending", False)
),
reservation_focus_until_ts=float(
getattr(analyser, "reservation_focus_until_ts", 0.0) or 0.0
),
waiting_for_lottery=bool(
getattr(analyser, "_waiting_for_lottery_result", False)
),
room_pos=int(getattr(analyser, "room_pos", 0) or 0),
)
except Exception:
pass
# 从预约记录中获取详情信息
if not analyser.reservations or len(analyser.reservations) == 0:
+16 -8
View File
@@ -91,21 +91,29 @@ function appendLog(lines) {
const log = $("log");
if (!lines || lines.length === 0) return;
// 检查是否在底部附近(用户可能在看最新日志)
const wasNearBottom = log.scrollHeight - log.scrollTop - log.clientHeight < 100;
// 记录更新前与底部的距离,用于在非底部时保持视图位置
const distanceFromBottom = log.scrollHeight - log.scrollTop - log.clientHeight;
// 是否严格位于底部(仅在几像素内视为底部),避免每次刷新都跳到底部
const wasAtBottom = distanceFromBottom < 5;
// 性能优化:批量追加文本,减少重排次数
const newText = lines.join("\n") + "\n";
log.textContent += newText;
// 性能优化:限制日志长度,避免内存溢出(保留最近 50000 字符)
const maxLength = 50000;
// 性能优化:限制日志长度,避免内存溢出(保留最近较多字符以便桌面端查看)
const maxLength = 200000;
if (log.textContent.length > maxLength) {
// 在截断文本时,保留用户原来的距离(除非用户确实在底部)
log.textContent = log.textContent.slice(-maxLength);
if (!wasAtBottom) {
// 调整 scrollTop 保持原先距离
const newScrollTop = log.scrollHeight - log.clientHeight - distanceFromBottom;
log.scrollTop = Math.max(0, newScrollTop);
}
}
// 只有在底部附近时才自动滚动,避免打断用户查看历史日志
if (wasNearBottom) {
// 只有在确实位于底部时才自动滚动到最新内容,避免打断用户查看历史日志
if (wasAtBottom) {
log.scrollTop = log.scrollHeight;
}
}
+4 -2
View File
@@ -226,8 +226,10 @@ body {
.log {
height: 100%;
min-height: 500px;
max-height: 650px;
/* 增大日志面板的初始可视高度,便于桌面端查看更多行为日志 */
min-height: 700px;
/* 最大高度使用视窗计算,避免在超大屏幕下被限制得太短 */
max-height: calc(100vh - 120px);
overflow: auto;
margin: 0;
padding: 10px;