Files
douyi_guaji_main/gui/dashboard_app/pages.py
T

351 lines
18 KiB
Python

from __future__ import annotations
import tkinter as tk
from pathlib import Path
from tkinter import ttk
from . import theme
from .data import DashboardData, cfg_get, fmt_ts
from .widgets import card, field, flow_grid, metric, preview_panel, section, switch_row, table_panel, text_panel
def _pack_fields(parent, items: list[tuple[str, object]], columns: int = 2) -> None:
grid = tk.Frame(parent, bg=theme.SURFACE)
grid.pack(fill="x", padx=14, pady=(4, 14))
for col in range(columns):
grid.grid_columnconfigure(col, weight=1, uniform="field")
for idx, (label, value) in enumerate(items):
cell = field(grid, label, value)
cell.grid(row=idx // columns, column=idx % columns, sticky="ew", padx=8, pady=5)
def _sample_reservation_rows(data: DashboardData) -> list[dict]:
rows = []
for idx, item in enumerate(data.reservations[:18], 1):
rows.append({
"idx": idx,
"title": item.get("room_title") or item.get("title") or item.get("live_title") or "-",
"content": item.get("content") or item.get("prize_content") or item.get("gift") or "-",
"value": item.get("prize_value_yuan") or item.get("value") or "-",
"status": item.get("status") or item.get("state") or "等待中",
"time": fmt_ts(item.get("target_ts") or item.get("created_ts") or item.get("updated_ts")),
})
return rows
def build_home(parent, data: DashboardData) -> None:
section(parent, "数据看板", "现有统计 / 实时状态 / 队列 / 截图集中视图")
metrics = flow_grid(parent, 6)
values = [
("运行日志", len(data.run_log), "最近记录", theme.TEAL),
("行为日志", len(data.action_log), "最近行为", theme.BLUE),
("预约队列", len(data.reservations), "等待处理", theme.AMBER),
("高价值房间", len(data.high_value_rooms), "本地记录", theme.GREEN),
("中奖截图", len(data.win_images), "pic/lottery", theme.RED),
("映射条目", len(data.mapping_rows), "room_pos", theme.TEAL_DARK),
]
for idx, item in enumerate(values):
metric(metrics, *item).grid(row=0, column=idx, sticky="ew", padx=5)
body = tk.Frame(parent, bg=theme.BG)
body.pack(fill="both", expand=True)
body.grid_columnconfigure(0, weight=7)
body.grid_columnconfigure(1, weight=4)
body.grid_rowconfigure(0, weight=1)
left = tk.Frame(body, bg=theme.BG)
right = tk.Frame(body, bg=theme.BG)
left.grid(row=0, column=0, sticky="nsew", padx=(0, 8))
right.grid(row=0, column=1, sticky="nsew", padx=(8, 0))
table_panel(left, "预约队列", (
("idx", "#", 40), ("title", "直播间", 180), ("content", "内容", 220),
("value", "价值", 80), ("status", "状态", 90), ("time", "时间", 150),
), _sample_reservation_rows(data), height=8).pack(fill="both", expand=True, pady=(0, 12))
text_panel(left, "运行日志", data.run_log[-80:], height=11).pack(fill="both", expand=True)
preview_panel(right, data.screenshot_path).pack(fill="both", expand=True, pady=(0, 12))
text_panel(right, "实时动作", data.action_log[-80:], height=9).pack(fill="both", expand=True)
def build_run_settings(parent, data: DashboardData) -> None:
section(parent, "运行设置", "运行时长、切换、滑动回退与直播间进入策略")
box = card(parent, "核心运行参数")
box.pack(fill="x")
_pack_fields(box, [
("允许切换直播间", cfg_get(data.cfg, "needswitch")),
("无限循环模式", cfg_get(data.cfg, "infinite_loop_enabled")),
("运行时长(分钟)", cfg_get(data.cfg, "wait_minutes")),
("倒计时大于不参与", cfg_get(data.cfg, "skip_if_countdown_gt_seconds")),
("倒计时小于不参与", cfg_get(data.cfg, "skip_if_countdown_lt_seconds")),
("滑动次数限制", cfg_get(data.cfg, "reset_after_swipes_enabled")),
("次数", cfg_get(data.cfg, "reset_after_swipes")),
("回退方式", cfg_get(data.cfg, "reset_after_swipes_action")),
("进入坐标模式", cfg_get(data.cfg, "reenter_live_entry_mode")),
("连续无福袋退出阈值", cfg_get(data.cfg, "fudai_exit_after_misses")),
])
def build_filter_settings(parent, data: DashboardData) -> None:
section(parent, "筛选设置", "价值、人数、倒计时与粉丝团策略")
box = card(parent, "筛选规则")
box.pack(fill="x")
_pack_fields(box, [
("最低价值(元)", cfg_get(data.cfg, "min_prize_value_yuan")),
("参与人数最小", cfg_get(data.cfg, "min_participants")),
("参与人数最大", cfg_get(data.cfg, "max_participants")),
("倒计时小于不参与", cfg_get(data.cfg, "skip_if_countdown_lt_seconds")),
("粉丝团失败策略", cfg_get(data.cfg, "fansclub_fail_action")),
("粉丝团点击延迟(s)", cfg_get(data.cfg, "fansclub_click_delay")),
("自动加入等待(s)", cfg_get(data.cfg, "fansclub_auto_join_wait_seconds")),
("手动加入等待(s)", cfg_get(data.cfg, "fansclub_manual_wait_seconds")),
])
def build_reservation_settings(parent, data: DashboardData) -> None:
section(parent, "高价值预约", "高价值记录、回访与回退校验")
box = card(parent, "预约策略")
box.pack(fill="x")
_pack_fields(box, [
("启用高价值预约", cfg_get(data.cfg, "high_value_fudai_enabled")),
("价值阈值(元)", cfg_get(data.cfg, "high_value_fudai_threshold_yuan")),
("提前参与(秒)", cfg_get(data.cfg, "high_value_fudai_join_ahead_seconds")),
("最多回退次数", cfg_get(data.cfg, "high_value_fudai_max_swipe_back")),
("最多记录数量", cfg_get(data.cfg, "high_value_fudai_max_reservations")),
("完成后策略", cfg_get(data.cfg, "high_value_fudai_after_action")),
("启用高价值房间回访", cfg_get(data.cfg, "high_value_room_revisit_enabled")),
("重访最小间隔(秒)", cfg_get(data.cfg, "high_value_room_revisit_min_interval_seconds")),
("每次运行最大次数", cfg_get(data.cfg, "high_value_room_revisit_max_per_run")),
("最低奖品价值(元)", cfg_get(data.cfg, "high_value_room_revisit_min_prize_value")),
("最大年龄(天)", cfg_get(data.cfg, "high_value_room_revisit_max_age_days")),
("开奖前轮询(秒)", cfg_get(data.cfg, "high_value_fudai_poll_before_seconds")),
("回退滑延迟(秒)", cfg_get(data.cfg, "reservation_back_swipe_delay")),
("校验等待时间(秒)", cfg_get(data.cfg, "reservation_back_verify_wait_seconds")),
])
def build_yanghao_settings(parent, data: DashboardData) -> None:
section(parent, "养号设置", "推荐流停留、互动概率与关键词")
metrics = flow_grid(parent, 4)
for idx, item in enumerate([
("养号启用", cfg_get(data.cfg, "yanghao_enabled"), "开关", theme.TEAL),
("推荐停留", cfg_get(data.cfg, "yanghao_recommend_duration_seconds"), "秒", theme.BLUE),
("点赞概率", cfg_get(data.cfg, "yanghao_like_prob"), "0-1", theme.AMBER),
("收藏概率", cfg_get(data.cfg, "yanghao_fav_prob"), "0-1", theme.GREEN),
]):
metric(metrics, *item).grid(row=0, column=idx, sticky="ew", padx=5)
box = card(parent, "互动参数")
box.pack(fill="x")
_pack_fields(box, [
("启用互动动作", cfg_get(data.cfg, "yanghao_enable_actions")),
("只养号模式", cfg_get(data.cfg, "yanghao_only")),
("评论概率", cfg_get(data.cfg, "yanghao_comment_prob")),
("动作停留最小(s)", cfg_get(data.cfg, "yanghao_dwell_on_action_min_seconds")),
("动作停留最大(s)", cfg_get(data.cfg, "yanghao_dwell_on_action_max_seconds")),
("图标匹配阈值", cfg_get(data.cfg, "yanghao_icon_match_threshold")),
])
def build_debug_settings(parent, data: DashboardData) -> None:
section(parent, "调试参数", "识别阈值、模板匹配、像素检测与截图调试")
metrics = flow_grid(parent, 4)
for idx, item in enumerate([
("调试截图", cfg_get(data.cfg, "debug_captures", "enabled"), "enabled", theme.TEAL),
("保留天数", cfg_get(data.cfg, "debug_captures", "keep_days"), "debug", theme.BLUE),
("最大文件", cfg_get(data.cfg, "debug_captures", "max_files"), "cleanup", theme.AMBER),
("OCR模式", cfg_get(data.cfg, "fudai_detail_ui_mode"), "detail", theme.GREEN),
]):
metric(metrics, *item).grid(row=0, column=idx, sticky="ew", padx=5)
box = card(parent, "识别与点击参数")
box.pack(fill="x")
_pack_fields(box, [
("找福袋最大尝试", cfg_get(data.cfg, "fudai_find_max_attempts")),
("找福袋重试间隔", cfg_get(data.cfg, "fudai_icon_retry_delay_seconds")),
("最大等待时间", cfg_get(data.cfg, "fudai_icon_max_wait_seconds")),
("点击后等待", cfg_get(data.cfg, "fudai_click_wait_seconds")),
("模板匹配阈值", cfg_get(data.cfg, "fudai_template_match_threshold")),
("详情模板阈值", cfg_get(data.cfg, "fudai_detail_template_threshold")),
("开奖页红色阈值", cfg_get(data.cfg, "lucky_draw_red_ratio_threshold")),
("滑动速度(ms)", cfg_get(data.cfg, "swipe_duration_ms")),
])
def build_license(parent, data: DashboardData) -> None:
section(parent, "授权", "卡密授权、本机机器码与授权文件")
box = card(parent, "卡密授权")
box.pack(fill="x")
_pack_fields(box, [
("授权状态", "本地检测"),
("本机机器码", ""),
("授权文件", "license.dat"),
("输入卡密", ""),
], columns=1)
ttk.Button(box, text="安装卡密", style="Primary.TButton").pack(side="left", padx=22, pady=(0, 18))
ttk.Button(box, text="刷新状态", style="Soft.TButton").pack(side="left", padx=6, pady=(0, 18))
def build_logging(parent, data: DashboardData) -> None:
section(parent, "日志配置", "日志格式、保留策略、实时状态刷新")
box = card(parent, "日志与界面")
box.pack(fill="x")
_pack_fields(box, [
("日志格式模式", cfg_get(data.cfg, "logging", "log_format_mode")),
("统一日志路径", cfg_get(data.cfg, "log_unified_path")),
("保留天数", cfg_get(data.cfg, "logging", "keep_days")),
("总量上限(GB)", cfg_get(data.cfg, "logging", "max_total_size_gb")),
("压缩阈值(天)", cfg_get(data.cfg, "logging", "compress_after_days")),
("警告阈值(MB)", cfg_get(data.cfg, "logging", "monitor_warning_mb")),
("严重阈值(MB)", cfg_get(data.cfg, "logging", "monitor_critical_mb")),
("界面主题", cfg_get(data.cfg, "gui", "theme")),
("实时刷新(ms)", cfg_get(data.cfg, "gui", "realtime_status", "refresh_interval_ms")),
("实时状态条数", cfg_get(data.cfg, "gui", "realtime_status", "tail_items")),
])
def build_keywords(parent, data: DashboardData) -> None:
section(parent, "关键词", "白名单与黑名单")
body = tk.Frame(parent, bg=theme.BG)
body.pack(fill="both", expand=True)
for idx, title in enumerate(("想要 contains_want(白名单,空=不限制)", "不想要 contains_not_want(黑名单)")):
box = card(body, title)
box.grid(row=0, column=idx, sticky="nsew", padx=(0, 8) if idx == 0 else (8, 0))
text = tk.Text(box, height=18, bd=0, bg="#fbfcfa", font=theme.FONT_UI)
text.pack(fill="both", expand=True, padx=12, pady=(4, 12))
body.grid_columnconfigure(0, weight=1)
body.grid_columnconfigure(1, weight=1)
body.grid_rowconfigure(0, weight=1)
def build_run_log(parent, data: DashboardData) -> None:
section(parent, "运行日志", "筛选 / 搜索 / 刷新")
text_panel(parent, "最近运行日志", data.run_log, height=24).pack(fill="both", expand=True)
def build_preview(parent, data: DashboardData) -> None:
section(parent, "截图预览", "选择图片 / 打开pic目录 / 刷新预览")
preview_panel(parent, data.screenshot_path, "当前截图").pack(fill="both", expand=True)
def build_statistics(parent, data: DashboardData) -> None:
section(parent, "统计信息", "本地运行统计看板")
build_home(parent, data)
def build_reservation_queue(parent, data: DashboardData) -> None:
section(parent, "预约队列", "预约队列总览与等待状态")
table_panel(parent, "预约队列", (
("idx", "#", 40), ("title", "直播间", 190), ("content", "内容", 250),
("value", "价值", 80), ("status", "状态", 90), ("time", "时间", 170),
), _sample_reservation_rows(data), height=18).pack(fill="both", expand=True)
def build_high_value(parent, data: DashboardData) -> None:
section(parent, "高价值房间", "持久化高价值记录")
rows = []
for idx, item in enumerate(data.high_value_rooms[:50], 1):
rows.append({
"idx": idx,
"title": item.get("room_title") or item.get("title") or "-",
"content": item.get("content") or item.get("prize_content") or "-",
"value": item.get("prize_value_yuan") or item.get("value") or "-",
"status": item.get("status") or "已记录",
})
table_panel(parent, "高价值房间", (
("idx", "#", 40), ("title", "直播间", 220), ("content", "内容", 280),
("value", "价值", 90), ("status", "状态", 100),
), rows, height=20).pack(fill="both", expand=True)
def build_actions_log(parent, data: DashboardData) -> None:
section(parent, "行为日志", "关键字筛选 / 搜索 / 刷新")
text_panel(parent, "最近行为日志", data.action_log, height=24).pack(fill="both", expand=True)
def build_realtime(parent, data: DashboardData) -> None:
section(parent, "实时状态", "显示动作流 / 显示截图 / 自动滚动 / 查看大图")
body = tk.Frame(parent, bg=theme.BG)
body.pack(fill="both", expand=True)
body.grid_columnconfigure(0, weight=5)
body.grid_columnconfigure(1, weight=3)
text_panel(body, "动作流", data.action_log[-120:], height=24).grid(row=0, column=0, sticky="nsew", padx=(0, 8))
preview_panel(body, data.screenshot_path, "实时截图").grid(row=0, column=1, sticky="nsew", padx=(8, 0))
def build_ocr(parent, data: DashboardData) -> None:
section(parent, "OCR结果", "点击 截图+OCR 后更新")
lines = []
for p in data.ocr_files[:8]:
lines.append(f"{p.name} {fmt_ts(p.stat().st_mtime)}")
text_panel(parent, "OCR结果", lines or ['暂无OCR结果,请先点击"截图+OCR"按钮'], height=24).pack(fill="both", expand=True)
def build_wins(parent, data: DashboardData) -> None:
section(parent, "中奖记录", "中奖截图列表与预览")
body = tk.Frame(parent, bg=theme.BG)
body.pack(fill="both", expand=True)
body.grid_columnconfigure(0, weight=4)
body.grid_columnconfigure(1, weight=5)
rows = [
{"idx": i + 1, "file": p.name, "time": fmt_ts(p.stat().st_mtime)}
for i, p in enumerate(data.win_images[:40])
]
table_panel(body, "中奖截图", (("idx", "#", 40), ("file", "文件", 260), ("time", "时间", 160)), rows, height=18).grid(row=0, column=0, sticky="nsew", padx=(0, 8))
preview_panel(body, data.win_images[0] if data.win_images else None, "中奖预览").grid(row=0, column=1, sticky="nsew", padx=(8, 0))
def build_mapping(parent, data: DashboardData) -> None:
section(parent, "映射表", "显示原始标题 / 刷新映射表 / 搜索 / 复制条目")
table_panel(parent, "运行时映射表", (
("pos", "位置", 80), ("title", "标题", 360), ("status", "状态", 100), ("updated", "更新时间", 180),
), data.mapping_rows, height=22).pack(fill="both", expand=True)
def build_wifi_adb(parent, data: DashboardData) -> None:
section(parent, "WiFi ADB", "扫描 WiFi / USB 授权提示 / 连接状态")
metrics = flow_grid(parent, 4)
for idx, item in enumerate([
("USB设备", "待检测", "首次启用", theme.TEAL),
("扫描状态", "未扫描", "局域网", theme.BLUE),
("默认端口", "5555", "ADB TCP", theme.AMBER),
("当前设备", "自动选择", "设备选择器", theme.GREEN),
]):
metric(metrics, *item).grid(row=0, column=idx, sticky="ew", padx=5)
rows = [
{"device": "待扫描设备", "ip": "192.168.1.x", "port": "5555", "state": "未连接"},
]
table_panel(parent, "WiFi 设备列表", (
("device", "设备ID", 220), ("ip", "IP地址", 180), ("port", "端口", 90), ("state", "状态", 120),
), rows, height=8).pack(fill="x", pady=(0, 12))
box = card(parent, "连接操作")
box.pack(fill="x")
row = tk.Frame(box, bg=theme.SURFACE)
row.pack(fill="x", padx=14, pady=14)
ttk.Button(row, text="扫描WiFi", style="Primary.TButton").pack(side="left", padx=(0, 8))
ttk.Button(row, text="刷新", style="Soft.TButton").pack(side="left", padx=4)
ttk.Button(row, text="连接", style="Soft.TButton").pack(side="left", padx=4)
ttk.Button(row, text="取消", style="Soft.TButton").pack(side="left", padx=4)
ttk.Label(row, text="请确保设备已通过 USB 授权,并已开启无线调试/ADB TCP。", style="Small.TLabel").pack(side="left", padx=14)
PAGES = {
"数据看板": build_home,
"运行设置": build_run_settings,
"筛选设置": build_filter_settings,
"高价值预约": build_reservation_settings,
"养号设置": build_yanghao_settings,
"调试参数": build_debug_settings,
"授权": build_license,
"日志配置": build_logging,
"关键词": build_keywords,
"运行日志": build_run_log,
"截图预览": build_preview,
"统计信息": build_statistics,
"预约队列": build_reservation_queue,
"高价值房间": build_high_value,
"行为日志": build_actions_log,
"实时状态": build_realtime,
"OCR结果": build_ocr,
"中奖记录": build_wins,
"映射表": build_mapping,
"WiFi ADB": build_wifi_adb,
}