增加地图广播限制速率
This commit is contained in:
@@ -8,9 +8,11 @@ import (
|
|||||||
|
|
||||||
"github.com/antlabs/cronex"
|
"github.com/antlabs/cronex"
|
||||||
"github.com/gogf/gf/v2/os/glog"
|
"github.com/gogf/gf/v2/os/glog"
|
||||||
|
"github.com/xiaoqidun/limit"
|
||||||
sensitive "github.com/zmexing/go-sensitive-word"
|
sensitive "github.com/zmexing/go-sensitive-word"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var Limiter = limit.New()
|
||||||
var ctx = context.TODO()
|
var ctx = context.TODO()
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
|||||||
@@ -53,7 +53,7 @@ func (h *Controller) MapList(data *maps.ListMapPlayerInboundInfo, c *player.Play
|
|||||||
result1 := maps.NewOutInfo()
|
result1 := maps.NewOutInfo()
|
||||||
copier.CopyWithOption(result1, player.GetInfo(), copier.Option{DeepCopy: true})
|
copier.CopyWithOption(result1, player.GetInfo(), copier.Option{DeepCopy: true})
|
||||||
result.Player = append(result.Player, *result1)
|
result.Player = append(result.Player, *result1)
|
||||||
result.Player=LastFourElements(result.Player)
|
result.Player = LastFourElements(result.Player)
|
||||||
})
|
})
|
||||||
|
|
||||||
c.Canmon = true //可以刷怪
|
c.Canmon = true //可以刷怪
|
||||||
@@ -66,5 +66,5 @@ func LastFourElements[T any](s []T) []T {
|
|||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
// 切片长度大于4时,返回最后4个元素(从n-4索引到末尾)
|
// 切片长度大于4时,返回最后4个元素(从n-4索引到末尾)
|
||||||
return s[n-4:]
|
return s[n-30:]
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,15 +8,17 @@ import (
|
|||||||
|
|
||||||
func (h Controller) Walk(data *maps.WalkInInfo, c *player.Player) (result *maps.WalkOutInfo, err errorcode.ErrorCode) {
|
func (h Controller) Walk(data *maps.WalkInInfo, c *player.Player) (result *maps.WalkOutInfo, err errorcode.ErrorCode) {
|
||||||
result = &maps.WalkOutInfo{
|
result = &maps.WalkOutInfo{
|
||||||
Flag: data.Flag,
|
Flag: data.Flag,
|
||||||
Point: data.Point,
|
Point: data.Point,
|
||||||
Path: data.Path,
|
Path: data.Path,
|
||||||
}
|
}
|
||||||
|
|
||||||
result.UserID = data.Head.UserID
|
result.UserID = data.Head.UserID
|
||||||
c.Info.Pos = data.Point
|
c.Info.Pos = data.Point
|
||||||
//glog.Debug(context.Background(), err1)
|
//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
|
return nil, -1
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,11 +1,15 @@
|
|||||||
package maps
|
package maps
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"blazing/cool"
|
||||||
"blazing/logic/service/common"
|
"blazing/logic/service/common"
|
||||||
"blazing/logic/service/player"
|
"blazing/logic/service/player"
|
||||||
"blazing/logic/service/space"
|
"blazing/logic/service/space"
|
||||||
|
|
||||||
"blazing/modules/blazing/model"
|
"blazing/modules/blazing/model"
|
||||||
|
|
||||||
|
"github.com/gogf/gf/v2/util/gconv"
|
||||||
|
"golang.org/x/time/rate"
|
||||||
)
|
)
|
||||||
|
|
||||||
type WalkInInfo struct {
|
type WalkInInfo struct {
|
||||||
@@ -22,6 +26,10 @@ type WalkInInfo struct {
|
|||||||
|
|
||||||
func (t *WalkInInfo) Broadcast(mapid uint32, o WalkOutInfo) {
|
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) {
|
space.GetSpace(mapid).User.IterCb(func(playerID uint32, player common.PlayerI) {
|
||||||
t.Head.Result = 0
|
t.Head.Result = 0
|
||||||
tt := t.Head.Pack(&o)
|
tt := t.Head.Pack(&o)
|
||||||
|
|||||||
@@ -12,13 +12,11 @@ import (
|
|||||||
"github.com/gogf/gf/v2/net/ghttp"
|
"github.com/gogf/gf/v2/net/ghttp"
|
||||||
"github.com/gogf/gf/v2/os/gcmd"
|
"github.com/gogf/gf/v2/os/gcmd"
|
||||||
"github.com/gogf/gf/v2/os/gfile"
|
"github.com/gogf/gf/v2/os/gfile"
|
||||||
"github.com/xiaoqidun/limit"
|
|
||||||
"golang.org/x/time/rate"
|
"golang.org/x/time/rate"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
limiter = limit.New()
|
Main = gcmd.Command{
|
||||||
Main = gcmd.Command{
|
|
||||||
Name: "main",
|
Name: "main",
|
||||||
Usage: "main",
|
Usage: "main",
|
||||||
Brief: "start http server",
|
Brief: "start http server",
|
||||||
@@ -60,7 +58,7 @@ func Limiter(r *ghttp.Request) {
|
|||||||
// 3. 为任意键 "some-key" 获取一个速率限制器
|
// 3. 为任意键 "some-key" 获取一个速率限制器
|
||||||
// - rate.Limit(2): 表示速率为 "每秒2个请求"
|
// - rate.Limit(2): 表示速率为 "每秒2个请求"
|
||||||
// - 2: 表示桶的容量 (Burst),允许瞬时处理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() {
|
if !rateLimiter.Allow() {
|
||||||
r.Response.WriteStatusExit(429) // Return 429 Too Many Requests
|
r.Response.WriteStatusExit(429) // Return 429 Too Many Requests
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package model
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"blazing/cool"
|
"blazing/cool"
|
||||||
|
"math"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/creasty/defaults"
|
"github.com/creasty/defaults"
|
||||||
@@ -23,6 +24,23 @@ type Pos struct {
|
|||||||
Y uint32 `struc:"uint32" default:"0"`
|
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)
|
// PeopleItemInfo 穿戴装备信息结构(PeopleItemInfo)
|
||||||
type PeopleItemInfo struct {
|
type PeopleItemInfo struct {
|
||||||
ID uint32 `struc:"uint32"` // 装备id
|
ID uint32 `struc:"uint32"` // 装备id
|
||||||
|
|||||||
Reference in New Issue
Block a user