refactor: 重构宠物背包逻辑到玩家服务
All checks were successful
ci/woodpecker/push/my-first-workflow Pipeline was successful

This commit is contained in:
xinian
2026-04-05 05:47:25 +08:00
committed by cnb
parent bceb7965f7
commit e71971d0b4
7 changed files with 175 additions and 192 deletions

View File

@@ -6,10 +6,6 @@ import (
"blazing/modules/player/model"
)
type GetPetListInboundEmpty struct {
Head common.TomeeHeader `cmd:"2303" struc:"skip"`
}
type GetPetListOutboundInfo struct {
ShortInfoListLen uint32 `struc:"int32,sizeof=ShortInfoList"`
ShortInfoList []PetShortInfo

View File

@@ -1,10 +1,34 @@
package player
import (
"blazing/common/utils"
"blazing/logic/service/pet"
"blazing/modules/player/model"
)
type PetBagSlot struct {
list *[]model.PetInfo
index int
info model.PetInfo
main bool
}
func (slot PetBagSlot) IsValid() bool {
return slot.list != nil && slot.index >= 0 && slot.index < len(*slot.list)
}
func (slot PetBagSlot) Remove() {
*slot.list = append((*slot.list)[:slot.index], (*slot.list)[slot.index+1:]...)
}
func (slot PetBagSlot) PetInfo() model.PetInfo {
return slot.info
}
func (slot PetBagSlot) IsMainBag() bool {
return slot.main
}
func buildCatchTimeSet(petLists ...[]model.PetInfo) map[uint32]struct{} {
total := 0
for _, petList := range petLists {
@@ -20,14 +44,140 @@ func buildCatchTimeSet(petLists ...[]model.PetInfo) map[uint32]struct{} {
return catchTimes
}
func removePetByCatchTime(petList []model.PetInfo, catchTime uint32) []model.PetInfo {
for i := range petList {
if petList[i].CatchTime == catchTime {
return append(petList[:i], petList[i+1:]...)
}
}
return petList
}
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)
for _, petList := range petLists {
for _, petInfo := range petList {
petMap[petInfo.CatchTime] = petInfo
}
}
return petMap
}
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
}
// GetUserBagPetInfo 返回主背包和并列备用精灵列表。
func (p *Player) GetUserBagPetInfo() *pet.GetUserBagPetInfoOutboundInfo {
result := &pet.GetUserBagPetInfoOutboundInfo{
PetList: make([]model.PetInfo, len(p.Info.PetList)),
BackupPetList: make([]model.PetInfo, len(p.Info.BackupPetList)),
PetList: p.Info.PetList,
BackupPetList: p.Info.BackupPetList,
}
copy(result.PetList, p.Info.PetList)
copy(result.BackupPetList, p.Info.BackupPetList)
return result
}
func (p *Player) FindBackupPet(catchTime uint32) (int, *model.PetInfo, bool) {
return utils.FindWithIndex(p.Info.BackupPetList, func(petInfo model.PetInfo) bool {
return petInfo.CatchTime == catchTime
})
}
func (p *Player) FindPetBagSlot(catchTime uint32) (PetBagSlot, bool) {
if index, petInfo, ok := p.FindPet(catchTime); ok {
return PetBagSlot{
list: &p.Info.PetList,
index: index,
info: *petInfo,
main: true,
}, true
}
if index, petInfo, ok := p.FindBackupPet(catchTime); ok {
return PetBagSlot{
list: &p.Info.BackupPetList,
index: index,
info: *petInfo,
main: false,
}, true
}
return PetBagSlot{}, false
}
func (p *Player) AddPetToAvailableBag(petInfo model.PetInfo) bool {
if len(p.Info.PetList) < 6 {
p.Info.PetList = append(p.Info.PetList, petInfo)
return true
}
if len(p.Info.BackupPetList) < 6 {
p.Info.BackupPetList = append(p.Info.BackupPetList, petInfo)
return true
}
return false
}
func (p *Player) RemoveBackupPet(catchTime uint32) {
p.Info.BackupPetList = removePetByCatchTime(p.Info.BackupPetList, catchTime)
}
func (p *Player) SavePetBagOrder(petList []uint32, backupPetList []uint32) bool {
if len(petList) > 6 || len(backupPetList) > 6 {
return false
}
totalPetCount := len(p.Info.PetList) + len(p.Info.BackupPetList)
if len(petList)+len(backupPetList) != totalPetCount {
return false
}
petMap := buildPetInfoMap(p.Info.PetList, p.Info.BackupPetList)
used := make(map[uint32]struct{}, totalPetCount)
battleList, ok := buildOrderedPetList(petList, petMap, used)
if !ok {
return false
}
backupList, ok := buildOrderedPetList(backupPetList, petMap, used)
if !ok {
return false
}
if len(used) != totalPetCount {
return false
}
p.Info.PetList = battleList
p.Info.BackupPetList = backupList
return true
}