193 lines
5.4 KiB
Go
193 lines
5.4 KiB
Go
package controller
|
|
|
|
import (
|
|
"blazing/common/socket/errorcode"
|
|
"blazing/logic/service/fight"
|
|
"blazing/logic/service/pet"
|
|
"blazing/logic/service/player"
|
|
)
|
|
|
|
// SavePetBagOrder 保存当前主背包和备用背包顺序
|
|
func (h Controller) SavePetBagOrder(
|
|
data *pet.SavePetBagOrderInboundInfo,
|
|
player *player.Player) (result *fight.NullOutboundInfo,
|
|
err errorcode.ErrorCode) {
|
|
|
|
if len(data.PetList) > 6 || len(data.BackupPetList) > 6 {
|
|
return nil, errorcode.ErrorCodes.ErrPokemonIDMismatch
|
|
}
|
|
|
|
totalPetCount := len(player.Info.PetList) + len(player.Info.BackupPetList)
|
|
if len(data.PetList)+len(data.BackupPetList) != totalPetCount {
|
|
return nil, errorcode.ErrorCodes.ErrPokemonIDMismatch
|
|
}
|
|
|
|
petMap := buildPetInfoMap(player.Info.PetList, player.Info.BackupPetList)
|
|
used := make(map[uint32]struct{}, totalPetCount)
|
|
|
|
battleList, ok := buildOrderedPetList(data.PetList, petMap, used)
|
|
if !ok {
|
|
return nil, errorcode.ErrorCodes.ErrPokemonIDMismatch
|
|
}
|
|
|
|
backupList, ok := buildOrderedPetList(data.BackupPetList, petMap, used)
|
|
if !ok {
|
|
return nil, errorcode.ErrorCodes.ErrPokemonIDMismatch
|
|
}
|
|
|
|
if len(used) != totalPetCount {
|
|
return nil, errorcode.ErrorCodes.ErrPokemonIDMismatch
|
|
}
|
|
|
|
player.Info.PetList = battleList
|
|
player.Info.BackupPetList = backupList
|
|
player.Service.Info.Save(*player.Info)
|
|
return nil, 0
|
|
}
|
|
|
|
// PetRetrieveFromWarehouse 领回仓库精灵
|
|
func (h Controller) PetRetrieveFromWarehouse(
|
|
data *pet.PET_RETRIEVE, player *player.Player) (result *fight.NullOutboundInfo, err errorcode.ErrorCode) {
|
|
if _, ok := findPetListSlot(player, data.CatchTime); ok {
|
|
return nil, 0
|
|
}
|
|
|
|
petInfo := player.Service.Pet.PetInfoOneByCatchTime(data.CatchTime)
|
|
if petInfo == nil {
|
|
return nil, 0
|
|
}
|
|
|
|
if len(player.Info.PetList) < 6 {
|
|
player.Info.PetList = append(player.Info.PetList, petInfo.Data)
|
|
|
|
} else if len(player.Info.BackupPetList) < 6 {
|
|
player.Info.BackupPetList = append(player.Info.BackupPetList, petInfo.Data)
|
|
|
|
}
|
|
|
|
return nil, 0
|
|
}
|
|
|
|
// TogglePetBagWarehouse 精灵背包仓库切换
|
|
func (h Controller) TogglePetBagWarehouse(
|
|
data *pet.PetReleaseInboundInfo,
|
|
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:
|
|
slot, ok := findPetListSlot(player, data.CatchTime)
|
|
if !ok {
|
|
return result, errorcode.ErrorCodes.ErrPokemonNotExists
|
|
}
|
|
if !slot.isValid() {
|
|
return result, errorcode.ErrorCodes.ErrPokemonIDMismatch
|
|
}
|
|
if !player.Service.Pet.Update(slot.info) {
|
|
return result, errorcode.ErrorCodes.ErrSystemError
|
|
}
|
|
|
|
slot.remove()
|
|
if slot.kind == petListKindMain {
|
|
player.Service.Info.Save(*player.Info)
|
|
}
|
|
|
|
case 1:
|
|
if len(player.Info.PetList) >= 6 && len(player.Info.BackupPetList) >= 6 {
|
|
return result, errorcode.ErrorCodes.ErrPokemonIDMismatch
|
|
}
|
|
if _, ok := findPetListSlot(player, data.CatchTime); ok {
|
|
return result, 0
|
|
}
|
|
|
|
petInfo := player.Service.Pet.PetInfoOneByCatchTime(data.CatchTime)
|
|
if petInfo == nil {
|
|
return result, errorcode.ErrorCodes.ErrPokemonNotExists
|
|
}
|
|
if len(player.Info.PetList) < 6 {
|
|
player.Info.PetList = append(player.Info.PetList, petInfo.Data)
|
|
} else {
|
|
player.Info.BackupPetList = append(player.Info.BackupPetList, petInfo.Data)
|
|
}
|
|
result.PetInfo = petInfo.Data
|
|
}
|
|
|
|
if len(player.Info.PetList) > 0 {
|
|
result.FirstPetTime = player.Info.PetList[0].CatchTime
|
|
}
|
|
|
|
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.Info.BackupPetList = removePetByCatchTime(player.Info.BackupPetList, 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.Info.BackupPetList = removePetByCatchTime(player.Info.BackupPetList, data.CatchTime)
|
|
break
|
|
}
|
|
|
|
if index, backupPet, ok := findBackupPet(player, 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
|
|
}
|