```
All checks were successful
ci/woodpecker/push/my-first-workflow Pipeline was successful

feat(pet): 宠物系统重构和功能增强

- 修复战斗boss中effect ID索引错误问题
- 实现宠物仓库和背包管理功能
- 添加宠物列表排序保存功能
- 重构宠物备份列表同步逻辑
- 优化宠物释放和获取逻辑
- 添加宠物背包仓库切换功能
- 修复地图模型广播信息结构问题
- 调整宠物特效数据库查询逻辑
```
This commit is contained in:
昔念
2026-04-02 07:49:49 +08:00
parent 3a13bcc99c
commit f810a2ae86
13 changed files with 280 additions and 43 deletions

View File

@@ -160,7 +160,7 @@ func appendPetEffects(monster *model.PetInfo, effectIDs []uint32) {
effects := service.NewEffectService().Args(effectIDs)
for _, effect := range effects {
monster.EffectInfo = append(monster.EffectInfo, model.PetEffectInfo{
Idx: uint16(effect.SeIdx),
Idx: uint16(effect.ID),
EID: gconv.Uint16(effect.Eid),
Args: gconv.Ints(effect.Args),
})

View File

@@ -224,7 +224,7 @@ func buildTowerMonsterInfo(towerBoss configmodel.BaseTowerConfig) (*model.Player
effects := service.NewEffectService().Args(boss.Effect)
for _, effect := range effects {
monster.EffectInfo = append(monster.EffectInfo, model.PetEffectInfo{
Idx: uint16(effect.SeIdx),
Idx: uint16(effect.ID),
EID: gconv.Uint16(effect.Eid),
Args: gconv.Ints(effect.Args),
})

View File

@@ -39,21 +39,73 @@ func removePetByCatchTime(petList []model.PetInfo, catchTime uint32) []model.Pet
return petList
}
func syncBackupPetList(player *player.Player) {
if len(player.Info.BackupPetList) > 0 {
return
func buildWarehousePetList(player *player.Player) []model.PetInfo {
allPets := player.Service.Pet.PetInfo(0)
if len(allPets) == 0 {
return make([]model.PetInfo, 0)
}
storagePets := player.Service.Pet.PetInfo(1)
if len(storagePets) == 0 {
usedCatchTimes := make(map[uint32]struct{}, len(player.Info.PetList)+len(player.Info.BackupPetList))
for _, petInfo := range player.Info.PetList {
usedCatchTimes[petInfo.CatchTime] = struct{}{}
}
for _, petInfo := range player.Info.BackupPetList {
usedCatchTimes[petInfo.CatchTime] = struct{}{}
}
result := make([]model.PetInfo, 0, len(allPets))
for i := range allPets {
catchTime := allPets[i].Data.CatchTime
if _, exists := usedCatchTimes[catchTime]; exists {
continue
}
result = append(result, allPets[i].Data)
}
return result
}
func findBackupPet(player *player.Player, catchTime uint32) (int, *model.PetInfo, bool) {
for i := range player.Info.BackupPetList {
if player.Info.BackupPetList[i].CatchTime == catchTime {
return i, &player.Info.BackupPetList[i], true
}
}
return -1, nil, false
}
func syncBackupPetList(player *player.Player) {
if player.Info.BackupPetList == nil {
player.Info.BackupPetList = make([]model.PetInfo, 0)
return
}
player.Info.BackupPetList = make([]model.PetInfo, len(storagePets))
for i := range storagePets {
player.Info.BackupPetList[i] = storagePets[i].Data
bagPets := player.Service.Pet.PetInfo(0)
if len(bagPets) == 0 {
player.Info.BackupPetList = make([]model.PetInfo, 0)
return
}
bagCatchTimes := make(map[uint32]struct{}, len(bagPets))
for i := range bagPets {
bagCatchTimes[bagPets[i].Data.CatchTime] = struct{}{}
}
mainPetCatchTimes := make(map[uint32]struct{}, len(player.Info.PetList))
for _, petInfo := range player.Info.PetList {
mainPetCatchTimes[petInfo.CatchTime] = struct{}{}
}
nextBackupList := make([]model.PetInfo, 0, len(player.Info.BackupPetList))
for _, petInfo := range player.Info.BackupPetList {
if _, inBag := bagCatchTimes[petInfo.CatchTime]; !inBag {
continue
}
if _, inMain := mainPetCatchTimes[petInfo.CatchTime]; inMain {
continue
}
nextBackupList = append(nextBackupList, petInfo)
}
player.Info.BackupPetList = nextBackupList
}
func buildUserBagPetInfo(player *player.Player) *pet.GetUserBagPetInfoOutboundInfo {
@@ -68,6 +120,31 @@ func buildUserBagPetInfo(player *player.Player) *pet.GetUserBagPetInfoOutboundIn
return result
}
func buildOrderedPetList(
catchTimes []uint32,
petMap map[uint32]model.PetInfo,
used map[uint32]struct{},
) ([]model.PetInfo, bool) {
result := make([]model.PetInfo, 0, len(catchTimes))
for _, catchTime := range catchTimes {
if catchTime == 0 {
return nil, false
}
if _, exists := used[catchTime]; exists {
return nil, false
}
petInfo, exists := petMap[catchTime]
if !exists {
return nil, false
}
used[catchTime] = struct{}{}
result = append(result, petInfo)
}
return result, true
}
func buildPetShowOutboundInfo(userID, flag uint32, info *model.PetInfo) *pet.PetShowOutboundInfo {
return &pet.PetShowOutboundInfo{
UserID: userID,
@@ -115,6 +192,52 @@ func (h Controller) GetUserBagPetInfo(
// data: 空输入结构
// player: 当前玩家对象
// 返回: 精灵列表和错误码
// SavePetBagOrder 保存当前主背包和备用背包顺序
func (h Controller) SavePetBagOrder(
data *pet.SavePetBagOrderInboundInfo,
player *player.Player) (result *fight.NullOutboundInfo,
err errorcode.ErrorCode) {
syncBackupPetList(player)
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 := make(map[uint32]model.PetInfo, totalPetCount)
for _, petInfo := range player.Info.PetList {
petMap[petInfo.CatchTime] = petInfo
}
for _, petInfo := range player.Info.BackupPetList {
petMap[petInfo.CatchTime] = petInfo
}
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
}
func (h Controller) GetPetList(
data *pet.GetPetListInboundEmpty,
player *player.Player) (result *pet.GetPetListOutboundInfo,
@@ -131,7 +254,7 @@ func (h Controller) GetPetReleaseList(
player *player.Player) (result *pet.GetPetListOutboundInfo,
err errorcode.ErrorCode) {
syncBackupPetList(player)
return buildPetListOutboundInfo(player.Info.BackupPetList), 0
return buildPetListOutboundInfo(buildWarehousePetList(player)), 0
}
// PetReleaseToWarehouse 将精灵从仓库包中放生
@@ -141,9 +264,10 @@ func (h Controller) GetPetReleaseList(
func (h Controller) PetReleaseToWarehouse(
data *pet.PET_ROWEI, player *player.Player) (result *fight.NullOutboundInfo, err errorcode.ErrorCode) {
_, _, inBag := player.FindPet(data.CatchTime)
_, _, inBackup := findBackupPet(player, data.CatchTime)
freeForbidden := xmlres.PetMAP[int(data.ID)].FreeForbidden
// 如果背包没找到,再放入背包
if inBag || freeForbidden == 1 {
if inBag || inBackup || freeForbidden == 1 {
return nil, errorcode.ErrorCodes.ErrCannotReleaseNonWarehouse
}
@@ -160,10 +284,15 @@ func (h Controller) PetRetrieveFromWarehouse(
//如果背包没找到,再放入背包
if _, _, ok := player.FindPet(data.CatchTime); !ok {
if !player.Service.Pet.UpdateFree(data.CatchTime, 0) {
return nil, errorcode.ErrorCodes.ErrSystemError
if petInfo := player.Service.Pet.PetInfoOneByCatchTime(data.CatchTime); petInfo != nil {
syncBackupPetList(player)
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)
}
player.Service.Info.Save(*player.Info)
}
}
return nil, 0
@@ -173,6 +302,76 @@ func (h Controller) PetRetrieveFromWarehouse(
// TogglePetBagWarehouse 精灵背包仓库切换
func (h Controller) TogglePetBagWarehouse(
data *pet.PetReleaseInboundInfo,
player *player.Player) (result *pet.PetReleaseOutboundInfo, err errorcode.ErrorCode) {
result = &pet.PetReleaseOutboundInfo{}
result.Flag = uint32(data.Flag)
if player.GetSpace().Owner.UserID == player.Info.UserID {
return result, errorcode.ErrorCodes.ErrChampionCannotSwitch
}
syncBackupPetList(player)
switch data.Flag {
case 0:
if index, currentPet, ok := player.FindPet(data.CatchTime); ok {
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.Service.Info.Save(*player.Info)
break
}
index, currentPet, ok := findBackupPet(player, data.CatchTime)
if !ok {
return result, errorcode.ErrorCodes.ErrPokemonNotExists
}
if index < 0 || index >= len(player.Info.BackupPetList) {
return result, errorcode.ErrorCodes.ErrPokemonIDMismatch
}
if !player.Service.Pet.Update(*currentPet) {
return result, errorcode.ErrorCodes.ErrSystemError
}
player.Info.BackupPetList = append(player.Info.BackupPetList[:index], player.Info.BackupPetList[index+1:]...)
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 := player.FindPet(data.CatchTime); ok {
return result, 0
}
if _, _, ok := findBackupPet(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
player.Service.Info.Save(*player.Info)
}
if len(player.Info.PetList) > 0 {
result.FirstPetTime = player.Info.PetList[0].CatchTime
}
return nil, 0
}
func (h Controller) TogglePetBagWarehouseLegacy(
data *pet.PetReleaseLegacyInboundInfo,
player *player.Player) (
result *pet.PetReleaseOutboundInfo,
err errorcode.ErrorCode) { //这个时候player应该是空的