diff --git a/packages/utils/tool.js b/packages/utils/tool.js index 4cac82a22..07294bcd0 100644 --- a/packages/utils/tool.js +++ b/packages/utils/tool.js @@ -196,6 +196,49 @@ async function findText( return null; } +/** + * 读取指定区域内的全部 OCR 文本。 + * 与 findText 不同,本方法不要求预先提供关键词,适合先识别再在内存中匹配候选项。 + * + * @param {number} [x=0] OCR 区域左上角 X + * @param {number} [y=0] OCR 区域左上角 Y + * @param {number} [w=1920] OCR 区域宽度 + * @param {number} [h=1080] OCR 区域高度 + * @param {number} [attempts=1] OCR 尝试次数 + * @param {number} [interval=50] 每次 OCR 之间的等待间隔(毫秒) + * @returns {Promise} 按识别顺序拼接的文本,未识别到内容时返回空字符串 + */ +async function ocrRegion( + x = 0, + y = 0, + w = 1920, + h = 1080, + attempts = 1, + interval = 50 +) { + for (let i = 0; i < attempts; i++) { + const gameRegion = captureGameRegion(); + try { + const ro = RecognitionObject.Ocr(x, y, w, h); + const results = gameRegion.findMulti(ro); + const texts = []; + + for (let j = 0; j < results.count; j++) { + const result = results[j]; + if (result.isExist() && result.text) texts.push(result.text.trim()); + } + + if (texts.length > 0) return texts.join(' '); + } finally { + gameRegion.dispose(); + } + + if (i < attempts - 1) await sleep(interval); + } + + return ''; +} + /** * 通用找文本并点击(OCR) * @param {string|string[]} text 目标文本(单个文本或文本列表,列表时需全部匹配) @@ -588,6 +631,7 @@ export { findImg, findImgAndClick, findText, + ocrRegion, findTextAndClick, waitUntilImgAppear, waitUntilImgDisappear, @@ -596,4 +640,4 @@ export { isInMainUI, startMonthCardWatcher, openBag -}; \ No newline at end of file +}; diff --git a/repo/js/ArtifactScore/assets/score-mask.html b/repo/js/ArtifactScore/assets/score-mask.html index b3faf88eb..7183a68e9 100644 --- a/repo/js/ArtifactScore/assets/score-mask.html +++ b/repo/js/ArtifactScore/assets/score-mask.html @@ -5,103 +5,256 @@ 圣遗物评分 -
-
-
圣遗物评分
+
+
+
+
圣遗物评分
+
+
请打开圣遗物详情界面
+
- + } + diff --git a/repo/js/ArtifactScore/main.js b/repo/js/ArtifactScore/main.js index ea399aeaa..1dc3904a7 100644 --- a/repo/js/ArtifactScore/main.js +++ b/repo/js/ArtifactScore/main.js @@ -7,7 +7,7 @@ * * 所有坐标均基于 BetterGI 的 1920x1080 标准截图。 */ -import { findImg, findText } from "../../../packages/utils/tool.js" +import { findImg, findText, ocrRegion } from "../../../packages/utils/tool.js" import scoreData, { analyzeArtifact, calcArtifactScore, @@ -18,7 +18,7 @@ const MASK_PATH = "assets/score-mask.html" const MASK_ID_PREFIX = "artiscope-score" const BACK_TEMPLATE = "assets/images/back.png" const POLL_INTERVAL = 500 -const TYPE_SETTLE_DELAY = 300 +const TYPE_SETTLE_DELAY = 50 const CHARACTER_SCAN_BUDGET = 2000 const REGIONS = { @@ -48,7 +48,6 @@ const SUB_STAT_NAMES = [ "暴击率", "暴击伤害", "攻击力", "防御力", "生命值", "元素精通", "元素充能效率" ] -const DIGITS = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"] const CHARACTER_NAMES = Object.keys(scoreData.usefulAttr).sort((a, b) => b.length - a.length) const CHARACTER_ELEMENT_MAP = scoreData.charElemMap || {} const ELEMENTS = [ @@ -163,6 +162,12 @@ async function findAnyText(keywords, region) { return null } +/** 将一次区域 OCR 的结果与候选词匹配,不再为每个候选词重复 OCR。 */ +function matchKnownText(text, candidates) { + const normalized = String(text || "").replace(/\s/g, "") + return candidates.find(candidate => normalized.includes(candidate.replace(/\s/g, ""))) || "" +} + /** * 分批扫描角色名。单轮达到时间预算后保存索引,下一轮继续, * 防止较慢电脑一次执行上百次 OCR 时表现为长时间卡死。 @@ -188,12 +193,13 @@ async function scanCharacterNames(region) { /** 读取圣遗物部位,并转换为评分模块使用的 0~4 索引。 */ async function readArtifactPosition() { - const result = await findAnyText(POSITION_NAMES, REGIONS.position) - if (!result) return null + const text = await ocrRegion(...REGIONS.position, 1, 0) + const name = matchKnownText(text, POSITION_NAMES) + if (!name) return null return { - name: result.keyword, - pos: POSITION_NAMES.indexOf(result.keyword) + name, + pos: POSITION_NAMES.indexOf(name) } } @@ -340,8 +346,8 @@ function getSubStatKey(name, hasPercent) { /** 读取一条副词条;待激活和未识别项以 0 分占位。 */ async function readSubStat(region, index) { - const pending = await findText("待激活", ...region, 1, 0) - if (pending) { + const text = await ocrRegion(...region, 1, 0) + if (text.includes("待激活")) { return { key: `待激活${index + 1}`, value: 0, @@ -350,8 +356,8 @@ async function readSubStat(region, index) { } } - const stat = await findAnyText(SUB_STAT_NAMES, region) - if (!stat) { + const statName = matchKnownText(text, SUB_STAT_NAMES) + if (!statName) { return { key: `未识别${index + 1}`, value: 0, @@ -360,35 +366,21 @@ async function readSubStat(region, index) { } } - let value = parseNumber(stat.text) - let valueText = stat.text - if (!Number.isFinite(value)) { - const numberResult = await findAnyText(DIGITS, region) - if (!numberResult) { - return { - key: `未识别${index + 1}`, - value: 0, - name: stat.keyword, - displayValue: "--" - } - } - valueText = numberResult.text - value = parseNumber(valueText) - } + const value = parseNumber(text) if (!Number.isFinite(value)) { return { key: `未识别${index + 1}`, value: 0, - name: stat.keyword, + name: statName, displayValue: "--" } } - const hasPercent = /[%%]/.test(`${stat.text}${valueText}`) + const hasPercent = /[%%]/.test(text) return { - key: getSubStatKey(stat.keyword, hasPercent), + key: getSubStatKey(statName, hasPercent), value, - name: stat.keyword, + name: statName, displayValue: formatStatValue(value, hasPercent, true) } } @@ -398,11 +390,12 @@ async function readSubStat(region, index) { * 若前后不一致则丢弃本轮,防止切换动画混入新旧数据。 */ async function readArtifactFromOcr(position) { - const mainStat = await findAnyText(MAIN_STAT_NAMES, REGIONS.mainName) - const mainValueResult = await findAnyText(DIGITS, REGIONS.mainValue) - if (!mainStat || !mainValueResult) return null + const mainNameText = await ocrRegion(...REGIONS.mainName, 1, 0) + const mainName = matchKnownText(mainNameText, MAIN_STAT_NAMES) + const mainValueText = await ocrRegion(...REGIONS.mainValue, 1, 0) + if (!mainName || !mainValueText) return null - const mainValue = parseNumber(mainValueResult.text) + const mainValue = parseNumber(mainValueText) if (!Number.isFinite(mainValue)) return null const subs = {} @@ -419,9 +412,9 @@ async function readArtifactFromOcr(position) { const artifact = { pos: position.pos, - main: mainStat.keyword, + main: mainName, value: mainValue, - mainDisplayValue: formatStatValue(mainValue, /[%%]/.test(mainValueResult.text)), + mainDisplayValue: formatStatValue(mainValue, /[%%]/.test(mainValueText)), subs, subStats } diff --git a/repo/js/ArtifactScore/manifest.json b/repo/js/ArtifactScore/manifest.json index 3ab89f478..eaabed9bf 100644 --- a/repo/js/ArtifactScore/manifest.json +++ b/repo/js/ArtifactScore/manifest.json @@ -1,7 +1,7 @@ { "manifest_version": 1, "name": "圣遗物实时评分", - "version": "1.0.0", + "version": "1.0.1", "bgi_version": "0.61.2", "description": "在角色圣遗物界面通过浮窗实时显示圣遗物的评分", "authors": [