feat(pet): 重构宠物属性计算与技能学习逻辑,优化代码结构

This commit is contained in:
1
2025-09-21 17:01:31 +00:00
parent 691cfc878b
commit afb31bd79b
6 changed files with 260 additions and 142 deletions

View File

@@ -114,34 +114,28 @@ func (h *Controller) PlayerShowPet(
}
func (h *Controller) PetOneCure(
data *pet.PetOneCureInboundInfo, c *player.Player) (result *pet.PetOneCureOutboundInfo, err errorcode.ErrorCode) { //这个时候player应该是空的
result = &pet.PetOneCureOutboundInfo{
data.CatchTime,
}
var temp []model.PetInfo
for _, pi := range c.Info.PetList {
if pi.CatchTime == data.CatchTime {
pi.Hp = pi.MaxHp
// 恢复技能PP值
var skillList [4]model.SkillInfo
for i, skill := range pi.SkillList {
// 跳过无效技能
if skill.ID == 0 {
continue
}
// 恢复至最大PP值从配置表获取
if maxPP, ok := xmlres.SkillMap[int(skill.ID)]; ok {
skill.PP = uint32(maxPP.MaxPP)
skillList[i] = skill
}
_, onpet, ok := FindWithIndex(c.Info.PetList, func(item model.PetInfo) bool {
return item.CatchTime == data.CatchTime
})
if ok {
onpet.Hp = onpet.MaxHp
for i := 0; i < 4; i++ {
// 跳过无效技能
if onpet.SkillList[i].ID == 0 {
continue
}
// 恢复至最大PP值从配置表获取
if maxPP, ok := xmlres.SkillMap[int(onpet.SkillList[i].ID)]; ok {
onpet.SkillList[i].PP = uint32(maxPP.MaxPP)
}
pi.SkillList = skillList
}
temp = append(temp, pi)
}
c.Info.PetList = temp
return result, 0
return &pet.PetOneCureOutboundInfo{
CatchTime: data.CatchTime,
}, 0
}
@@ -165,3 +159,14 @@ func (h *Controller) PetFirst(
return result, 0
}
// FindWithIndex 遍历slice找到第一个满足条件的元素
// 返回:索引、元素指针、是否找到
func FindWithIndex[T any](slice []T, predicate func(item T) bool) (int, *T, bool) {
for i := range slice {
if predicate(slice[i]) {
return i, &slice[i], true
}
}
return -1, nil, false
}