Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 13 additions & 23 deletions src/zzz_od/application/shiyu_defense/shiyu_defense_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,35 +97,25 @@ def choose_node_idx(self) -> OperationRoundResult:
break
if not progress_text:
progress_text = list(ocr_results.keys())[0]
match = re.search(r'(\d+)\s*/\s*(\d+)', progress_text)
if match:
total = int(match.group(2))
current = int(match.group(1))
# OCR 可能将 "2/5" 连写为 "215",取首尾数字分别作为当前和总节点数。
digits = re.findall(r'\d', progress_text)
if len(digits) >= 2:
current = int(digits[0])
total = int(digits[-1])
log.info('剧变节点进度 %d/%d', current, total)
self.config.critical_max_node_idx = total
next_idx = current + 1
if next_idx > total:
return self.round_success(ShiyuDefenseApp.STATUS_ALL_FINISHED)
elif len(digits) == 1:
# 只有一个数字,可能是 "5",说明已完成
total = int(digits[0])
log.info('剧变节点进度 已完成 %d', total)
self.config.critical_max_node_idx = total
return self.round_success(ShiyuDefenseApp.STATUS_ALL_FINISHED)
Comment on lines +102 to +115

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win

增加对 OCR 解析结果的基础合法性校验

提取首尾数字作为进度的方法在遇到极端的 OCR 误读(例如解析出 current > total)或未来游戏更新中节点数达到两位数(例如 "10/10" 会被拆分为 ['1', '0', '1', '0'],导致 current=1, total=0)时,会解析出错误的数值。这不仅会导致误判为全部完成,还会将错误的 total 值覆写到配置 critical_max_node_idx 中。

建议增加基础的数值合法性校验,若不合法则回退到解析失败的逻辑,以保护配置数据的完整性。

🛡️ 建议的修复
-            if len(digits) >= 2:
-                current = int(digits[0])
-                total = int(digits[-1])
-                log.info('剧变节点进度 %d/%d', current, total)
-                self.config.critical_max_node_idx = total
-                next_idx = current + 1
-                if next_idx > total:
-                    return self.round_success(ShiyuDefenseApp.STATUS_ALL_FINISHED)
-            elif len(digits) == 1:
-                # 只有一个数字,可能是 "5",说明已完成
-                total = int(digits[0])
-                log.info('剧变节点进度 已完成 %d', total)
-                self.config.critical_max_node_idx = total
-                return self.round_success(ShiyuDefenseApp.STATUS_ALL_FINISHED)
+            if len(digits) >= 2:
+                current = int(digits[0])
+                total = int(digits[-1])
+                if current > total or total == 0:
+                    log.info('OCR 进度解析结果异常 (current=%d, total=%d): %s', current, total, progress_text)
+                    next_idx = 1
+                else:
+                    log.info('剧变节点进度 %d/%d', current, total)
+                    self.config.critical_max_node_idx = total
+                    next_idx = current + 1
+                    if next_idx > total:
+                        return self.round_success(ShiyuDefenseApp.STATUS_ALL_FINISHED)
+            elif len(digits) == 1:
+                # 只有一个数字,可能是 "5",说明已完成
+                total = int(digits[0])
+                if total == 0:
+                    log.info('OCR 进度解析结果异常 (total=0): %s', progress_text)
+                    next_idx = 1
+                else:
+                    log.info('剧变节点进度 已完成 %d', total)
+                    self.config.critical_max_node_idx = total
+                    return self.round_success(ShiyuDefenseApp.STATUS_ALL_FINISHED)
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
if len(digits) >= 2:
current = int(digits[0])
total = int(digits[-1])
log.info('剧变节点进度 %d/%d', current, total)
self.config.critical_max_node_idx = total
next_idx = current + 1
if next_idx > total:
return self.round_success(ShiyuDefenseApp.STATUS_ALL_FINISHED)
elif len(digits) == 1:
# 只有一个数字,可能是 "5",说明已完成
total = int(digits[0])
log.info('剧变节点进度 已完成 %d', total)
self.config.critical_max_node_idx = total
return self.round_success(ShiyuDefenseApp.STATUS_ALL_FINISHED)
if len(digits) >= 2:
current = int(digits[0])
total = int(digits[-1])
if current > total or total == 0:
log.info('OCR 进度解析结果异常 (current=%d, total=%d): %s', current, total, progress_text)
next_idx = 1
else:
log.info('剧变节点进度 %d/%d', current, total)
self.config.critical_max_node_idx = total
next_idx = current + 1
if next_idx > total:
return self.round_success(ShiyuDefenseApp.STATUS_ALL_FINISHED)
elif len(digits) == 1:
# 只有一个数字,可能是 "5",说明已完成
total = int(digits[0])
if total == 0:
log.info('OCR 进度解析结果异常 (total=0): %s', progress_text)
next_idx = 1
else:
log.info('剧变节点进度 已完成 %d', total)
self.config.critical_max_node_idx = total
return self.round_success(ShiyuDefenseApp.STATUS_ALL_FINISHED)
🧰 Tools
🪛 Ruff (0.15.21)

[warning] 111-111: Comment contains ambiguous (FULLWIDTH COMMA). Did you mean , (COMMA)?

(RUF003)


[warning] 111-111: Comment contains ambiguous (FULLWIDTH COMMA). Did you mean , (COMMA)?

(RUF003)

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/zzz_od/application/shiyu_defense/shiyu_defense_app.py` around lines 102 -
115, 在处理多个 OCR 数字的分支中,更新进度解析逻辑以校验 current 和 total 的合法性:要求 current 不大于 total,且
total 不小于 1;校验失败时不要写入
critical_max_node_idx,也不要判定完成,改为沿用现有的解析失败流程。保留合法进度的日志、配置更新和完成判定行为。

else:
# 解析失败,回退到单数字(全部完成时可能只显示总节点数)
digits = re.findall(r'\d+', progress_text)
if len(digits) >= 2:
current = int(digits[0])
total = int(digits[1])
log.info('剧变节点进度 %d/%d', current, total)
self.config.critical_max_node_idx = total
next_idx = current + 1
if next_idx > total:
return self.round_success(ShiyuDefenseApp.STATUS_ALL_FINISHED)
elif len(digits) == 1:
# 只有一个数字,可能是 "5",说明已完成
total = int(digits[0])
log.info('剧变节点进度 已完成 %d', total)
self.config.critical_max_node_idx = total
return self.round_success(ShiyuDefenseApp.STATUS_ALL_FINISHED)
else:
log.info('OCR 进度解析失败: %s', progress_text)
next_idx = 1
log.info('OCR 进度解析失败: %s', progress_text)
next_idx = 1
else:
log.info('未读到剧变节点进度,尝试从 run_record 获取')
next_idx = self.run_record.next_node_idx()
Expand Down
Loading