fix(fight): 使用模型层方法生成精灵信息 refactor(controller): 移除冗余变量与内联XML读取逻辑 refactor(pet): 重构经验更新与进化逻辑 refactor(item): 校验并扣减使用道具数量 feat(item): 新增金豆购买商品协议结构体 feat(user): 迁移角色服装变更逻辑至user控制器 feat(pet): 增加技能排序协议定义 refactor(utils): 移除未使用的工具函数引用 chore(config): 更新地图怪物配置信息 详细变更内容包括: - 在`xmlres/file.go`中初始化`GoldProductMap`并加载相关配置。 - 将`GenPetInfo`方法从玩家服务迁移至`model`包以统一管理。 - 合并部分不必要的局部变量声明,并优化XML资源加载方式。 - 拆分精灵升级与进化方法,明确调用职责。 - 在战斗和治疗等操作前增加货币校验及扣除逻辑。 - 补充金豆购买相关的客户端/服务端通信结构体。 - 调整技能选择逻辑避免潜在索引越界问题。 - 更新部分注释说明和代码结构以提升可维护性。
202 lines
4.9 KiB
Go
202 lines
4.9 KiB
Go
package controller
|
||
|
||
import (
|
||
"blazing/common/data/xmlres"
|
||
"blazing/common/socket/errorcode"
|
||
"strings"
|
||
|
||
"blazing/logic/service/fight"
|
||
"blazing/logic/service/fight/info"
|
||
|
||
"blazing/logic/service/player"
|
||
"blazing/modules/blazing/model"
|
||
|
||
"github.com/gogf/gf/v2/util/gconv"
|
||
"github.com/gogf/gf/v2/util/grand"
|
||
"github.com/samber/lo"
|
||
)
|
||
|
||
func processMonID(bm string) string {
|
||
// 按空格分割字符串
|
||
monid := strings.Split(bm, " ")
|
||
|
||
// 过滤分割后可能的空字符串(如连续空格导致的空元素)
|
||
filtered := make([]string, 0, len(monid))
|
||
for _, m := range monid {
|
||
if m != "" {
|
||
filtered = append(filtered, m)
|
||
}
|
||
}
|
||
monid = filtered
|
||
|
||
var selected string
|
||
switch len(monid) {
|
||
case 0:
|
||
// 无元素时,可返回空或默认值(根据业务需求调整)
|
||
selected = ""
|
||
case 1:
|
||
// 长度为1时,取第一个(唯一的元素)
|
||
selected = monid[0]
|
||
default:
|
||
// 长度大于1时,随机选取一个
|
||
randomIdx := grand.Intn(len(monid))
|
||
selected = monid[randomIdx]
|
||
}
|
||
return selected
|
||
}
|
||
|
||
// 挑战地图boss
|
||
func (h Controller) PlayerFightBoss(data *fight.ChallengeBossInboundInfo, c *player.Player) (result *fight.NullOutboundInfo, err errorcode.ErrorCode) {
|
||
|
||
var mo *model.PetInfo
|
||
moinfo := &model.PlayerInfo{}
|
||
|
||
var taskid int
|
||
var cancpet int
|
||
mdata, ok := xmlres.MonsterMap[int(c.Info.MapID)]
|
||
if !ok {
|
||
return nil, errorcode.ErrorCodes.ErrPokemonNotExists
|
||
}
|
||
if len(mdata.Bosses) == 0 {
|
||
return nil, errorcode.ErrorCodes.ErrPokemonNotExists
|
||
}
|
||
for _, bc := range mdata.Bosses {
|
||
|
||
if bc.Id == nil {
|
||
|
||
bc.Id = gconv.PtrInt(0)
|
||
}
|
||
|
||
if (bc.Id == nil && data.BossId == 0) || uint32(*bc.Id) == data.BossId { //打默认第一个boss
|
||
if bc.TaskID != nil {
|
||
taskid = *bc.TaskID
|
||
}
|
||
|
||
for _, bm := range bc.BossMon {
|
||
|
||
mo = model.GenPetInfo(
|
||
gconv.Int(processMonID(bm.MonID)), 24, //24个体
|
||
-1,
|
||
0, //野怪没特性
|
||
0,
|
||
bm.Lv)
|
||
|
||
mo.Hp = uint32(bm.Hp)
|
||
mo.MaxHp = uint32(bm.Hp)
|
||
moinfo.PetList = append(moinfo.PetList, *mo)
|
||
|
||
}
|
||
if bc.BossCatchable == 1 {
|
||
cancpet = xmlres.PetMAP[int(mo.ID)].CatchRate
|
||
}
|
||
moinfo.Nick = xmlres.PetMAP[int(mo.ID)].DefName
|
||
break
|
||
}
|
||
|
||
}
|
||
|
||
c.Fightinfo.Status = info.BattleMode.FIGHT_WITH_NPC
|
||
c.Fightinfo.Mode = info.BattleMode.MULTI_MODE
|
||
|
||
ai := player.NewAI_player(moinfo)
|
||
ai.CanCapture = cancpet
|
||
fight.NewFight(c, ai, func(foi *info.FightOverInfo) {
|
||
|
||
c.Done.Exec(model.MilestoneMode.BOSS, []uint32{c.Info.MapID, data.BossId, uint32(foi.Reason)}, func(results *model.MilestoneEX) uint32 {
|
||
if foi.Reason == 0 && foi.WinnerId == c.Info.UserID {
|
||
if results.CheakNoNumber(1) && taskid != 0 { //触发3次奖励
|
||
c.Info.TaskList[taskid-1] = 3
|
||
|
||
moinfo.PetList[0].Downgrade(1)
|
||
PetID := moinfo.PetList[0].ID
|
||
|
||
newm1 := model.GenPetInfo(int(PetID), -1, -1, 0, 0, 1)
|
||
c.Service.Pet.PetAdd(*newm1)
|
||
|
||
c.SendPackCmd(8004, &info.S2C_GET_BOSS_MONSTER{
|
||
BonusID: uint32(taskid),
|
||
PetID: PetID,
|
||
CaptureTm: newm1.CatchTime,
|
||
})
|
||
|
||
return 1
|
||
}
|
||
}
|
||
return 0
|
||
|
||
})
|
||
|
||
})
|
||
|
||
return nil, -1
|
||
}
|
||
|
||
// 战斗野怪
|
||
func (h Controller) OnPlayerFightNpcMonster(data *fight.FightNpcMonsterInboundInfo, c *player.Player) (result *fight.NullOutboundInfo, err errorcode.ErrorCode) {
|
||
|
||
refpet := c.OgreInfo.Data[data.Number]
|
||
if refpet.Id == 0 {
|
||
|
||
return nil, errorcode.ErrorCodes.ErrPokemonNotExists
|
||
}
|
||
mo := model.GenPetInfo(
|
||
int(refpet.Id), -1,
|
||
-1,
|
||
0, //野怪没特性
|
||
int(refpet.Shiny),
|
||
int(refpet.Lv))
|
||
|
||
moinfo := &model.PlayerInfo{}
|
||
moinfo.Nick = xmlres.PetMAP[int(mo.ID)].DefName
|
||
moinfo.PetList = append(moinfo.PetList, *mo)
|
||
ai := player.NewAI_player(moinfo)
|
||
ai.CanCapture = handleNPCFightSpecial(mo.ID)
|
||
|
||
c.Fightinfo.Status = info.BattleMode.FIGHT_WITH_NPC //打野怪
|
||
c.Fightinfo.Mode = info.BattleMode.MULTI_MODE //多人模式
|
||
|
||
fight.NewFight(c, ai, func(foi *info.FightOverInfo) {
|
||
c.Done.Exec(model.MilestoneMode.Moster, []uint32{c.Info.MapID, moinfo.PetList[0].ID, uint32(foi.Reason)}, func(results *model.MilestoneEX) uint32 {
|
||
if foi.Reason == 0 && foi.WinnerId == c.Info.UserID {
|
||
exp := uint32(xmlres.PetMAP[int(mo.ID)].YieldingExp) * mo.Level / 7
|
||
items := &info.S2C_GET_BOSS_MONSTER{
|
||
//EV: 45,
|
||
EXP: exp * 2,
|
||
}
|
||
if refpet.Item != 0 {
|
||
|
||
items.ItemList = c.ItemAdd(model.ItemInfo{
|
||
ItemId: refpet.Item,
|
||
ItemCnt: uint32(grand.Intn(2) + 1),
|
||
})
|
||
|
||
}
|
||
evs := gconv.Uint32s(strings.Split(xmlres.PetMAP[int(mo.ID)].YieldingEV, " "))
|
||
items.EV = lo.Sum(evs)
|
||
c.Info.EVPool += lo.Sum(evs) //给予累计学习力
|
||
foi.Winpet.AddEV(evs)
|
||
|
||
c.Info.ExpPool += exp * 4
|
||
c.AddPetExp(foi.Winpet, uint32(exp)*2)
|
||
c.SendPackCmd(8004, items)
|
||
}
|
||
return 0
|
||
|
||
})
|
||
})
|
||
|
||
return nil, -1
|
||
}
|
||
func handleNPCFightSpecial(petid uint32) int {
|
||
|
||
npcPetID := int(petid)
|
||
petCfg, ok := xmlres.PetMAP[npcPetID]
|
||
if !ok {
|
||
// log.Error(context.Background(), "NPC宠物配置不存在", "petID", npcPetID)
|
||
return 0
|
||
}
|
||
|
||
catchRate := gconv.Int(petCfg.CatchRate)
|
||
return catchRate
|
||
}
|