fix: 修复保存预设时未初始化数组和重复星球 ID 迁移逻辑

修复 GM 视图中保存自定义预设时,当对应标签页的预设数组未初始化导致的保存失败问题。同时改进迁移工具中重复星球 ID 的处理逻辑,确保正确分组并更新关联的月球数据。
This commit is contained in:
wenyu
2026-03-18 18:03:38 +08:00
parent 8e49998205
commit bd46c24824
2 changed files with 43 additions and 22 deletions

View File

@@ -102,31 +102,51 @@ export const migrateGameData = (): void => {
// 修复重复的星球ID
if (oldData.player?.planets && Array.isArray(oldData.player.planets)) {
const idCounts = new Map<string, number>()
const idMap = new Map<string, string>() // 映射原始ID + 坐标 -> 新ID
const planetsByOriginalId = new Map<string, Planet[]>()
// 第一步按ID分组
oldData.player.planets.forEach((planet: Planet) => {
const count = idCounts.get(planet.id) || 0
if (count > 0) {
// 发现重复ID
const newId = `${planet.id}_${Math.random().toString(36).substring(2, 9)}`
const posKey = `${planet.position.galaxy}:${planet.position.system}:${planet.position.position}`
idMap.set(`${planet.id}_${posKey}`, newId)
planet.id = newId
needsSave = true
if (!planetsByOriginalId.has(planet.id)) {
planetsByOriginalId.set(planet.id, [])
}
planetsByOriginalId.get(planet.id)!.push(planet)
})
// 第二步处理重复ID并建立映射
const idMap = new Map<string, string>() // Key: oldId_galaxy:system:position -> Value: newId
planetsByOriginalId.forEach((planets, originalId) => {
if (planets.length > 1) {
// 对重复组中的星球,保留第一个,修改后续的
planets.forEach((planet, index) => {
if (index > 0) {
const newId = `${originalId}_${Math.random().toString(36).substring(2, 9)}`
const posKey = `${planet.position.galaxy}:${planet.position.system}:${planet.position.position}`
// 记录映射原始ID + 坐标 -> 新ID
idMap.set(`${originalId}_${posKey}`, newId)
// 修改星球ID
planet.id = newId
needsSave = true
}
})
}
idCounts.set(planet.id, count + 1)
})
// 如果有ID被修改需要更新月球的 parentPlanetId
// 第三步:更新月球的 parentPlanetId
if (idMap.size > 0) {
oldData.player.planets.forEach((planet: Planet) => {
if (planet.isMoon && planet.parentPlanetId) {
// 假设月球和母星坐标一致通过月球坐标查找母星的新ID
const posKey = `${planet.position.galaxy}:${planet.position.system}:${planet.position.position}`
const newParentId = idMap.get(`${planet.parentPlanetId}_${posKey}`)
if (newParentId) {
planet.parentPlanetId = newParentId
needsSave = true
const mapKey = `${planet.parentPlanetId}_${posKey}`
if (idMap.has(mapKey)) {
const newParentId = idMap.get(mapKey)
if (newParentId) {
planet.parentPlanetId = newParentId
needsSave = true
}
}
}
})

View File

@@ -350,13 +350,14 @@
values
}
if (customPresets.value[section.tabValue]) {
customPresets.value[section.tabValue]!.push(newPreset)
savePresets(section.tabValue, customPresets.value[section.tabValue]!)
presetNames.value[section.tabValue] = ''
selectedPresets.value[section.tabValue] = newPreset.id
toast.success(t('gmView.presetSaved') || '预设保存成功')
if (!customPresets.value[section.tabValue]) {
customPresets.value[section.tabValue] = []
}
customPresets.value[section.tabValue]!.push(newPreset)
savePresets(section.tabValue, customPresets.value[section.tabValue]!)
presetNames.value[section.tabValue] = ''
selectedPresets.value[section.tabValue] = newPreset.id
toast.success(t('gmView.presetSaved') || '预设保存成功')
}
const handleApplyPreset = (section: any) => {