From d5f776dd03d59c7ab91fbb2f1cbacd7925f9f80f Mon Sep 17 00:00:00 2001 From: KUAI Date: Tue, 31 Mar 2026 17:37:59 +0800 Subject: [PATCH 1/2] quick repair retry --- autowsgr/ops/normal_fight.py | 4 +++ autowsgr/ui/battle/repair.py | 64 +++++++++++++++++++++++++++--------- 2 files changed, 53 insertions(+), 15 deletions(-) diff --git a/autowsgr/ops/normal_fight.py b/autowsgr/ops/normal_fight.py index 3e5cc979..68d8eef5 100644 --- a/autowsgr/ops/normal_fight.py +++ b/autowsgr/ops/normal_fight.py @@ -12,6 +12,7 @@ from autowsgr.combat import CombatMode, CombatPlan, CombatResult from autowsgr.combat.engine import run_combat +from autowsgr.infra import ActionFailedError from autowsgr.infra.logger import get_logger from autowsgr.ops import goto_page from autowsgr.types import ConditionFlag, PageName, RepairMode, ShipDamageState @@ -308,6 +309,9 @@ def _prepare_for_battle(self) -> list[ShipDamageState]: # 检测战前舰队信息 (血量 + 等级) fleet_info = page.detect_fleet_info() ship_stats = [fleet_info.ship_damage.get(i, ShipDamageState.NORMAL) for i in range(6)] + if ShipDamageState.SEVERE in ship_stats: + _log.error('[OPS] 出征前检测到大破舰船,退出程序') + raise ActionFailedError('出征前检测到大破舰船,退出程序') self._fleet_ships = fleet_info.to_ships(self._fleet) # 出征 diff --git a/autowsgr/ui/battle/repair.py b/autowsgr/ui/battle/repair.py index 6b855cbb..f105238f 100644 --- a/autowsgr/ui/battle/repair.py +++ b/autowsgr/ui/battle/repair.py @@ -42,11 +42,11 @@ def repair_slots(self, positions: list[int]) -> None: time.sleep(1.5) _log.info('[UI] 出征准备 → 修理位置 {}', pos) - def apply_repair( + def check_repair( self, - strategy: RepairStrategy | None = None, + strategy: RepairStrategy, ) -> list[int]: - """根据策略执行快速修理。 + """根据策略执行快速修理检查(不实际修理)。 Parameters ---------- @@ -60,12 +60,6 @@ def apply_repair( """ from autowsgr.ui.battle.base import RepairStrategy - if strategy is None: - strategy = RepairStrategy.SEVERE - - if strategy is RepairStrategy.NEVER: - return [] - screen = self._ctrl.screenshot() damage = self.detect_ship_damage(screen) @@ -79,10 +73,50 @@ def apply_repair( or (strategy is RepairStrategy.SEVERE and dmg >= ShipDamageState.SEVERE) ): positions.append(slot) - - if positions: - if self._ctx.config.repair_manually: - raise ActionFailedError('需要进行手动修理') - self.repair_slots(positions) - _log.info('[UI] 修理位置: {} (策略: {})', positions, strategy.value) return positions + + def apply_repair( + self, + strategy: RepairStrategy | None = None, + *, + repair_manually: bool = False, + retry_count: int = 3, + ) -> list[int]: + """根据策略执行快速修理。 + + Parameters + ---------- + strategy: + 修理策略,默认 ``RepairStrategy.SEVERE``。 + + Returns + ------- + list[int] + 实际修理的槽位列表。 + """ + if strategy is None: + strategy = RepairStrategy.SEVERE + + if strategy is RepairStrategy.NEVER: + return [] + + repair_pos = [] + positions = self.check_repair(strategy) + for i in range(retry_count): + # 没有需要修理的舰船,直接返回 + if not positions: + return [] + # 需要手动修理,退出程序 + if self._ctx.config.repair_manually or repair_manually: + raise ActionFailedError('需要进行手动修理') + self.repair_slots(positions) + repair_pos.extend(positions) + # 修理完成再检查一遍 + positions = self.check_repair(strategy) + if not positions: + _log.info('[UI] 修理位置: {} (策略: {})', repair_pos, strategy.value) + return repair_pos + _log.info(f'[UI] 有舰船修理失败: {positions}, 重试第 {i} 次') + # 经过重试仍修理失败 + _log.error('[UI] 舰船修理异常(策略: {})', strategy.value) + raise ActionFailedError('舰船修理异常') From daf3afb0c5dafe933e5512d26e80535219a21509 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 31 Mar 2026 09:39:17 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- autowsgr/ui/battle/repair.py | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/autowsgr/ui/battle/repair.py b/autowsgr/ui/battle/repair.py index f105238f..899f1d52 100644 --- a/autowsgr/ui/battle/repair.py +++ b/autowsgr/ui/battle/repair.py @@ -103,20 +103,20 @@ def apply_repair( repair_pos = [] positions = self.check_repair(strategy) for i in range(retry_count): - # 没有需要修理的舰船,直接返回 - if not positions: - return [] - # 需要手动修理,退出程序 - if self._ctx.config.repair_manually or repair_manually: - raise ActionFailedError('需要进行手动修理') - self.repair_slots(positions) - repair_pos.extend(positions) - # 修理完成再检查一遍 - positions = self.check_repair(strategy) - if not positions: - _log.info('[UI] 修理位置: {} (策略: {})', repair_pos, strategy.value) - return repair_pos - _log.info(f'[UI] 有舰船修理失败: {positions}, 重试第 {i} 次') + # 没有需要修理的舰船,直接返回 + if not positions: + return [] + # 需要手动修理,退出程序 + if self._ctx.config.repair_manually or repair_manually: + raise ActionFailedError('需要进行手动修理') + self.repair_slots(positions) + repair_pos.extend(positions) + # 修理完成再检查一遍 + positions = self.check_repair(strategy) + if not positions: + _log.info('[UI] 修理位置: {} (策略: {})', repair_pos, strategy.value) + return repair_pos + _log.info(f'[UI] 有舰船修理失败: {positions}, 重试第 {i} 次') # 经过重试仍修理失败 _log.error('[UI] 舰船修理异常(策略: {})', strategy.value) raise ActionFailedError('舰船修理异常')