From 742bcd8a3fba9024888782b49f3f3420e863e416 Mon Sep 17 00:00:00 2001 From: 1 <1@72wo.cn> Date: Sun, 9 Nov 2025 06:50:12 +0000 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=9C=B0=E5=9B=BE=E5=B9=BF?= =?UTF-8?q?=E6=92=AD=E9=99=90=E5=88=B6=E9=80=9F=E7=8E=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- common/cool/global.go | 2 ++ logic/controller/map.go | 6 +++--- logic/controller/walk.go | 10 ++++++---- logic/service/maps/walk.go | 8 ++++++++ login/internal/cmd/cmd.go | 6 ++---- modules/blazing/model/player.go | 18 ++++++++++++++++++ 6 files changed, 39 insertions(+), 11 deletions(-) diff --git a/common/cool/global.go b/common/cool/global.go index e3100a54b..c16b1292b 100644 --- a/common/cool/global.go +++ b/common/cool/global.go @@ -8,9 +8,11 @@ import ( "github.com/antlabs/cronex" "github.com/gogf/gf/v2/os/glog" + "github.com/xiaoqidun/limit" sensitive "github.com/zmexing/go-sensitive-word" ) +var Limiter = limit.New() var ctx = context.TODO() var ( diff --git a/logic/controller/map.go b/logic/controller/map.go index 1d792e798..5ae3afa21 100644 --- a/logic/controller/map.go +++ b/logic/controller/map.go @@ -53,7 +53,7 @@ func (h *Controller) MapList(data *maps.ListMapPlayerInboundInfo, c *player.Play result1 := maps.NewOutInfo() copier.CopyWithOption(result1, player.GetInfo(), copier.Option{DeepCopy: true}) result.Player = append(result.Player, *result1) - result.Player=LastFourElements(result.Player) + result.Player = LastFourElements(result.Player) }) c.Canmon = true //可以刷怪 @@ -66,5 +66,5 @@ func LastFourElements[T any](s []T) []T { return s } // 切片长度大于4时,返回最后4个元素(从n-4索引到末尾) - return s[n-4:] -} \ No newline at end of file + return s[n-30:] +} diff --git a/logic/controller/walk.go b/logic/controller/walk.go index fc7018fb2..f2eff74b5 100644 --- a/logic/controller/walk.go +++ b/logic/controller/walk.go @@ -8,15 +8,17 @@ import ( func (h Controller) Walk(data *maps.WalkInInfo, c *player.Player) (result *maps.WalkOutInfo, err errorcode.ErrorCode) { result = &maps.WalkOutInfo{ - Flag: data.Flag, - Point: data.Point, - Path: data.Path, + Flag: data.Flag, + Point: data.Point, + Path: data.Path, } result.UserID = data.Head.UserID c.Info.Pos = data.Point //glog.Debug(context.Background(), err1) - data.Broadcast(c.Info.MapID, *result) //走路的广播 + if !c.Info.Pos.BothLessThan50(data.Point) { //距离超过50才广播 + data.Broadcast(c.Info.MapID, *result) //走路的广播 + } return nil, -1 } diff --git a/logic/service/maps/walk.go b/logic/service/maps/walk.go index 01a44a508..f443eb86b 100644 --- a/logic/service/maps/walk.go +++ b/logic/service/maps/walk.go @@ -1,11 +1,15 @@ package maps import ( + "blazing/cool" "blazing/logic/service/common" "blazing/logic/service/player" "blazing/logic/service/space" "blazing/modules/blazing/model" + + "github.com/gogf/gf/v2/util/gconv" + "golang.org/x/time/rate" ) type WalkInInfo struct { @@ -22,6 +26,10 @@ type WalkInInfo struct { func (t *WalkInInfo) Broadcast(mapid uint32, o WalkOutInfo) { + r := cool.Limiter.Get("Broadcast"+gconv.String(mapid), rate.Limit(10), 5) + if !r.Allow() { + return + } space.GetSpace(mapid).User.IterCb(func(playerID uint32, player common.PlayerI) { t.Head.Result = 0 tt := t.Head.Pack(&o) diff --git a/login/internal/cmd/cmd.go b/login/internal/cmd/cmd.go index efb193867..07c4b89ff 100644 --- a/login/internal/cmd/cmd.go +++ b/login/internal/cmd/cmd.go @@ -12,13 +12,11 @@ import ( "github.com/gogf/gf/v2/net/ghttp" "github.com/gogf/gf/v2/os/gcmd" "github.com/gogf/gf/v2/os/gfile" - "github.com/xiaoqidun/limit" "golang.org/x/time/rate" ) var ( - limiter = limit.New() - Main = gcmd.Command{ + Main = gcmd.Command{ Name: "main", Usage: "main", Brief: "start http server", @@ -60,7 +58,7 @@ func Limiter(r *ghttp.Request) { // 3. 为任意键 "some-key" 获取一个速率限制器 // - rate.Limit(2): 表示速率为 "每秒2个请求" // - 2: 表示桶的容量 (Burst),允许瞬时处理2个请求 - rateLimiter := limiter.Get(r.GetClientIp(), rate.Limit(10), 5) + rateLimiter := cool.Limiter.Get(r.GetClientIp(), rate.Limit(10), 5) if !rateLimiter.Allow() { r.Response.WriteStatusExit(429) // Return 429 Too Many Requests diff --git a/modules/blazing/model/player.go b/modules/blazing/model/player.go index aea7ae032..e7689fa3b 100644 --- a/modules/blazing/model/player.go +++ b/modules/blazing/model/player.go @@ -2,6 +2,7 @@ package model import ( "blazing/cool" + "math" "time" "github.com/creasty/defaults" @@ -23,6 +24,23 @@ type Pos struct { Y uint32 `struc:"uint32" default:"0"` } +// 计算两个uint32的差值绝对值(转为int64避免溢出) +func absDiff(a, b uint32) int64 { + return int64(math.Abs(float64(int64(a) - int64(b)))) +} + +// 判断两个Pos的X和Y差值的绝对值是否均小于50 +func bothDiffsLessThan50(pos1, pos2 Pos) bool { + xDiff := absDiff(pos1.X, pos2.X) + yDiff := absDiff(pos1.Y, pos2.Y) + return xDiff < 50 && yDiff < 50 +} + +func (p Pos) BothLessThan50(t Pos) bool { + + return bothDiffsLessThan50(p, t) +} + // PeopleItemInfo 穿戴装备信息结构(PeopleItemInfo) type PeopleItemInfo struct { ID uint32 `struc:"uint32"` // 装备id