refactor: 优化精灵背包仓库切换逻辑
All checks were successful
ci/woodpecker/push/my-first-workflow Pipeline was successful

This commit is contained in:
xinian
2026-04-05 06:02:27 +08:00
committed by cnb
parent e71971d0b4
commit 8352d23164
2 changed files with 29 additions and 95 deletions

View File

@@ -62,9 +62,6 @@ func (h Controller) TogglePetBagWarehouse(
}
slot.Remove()
if slot.IsMainBag() {
player.Service.Info.Save(*player.Info)
}
case 1:
if len(player.Info.PetList) >= 6 && len(player.Info.BackupPetList) >= 6 {
@@ -88,69 +85,3 @@ func (h Controller) TogglePetBagWarehouse(
return result, 0
}
// TogglePetBagWarehouseLegacy 旧版精灵背包仓库切换
func (h Controller) TogglePetBagWarehouseLegacy(
data *pet.PetReleaseLegacyInboundInfo,
player *player.Player) (result *pet.PetReleaseOutboundInfo, err errorcode.ErrorCode) {
result = &pet.PetReleaseOutboundInfo{
Flag: uint32(data.Flag),
}
if player.IsArenaSwitchLocked() {
return result, errorcode.ErrorCodes.ErrChampionCannotSwitch
}
switch data.Flag {
case 0:
index, currentPet, ok := player.FindPet(data.CatchTime)
if !ok {
break
}
if index < 0 || index >= len(player.Info.PetList) {
return result, errorcode.ErrorCodes.ErrPokemonIDMismatch
}
if !player.Service.Pet.Update(*currentPet) {
return result, errorcode.ErrorCodes.ErrSystemError
}
player.Info.PetList = append(player.Info.PetList[:index], player.Info.PetList[index+1:]...)
player.RemoveBackupPet(data.CatchTime)
player.Info.BackupPetList = append(player.Info.BackupPetList, *currentPet)
case 1:
if len(player.Info.PetList) >= 6 {
break
}
if _, _, ok := player.FindPet(data.CatchTime); ok {
player.RemoveBackupPet(data.CatchTime)
break
}
if index, backupPet, ok := player.FindBackupPet(data.CatchTime); ok {
if index < 0 || index >= len(player.Info.BackupPetList) {
return result, errorcode.ErrorCodes.ErrPokemonIDMismatch
}
result.PetInfo = *backupPet
player.Info.PetList = append(player.Info.PetList, *backupPet)
player.Info.BackupPetList = append(player.Info.BackupPetList[:index], player.Info.BackupPetList[index+1:]...)
break
}
petInfo := player.Service.Pet.PetInfoOneByCatchTime(data.CatchTime)
if petInfo == nil {
return result, errorcode.ErrorCodes.ErrPokemonNotExists
}
player.Info.PetList = append(player.Info.PetList, petInfo.Data)
result.PetInfo = petInfo.Data
}
if len(player.Info.PetList) > 0 {
result.FirstPetTime = player.Info.PetList[0].CatchTime
}
return result, 0
}

View File

@@ -53,16 +53,16 @@ func removePetByCatchTime(petList []model.PetInfo, catchTime uint32) []model.Pet
return petList
}
func buildPetInfoMap(petLists ...[]model.PetInfo) map[uint32]model.PetInfo {
func buildPetInfoMap(petLists ...[]model.PetInfo) map[uint32]*model.PetInfo {
total := 0
for _, petList := range petLists {
total += len(petList)
}
petMap := make(map[uint32]model.PetInfo, total)
petMap := make(map[uint32]*model.PetInfo, total)
for _, petList := range petLists {
for _, petInfo := range petList {
petMap[petInfo.CatchTime] = petInfo
for i := range petList {
petMap[petList[i].CatchTime] = &petList[i]
}
}
return petMap
@@ -70,27 +70,33 @@ func buildPetInfoMap(petLists ...[]model.PetInfo) map[uint32]model.PetInfo {
func buildOrderedPetList(
catchTimes []uint32,
petMap map[uint32]model.PetInfo,
petMap map[uint32]*model.PetInfo,
) []model.PetInfo {
result := make([]model.PetInfo, len(catchTimes))
for i, catchTime := range catchTimes {
result[i] = *petMap[catchTime]
}
return result
}
func validatePetBagOrder(
catchTimes []uint32,
bagCatchTimes map[uint32]struct{},
used map[uint32]struct{},
) ([]model.PetInfo, bool) {
result := make([]model.PetInfo, 0, len(catchTimes))
) bool {
for _, catchTime := range catchTimes {
if catchTime == 0 {
return nil, false
return false
}
if _, exists := bagCatchTimes[catchTime]; !exists {
return false
}
if _, exists := used[catchTime]; exists {
return nil, false
return false
}
petInfo, exists := petMap[catchTime]
if !exists {
return nil, false
}
used[catchTime] = struct{}{}
result = append(result, petInfo)
}
return result, true
return true
}
// GetUserBagPetInfo 返回主背包和并列备用精灵列表。
@@ -160,24 +166,21 @@ func (p *Player) SavePetBagOrder(petList []uint32, backupPetList []uint32) bool
return false
}
petMap := buildPetInfoMap(p.Info.PetList, p.Info.BackupPetList)
bagCatchTimes := buildCatchTimeSet(p.Info.PetList, p.Info.BackupPetList)
used := make(map[uint32]struct{}, totalPetCount)
battleList, ok := buildOrderedPetList(petList, petMap, used)
if !ok {
if !validatePetBagOrder(petList, bagCatchTimes, used) {
return false
}
backupList, ok := buildOrderedPetList(backupPetList, petMap, used)
if !ok {
if !validatePetBagOrder(backupPetList, bagCatchTimes, used) {
return false
}
if len(used) != totalPetCount {
return false
}
p.Info.PetList = battleList
p.Info.BackupPetList = backupList
petMap := buildPetInfoMap(p.Info.PetList, p.Info.BackupPetList)
p.Info.PetList = buildOrderedPetList(petList, petMap)
p.Info.BackupPetList = buildOrderedPetList(backupPetList, petMap)
return true
}