mirror of
https://github.com/setube/ogame-vue-ts.git
synced 2026-05-12 07:55:11 +08:00
fix: 修复保存预设时未初始化数组和重复星球 ID 迁移逻辑
修复 GM 视图中保存自定义预设时,当对应标签页的预设数组未初始化导致的保存失败问题。同时改进迁移工具中重复星球 ID 的处理逻辑,确保正确分组并更新关联的月球数据。
This commit is contained in:
@@ -102,31 +102,51 @@ export const migrateGameData = (): void => {
|
|||||||
|
|
||||||
// 修复重复的星球ID
|
// 修复重复的星球ID
|
||||||
if (oldData.player?.planets && Array.isArray(oldData.player.planets)) {
|
if (oldData.player?.planets && Array.isArray(oldData.player.planets)) {
|
||||||
const idCounts = new Map<string, number>()
|
const planetsByOriginalId = new Map<string, Planet[]>()
|
||||||
const idMap = new Map<string, string>() // 映射:原始ID + 坐标 -> 新ID
|
|
||||||
|
|
||||||
|
// 第一步:按ID分组
|
||||||
oldData.player.planets.forEach((planet: Planet) => {
|
oldData.player.planets.forEach((planet: Planet) => {
|
||||||
const count = idCounts.get(planet.id) || 0
|
if (!planetsByOriginalId.has(planet.id)) {
|
||||||
if (count > 0) {
|
planetsByOriginalId.set(planet.id, [])
|
||||||
// 发现重复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
|
|
||||||
}
|
}
|
||||||
idCounts.set(planet.id, count + 1)
|
planetsByOriginalId.get(planet.id)!.push(planet)
|
||||||
})
|
})
|
||||||
|
|
||||||
// 如果有ID被修改,需要更新月球的 parentPlanetId
|
// 第二步:处理重复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
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
// 第三步:更新月球的 parentPlanetId
|
||||||
if (idMap.size > 0) {
|
if (idMap.size > 0) {
|
||||||
oldData.player.planets.forEach((planet: Planet) => {
|
oldData.player.planets.forEach((planet: Planet) => {
|
||||||
if (planet.isMoon && planet.parentPlanetId) {
|
if (planet.isMoon && planet.parentPlanetId) {
|
||||||
|
// 假设月球和母星坐标一致,通过月球坐标查找母星的新ID
|
||||||
const posKey = `${planet.position.galaxy}:${planet.position.system}:${planet.position.position}`
|
const posKey = `${planet.position.galaxy}:${planet.position.system}:${planet.position.position}`
|
||||||
const newParentId = idMap.get(`${planet.parentPlanetId}_${posKey}`)
|
const mapKey = `${planet.parentPlanetId}_${posKey}`
|
||||||
if (newParentId) {
|
|
||||||
planet.parentPlanetId = newParentId
|
if (idMap.has(mapKey)) {
|
||||||
needsSave = true
|
const newParentId = idMap.get(mapKey)
|
||||||
|
if (newParentId) {
|
||||||
|
planet.parentPlanetId = newParentId
|
||||||
|
needsSave = true
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -350,13 +350,14 @@
|
|||||||
values
|
values
|
||||||
}
|
}
|
||||||
|
|
||||||
if (customPresets.value[section.tabValue]) {
|
if (!customPresets.value[section.tabValue]) {
|
||||||
customPresets.value[section.tabValue]!.push(newPreset)
|
customPresets.value[section.tabValue] = []
|
||||||
savePresets(section.tabValue, customPresets.value[section.tabValue]!)
|
|
||||||
presetNames.value[section.tabValue] = ''
|
|
||||||
selectedPresets.value[section.tabValue] = newPreset.id
|
|
||||||
toast.success(t('gmView.presetSaved') || '预设保存成功')
|
|
||||||
}
|
}
|
||||||
|
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) => {
|
const handleApplyPreset = (section: any) => {
|
||||||
|
|||||||
Reference in New Issue
Block a user