Files
bl/logic/controller/pet.go
昔念 e54d4bacaa ```
feat(fight): 增加战斗模式枚举并重构战斗逻辑判断

- 引入完整的 BattleMode 枚举定义,替代原有的 BattleStatus,明确区分各类战斗场景
- 在多个控制器中替换对旧 Status 字段的依赖,统一使用 Mode 判断战斗状态
- 修复部分函数调用前未检查 FightC 是否为空的问题,增加 ErrBattleEnded 错误返回
- 调整
2025-11-21 02:40:27 +08:00

226 lines
5.9 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package controller
import (
"blazing/common/data/xmlres"
"blazing/common/socket/errorcode"
"blazing/common/utils"
"blazing/logic/service/pet"
"blazing/logic/service/player"
"blazing/modules/blazing/model"
"github.com/jinzhu/copier"
)
// 获取精灵信息
func (h *Controller) GetPetInfo(
data *pet.InInfo,
c *player.Player) (result *pet.OutInfo,
err errorcode.ErrorCode) { //这个时候player应该是空的
for _, pi := range c.Info.PetList {
if pi.CatchTime == data.CatchTime {
result = &pet.OutInfo{
PetInfo: pi,
}
return result, 0
}
}
if result == nil {
result = &pet.OutInfo{
PetInfo: c.Service.Pet.PetInfo_One(data.CatchTime).Data,
}
}
return result, 0
}
// 获取仓库列表
func (h *Controller) GetPetList(
data *pet.GetPetListInboundEmpty,
c *player.Player) (result *pet.GetPetListOutboundInfo,
err errorcode.ErrorCode) { //这个时候player应该是空的
result = &pet.GetPetListOutboundInfo{}
tt := c.Service.Pet.PetInfo(0) //获得未放生的精灵
result.ShortInfoList = make([]pet.PetShortInfo, len(tt))
for i, v := range tt {
copier.Copy(&result.ShortInfoList[i], &v.Data)
}
return result, 0
}
// 精灵背包仓库切换
func (h *Controller) PetRelease(
data *pet.PetReleaseInboundInfo,
c *player.Player) (
result *pet.PetReleaseOutboundInfo,
err errorcode.ErrorCode) { //这个时候player应该是空的
//放入背包=数据库置1+添加到背包+pet release发包 仓库=数据库置0+移除背包 设置首发等于取到首发精灵后重新排序
//这里只修改,因为添加和移除背包在宠物获取时已经做了
result = &pet.PetReleaseOutboundInfo{}
result.Flag = uint32(data.Flag)
//擂台住不能换精灵
if c.GetSpace().Owner.UserID == c.Info.UserID {
return result, errorcode.ErrorCodes.ErrChampionCannotSwitch
}
switch data.Flag {
case 0:
var temp []model.PetInfo
for _, v := range c.Info.PetList {
if v.CatchTime == uint32(data.CatchTime) {
c.Service.Pet.PetInfo_One_exec(data.CatchTime, func(t *model.PetEX) {
t.Data = v
//t.InBag = 0
})
} else {
temp = append(temp, v)
}
}
c.Info.PetList = temp
// break // 只移除第一个匹配值,若需移除所有,可省略 break 继续循环
case 1:
//todo 背包
c.Service.Pet.PetInfo_One_exec(data.CatchTime, func(t *model.PetEX) {
_, _, ok := utils.FindWithIndex(c.Info.PetList, func(item model.PetInfo) bool {
return item.CatchTime == uint32(data.CatchTime)
})
//如果背包没找到,再放入背包
if !ok && t.CatchTime != 0 {
//t.InBag = 1
c.Info.PetList = append(c.Info.PetList, t.Data)
result.PetInfo = t.Data
}
})
}
if len(c.Info.PetList) > 0 {
result.FirstPetTime = c.Info.PetList[0].CatchTime //设置首发
}
//service.NewUserService(c.Info.UserID).PetAdd( *r)
return result, 0
}
// 精灵展示
func (h *Controller) PlayerShowPet(
data *pet.PetShowInboundInfo, c *player.Player) (result *pet.PetShowOutboundInfo, err errorcode.ErrorCode) { //这个时候player应该是空的
result = &pet.PetShowOutboundInfo{}
for _, pi := range c.Info.PetList {
if pi.CatchTime == data.CatchTime {
copier.Copy(&result, pi)
result.Flag = data.Flag
result.UserID = data.Head.UserID
c.GetSpace().Broadcast(c, data.Head.CMD, result)
}
}
return
}
func (h *Controller) PetOneCure(
data *pet.PetOneCureInboundInfo, c *player.Player) (result *pet.PetOneCureOutboundInfo, err errorcode.ErrorCode) { //这个时候player应该是空的
if c.GetSpace().Owner.UserID == c.Info.UserID {
return result, errorcode.ErrorCodes.ErrChampionCannotHeal
}
_, onpet, ok := utils.FindWithIndex(c.Info.PetList, func(item model.PetInfo) bool {
return item.CatchTime == data.CatchTime
})
if ok {
onpet.Cure()
}
return &pet.PetOneCureOutboundInfo{
CatchTime: data.CatchTime,
}, 0
}
// 精灵首发
func (h *Controller) PetFirst(
data *pet.PetDefaultInboundInfo, c *player.Player) (result *pet.PetDefaultOutboundInfo, err errorcode.ErrorCode) { //这个时候player应该是空的
result = &pet.PetDefaultOutboundInfo{}
var ttt []model.PetInfo
for index, pi := range c.Info.PetList {
if pi.CatchTime == data.CatchTime {
ttt = append(ttt, pi)
ttt = append(ttt, c.Info.PetList[:index]...)
ttt = append(ttt, c.Info.PetList[index+1:]...)
result.IsDefault = 1
break
}
}
c.Info.PetList = ttt
return result, 0
}
// FindWithIndex 遍历slice找到第一个满足条件的元素
// 返回:索引、元素指针、是否找到
func (h Controller) SetPetExp(data *pet.PetSetExpInboundInfo, c *player.Player) (result *pet.PetSetExpOutboundInfo, err errorcode.ErrorCode) {
_, onpet, ok := utils.FindWithIndex(c.Info.PetList, func(item model.PetInfo) bool {
return item.CatchTime == data.CatchTime
})
if ok {
c.AddPetExp(onpet, data.Exp)
}
return &pet.PetSetExpOutboundInfo{
Exp: c.Info.ExpPool,
}, 0
}
func (h Controller) SetPetSkill(data *pet.ChangeSkillInfo, c *player.Player) (result *pet.ChangeSkillOutInfo, err errorcode.ErrorCode) {
_, onpet, ok := utils.FindWithIndex(c.Info.PetList, func(item model.PetInfo) bool {
return item.CatchTime == data.CatchTime
})
if ok {
_, _, ok := utils.FindWithIndex(onpet.SkillList, func(item model.SkillInfo) bool { //已经存在技能
return item.ID == data.ReplaceSkill
})
if ok {
return
}
for i := 0; i < len(onpet.SkillList); i++ {
if onpet.SkillList[i].ID == data.HasSkill {
onpet.SkillList[i].ID = data.ReplaceSkill
onpet.SkillList[i].PP = uint32(xmlres.SkillMap[int(onpet.SkillList[i].ID)].MaxPP)
}
}
}
// onpet.SkillList = lo.UniqBy(onpet.SkillList, func(s model.SkillInfo) int {
// return int(s.ID)
// })
return &pet.ChangeSkillOutInfo{
CatchTime: data.CatchTime,
}, 0
}
func (h Controller) PetBargeList(data *pet.PetBargeListInboundInfo, c *player.Player) (result *pet.PetBargeListOutboundInfo, err errorcode.ErrorCode) {
return &pet.PetBargeListOutboundInfo{
PetBargeList: make([]pet.PetBargeListInfo, 0),
}, 0
}