refactor(fight): 重构战斗结束逻辑,统一使用Over方法处理逃跑、掉线和超时情况
This commit is contained in:
@@ -106,7 +106,7 @@ func (h Controller) UseSkill(data *fight.UseSkillInboundInfo, c *player.Player)
|
||||
// 战斗逃跑
|
||||
func (h Controller) Escape(data *fight.EscapeFightInboundInfo, c *player.Player) (result *fight.NullOutboundInfo, err errorcode.ErrorCode) {
|
||||
|
||||
c.FightC.Escape(c)
|
||||
c.FightC.Over(c,info.BattleOverReason.PlayerEscape)
|
||||
return nil, 0
|
||||
}
|
||||
|
||||
|
||||
@@ -6,9 +6,9 @@ import (
|
||||
)
|
||||
|
||||
type FightI interface {
|
||||
Escape(c PlayerI) //逃跑
|
||||
UseSkill(c PlayerI, id int32) //使用技能
|
||||
GetCurrPET(c PlayerI) *info.BattlePetEntity //当前精灵
|
||||
Over(c PlayerI, id info.EnumBattleOverReason) //逃跑
|
||||
UseSkill(c PlayerI, id int32) //使用技能
|
||||
GetCurrPET(c PlayerI) *info.BattlePetEntity //当前精灵
|
||||
Ownerid() uint32
|
||||
ReadyFight(c PlayerI) //是否准备战斗
|
||||
ChangePet(c PlayerI, id uint32)
|
||||
|
||||
@@ -146,6 +146,23 @@ func (e *EscapeAction) Priority() int {
|
||||
return int(PlayerOperations.Escape)
|
||||
}
|
||||
|
||||
// EscapeAction 逃跑的战斗动作
|
||||
type OverTimeAction struct {
|
||||
BaseAction
|
||||
Reason info.FightOverInfo
|
||||
Our common.PlayerI
|
||||
Opp common.PlayerI
|
||||
}
|
||||
|
||||
func (e *OverTimeAction) GetInfo() info.FightOverInfo {
|
||||
return e.Reason
|
||||
}
|
||||
|
||||
// Priority 返回动作优先级
|
||||
func (e *OverTimeAction) Priority() int {
|
||||
return int(PlayerOperations.PlayerOffline)
|
||||
}
|
||||
|
||||
// SystemGiveUpAction 系统强制放弃出手的动作
|
||||
type SystemGiveUpAction struct {
|
||||
BaseAction
|
||||
|
||||
@@ -277,12 +277,11 @@ func (f *FightC) battleLoop() {
|
||||
case <-timeout:
|
||||
|
||||
if _, exists := actions[f.Our.Player.GetInfo().UserID]; !exists {
|
||||
actions[f.Our.Player.GetInfo().UserID] = &SystemGiveUpAction{
|
||||
BaseAction: NewBaseAction(f.Our.Player.GetInfo().UserID)} //系统选择出手
|
||||
f.Over(f.Opp.Player, info.BattleOverReason.PlayerOVerTime)
|
||||
}
|
||||
if _, exists := actions[f.Opp.Player.GetInfo().UserID]; !exists {
|
||||
|
||||
actions[f.Opp.Player.GetInfo().UserID] = &SystemGiveUpAction{BaseAction: NewBaseAction(f.Opp.Player.GetInfo().UserID)} //系统选择出手
|
||||
f.Over(f.Opp.Player, info.BattleOverReason.PlayerOVerTime)
|
||||
}
|
||||
|
||||
}
|
||||
@@ -298,14 +297,13 @@ func (f *FightC) battleLoop() {
|
||||
BattleActionI[0], BattleActionI[1] = f.Compare(p1Action, p2Action)
|
||||
|
||||
switch faction := BattleActionI[0].(type) {
|
||||
case *EscapeAction: //优先逃跑
|
||||
case *PlayerOfflineAction: //单方掉线
|
||||
|
||||
f.Broadcast(func(ff *input.Input) {
|
||||
ff.Player.SendFightEndInfo(faction.Reason) //广播逃跑原因
|
||||
})
|
||||
f.closefight = true
|
||||
|
||||
case *PlayerOfflineAction: //单方掉线
|
||||
case *EscapeAction: //优先逃跑
|
||||
|
||||
f.Broadcast(func(ff *input.Input) {
|
||||
ff.Player.SendFightEndInfo(faction.Reason) //广播逃跑原因
|
||||
|
||||
@@ -62,9 +62,10 @@ type EnumBattleOverReason int
|
||||
|
||||
var BattleOverReason = enum.New[struct {
|
||||
PlayerOffline EnumBattleOverReason `enum:"1"` //掉线
|
||||
PlayerEscape EnumBattleOverReason `enum:"2"` //逃跑
|
||||
PlayerOVerTime EnumBattleOverReason `enum:"2"` //超时
|
||||
PlayerCaptureSuccess EnumBattleOverReason `enum:"3"` //捕捉成功
|
||||
DefaultEnd EnumBattleOverReason `enum:"4"` //默认结束
|
||||
PlayerEscape EnumBattleOverReason `enum:"5"` //逃跑
|
||||
}]()
|
||||
|
||||
// 战斗模式
|
||||
|
||||
@@ -12,8 +12,8 @@ import (
|
||||
"github.com/panjf2000/ants/v2"
|
||||
)
|
||||
|
||||
// 玩家逃跑
|
||||
func (f *FightC) Escape(c common.PlayerI) {
|
||||
// 玩家逃跑/无响应/掉线
|
||||
func (f *FightC) Over(c common.PlayerI, res info.EnumBattleOverReason) {
|
||||
ret := &EscapeAction{
|
||||
BaseAction: NewBaseAction(c.GetInfo().UserID),
|
||||
Reason: info.FightOverInfo{
|
||||
@@ -21,20 +21,12 @@ func (f *FightC) Escape(c common.PlayerI) {
|
||||
Reason: uint32(info.BattleOverReason.PlayerEscape),
|
||||
},
|
||||
}
|
||||
if c.GetInfo().UserID == f.ownerID {
|
||||
ret.Reason.WinnerId = f.Opp.Player.GetInfo().UserID
|
||||
|
||||
f.actionChan <- ret
|
||||
}
|
||||
|
||||
// 玩家掉线
|
||||
func (f *FightC) Offline(c common.PlayerI) {
|
||||
ret := &PlayerOfflineAction{
|
||||
BaseAction: NewBaseAction(c.GetInfo().UserID),
|
||||
Reason: info.FightOverInfo{
|
||||
|
||||
Reason: uint32(info.BattleOverReason.PlayerOffline),
|
||||
},
|
||||
} else {
|
||||
ret.Reason.WinnerId = f.Our.Player.GetInfo().UserID
|
||||
}
|
||||
|
||||
f.actionChan <- ret
|
||||
}
|
||||
|
||||
|
||||
@@ -321,7 +321,7 @@ func (p *Player) Save() {
|
||||
Mainplayer.Delete(p.Info.UserID)
|
||||
share.ShareManager.DeleteUserOnline(p.Info.UserID) //设置用户登录服务器
|
||||
if p.FightC != nil {
|
||||
p.FightC.Escape(p) //玩家逃跑
|
||||
p.FightC.Over(p, info.BattleOverReason.PlayerOffline) //玩家逃跑
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user