Compare commits

...

4 Commits

Author SHA1 Message Date
a87e7e955b
Merge pull request #467 from a2893005741/dev
refactor(chart): 修复修复重复代码等问题,啊这上一个pr我还没有完成呢
2026-05-13 11:47:15 +08:00
wess09
e9fffb7b3b debug 2026-05-13 11:46:27 +08:00
a2893005741
1bafff4658 refactor(ap_chart): 重构硬币刻度与坐标映射逻辑
统一使用yScale函数处理坐标计算,将硬币刻度配置参数化,简化重复代码,提升可维护性
2026-05-13 11:02:10 +08:00
a2893005741
5b159621a2 refactor(chart): 重构硬币刻度绘制逻辑,提取通用方法
- 提取yScale通用坐标映射函数,简化重复代码
- 封装drawCoinTicks统一绘制右侧硬币刻度标签
- 移除两处重复的刻度绘制代码,替换为调用新封装方法
- 新增布局常量统一管理刻度偏移参数
2026-05-13 10:51:33 +08:00
3 changed files with 61 additions and 67 deletions

View File

@ -162,17 +162,33 @@ class ActionPointHandler(UI, MapEventHandler):
oil = OIL_ITEM.predict(self.device.image, name=False, amount=True)
items = ACTION_POINT_ITEMS.predict(self.device.image, name=False, amount=True)
box = [item.amount for item in oil] + [item.amount for item in items]
now_str = datetime.now().strftime("%Y%m%d_%H%M%S_%f")
# 保存行动力箱子 (AP Box) 的识别截图
from module.statistics.item import AMOUNT_OCR
for i, item in enumerate(oil):
crop_img = item.crop(OIL_ITEM.amount_area)
pre = AMOUNT_OCR.pre_process(crop_img)
pre = crop_to_text(pre)
save_image(pre, f'debug_img/ap_box_oil_{i}_{now_str}.png')
logger.info(f'[Debug] AP Box Oil {i} amount: {item.amount}')
for i, item in enumerate(items):
crop_img = item.crop(ACTION_POINT_ITEMS.amount_area)
pre = AMOUNT_OCR.pre_process(crop_img)
pre = crop_to_text(pre)
save_image(pre, f'debug_img/ap_box_item_{i}_{now_str}.png')
logger.info(f'[Debug] AP Box Item {i} amount: {item.amount}')
current = OCR_ACTION_POINT_REMAIN.ocr(self.device.image)
logger.info(f'[Debug] OCR_ACTION_POINT_REMAIN: {current}')
if not os.path.exists('debug_img'):
os.makedirs('debug_img')
now_str = datetime.now().strftime("%Y%m%d_%H%M%S_%f")
# 保存原始全屏截图
save_image(self.device.image, f'debug_img/ap_{now_str}_orig.png')
# 保存传给 OCR 的预处理截图
for i, area in enumerate(OCR_ACTION_POINT_REMAIN.buttons):
pre = OCR_ACTION_POINT_REMAIN.pre_process(crop(self.device.image, area))
pre = crop_to_text(pre)
save_image(pre, f'debug_img/ap_{now_str}_ocr_{i}.png')
total = current
if self.config.OS_ACTION_POINT_BOX_USE:

View File

@ -16,7 +16,7 @@ from module.ocr.ocr import Digit
from module.os_shop.assets import OS_SHOP_CHECK, OS_SHOP_PURPLE_COINS, SHOP_PURPLE_COINS, SHOP_YELLOW_COINS
from module.ui.ui import UI
from module.log_res.log_res import LogRes
from module.base.utils import crop, save_image
from module.base.utils import crop, crop_to_text, save_image
if server.server != 'jp':
OCR_SHOP_YELLOW_COINS = Digit(SHOP_YELLOW_COINS, letter=(239, 239, 239), threshold=160, name='OCR_SHOP_YELLOW_COINS')
@ -99,9 +99,10 @@ class OSStatus(UI):
now_str = datetime.now().strftime("%Y%m%d_%H%M%S_%f")
# 保存原始全屏截图
save_image(self.device.image, f'debug_img/yellow_coins_{now_str}_orig.png')
# 保存传给 OCR 的预处理截图(裁剪并提取文字后)
# 保存传给 OCR 的预处理截图(裁剪、提取文字、并去除边缘空白后)
for i, area in enumerate(OCR_SHOP_YELLOW_COINS.buttons):
pre = OCR_SHOP_YELLOW_COINS.pre_process(crop(self.device.image, area))
pre = crop_to_text(pre)
save_image(pre, f'debug_img/yellow_coins_{now_str}_ocr_{i}.png')
if timeout.reached():
logger.warning('Get yellow coins timeout')

View File

@ -74,6 +74,11 @@
ctx.scale(dpr, dpr);
var oc = ovCv.getContext("2d");
// 硬币刻度标签布局常量
var COIN_TICK_X = 8; // 右侧刻度标签相对 pad.r 的水平偏移
var COIN_TICK_BASELINE = 4; // 单币时文字基线垂直偏移
var COIN_TICK_STACK_GAP = 12; // 双币堆叠时紫币相对黄币的垂直偏移
var pad = {t: 20, r: showCoins ? 72 : 20, b: 52, l: 52};
var gW = W - pad.l - pad.r, gH = H - pad.t - pad.b;
@ -129,16 +134,36 @@
var hasCoins = hasYellowCoins || hasPurpleCoins;
function xOfLine(i) { return pad.l + (i / Math.max(nn - 1, 1)) * gW; }
function yOf(v) { return pad.t + gH - (v - allMin) / (allMax - allMin) * gH; }
// 硬币绘制参数化配置(颜色 / 范围 / 垂直偏移driven by drawCoinTicks
var COIN_CONFIGS = [
{ has: hasYellowCoins, color: "#ffd54f", dataMin: yellowMin, dataMax: yellowMax, offsetY: 0 },
{ has: hasPurpleCoins, color: "#ce93d8", dataMin: purpleMin, dataMax: purpleMax, offsetY: hasYellowCoins ? COIN_TICK_STACK_GAP : 0 }
];
// 黄币曲线映射到完整图表区域
function yOfYellow(v) {
return pad.t + gH - (v - yellowMin) / (yellowMax - yellowMin) * gH;
function xOfLine(i) { return pad.l + (i / Math.max(nn - 1, 1)) * gW; }
// 通用 Y 坐标映射:将 value 从 [rangeMin, rangeMax] 映射到绘图区域(所有纵向映射的唯一来源)
function yScale(value, rangeMin, rangeMax) {
return pad.t + gH - (value - rangeMin) / (rangeMax - rangeMin) * gH;
}
// 紫币曲线映射到完整图表区域
function yOfPurple(v) {
return pad.t + gH - (v - purpleMin) / (purpleMax - purpleMin) * gH;
function yOf(v) { return yScale(v, allMin, allMax); }
function yOfYellow(v) { return yScale(v, yellowMin, yellowMax); }
function yOfPurple(v) { return yScale(v, purpleMin, purpleMax); }
function drawCoinTicks(ctx, yOfMain, mainMin, mainMax) {
if (!hasCoins) return;
ctx.font = "10px -apple-system, sans-serif";
ctx.textAlign = "left";
for (var i = 0; i <= 5; i++) {
var mainVal = mainMin + (mainMax - mainMin) * (i / 5);
var y = yOfMain(mainVal);
for (var ci = 0; ci < COIN_CONFIGS.length; ci++) {
var cfg = COIN_CONFIGS[ci];
if (!cfg.has) continue;
var val = cfg.dataMin + (cfg.dataMax - cfg.dataMin) * (i / 5);
ctx.fillStyle = cfg.color;
ctx.fillText(Math.round(val), W - pad.r + COIN_TICK_X, y + COIN_TICK_BASELINE + cfg.offsetY);
}
}
}
function drawCoinsLine(xOf, start, end) {
@ -203,31 +228,7 @@
ctx.fillText(Math.round(v), pad.l - 8, y);
}
// 右侧同行双币刻度与左侧K线价格行一一对应黄币在上、紫币紧挨在下
if (hasCoins) {
for (var i = 0; i <= 5; i++) {
var mainVal = allMin + (allMax - allMin) * (i / 5);
var y = yOf(mainVal);
if (hasYellowCoins) {
var yv = yellowMin + (yellowMax - yellowMin) * (i / 5);
ctx.fillStyle = "#ffd54f";
ctx.font = "10px -apple-system, sans-serif";
ctx.textAlign = "left";
ctx.fillText(Math.round(yv), W - pad.r + 8, y + 4);
}
if (hasPurpleCoins) {
var pv = purpleMin + (purpleMax - purpleMin) * (i / 5);
ctx.fillStyle = "#ce93d8";
ctx.font = "10px -apple-system, sans-serif";
ctx.textAlign = "left";
if (hasYellowCoins) {
ctx.fillText(Math.round(pv), W - pad.r + 8, y + 4 + 12);
} else {
ctx.fillText(Math.round(pv), W - pad.r + 8, y + 4);
}
}
}
}
drawCoinTicks(ctx, yOf, allMin, allMax);
var avgY = yOf(avg);
ctx.save();
@ -399,7 +400,7 @@
var idx = Math.round(visibleStart + (mx_ - pad.l) / xScale);
idx = Math.max(0, Math.min(nn - 1, idx));
var px = pad.l + (idx - visibleStart) * xScale;
var py = pad.t + gH - (ap[idx] - dMin) / (dMax - dMin) * gH;
var py = yScale(ap[idx], dMin, dMax);
oc.strokeStyle = "rgba(255,255,255,0.18)";
oc.lineWidth = 1;
@ -554,46 +555,22 @@
ctx.textBaseline = "middle";
for (var i = 0; i <= 5; i++) {
var v = dMin + (dMax - dMin) * (i / 5);
var y = pad.t + gH - (v - dMin) / (dMax - dMin) * gH;
var y = yScale(v, dMin, dMax);
ctx.beginPath(); ctx.moveTo(pad.l, y); ctx.lineTo(W - pad.r, y); ctx.stroke();
ctx.fillText(Math.round(v), pad.l - 8, y);
}
// 右侧同行双币刻度与左侧K线价格行一一对应黄币在上、紫币紧挨在下
if (hasCoins) {
for (var i = 0; i <= 5; i++) {
var mainVal = dMin + (dMax - dMin) * (i / 5);
var y = dyOf(mainVal);
if (hasYellowCoins) {
var yv = yellowMin + (yellowMax - yellowMin) * (i / 5);
ctx.fillStyle = "#ffd54f";
ctx.font = "10px -apple-system, sans-serif";
ctx.textAlign = "left";
ctx.fillText(Math.round(yv), W - pad.r + 8, y + 4);
}
if (hasPurpleCoins) {
var pv = purpleMin + (purpleMax - purpleMin) * (i / 5);
ctx.fillStyle = "#ce93d8";
ctx.font = "10px -apple-system, sans-serif";
ctx.textAlign = "left";
if (hasYellowCoins) {
ctx.fillText(Math.round(pv), W - pad.r + 8, y + 4 + 12);
} else {
ctx.fillText(Math.round(pv), W - pad.r + 8, y + 4);
}
}
}
}
var xScale = gW / Math.max(visibleNn - 1, 1);
function dxOf(i) { return pad.l + (i - visibleStart) * xScale; }
function dyOf(v) { return yScale(v, dMin, dMax); }
drawCoinTicks(ctx, dyOf, dMin, dMax);
ctx.fillStyle = "#666";
ctx.font = "10px -apple-system, sans-serif";
ctx.textAlign = "center";
ctx.textBaseline = "top";
var xScale = gW / Math.max(visibleNn - 1, 1);
function dxOf(i) { return pad.l + (i - visibleStart) * xScale; }
function dyOf(v) { return pad.t + gH - (v - dMin) / (dMax - dMin) * gH; }
var dgrad = ctx.createLinearGradient(0, pad.t, 0, pad.t + gH);
dgrad.addColorStop(0, "rgba(100,181,246,0.15)");
dgrad.addColorStop(1, "rgba(100,181,246,0.02)");