diff --git a/logic/service/common/fight.go b/logic/service/common/fight.go index 6c0be88a7..803da7941 100644 --- a/logic/service/common/fight.go +++ b/logic/service/common/fight.go @@ -1,6 +1,7 @@ package common import ( + "blazing/logic/service/fight/action" "blazing/logic/service/fight/info" "math/rand" ) @@ -18,4 +19,5 @@ type FightI interface { UseItem(c PlayerI, cacthid, itemid uint32) CanEscape() bool IsFirst(c PlayerI) bool + GetActionChan() chan action.BattleActionI } diff --git a/logic/service/fight/fightc.go b/logic/service/fight/fightc.go index dd5879c04..51cfc7063 100644 --- a/logic/service/fight/fightc.go +++ b/logic/service/fight/fightc.go @@ -28,7 +28,7 @@ type FightC struct { rand *rand.Rand StartTime time.Time - actionChan chan action.BattleActionI // 所有操作统一从这里进入 + ActionChan chan action.BattleActionI // 所有操作统一从这里进入 Round int //回合数 First *input.Input diff --git a/logic/service/fight/loop.go b/logic/service/fight/loop.go index b76552bcb..59bcf2265 100644 --- a/logic/service/fight/loop.go +++ b/logic/service/fight/loop.go @@ -15,7 +15,7 @@ import ( ) func (f *FightC) battleLoop() { - f.actionChan = make(chan action.BattleActionI, 2) + f.ActionChan = make(chan action.BattleActionI, 2) fmt.Println("战斗开始精灵", f.Our.Player.GetInfo().PetList[0].CatchTime) ourID := f.Our.Player.GetInfo().UserID @@ -30,7 +30,7 @@ func (f *FightC) battleLoop() { ff.Player.SendFightEndInfo(f.FightOverInfo) }) - close(f.actionChan) + close(f.ActionChan) break } @@ -52,7 +52,7 @@ func (f *FightC) collectPlayerActions(ourID, oppID uint32) map[uint32]action.Bat for len(actions) < 2 { select { - case paction, ok := <-f.actionChan: + case paction, ok := <-f.ActionChan: if !ok || f.closefight { return actions } @@ -245,3 +245,10 @@ func (f *FightC) getPlayerByID(id uint32) common.PlayerI { } return f.Opp.Player } + +// 根据玩家ID返回对应对象 +func (f *FightC) GetActionChan( ) chan action.BattleActionI { + + return f.ActionChan +} + \ No newline at end of file diff --git a/logic/service/fight/playeraction.go b/logic/service/fight/playeraction.go index f75d4ad1c..e8a5e3b4b 100644 --- a/logic/service/fight/playeraction.go +++ b/logic/service/fight/playeraction.go @@ -2,11 +2,13 @@ package fight import ( "blazing/common/data/xmlres" + "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" + "context" "math" "github.com/gogf/gf/v2/util/gconv" @@ -29,16 +31,24 @@ func (f *FightC) Compare(a, b action.BattleActionI) (action.BattleActionI, actio // 玩家逃跑/无响应/掉线 func (f *FightC) Over(c common.PlayerI, res info.EnumBattleOverReason) { + if f.closefight { + cool.Loger.Debug(context.Background(), " 战斗chan已关闭") + return + } ret := &action.EscapeAction{ BaseAction: action.NewBaseAction(c.GetInfo().UserID), Reason: res, } - f.actionChan <- ret + f.ActionChan <- ret } // 切换精灵 主动和被驱逐 func (f *FightC) ChangePet(c common.PlayerI, id uint32) { + if f.closefight { + cool.Loger.Debug(context.Background(), " 战斗chan已关闭") + return + } ret := &action.ActiveSwitchAction{ BaseAction: action.NewBaseAction(c.GetInfo().UserID), } @@ -67,12 +77,15 @@ func (f *FightC) ChangePet(c common.PlayerI, id uint32) { return true }) - f.actionChan <- ret + f.ActionChan <- ret } // 玩家使用技能 func (f *FightC) UseSkill(c common.PlayerI, id int32) { - + if f.closefight { + cool.Loger.Debug(context.Background(), " 战斗chan已关闭") + return + } ret := &action.SelectSkillAction{ BaseAction: action.NewBaseAction(c.GetInfo().UserID), } @@ -85,18 +98,24 @@ func (f *FightC) UseSkill(c common.PlayerI, id int32) { } } - f.actionChan <- ret + f.ActionChan <- ret } // 玩家使用技能 func (f *FightC) Capture(c common.PlayerI, id uint32) { - - f.actionChan <- &action.UseItemAction{BaseAction: action.NewBaseAction(c.GetInfo().UserID), ItemID: id} + if f.closefight { + cool.Loger.Debug(context.Background(), " 战斗chan已关闭") + return + } + f.ActionChan <- &action.UseItemAction{BaseAction: action.NewBaseAction(c.GetInfo().UserID), ItemID: id} } func (f *FightC) UseItem(c common.PlayerI, cacthid, itemid uint32) { - - f.actionChan <- &action.UseItemAction{BaseAction: action.NewBaseAction(c.GetInfo().UserID), ItemID: itemid, CacthTime: cacthid} + if f.closefight { + cool.Loger.Debug(context.Background(), " 战斗chan已关闭") + return + } + f.ActionChan <- &action.UseItemAction{BaseAction: action.NewBaseAction(c.GetInfo().UserID), ItemID: itemid, CacthTime: cacthid} } // 战斗准备 diff --git a/logic/service/player/player.go b/logic/service/player/player.go index f8501d334..dc0ffa52e 100644 --- a/logic/service/player/player.go +++ b/logic/service/player/player.go @@ -331,7 +331,7 @@ func (p *Player) Save() { }() p.FightC.Over(p, info.BattleOverReason.PlayerOffline) //玩家逃跑,但是不能锁线程 }() - + <-p.FightC.GetActionChan() //等待结束 } p.Info.TimeToday = p.Info.TimeToday + uint32(time.Now().Unix()) - uint32(p.Onlinetime) //保存电池时间 p.Onlinetime = uint32(time.Now().Unix())