From 6e01848b044d072ec11ab5551ee3235ae5bd5f11 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=98=94=E5=BF=B5?= <1@72wo.cn> Date: Thu, 13 Nov 2025 05:05:05 +0800 Subject: [PATCH] =?UTF-8?q?```=20refactor(socket):=20=E7=A7=BB=E9=99=A4?= =?UTF-8?q?=E6=9C=AA=E4=BD=BF=E7=94=A8=E7=9A=84=E7=BD=91=E7=BB=9C=E7=9B=B8?= =?UTF-8?q?=E5=85=B3=E5=AF=BC=E5=85=A5=E5=92=8C=E6=B3=A8=E9=87=8A=E6=8E=89?= =?UTF-8?q?=E7=9A=84=20RST=20=E6=94=BB=E5=87=BB=E6=A3=80=E6=B5=8B=E9=80=BB?= =?UTF-8?q?=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 移除了 `ServerEvent.go` 中未使用的 `net` 和 `strings` 包导入。同时, 将原有的 RST 攻击检测及防护逻辑代码注释掉,便于后续重新设计或彻底删除。 fix(logic): 调整 fight pool 初始化与释放策略 将 `fight/action.go` 中的 `Fightpool` 类型从 `*ants.MultiPool` 改为 `*ants.Pool`,并调整其初始化方式为 `NewPool(-1)` 以适应动态扩容。 此外,在 `main.go` 中将 `ReleaseTimeout` 参数由 100 调整为 0, 确保立即清理超时任务。 feat(fight): 优化战斗输入状态重置逻辑 在 `fight/action.go` 的 `ReadyFight` 方法中提前设置 `GetInputByPlayer(c, false).Finished = true`,避免重复赋值。 同时更新了状态睡眠效果的处理流程,并简化了输入模块的状态缓存机制, 移除了冗余的 `Initeffectcache` 函数调用及相关逻辑。 perf(fight): 动态计算玩家动作等待时间 在 `loop.go` 的 `collectPlayerActions` 方法中, 将固定超时时间替换为基于 `waittime` 的动态等待时间计算公式, 提高响应灵活性。同时修复通道关闭判断条件以增强稳定性。 refactor(fight): 更新技能效果解析和状态持续逻辑 修改 `input.go` 中 `GenSataus` 方法以正确初始化状态数组; 在 `Turn.go` 中重构 `Turn_End` 方法内的执行逻辑, 确保仅在我方后手时增加回合数,提升战斗流程准确性。 chore(service): 删除废弃文件 SocketHandler_Tomee.go 完全移除已弃用的 `SocketHandler_Tomee.go` 文件及其全部内容, 减少项目冗余代码。 ``` --- common/socket/ServerEvent.go | 14 +++++----- logic/main.go | 2 +- logic/service/fight/action.go | 17 ++++++------ logic/service/fight/effect/effect_status.go | 7 ++--- logic/service/fight/input/input.go | 19 ++----------- logic/service/fight/loop.go | 13 ++++++--- logic/service/fight/node/Turn.go | 12 ++++----- .../{SocketHandler_Tomee.go => pack.go} | 27 ------------------- 8 files changed, 34 insertions(+), 77 deletions(-) rename logic/service/player/{SocketHandler_Tomee.go => pack.go} (93%) diff --git a/common/socket/ServerEvent.go b/common/socket/ServerEvent.go index 29014a4d1..07dc76347 100644 --- a/common/socket/ServerEvent.go +++ b/common/socket/ServerEvent.go @@ -3,9 +3,7 @@ package socket import ( "context" "log" - "net" "os" - "strings" "sync/atomic" "time" @@ -49,14 +47,14 @@ func (s *Server) OnClose(c gnet.Conn, err error) (action gnet.Action) { } }() // 识别 RST 导致的连接中断(错误信息含 "connection reset") - if err != nil && (strings.Contains(err.Error(), "connection reset") || strings.Contains(err.Error(), "reset by peer")) { - remoteIP := c.RemoteAddr().(*net.TCPAddr).IP.String() + // if err != nil && (strings.Contains(err.Error(), "connection reset") || strings.Contains(err.Error(), "reset by peer")) { + // remoteIP := c.RemoteAddr().(*net.TCPAddr).IP.String() - log.Printf("RST 攻击检测: 来源 %s, 累计攻击次数 %d", remoteIP) + // log.Printf("RST 攻击检测: 来源 %s, 累计攻击次数 %d", remoteIP) - // 防护逻辑:临时封禁异常 IP(可扩展为 IP 黑名单) - // go s.tempBlockIP(remoteIP, 5*time.Minute) - } + // // 防护逻辑:临时封禁异常 IP(可扩展为 IP 黑名单) + // // go s.tempBlockIP(remoteIP, 5*time.Minute) + // } //fmt.Println(err, c.RemoteAddr().String(), "断开连接") atomic.AddInt64(&s.connected, -1) diff --git a/logic/main.go b/logic/main.go index 002cba8bb..ba5f1649e 100644 --- a/logic/main.go +++ b/logic/main.go @@ -33,7 +33,7 @@ func PprofWeb() { } } func signalHandlerForMain(sig os.Signal) { - fight.Fightpool.ReleaseTimeout(100) + fight.Fightpool.ReleaseTimeout(0) player.Mainplayer.Range(func(key uint32, value *player.Player) bool { value.Save() diff --git a/logic/service/fight/action.go b/logic/service/fight/action.go index 2df203963..079d0848d 100644 --- a/logic/service/fight/action.go +++ b/logic/service/fight/action.go @@ -146,13 +146,13 @@ func (f *FightC) ReadyFight(c common.PlayerI) { rett.Info2.UserID = f.Info.OpponentInfo.UserID rrsult := func() { //传回函数 - i := Fightpool.Free() - if i <= 0 { - Fightpool.Tune(Fightpool.Cap() + 1) + // i := Fightpool.Free() + // if i <= 0 { + // Fightpool.Tune(Fightpool.Cap() + 1) - cool.Loger.Error(context.Background(), "Fightpool is full") + // cool.Loger.Error(context.Background(), "Fightpool is full") - } + // } rr := Fightpool.Submit(f.battleLoop) if rr != nil { panic(rr) @@ -164,11 +164,10 @@ func (f *FightC) ReadyFight(c common.PlayerI) { //然后开始战斗循环 } - + f.GetInputByPlayer(c, false).Finished = true switch f.Info.Status { case info.BattleStatus.FIGHT_WITH_PLAYER: //pvp - f.GetInputByPlayer(c, false).Finished = true if f.GetInputByPlayer(c, true).Finished { rrsult() } @@ -187,9 +186,9 @@ func (f *FightC) ReadyFight(c common.PlayerI) { } } -var Fightpool *ants.MultiPool +var Fightpool *ants.Pool func init() { - Fightpool, _ = ants.NewMultiPool(10, 100, ants.LeastTasks) + Fightpool, _ = ants.NewPool(-1) //defer p.Release() } diff --git a/logic/service/fight/effect/effect_status.go b/logic/service/fight/effect/effect_status.go index a3a263bcf..dba92b498 100644 --- a/logic/service/fight/effect/effect_status.go +++ b/logic/service/fight/effect/effect_status.go @@ -63,11 +63,8 @@ func (e *StatusSleep) Skill_Use_ex() bool { if e.Ctx().SkillEntity == nil { return true } - if e.Ctx().SkillEntity.Category() != info.Category.STATUS { - t := e.Input.GetEffect(input.EffectType.Status, int(info.PetStatus.Sleep)) - if t != nil { - t.Alive(false) - } + if e.Ctx().Category() != info.Category.STATUS { + e.Alive(false) } return true } diff --git a/logic/service/fight/input/input.go b/logic/service/fight/input/input.go index 210c65abd..d753e3c68 100644 --- a/logic/service/fight/input/input.go +++ b/logic/service/fight/input/input.go @@ -65,7 +65,7 @@ func (our *Input) SetOPP(t *Input) { } func (our *Input) GenSataus() { - + our.Status = [20]int8{} for i := 0; i < 20; i++ { //堆叠状态剩余回合 t := our.GetEffect(EffectType.Status, i) @@ -143,26 +143,11 @@ func (our *Input) GetStatusBonus() float64 { return maxBonus } -func (our *Input) Initeffectcache() { - - for _, v := range our.Effects { - - if v.Alive() { //说明存活效果而且是延续类效果,将之添加到初始化列表中 - - //这里添加的效果是已经生效的效果对effect的复制,相当于技能施的效果的前置比如改命中的效果等 - our.EffectCache = append(our.EffectCache, v) - - } - - } - -} - // 解析并 施加effect func (our *Input) Parseskill(defender *Input, skill *action.SelectSkillAction) { our.EffectCache = make([]Effect, 0) //先把上一回合数据清空,但是应该把本身延续类效果集成过来 our.Effect_Lost = make([]Effect, 0) - our.Initeffectcache() //这里说明是延续的效果,每次复制出来一个新的就好了 + // our.Initeffectcache() //这里说明是延续的效果,每次复制出来一个新的就好了 //i.NewEffects = make([]Effect, 0) //这里说明是新增的效果 temparg := skill.SideEffectArgS diff --git a/logic/service/fight/loop.go b/logic/service/fight/loop.go index 08248b8a4..f2b2ea762 100644 --- a/logic/service/fight/loop.go +++ b/logic/service/fight/loop.go @@ -52,16 +52,19 @@ func (f *FightC) battleLoop() { // 收集玩家动作(含超时判定) func (f *FightC) collectPlayerActions(ourID, oppID uint32) map[uint32]action.BattleActionI { actions := make(map[uint32]action.BattleActionI) - timeout := time.After(60*time.Second + time.Duration(f.waittime)) + waitr := time.Duration(f.waittime)*time.Millisecond*10 + 30*time.Second + fmt.Println("开始收集玩家动作", waitr) + timeout := time.After(waitr) for len(actions) < 2 { + select { case <-f.quit: f.closefight = true return actions case paction, ok := <-f.actionChan: - if !ok || f.closefight { + if !ok { f.closefight = true return actions } @@ -142,7 +145,8 @@ func (f *FightC) resolveRound(p1Action, p2Action action.BattleActionI) { switch a := b1.(type) { case *action.ActiveSwitchAction: - if _, ok := b2.(*action.SelectSkillAction); ok { + if b2k, ok := b2.(*action.SelectSkillAction); ok { + f.waittime = b2k.CD f.enterturn(b2.(*action.SelectSkillAction), nil) } else { @@ -151,7 +155,8 @@ func (f *FightC) resolveRound(p1Action, p2Action action.BattleActionI) { case *action.UseItemAction: f.handleItemAction(a) - if _, ok := b2.(*action.SelectSkillAction); ok { + if b2k, ok := b2.(*action.SelectSkillAction); ok { + f.waittime = b2k.CD f.enterturn(b2.(*action.SelectSkillAction), nil) } else { if a1, ok := b2.(*action.UseItemAction); ok { diff --git a/logic/service/fight/node/Turn.go b/logic/service/fight/node/Turn.go index 645ac8203..2b3963b2b 100644 --- a/logic/service/fight/node/Turn.go +++ b/logic/service/fight/node/Turn.go @@ -19,14 +19,14 @@ func (e *EffectNode) Turn_End() { e.Alive(false) } else { - // e.trunl.Do(func() { + e.trunl.Do(func() { - // if e.Ctx().Opp.FightC.IsFirst(e.Ctx().Opp.Player) { //如果对方先手 - // e.duration++ - // // e.Alive(true) - // } + if !e.Ctx().Our.FightC.IsFirst(e.Ctx().Our.Player) { //如果我方后手,那就给回合+1 + e.duration++ + // e.Alive(true) + } - // }) + }) e.duration-- } diff --git a/logic/service/player/SocketHandler_Tomee.go b/logic/service/player/pack.go similarity index 93% rename from logic/service/player/SocketHandler_Tomee.go rename to logic/service/player/pack.go index a41babde4..9144f5179 100644 --- a/logic/service/player/SocketHandler_Tomee.go +++ b/logic/service/player/pack.go @@ -100,33 +100,6 @@ func (h *TomeeHeader) Pack(data any) []byte { //组包 } -// var _ Blazingservice = (*TomeeHeader)(nil) - -// type Blazingservice interface { -// Ret() []byte -// } - -// MergeBytes 将多个字节数组合并为一个 -func MergeBytes(arrays ...[]byte) []byte { - // 计算所有数组的总长度 - totalLen := 0 - for _, arr := range arrays { - totalLen += len(arr) - } - - // 创建结果切片 - result := make([]byte, totalLen) - - // 逐个复制数组内容 - currentIndex := 0 - for _, arr := range arrays { - copy(result[currentIndex:], arr) - currentIndex += len(arr) - } - - return result -} - // 遍历结构体方法并执行RECV_cmd func (h *ClientData) Recv(data TomeeHeader) {