diff --git a/logic/controller/fight.go b/logic/controller/fight.go index c50aa6ce..2b9e5980 100644 --- a/logic/controller/fight.go +++ b/logic/controller/fight.go @@ -170,7 +170,7 @@ func (h Controller) Escape(data *fight.EscapeFightInboundInfo, c *player.Player) return nil, 0 } - c.FightC.Over(c, info.BattleOverReason.PlayerEscape) + c.FightC.Over(c, info.BattleOverReason.PlayerEscape, nil) return nil, 0 } diff --git a/logic/service/common/fight.go b/logic/service/common/fight.go index 2d9fdeda..a7f2d06c 100644 --- a/logic/service/common/fight.go +++ b/logic/service/common/fight.go @@ -6,9 +6,9 @@ import ( ) type FightI interface { - Over(c PlayerI, id info.EnumBattleOverReason) //逃跑 - UseSkill(c PlayerI, id int32) //使用技能 - GetCurrPET(c PlayerI) *info.BattlePetEntity //当前精灵 + Over(c PlayerI, id info.EnumBattleOverReason, fn func()) //逃跑 + UseSkill(c PlayerI, id int32) //使用技能 + GetCurrPET(c PlayerI) *info.BattlePetEntity //当前精灵 Ownerid() uint32 ReadyFight(c PlayerI) //是否准备战斗 @@ -19,5 +19,5 @@ type FightI interface { UseItem(c PlayerI, cacthid, itemid uint32) CanEscape() bool IsFirst(c PlayerI) bool - GetOverChan() chan struct{} + //GetOverChan() chan struct{} } diff --git a/logic/service/fight/action.go b/logic/service/fight/action.go index 3867afe4..5158fec0 100644 --- a/logic/service/fight/action.go +++ b/logic/service/fight/action.go @@ -29,17 +29,31 @@ func (f *FightC) Compare(a, b action.BattleActionI) (action.BattleActionI, actio } // 玩家逃跑/无响应/掉线 -func (f *FightC) Over(c common.PlayerI, res info.EnumBattleOverReason) { +func (f *FightC) Over(c common.PlayerI, res info.EnumBattleOverReason, fn func()) { if f.closefight { cool.Loger.Debug(context.Background(), " 战斗chan已关闭") return } - ret := &action.EscapeAction{ - BaseAction: action.NewBaseAction(c.GetInfo().UserID), - Reason: res, - } + // case *action.EscapeAction: + // f.FightOverInfo.WinnerId = b2.GetPlayerID() //对方胜利 + // f.FightOverInfo.Reason = a.Reason - f.actionChan <- ret + // f.closefight = true + // ret := &action.EscapeAction{ + // BaseAction: action.NewBaseAction(c.GetInfo().UserID), + // Reason: res, + // } + f.Reason = res + f.WinnerId = f.GetInputByPlayer(c, true).UserID + + select { + + case f.quit <- struct{}{}: + if fn != nil { + fn() + } + + } } // 切换精灵 主动和被驱逐 diff --git a/logic/service/fight/action/BattleAction.go b/logic/service/fight/action/BattleAction.go index af1c0dc6..ff2b1739 100644 --- a/logic/service/fight/action/BattleAction.go +++ b/logic/service/fight/action/BattleAction.go @@ -13,11 +13,11 @@ type EnumPlayerOperation int var PlayerOperations = enum.New[struct { //SystemGiveUp EnumPlayerOperation `enum:"-1"` // 系统选择放弃出手(比如没有PP) //系统放弃出手就是SKILL ID=0 - SelectSkill EnumPlayerOperation `enum:"0"` // 选择技能-6到6 - ActiveSwitch EnumPlayerOperation `enum:"2"` // 主动切换(中切) - UsePotion EnumPlayerOperation `enum:"3"` // 使用药剂(捕捉、逃跑等) - Escape EnumPlayerOperation `enum:"4"` // 逃跑(等级最高,以及掉线) - PlayerOffline EnumPlayerOperation `enum:"5"` // 玩家掉线 + SelectSkill EnumPlayerOperation `enum:"0"` // 选择技能-6到6 + ActiveSwitch EnumPlayerOperation `enum:"2"` // 主动切换(中切) + UsePotion EnumPlayerOperation `enum:"3"` // 使用药剂(捕捉、逃跑等) + //Escape EnumPlayerOperation `enum:"4"` // 逃跑(等级最高,以及掉线) + // PlayerOffline EnumPlayerOperation `enum:"5"` // 玩家掉线 // BeExpelledSwitch EnumPlayerOperation `enum:"6"` // 被驱逐切换 }]() @@ -85,29 +85,3 @@ type UseItemAction struct { func (u *UseItemAction) Priority() int { return int(PlayerOperations.UsePotion) } - -// EscapeAction 逃跑的战斗动作 -type EscapeAction struct { - BaseAction - Reason info.EnumBattleOverReason -} - -// Priority 返回动作优先级 -func (e *EscapeAction) Priority() int { - return int(PlayerOperations.Escape) -} - -// EscapeAction 逃跑的战斗动作 -type OverTimeAction struct { - BaseAction - Reason info.FightOverInfo -} - -func (e *OverTimeAction) GetInfo() info.FightOverInfo { - return e.Reason -} - -// Priority 返回动作优先级 -func (e *OverTimeAction) Priority() int { - return int(PlayerOperations.PlayerOffline) -} diff --git a/logic/service/fight/input.go b/logic/service/fight/input.go index 9c0eb937..bd54717e 100644 --- a/logic/service/fight/input.go +++ b/logic/service/fight/input.go @@ -26,7 +26,7 @@ type FightC struct { StartTime time.Time actionChan chan action.BattleActionI // 所有操作统一从这里进入 Round int //回合数 - overchan chan struct{} + quit chan struct{} First *input.Input Second *input.Input closefight bool @@ -173,7 +173,7 @@ func (f *FightC) initplayer(c common.PlayerI, opp bool) bool { func NewFight(mode, status info.EnumBattleMode, p1 common.PlayerI, p2 common.PlayerI) *FightC { f := &FightC{} f.ownerID = p1.GetInfo().UserID - f.overchan = make(chan struct{}) + f.quit = make(chan struct{}) f.StartTime = time.Now() seed := f.StartTime.UnixNano() ^ int64(p1.GetInfo().UserID) ^ int64(p2.GetInfo().UserID) // ^ int64(f.Round) // 用异或运算混合多维度信息 f.rand = rand.New(rand.NewSource(seed)) diff --git a/logic/service/fight/loop.go b/logic/service/fight/loop.go index 3aee46e7..aaac0ab8 100644 --- a/logic/service/fight/loop.go +++ b/logic/service/fight/loop.go @@ -29,12 +29,12 @@ func (f *FightC) battleLoop() { fmt.Printf("—— 第 %d 回合开始 ——\n", f.Round) actions := f.collectPlayerActions(ourID, oppID) - - f.resolveRound(actions[ourID], actions[oppID]) if f.closefight { break } + f.resolveRound(actions[ourID], actions[oppID]) + } fmt.Println("战斗循环结束") @@ -45,7 +45,7 @@ func (f *FightC) battleLoop() { }) close(f.actionChan) - close(f.overchan) + close(f.quit) } // 收集玩家动作(含超时判定) @@ -55,8 +55,13 @@ func (f *FightC) collectPlayerActions(ourID, oppID uint32) map[uint32]action.Bat for len(actions) < 2 { select { + case <-f.quit: + f.closefight = true + return actions + case paction, ok := <-f.actionChan: if !ok || f.closefight { + f.closefight = true return actions } if paction == nil { @@ -111,7 +116,12 @@ func (f *FightC) handleTimeout(ourID, oppID uint32, actions map[uint32]action.Ba fmt.Printf("玩家%d 加载超时\n", pid) f.UseSkill(player, 0) //卡加载,给对方也一个action } - f.Over(f.getPlayerByID(pid), info.BattleOverReason.PlayerOVerTime) + + f.Reason = info.BattleOverReason.PlayerOVerTime + f.closefight = true + //对方赢 + f.WinnerId = f.GetInputByPlayer(f.getPlayerByID(pid), true).Player.GetInfo().UserID + } } @@ -129,12 +139,6 @@ func (f *FightC) resolveRound(p1Action, p2Action action.BattleActionI) { switch a := b1.(type) { - case *action.EscapeAction: - f.FightOverInfo.WinnerId = b2.GetPlayerID() //对方胜利 - f.FightOverInfo.Reason = a.Reason - - f.closefight = true - case *action.ActiveSwitchAction: if _, ok := b2.(*action.SelectSkillAction); ok { f.enterturn(b2.(*action.SelectSkillAction), nil) @@ -252,5 +256,5 @@ func (f *FightC) getPlayerByID(id uint32) common.PlayerI { // 根据玩家ID返回对应对象 func (f *FightC) GetOverChan() chan struct{} { - return f.overchan + return f.quit } diff --git a/logic/service/player/player.go b/logic/service/player/player.go index 3637ef9d..a9ade2dd 100644 --- a/logic/service/player/player.go +++ b/logic/service/player/player.go @@ -319,6 +319,7 @@ func (p *Player) Save() { } if p.FightC != nil { + ov := make(chan struct{}) go func() { defer func() { @@ -329,10 +330,12 @@ func (p *Player) Save() { } }() - p.FightC.Over(p, info.BattleOverReason.PlayerOffline) //玩家逃跑,但是不能锁线程 + p.FightC.Over(p, info.BattleOverReason.PlayerOffline, func() { + <-ov + }) //玩家逃跑,但是不能锁线程 }() - <-p.FightC.GetOverChan() //等待结束 + //<-p.FightC.GetOverChan() //等待结束 } p.Info.TimeToday = p.Info.TimeToday + uint32(time.Now().Unix()) - uint32(p.Onlinetime) //保存电池时间 p.Onlinetime = uint32(time.Now().Unix())