feat: Enhance documentation and clean up code structure
- Added comprehensive documentation for the unified screenshot and score recording process in the new markdown file. - Improved existing markdown files by adding spacing for better readability. - Cleaned up code by removing unnecessary blank lines across various scripts, enhancing overall code clarity. - Updated configuration settings to reflect recent changes and ensure consistency across the project. - Enhanced logging mechanisms to support better debugging and monitoring.
This commit is contained in:
1 parent
e68f1dd3de
commit
f92933641f
61 files changed
+103695
-1485
No files matched your search
+167
-8
@@ -1,6 +1,7 @@
|
||||
import json
|
||||
import os
|
||||
import queue
|
||||
import re
|
||||
import sys
|
||||
import threading
|
||||
import warnings
|
||||
@@ -163,10 +164,86 @@ def main():
|
||||
# 创建日志写入器实例
|
||||
log_writer = ThreadSafeLogWriter()
|
||||
|
||||
def _format_gui_log_line(line: str) -> str:
|
||||
"""
|
||||
将“完整日志行”转换为 GUI 显示用的精简版本。
|
||||
|
||||
需求:
|
||||
- 文件中:仍然写入完整统一日志(含时间戳 / 级别 / 模块等信息);
|
||||
- GUI 中:保留【时间戳 + 业务日志】;
|
||||
- 如果该行包含识别/匹配的分数(如 `匹配度=0.87` / `相似度=0.95` / `score=0.83`),
|
||||
需要在 GUI 文本中一并保留/补充出来。
|
||||
|
||||
精简规则(兼容统一日志格式):
|
||||
- 统一日志通常形如:
|
||||
YYYY-MM-DD HH:MM:SS.xxx - [LEVEL] - [线程] - [模块:函数:行号] - [traceId:xxx] - 业务内容
|
||||
- GUI 只展示:
|
||||
YYYY-MM-DD HH:MM:SS.xxx 业务内容(末尾可追加“(匹配度=..., 相似度=..., score=...)”)
|
||||
- 其他不符合该模式的行保持原样。
|
||||
"""
|
||||
s = (line or "").rstrip("\n")
|
||||
if not s:
|
||||
return s
|
||||
|
||||
ts_part: str | None = None
|
||||
msg_part: str | None = None
|
||||
|
||||
# 1)新统一日志格式:[YYYY-MM-DD HH:MM:SS.xxx] [LEVEL] ... 业务内容 字段_xxx=...
|
||||
m = re.match(r"^\[(\d{4}-\d{2}-\d{2} [0-9:.]+)\]\s+\[[A-Z]+\]\s+(.*)$", s)
|
||||
if m:
|
||||
ts_part = m.group(1).strip()
|
||||
msg_part = m.group(2).strip()
|
||||
# 2)旧统一日志格式:YYYY-MM-DD HH:MM:SS.xxx - [LEVEL] - ... - 业务内容
|
||||
elif s[0].isdigit() and " - " in s:
|
||||
parts = s.split(" - ")
|
||||
if len(parts) >= 3:
|
||||
ts_part = parts[0].strip()
|
||||
msg_part = parts[-1].strip()
|
||||
|
||||
# 如果没解析出“时间戳 + 业务文案”,原样返回
|
||||
if not ts_part or msg_part is None:
|
||||
return s
|
||||
|
||||
# 从业务文案中拆分出 字段_xxx=yyy,非 score 类字段在 GUI 中隐藏
|
||||
keep_tokens: list[str] = []
|
||||
|
||||
def _is_score_field(k: str) -> bool:
|
||||
kl = k.lower()
|
||||
return (
|
||||
"score" in kl
|
||||
or "match" in kl
|
||||
or "匹配" in k
|
||||
or "相似" in k
|
||||
)
|
||||
|
||||
for m2 in re.finditer(r"字段_([^\s=]+)=([^\s]+)", msg_part):
|
||||
key = m2.group(1)
|
||||
val = m2.group(2)
|
||||
if _is_score_field(key):
|
||||
keep_tokens.append(f"{key}={val}")
|
||||
|
||||
# 先把所有 字段_xxx=yyy 从业务文案中移除
|
||||
clean_msg = re.sub(r"\s*字段_[^\s=]+=[^\s]+", "", msg_part).strip()
|
||||
|
||||
# 追加需要展示的分数字段
|
||||
if keep_tokens:
|
||||
clean_msg = f"{clean_msg} ({', '.join(keep_tokens)})"
|
||||
|
||||
return f"{ts_part} {clean_msg}"
|
||||
|
||||
# 不符合“完整格式”的行,原样返回
|
||||
return s
|
||||
|
||||
def on_line(line: str):
|
||||
"""处理日志行:放入队列供GUI显示,同时写入文件"""
|
||||
q.put(line)
|
||||
# 异步写入文件(避免阻塞GUI)
|
||||
"""处理日志行:GUI 显示用精简版本,文件写入用完整版本"""
|
||||
# GUI 显示:使用精简后的版本
|
||||
try:
|
||||
display_line = _format_gui_log_line(line)
|
||||
except Exception:
|
||||
display_line = line
|
||||
q.put(display_line)
|
||||
|
||||
# 文件:写入完整日志行(不做精简处理)
|
||||
try:
|
||||
log_writer.write(line)
|
||||
except Exception:
|
||||
@@ -290,6 +367,9 @@ def main():
|
||||
fudai_icon_max_wait_seconds_var = tk.DoubleVar(
|
||||
value=float(cfg.get("fudai_icon_max_wait_seconds", 5.0) or 5.0)
|
||||
)
|
||||
fudai_click_wait_seconds_var = tk.DoubleVar(
|
||||
value=float(cfg.get("fudai_click_wait_seconds", 2.0) or 2.0)
|
||||
)
|
||||
live_room_match_threshold_var = tk.DoubleVar(
|
||||
value=float(cfg.get("live_room_match_threshold", 0.8) or 0.8)
|
||||
)
|
||||
@@ -403,6 +483,10 @@ def main():
|
||||
fudai_template_match_threshold_var = tk.DoubleVar(
|
||||
value=float(cfg.get("fudai_template_match_threshold", 0.7) or 0.7)
|
||||
)
|
||||
# 福袋详情页模板匹配阈值(0.0~1.0,默认0.7)
|
||||
fudai_detail_template_threshold_var = tk.DoubleVar(
|
||||
value=float(cfg.get("fudai_detail_template_threshold", 0.7) or 0.7)
|
||||
)
|
||||
# 开奖页面红色占比阈值(0.0~1.0,默认0.9)
|
||||
lucky_draw_red_ratio_threshold_var = tk.DoubleVar(
|
||||
value=float(cfg.get("lucky_draw_red_ratio_threshold", 0.9) or 0.9)
|
||||
@@ -850,6 +934,19 @@ def main():
|
||||
).pack(side=LEFT)
|
||||
ttk.Label(row4b, text="(实际识别常见 4-6 秒)").pack(side=LEFT, padx=(6, 0))
|
||||
|
||||
row4d = ttk.Frame(fudai_find_box)
|
||||
row4d.pack(fill=X, padx=10, pady=(0, 8))
|
||||
ttk.Label(row4d, text="点击福袋后等待时间(秒):").pack(side=LEFT)
|
||||
ttk.Spinbox(
|
||||
row4d,
|
||||
from_=0.5,
|
||||
to=10.0,
|
||||
increment=0.1,
|
||||
textvariable=fudai_click_wait_seconds_var,
|
||||
width=6,
|
||||
).pack(side=LEFT)
|
||||
ttk.Label(row4d, text="(等待福袋详情页加载,默认2.0秒)").pack(side=LEFT, padx=(6, 0))
|
||||
|
||||
row4c = ttk.Frame(fudai_find_box)
|
||||
row4c.pack(fill=X, padx=10, pady=(0, 10))
|
||||
ttk.Label(row4c, text="直播间检测阈值:").pack(side=LEFT)
|
||||
@@ -916,6 +1013,19 @@ def main():
|
||||
).pack(side=LEFT)
|
||||
ttk.Label(diag_time_r3, text="(0.0-1.0,彩色匹配)").pack(side=LEFT, padx=(6, 0))
|
||||
|
||||
diag_time_r3b = ttk.Frame(diag_time_box)
|
||||
diag_time_r3b.pack(fill=X, padx=10, pady=(0, 10))
|
||||
ttk.Label(diag_time_r3b, text="福袋详情页模板匹配阈值:").pack(side=LEFT)
|
||||
ttk.Spinbox(
|
||||
diag_time_r3b,
|
||||
from_=0.0,
|
||||
to=1.0,
|
||||
increment=0.01,
|
||||
textvariable=fudai_detail_template_threshold_var,
|
||||
width=8,
|
||||
).pack(side=LEFT)
|
||||
ttk.Label(diag_time_r3b, text="(0.0-1.0,默认0.7,区域(398,894)-(658,1179))").pack(side=LEFT, padx=(6, 0))
|
||||
|
||||
diag_time_r4 = ttk.Frame(diag_time_box)
|
||||
diag_time_r4.pack(fill=X, padx=10, pady=(0, 10))
|
||||
ttk.Label(diag_time_r4, text="开奖页红色占比阈值:").pack(side=LEFT)
|
||||
@@ -1018,6 +1128,9 @@ def main():
|
||||
fudai_icon_max_wait_seconds_var.set(
|
||||
float(cfg.get("fudai_icon_max_wait_seconds", 5.0) or 5.0)
|
||||
)
|
||||
fudai_click_wait_seconds_var.set(
|
||||
float(cfg.get("fudai_click_wait_seconds", 2.0) or 2.0)
|
||||
)
|
||||
live_room_match_threshold_var.set(
|
||||
float(cfg.get("live_room_match_threshold", 0.8) or 0.8)
|
||||
)
|
||||
@@ -1037,6 +1150,9 @@ def main():
|
||||
fudai_template_match_threshold_var.set(
|
||||
float(cfg.get("fudai_template_match_threshold", 0.7) or 0.7)
|
||||
)
|
||||
fudai_detail_template_threshold_var.set(
|
||||
float(cfg.get("fudai_detail_template_threshold", 0.7) or 0.7)
|
||||
)
|
||||
skip_lt_seconds_var.set(int(cfg.get("skip_if_countdown_lt_seconds", 5) or 5))
|
||||
|
||||
# 调试图像清理配置
|
||||
@@ -1191,6 +1307,9 @@ def main():
|
||||
cfg2["fudai_icon_max_wait_seconds"] = float(
|
||||
fudai_icon_max_wait_seconds_var.get() or 5.0
|
||||
)
|
||||
cfg2["fudai_click_wait_seconds"] = float(
|
||||
fudai_click_wait_seconds_var.get() or 2.0
|
||||
)
|
||||
cfg2["live_room_match_threshold"] = float(
|
||||
live_room_match_threshold_var.get() or 0.8
|
||||
)
|
||||
@@ -1208,6 +1327,9 @@ def main():
|
||||
cfg2["fudai_template_match_threshold"] = float(
|
||||
fudai_template_match_threshold_var.get() or 0.7
|
||||
)
|
||||
cfg2["fudai_detail_template_threshold"] = float(
|
||||
fudai_detail_template_threshold_var.get() or 0.7
|
||||
)
|
||||
cfg2["skip_if_countdown_lt_seconds"] = int(skip_lt_seconds_var.get() or 5)
|
||||
|
||||
# 调试图像清理配置
|
||||
@@ -1813,7 +1935,7 @@ def main():
|
||||
line = q.get_nowait()
|
||||
try:
|
||||
log_text.insert("end", line + "\n")
|
||||
log_text.see("end")
|
||||
# 不再自动滚动到底部,让用户自由控制滚动位置
|
||||
changed = True
|
||||
except Exception as e:
|
||||
# 如果插入日志失败,至少打印到控制台(调试用)
|
||||
@@ -2285,12 +2407,28 @@ def main():
|
||||
if os.path.exists(path):
|
||||
# 仿照 web_gui 的实现:只读最后一小段,避免文件巨大
|
||||
size = os.path.getsize(path)
|
||||
if size == 0:
|
||||
actions_log_text.delete("1.0", "end")
|
||||
actions_log_text.insert("1.0", "行为日志文件为空")
|
||||
return
|
||||
|
||||
read_bytes = min(size, 65536)
|
||||
with open(path, "rb") as f:
|
||||
if read_bytes < size:
|
||||
f.seek(-read_bytes, os.SEEK_END)
|
||||
data = f.read().decode("utf-8", errors="replace")
|
||||
lines = data.splitlines()
|
||||
data = f.read()
|
||||
# 尝试多种编码方式
|
||||
try:
|
||||
# 先尝试 utf-8-sig(带BOM)
|
||||
text = data.decode("utf-8-sig", errors="replace")
|
||||
except Exception:
|
||||
try:
|
||||
# 再尝试 utf-8
|
||||
text = data.decode("utf-8", errors="replace")
|
||||
except Exception:
|
||||
# 最后尝试 gbk(Windows常见编码)
|
||||
text = data.decode("gbk", errors="replace")
|
||||
lines = text.splitlines()
|
||||
# 只取最近 300 行
|
||||
tail = lines[-300:]
|
||||
|
||||
@@ -2299,9 +2437,23 @@ def main():
|
||||
if kw:
|
||||
tail = [ln for ln in tail if kw in ln]
|
||||
|
||||
# 保存当前滚动位置,避免刷新时自动置顶
|
||||
try:
|
||||
scroll_position = actions_log_text.yview()[0] # 获取当前滚动位置(顶部可见位置的比例)
|
||||
except Exception:
|
||||
scroll_position = 0.0
|
||||
|
||||
actions_log_text.delete("1.0", "end")
|
||||
actions_log_text.insert("1.0", "\n".join(tail))
|
||||
actions_log_text.see("end")
|
||||
if tail:
|
||||
actions_log_text.insert("1.0", "\n".join(tail))
|
||||
else:
|
||||
actions_log_text.insert("1.0", "暂无匹配的行为日志")
|
||||
|
||||
# 恢复之前的滚动位置,避免自动置顶
|
||||
try:
|
||||
actions_log_text.yview_moveto(scroll_position)
|
||||
except Exception:
|
||||
pass
|
||||
else:
|
||||
actions_log_text.delete("1.0", "end")
|
||||
actions_log_text.insert(
|
||||
@@ -2382,6 +2534,13 @@ def main():
|
||||
root.after(500, refresh_high_value_rooms) # type: ignore[arg-type]
|
||||
root.after(500, refresh_actions_log) # type: ignore[arg-type]
|
||||
|
||||
# 定期刷新行为日志(每2秒)
|
||||
def auto_refresh_actions_log() -> None:
|
||||
refresh_actions_log()
|
||||
root.after(2000, auto_refresh_actions_log) # type: ignore[arg-type]
|
||||
|
||||
root.after(2000, auto_refresh_actions_log) # type: ignore[arg-type]
|
||||
|
||||
# 定期刷新统计信息(每3秒)
|
||||
def auto_refresh_stats() -> None:
|
||||
if runner.is_running():
|
||||
|
||||
Reference in new issue
Block a user