2026-01-29 13:59:48 +08:00
|
|
|
package fight
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"blazing/common/socket/errorcode"
|
|
|
|
|
"blazing/cool"
|
|
|
|
|
"blazing/logic/service/common"
|
|
|
|
|
"blazing/logic/service/fight/action"
|
|
|
|
|
"blazing/logic/service/fight/info"
|
|
|
|
|
"blazing/logic/service/fight/input"
|
|
|
|
|
"blazing/logic/service/player"
|
2026-03-04 22:47:21 +08:00
|
|
|
"blazing/modules/player/model"
|
2026-01-29 13:59:48 +08:00
|
|
|
"time"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// 创建新战斗,邀请方和被邀请方,或者玩家和野怪方
|
2026-03-22 23:41:51 +08:00
|
|
|
func NewFight(p1, p2 common.PlayerI, b1, b2 []model.PetInfo, fn func(model.FightOverInfo)) (*FightC, errorcode.ErrorCode) {
|
2026-04-04 06:11:01 +08:00
|
|
|
return NewFightWithOptions(
|
|
|
|
|
WithFightPlayers(p1, p2),
|
|
|
|
|
WithFightPets(b1, b2),
|
|
|
|
|
WithFightCallback(fn),
|
|
|
|
|
)
|
|
|
|
|
}
|
2026-01-29 13:59:48 +08:00
|
|
|
|
2026-04-04 06:11:01 +08:00
|
|
|
func buildFight(opts *fightBuildOptions) (*FightC, errorcode.ErrorCode) {
|
|
|
|
|
if opts == nil || opts.owner == nil || opts.opponent == nil {
|
|
|
|
|
return nil, errorcode.ErrorCodes.ErrSystemBusyTryLater
|
|
|
|
|
}
|
|
|
|
|
opts.normalizePlayers()
|
2026-01-29 13:59:48 +08:00
|
|
|
|
2026-04-04 06:11:01 +08:00
|
|
|
f := &FightC{}
|
|
|
|
|
f.ownerID = opts.owner.GetInfo().UserID
|
|
|
|
|
f.OurPlayers = opts.ourPlayers
|
|
|
|
|
f.OppPlayers = opts.oppPlayers
|
2026-04-04 05:44:02 +08:00
|
|
|
f.Switch = make(map[actionSlotKey]*action.ActiveSwitchAction)
|
2026-04-04 06:11:01 +08:00
|
|
|
f.callback = opts.callback
|
2026-01-29 13:59:48 +08:00
|
|
|
f.quit = make(chan struct{})
|
|
|
|
|
f.over = make(chan struct{})
|
2026-04-02 23:05:18 +08:00
|
|
|
f.actionNotify = make(chan struct{}, 1)
|
|
|
|
|
f.pendingActions = make([]action.BattleActionI, 0, 4)
|
2026-04-04 06:11:01 +08:00
|
|
|
f.StartTime = opts.startTime
|
|
|
|
|
if opts.fightInfo != nil {
|
|
|
|
|
f.Info = *opts.fightInfo
|
|
|
|
|
} else {
|
|
|
|
|
f.Info = opts.owner.Getfightinfo()
|
|
|
|
|
}
|
2026-01-29 13:59:48 +08:00
|
|
|
f.ReadyInfo.Status = f.Info.Status
|
2026-03-22 23:41:51 +08:00
|
|
|
|
2026-01-29 13:59:48 +08:00
|
|
|
var err errorcode.ErrorCode
|
2026-04-04 06:11:01 +08:00
|
|
|
f.Our, err = f.buildFightInputs(opts.owner, opts.ourPets, opts.ourInputs)
|
2026-01-29 13:59:48 +08:00
|
|
|
if err > 0 {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2026-04-04 06:11:01 +08:00
|
|
|
f.Opp, err = f.buildFightInputs(opts.opponent, opts.oppPets, opts.oppInputs)
|
2026-01-29 13:59:48 +08:00
|
|
|
if err > 0 {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2026-04-04 06:11:01 +08:00
|
|
|
f.bindInputFightContext(f.Our)
|
|
|
|
|
f.bindInputFightContext(f.Opp)
|
|
|
|
|
f.linkOppInputs()
|
2026-01-29 13:59:48 +08:00
|
|
|
|
2026-04-04 06:11:01 +08:00
|
|
|
f.ReadyInfo.OurInfo, f.ReadyInfo.OurPetList = initfightready(f.primaryOur())
|
|
|
|
|
f.ReadyInfo.OpponentInfo, f.ReadyInfo.OpponentPetList = initfightready(f.primaryOpp())
|
2026-01-29 13:59:48 +08:00
|
|
|
|
2026-04-04 06:11:01 +08:00
|
|
|
loadtime := 120 * time.Second
|
2026-01-29 13:59:48 +08:00
|
|
|
if f.Info.Status == info.BattleMode.FIGHT_WITH_NPC {
|
2026-04-04 06:11:01 +08:00
|
|
|
if opp := f.primaryOpp(); opp != nil {
|
|
|
|
|
opp.Finished = true
|
|
|
|
|
loadtime = 60 * time.Second
|
|
|
|
|
if ai, ok := opp.Player.(*player.AI_player); ok {
|
|
|
|
|
if ai.CanCapture > 0 {
|
|
|
|
|
opp.CanCapture = ai.CanCapture
|
|
|
|
|
}
|
|
|
|
|
opp.AttackValue.Prop = ai.Prop
|
|
|
|
|
}
|
2026-01-29 13:59:48 +08:00
|
|
|
}
|
|
|
|
|
}
|
2026-04-04 05:44:02 +08:00
|
|
|
f.FightStartOutboundInfo = f.buildFightStartInfo()
|
2026-01-29 13:59:48 +08:00
|
|
|
|
|
|
|
|
f.Broadcast(func(ff *input.Input) {
|
|
|
|
|
ff.Player.SendPackCmd(2503, &f.ReadyInfo)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
cool.Cron.AfterFunc(loadtime, func() {
|
2026-04-04 06:11:01 +08:00
|
|
|
our := f.primaryOur()
|
|
|
|
|
opp := f.primaryOpp()
|
|
|
|
|
if our == nil || opp == nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if !our.Finished || !opp.Finished {
|
|
|
|
|
f.closefight = true
|
2026-03-04 22:47:21 +08:00
|
|
|
f.Reason = model.BattleOverReason.PlayerOffline
|
2026-01-29 13:59:48 +08:00
|
|
|
switch {
|
2026-04-04 06:11:01 +08:00
|
|
|
case !opp.Finished:
|
|
|
|
|
f.WinnerId = our.Player.GetInfo().UserID
|
|
|
|
|
case !our.Finished:
|
|
|
|
|
f.WinnerId = opp.Player.GetInfo().UserID
|
2026-01-29 13:59:48 +08:00
|
|
|
}
|
|
|
|
|
f.Broadcast(func(ff *input.Input) {
|
|
|
|
|
ff.Player.SendPackCmd(2506, &f.FightOverInfo)
|
|
|
|
|
ff.Player.QuitFight()
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
return f, 0
|
|
|
|
|
}
|
2026-04-04 06:11:01 +08:00
|
|
|
|
|
|
|
|
func (f *FightC) buildFightInputs(defaultPlayer common.PlayerI, pets []model.PetInfo, existing []*input.Input) ([]*input.Input, errorcode.ErrorCode) {
|
|
|
|
|
if len(existing) > 0 {
|
|
|
|
|
return existing, 0
|
|
|
|
|
}
|
|
|
|
|
in, err := f.initplayer(defaultPlayer, pets)
|
|
|
|
|
if err > 0 {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
return []*input.Input{in}, 0
|
|
|
|
|
}
|