refactor: 代码优化(常量复用、清理未使用 import、错误日志)

- settlement.ts: 复用 MAX_EV_PER_STAT/MAX_EV_TOTAL 常量替代硬编码
- settlement.ts: 删除未使用的 Creature/addItemToBag/removeItemFromBag/xpForLevel import
- effort.ts: 复用 EV_COOLDOWN_MS 常量替代硬编码 30000
- storage.ts: 空 catch 块添加错误日志输出

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
claude-code-best
2026-04-22 04:16:42 +08:00
parent 7c64199fc5
commit 1bba087942
3 changed files with 9 additions and 8 deletions

View File

@@ -1,11 +1,11 @@
import type { Creature, StatName, SpeciesId } from '../types' import type { StatName, SpeciesId } from '../types'
import { STAT_NAMES } from '../types' import { STAT_NAMES } from '../types'
import { TO_DEX_STAT } from '../data/pkmn' import { TO_DEX_STAT } from '../data/pkmn'
import type { BattleResult } from './types' import type { BattleResult } from './types'
import type { BuddyData } from '../types' import type { BuddyData } from '../types'
import { addItemToBag, removeItemFromBag } from '../core/storage' import { levelFromXp } from '../data/xpTable'
import { xpForLevel, levelFromXp } from '../data/xpTable'
import { getSpeciesData } from '../data/species' import { getSpeciesData } from '../data/species'
import { MAX_EV_PER_STAT, MAX_EV_TOTAL } from '../data/evMapping'
import { Dex } from '@pkmn/sim' import { Dex } from '@pkmn/sim'
/** /**
@@ -53,8 +53,8 @@ export async function settleBattle(
const newEv = { ...creature.ev } const newEv = { ...creature.ev }
let totalEV = STAT_NAMES.reduce((sum, s) => sum + newEv[s], 0) let totalEV = STAT_NAMES.reduce((sum, s) => sum + newEv[s], 0)
for (const stat of STAT_NAMES) { for (const stat of STAT_NAMES) {
if (totalEV >= 510) break if (totalEV >= MAX_EV_TOTAL) break
const gain = Math.min(evGained[stat], 252 - newEv[stat], 510 - totalEV) const gain = Math.min(evGained[stat], MAX_EV_PER_STAT - newEv[stat], MAX_EV_TOTAL - totalEV)
newEv[stat] += gain newEv[stat] += gain
totalEV += gain totalEV += gain
} }

View File

@@ -1,6 +1,6 @@
import type { Creature, StatName } from '../types' import type { Creature, StatName } from '../types'
import { STAT_NAMES } from '../types' import { STAT_NAMES } from '../types'
import { getEVForTool, MAX_EV_PER_STAT, MAX_EV_TOTAL } from '../data/evMapping' import { getEVForTool, MAX_EV_PER_STAT, MAX_EV_TOTAL, EV_COOLDOWN_MS } from '../data/evMapping'
import { getTotalEV } from './creature' import { getTotalEV } from './creature'
// Track last EV award time per tool to enforce cooldown // Track last EV award time per tool to enforce cooldown
@@ -22,7 +22,7 @@ export function awardEV(creature: Creature, toolName: string, timestamp?: number
// Check cooldown // Check cooldown
const lastTime = evCooldowns.get(toolName) const lastTime = evCooldowns.get(toolName)
if (lastTime !== undefined && now - lastTime < 30_000) return creature if (lastTime !== undefined && now - lastTime < EV_COOLDOWN_MS) return creature
const currentTotal = getTotalEV(creature) const currentTotal = getTotalEV(creature)
if (currentTotal >= MAX_EV_TOTAL) return creature if (currentTotal >= MAX_EV_TOTAL) return creature

View File

@@ -34,7 +34,8 @@ export async function loadBuddyData(): Promise<BuddyData> {
const raw = readFileSync(BUDDY_DATA_PATH, 'utf-8') const raw = readFileSync(BUDDY_DATA_PATH, 'utf-8')
const data = JSON.parse(raw) const data = JSON.parse(raw)
return migrateToV2(data) return migrateToV2(data)
} catch { } catch (e) {
console.error('[buddy] Failed to load buddy data:', e)
return getDefaultBuddyData() return getDefaultBuddyData()
} }
} }