refactor: 优化代码结构和逻辑
This commit is contained in:
@@ -7,9 +7,8 @@ import (
|
||||
"strings"
|
||||
|
||||
"blazing/logic/service/fight"
|
||||
"blazing/logic/service/fight/info"
|
||||
fightinfo "blazing/logic/service/fight/info"
|
||||
"blazing/logic/service/fight/input"
|
||||
|
||||
"blazing/logic/service/player"
|
||||
configmodel "blazing/modules/config/model"
|
||||
"blazing/modules/config/service"
|
||||
@@ -20,217 +19,259 @@ import (
|
||||
)
|
||||
|
||||
// PlayerFightBoss 挑战地图boss
|
||||
// data: 包含挑战Boss信息的输入数据
|
||||
// player: 当前玩家对象
|
||||
// 返回: 战斗结果和错误码
|
||||
func (Controller) PlayerFightBoss(data1 *fight.ChallengeBossInboundInfo, p *player.Player) (result *fight.NullOutboundInfo, err errorcode.ErrorCode) {
|
||||
r := p.CanFight()
|
||||
if p.CanFight() != 0 {
|
||||
return nil, r
|
||||
}
|
||||
var monster *model.PetInfo
|
||||
monsterInfo := &model.PlayerInfo{}
|
||||
|
||||
mdata := service.NewMapNodeService().GetDataNode(p.Info.MapID, data1.BossId)
|
||||
if mdata == nil {
|
||||
return nil, errorcode.ErrorCodes.ErrPokemonNotExists
|
||||
}
|
||||
var bosinfo []configmodel.BossConfig
|
||||
switch len(mdata.BossIds) {
|
||||
case 0:
|
||||
return nil, errorcode.ErrorCodes.ErrPokemonNotExists
|
||||
case 1:
|
||||
bosinfo = service.NewBossService().Get(mdata.BossIds[0])
|
||||
default:
|
||||
bosinfo = service.NewBossService().Get(mdata.BossIds[grand.Intn(len(mdata.BossIds))])
|
||||
func (Controller) PlayerFightBoss(req *fight.ChallengeBossInboundInfo, p *player.Player) (result *fight.NullOutboundInfo, err errorcode.ErrorCode) {
|
||||
if err = p.CanFight(); err != 0 {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if len(bosinfo) == 0 {
|
||||
return nil, errorcode.ErrorCodes.ErrPokemonNotExists
|
||||
mapNode := service.NewMapNodeService().GetDataNode(p.Info.MapID, req.BossId)
|
||||
bossConfigs, err := loadMapBossConfigs(mapNode)
|
||||
if err != 0 {
|
||||
return nil, err
|
||||
}
|
||||
dv := 24
|
||||
ger := 0
|
||||
if bosinfo[0].IsCapture == 1 {
|
||||
dv = -1
|
||||
ger = -1
|
||||
|
||||
monsterInfo, leadMonsterID, err := buildBossMonsterInfo(mapNode.NodeName, bossConfigs)
|
||||
if err != 0 {
|
||||
return nil, err
|
||||
}
|
||||
for i, bm := range bosinfo {
|
||||
|
||||
monster = model.GenPetInfo(
|
||||
gconv.Int(bm.MonID), dv, //24个体
|
||||
-1,
|
||||
0, //野怪没特性
|
||||
|
||||
int(bm.Lv), nil, ger)
|
||||
monster.CatchTime = uint32(i)
|
||||
monster.ConfigBoss(bm.PetBaseConfig)
|
||||
effects := service.NewEffectService().Args(bm.Effect)
|
||||
|
||||
for _, v := range effects {
|
||||
monster.EffectInfo = append(monster.EffectInfo, model.PetEffectInfo{
|
||||
Idx: uint16(v.SeIdx),
|
||||
EID: gconv.Uint16(v.Eid),
|
||||
Args: gconv.Ints(v.Args),
|
||||
})
|
||||
}
|
||||
monsterInfo.PetList = append(monsterInfo.PetList, *monster)
|
||||
}
|
||||
if bosinfo[0].IsCapture == 1 {
|
||||
monsterInfo.PetList[0].ShinyInfo = make([]data.GlowFilter, 0)
|
||||
if grand.Meet(1, 500) {
|
||||
monsterInfo.PetList[0].RandomByWeightShiny()
|
||||
}
|
||||
|
||||
}
|
||||
monsterInfo.Nick = mdata.NodeName //xmlres.PetMAP[int(monster.ID)].DefName
|
||||
|
||||
if len(monsterInfo.PetList) == 0 {
|
||||
return nil, errorcode.ErrorCodes.ErrPokemonNotExists
|
||||
}
|
||||
p.Fightinfo.Status = info.BattleMode.FIGHT_WITH_NPC
|
||||
p.Fightinfo.Mode = info.BattleMode.MULTI_MODE
|
||||
p.Fightinfo.Status = fightinfo.BattleMode.FIGHT_WITH_NPC
|
||||
p.Fightinfo.Mode = fightinfo.BattleMode.MULTI_MODE
|
||||
|
||||
ai := player.NewAI_player(monsterInfo)
|
||||
ai.CanCapture = bosinfo[0].IsCapture
|
||||
if bosinfo[0].IsCapture != 0 {
|
||||
ai.CanCapture = xmlres.PetMAP[int(monster.ID)].CatchRate
|
||||
if xmlres.PetMAP[int(monster.ID)].CatchRate == 0 {
|
||||
ai.CanCapture = 0
|
||||
}
|
||||
}
|
||||
|
||||
ai.CanCapture = resolveBossCaptureRate(bossConfigs[0].IsCapture, leadMonsterID)
|
||||
ai.Prop[0] = 2
|
||||
var fighc *fight.FightC
|
||||
fighc, _ = fight.NewFight(p, ai, p.GetPetInfo(100), ai.GetPetInfo(0), func(foi model.FightOverInfo) {
|
||||
if mdata.WinBonusID != 0 {
|
||||
if len(bosinfo[0].Rule) == 0 {
|
||||
if foi.Reason == 0 && foi.WinnerId == p.Info.UserID {
|
||||
p.SptCompletedTask(mdata.WinBonusID, 1)
|
||||
|
||||
}
|
||||
} else {
|
||||
//说明是带规则的
|
||||
iswin := true
|
||||
for _, v := range service.NewFightRuleService().GetByRuleIdxs(bosinfo[0].Rule) {
|
||||
r := input.GetRule(int64(v.RuleIdx))
|
||||
if r != nil {
|
||||
r.SetArgs(v.Args...)
|
||||
|
||||
if !(r.Exec(fighc, &foi)) {
|
||||
iswin = false
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
if iswin {
|
||||
p.SptCompletedTask(mdata.WinBonusID, 1)
|
||||
}
|
||||
}
|
||||
|
||||
var fightC *fight.FightC
|
||||
fightC, err = fight.NewFight(p, ai, p.GetPetInfo(100), ai.GetPetInfo(0), func(foi model.FightOverInfo) {
|
||||
if mapNode.WinBonusID == 0 {
|
||||
return
|
||||
}
|
||||
if shouldGrantBossWinBonus(fightC, p.Info.UserID, bossConfigs[0], foi) {
|
||||
p.SptCompletedTask(mapNode.WinBonusID, 1)
|
||||
}
|
||||
|
||||
//p.Done.Exec(model.MilestoneMode.BOSS, []uint32{p.Info.MapID, data.BossId, uint32(foi.Reason)}, nil)
|
||||
|
||||
})
|
||||
if err != 0 {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return nil, -1
|
||||
}
|
||||
|
||||
// OnPlayerFightNpcMonster 战斗野怪
|
||||
// data: 包含战斗野怪信息的输入数据
|
||||
// player: 当前玩家对象
|
||||
// 返回: 战斗结果和错误码
|
||||
func (Controller) OnPlayerFightNpcMonster(data1 *fight.FightNpcMonsterInboundInfo, p *player.Player) (result *fight.NullOutboundInfo, err errorcode.ErrorCode) {
|
||||
r := p.CanFight()
|
||||
if p.CanFight() != 0 {
|
||||
return nil, r
|
||||
func (Controller) OnPlayerFightNpcMonster(req *fight.FightNpcMonsterInboundInfo, p *player.Player) (result *fight.NullOutboundInfo, err errorcode.ErrorCode) {
|
||||
if err = p.CanFight(); err != 0 {
|
||||
return nil, err
|
||||
}
|
||||
if data1.Number > 9 {
|
||||
if req.Number > 9 {
|
||||
return nil, errorcode.ErrorCodes.ErrSystemError
|
||||
}
|
||||
refPet := p.Data[data1.Number]
|
||||
if refPet.ID == 0 {
|
||||
|
||||
return nil, errorcode.ErrorCodes.ErrPokemonNotExists
|
||||
refPet := p.Data[req.Number]
|
||||
monster, monsterInfo, err := buildNpcMonsterInfo(refPet, p.Info.MapID)
|
||||
if err != 0 {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
monster := model.GenPetInfo(
|
||||
int(refPet.GetID()), -1,
|
||||
-1,
|
||||
0, //野怪没特性
|
||||
|
||||
int(refPet.GetLevel()),
|
||||
refPet.ShinyInfo, -1)
|
||||
monster.CatchMap = p.Info.MapID //设置当前地图
|
||||
if refPet.Ext != 0 {
|
||||
if grand.Meet(1, 500) {
|
||||
monster.RandomByWeightShiny()
|
||||
}
|
||||
}
|
||||
|
||||
monsterInfo := &model.PlayerInfo{}
|
||||
monsterInfo.Nick = xmlres.PetMAP[int(monster.ID)].DefName
|
||||
monsterInfo.PetList = append(monsterInfo.PetList, *monster)
|
||||
ai := player.NewAI_player(monsterInfo)
|
||||
ai.CanCapture = refPet.IsCapture
|
||||
|
||||
ai.CanCapture = refPet.IsCapture //handleNPCFightSpecial(monster.ID)
|
||||
|
||||
p.Fightinfo.Status = info.BattleMode.FIGHT_WITH_NPC //打野怪
|
||||
p.Fightinfo.Mode = info.BattleMode.MULTI_MODE //多人模式
|
||||
|
||||
fight.NewFight(p, ai, p.GetPetInfo(100), ai.GetPetInfo(0), func(foi model.FightOverInfo) {
|
||||
//p.Done.Exec(model.MilestoneMode.Moster, []uint32{p.Info.MapID, monsterInfo.PetList[0].ID, uint32(foi.Reason)}, nil)
|
||||
if foi.Reason == 0 && foi.WinnerId == p.Info.UserID && p.CanGet() {
|
||||
|
||||
exp := uint32(xmlres.PetMAP[int(monster.ID)].YieldingExp) * monster.Level / 7
|
||||
addlevel, poolevel := p.CanGetExp()
|
||||
addexp := gconv.Float32(addlevel * gconv.Float32(exp))
|
||||
|
||||
poolexp := gconv.Float32(poolevel) * gconv.Float32((exp))
|
||||
items := &info.S2C_GET_BOSS_MONSTER{}
|
||||
|
||||
p.ItemAdd(3, int64(poolexp+addexp))
|
||||
items.ADDitem(3, uint32(poolexp))
|
||||
|
||||
p.AddPetExp(foi.Winpet, int64(addexp))
|
||||
pettype := int64(xmlres.PetMAP[int(refPet.GetID())].Type)
|
||||
|
||||
if p.CanGetItem() {
|
||||
|
||||
item := p.GetSpace().GetDrop()
|
||||
if item != 0 {
|
||||
count := int64(grand.N(1, 2))
|
||||
ok := p.ItemAdd(item, count)
|
||||
if ok {
|
||||
items.ADDitem(uint32(item), uint32(count))
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
if monster.IsShiny() && p.CanGetXUAN() && pettype < 16 {
|
||||
xuan := 400686 + pettype
|
||||
count := uint32(grand.N(1, 2))
|
||||
ok := p.ItemAdd(xuan, int64(count))
|
||||
if ok {
|
||||
items.ADDitem(uint32(xuan), count)
|
||||
}
|
||||
|
||||
}
|
||||
p.SendPackCmd(8004, items)
|
||||
|
||||
evs := gconv.Int64s(strings.Split(xmlres.PetMAP[int(monster.ID)].YieldingEV, " "))
|
||||
|
||||
foi.Winpet.AddEV(evs)
|
||||
//取消累计学习力掉落
|
||||
// if leve == 8 {
|
||||
// items.EV = lo.Sum(evs) - 1
|
||||
// p.Info.EVPool += lo.Sum(evs) //给予累计学习力
|
||||
// }
|
||||
|
||||
}
|
||||
p.Fightinfo.Status = fightinfo.BattleMode.FIGHT_WITH_NPC
|
||||
p.Fightinfo.Mode = fightinfo.BattleMode.MULTI_MODE
|
||||
|
||||
_, err = fight.NewFight(p, ai, p.GetPetInfo(100), ai.GetPetInfo(0), func(foi model.FightOverInfo) {
|
||||
handleNpcFightRewards(p, foi, monster)
|
||||
})
|
||||
if err != 0 {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return nil, -1
|
||||
}
|
||||
|
||||
func loadMapBossConfigs(mapNode *configmodel.MapNode) ([]configmodel.BossConfig, errorcode.ErrorCode) {
|
||||
if mapNode == nil || len(mapNode.BossIds) == 0 {
|
||||
return nil, errorcode.ErrorCodes.ErrPokemonNotExists
|
||||
}
|
||||
|
||||
bossID := mapNode.BossIds[0]
|
||||
if len(mapNode.BossIds) > 1 {
|
||||
bossID = mapNode.BossIds[grand.Intn(len(mapNode.BossIds))]
|
||||
}
|
||||
|
||||
bossConfigs := service.NewBossService().Get(bossID)
|
||||
if len(bossConfigs) == 0 {
|
||||
return nil, errorcode.ErrorCodes.ErrPokemonNotExists
|
||||
}
|
||||
|
||||
return bossConfigs, 0
|
||||
}
|
||||
|
||||
func buildBossMonsterInfo(nodeName string, bossConfigs []configmodel.BossConfig) (*model.PlayerInfo, uint32, errorcode.ErrorCode) {
|
||||
monsterInfo := &model.PlayerInfo{Nick: nodeName}
|
||||
var leadMonsterID uint32
|
||||
|
||||
for i, bossConfig := range bossConfigs {
|
||||
dv, generation := bossFightPetArgs(bossConfig.IsCapture)
|
||||
monster := model.GenPetInfo(
|
||||
gconv.Int(bossConfig.MonID),
|
||||
dv,
|
||||
-1,
|
||||
0,
|
||||
int(bossConfig.Lv),
|
||||
nil,
|
||||
generation,
|
||||
)
|
||||
if monster == nil {
|
||||
return nil, 0, errorcode.ErrorCodes.ErrPokemonNotExists
|
||||
}
|
||||
monster.CatchTime = uint32(i)
|
||||
monster.ConfigBoss(bossConfig.PetBaseConfig)
|
||||
appendPetEffects(monster, bossConfig.Effect)
|
||||
|
||||
if i == 0 {
|
||||
leadMonsterID = monster.ID
|
||||
}
|
||||
monsterInfo.PetList = append(monsterInfo.PetList, *monster)
|
||||
}
|
||||
|
||||
if len(monsterInfo.PetList) == 0 {
|
||||
return nil, 0, errorcode.ErrorCodes.ErrPokemonNotExists
|
||||
}
|
||||
|
||||
if bossConfigs[0].IsCapture == 1 {
|
||||
monsterInfo.PetList[0].ShinyInfo = make([]data.GlowFilter, 0)
|
||||
if grand.Meet(1, 500) {
|
||||
monsterInfo.PetList[0].RandomByWeightShiny()
|
||||
}
|
||||
}
|
||||
|
||||
return monsterInfo, leadMonsterID, 0
|
||||
}
|
||||
|
||||
func bossFightPetArgs(canCapture int) (dv int, generation int) {
|
||||
if canCapture == 1 {
|
||||
return -1, -1
|
||||
}
|
||||
return 24, 0
|
||||
}
|
||||
|
||||
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),
|
||||
EID: gconv.Uint16(effect.Eid),
|
||||
Args: gconv.Ints(effect.Args),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func resolveBossCaptureRate(canCapture int, petID uint32) int {
|
||||
if canCapture == 0 {
|
||||
return 0
|
||||
}
|
||||
|
||||
petCfg, ok := xmlres.PetMAP[int(petID)]
|
||||
if !ok || petCfg.CatchRate == 0 {
|
||||
return 0
|
||||
}
|
||||
|
||||
return petCfg.CatchRate
|
||||
}
|
||||
|
||||
func shouldGrantBossWinBonus(fightC *fight.FightC, playerID uint32, bossConfig configmodel.BossConfig, foi model.FightOverInfo) bool {
|
||||
if len(bossConfig.Rule) == 0 {
|
||||
return foi.Reason == 0 && foi.WinnerId == playerID
|
||||
}
|
||||
|
||||
for _, ruleConfig := range service.NewFightRuleService().GetByRuleIdxs(bossConfig.Rule) {
|
||||
rule := input.GetRule(int64(ruleConfig.RuleIdx))
|
||||
if rule == nil {
|
||||
continue
|
||||
}
|
||||
rule.SetArgs(ruleConfig.Args...)
|
||||
if !rule.Exec(fightC, &foi) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
func buildNpcMonsterInfo(refPet player.OgrePetInfo, mapID uint32) (*model.PetInfo, *model.PlayerInfo, errorcode.ErrorCode) {
|
||||
if refPet.ID == 0 {
|
||||
return nil, nil, errorcode.ErrorCodes.ErrPokemonNotExists
|
||||
}
|
||||
|
||||
monster := model.GenPetInfo(
|
||||
refPet.GetID(),
|
||||
-1,
|
||||
-1,
|
||||
0,
|
||||
refPet.GetLevel(),
|
||||
refPet.ShinyInfo,
|
||||
-1,
|
||||
)
|
||||
if monster == nil {
|
||||
return nil, nil, errorcode.ErrorCodes.ErrPokemonNotExists
|
||||
}
|
||||
monster.CatchMap = mapID
|
||||
if refPet.Ext != 0 && grand.Meet(1, 500) {
|
||||
monster.RandomByWeightShiny()
|
||||
}
|
||||
|
||||
petCfg, ok := xmlres.PetMAP[int(monster.ID)]
|
||||
if !ok {
|
||||
return nil, nil, errorcode.ErrorCodes.ErrPokemonNotExists
|
||||
}
|
||||
|
||||
monsterInfo := &model.PlayerInfo{
|
||||
Nick: petCfg.DefName,
|
||||
PetList: []model.PetInfo{*monster},
|
||||
}
|
||||
return monster, monsterInfo, 0
|
||||
}
|
||||
|
||||
func handleNpcFightRewards(p *player.Player, foi model.FightOverInfo, monster *model.PetInfo) {
|
||||
if foi.Reason != 0 || foi.WinnerId != p.Info.UserID || !p.CanGet() {
|
||||
return
|
||||
}
|
||||
|
||||
petCfg, ok := xmlres.PetMAP[int(monster.ID)]
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
|
||||
exp := uint32(petCfg.YieldingExp) * monster.Level / 7
|
||||
addlevel, poolevel := p.CanGetExp()
|
||||
addexp := gconv.Float32(addlevel * gconv.Float32(exp))
|
||||
poolexp := gconv.Float32(poolevel) * gconv.Float32(exp)
|
||||
rewards := &fightinfo.S2C_GET_BOSS_MONSTER{}
|
||||
|
||||
p.ItemAdd(3, int64(poolexp+addexp))
|
||||
rewards.ADDitem(3, uint32(poolexp))
|
||||
p.AddPetExp(foi.Winpet, int64(addexp))
|
||||
|
||||
if p.CanGetItem() {
|
||||
itemID := p.GetSpace().GetDrop()
|
||||
if itemID != 0 {
|
||||
count := uint32(grand.N(1, 2))
|
||||
if p.ItemAdd(itemID, int64(count)) {
|
||||
rewards.ADDitem(uint32(itemID), count)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
petType := int64(petCfg.Type)
|
||||
if monster.IsShiny() && p.CanGetXUAN() && petType < 16 {
|
||||
xuanID := uint32(400686 + petType)
|
||||
count := uint32(grand.N(1, 2))
|
||||
if p.ItemAdd(int64(xuanID), int64(count)) {
|
||||
rewards.ADDitem(xuanID, count)
|
||||
}
|
||||
}
|
||||
|
||||
p.SendPackCmd(8004, rewards)
|
||||
foi.Winpet.AddEV(gconv.Int64s(strings.Fields(petCfg.YieldingEV)))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user