Files
bl/logic/service/space/in_out.go
2025-11-18 21:03:00 +00:00

95 lines
1.9 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package space
import (
"blazing/logic/service/common"
"blazing/logic/service/maps/info"
maps "blazing/logic/service/maps/info"
"sync/atomic"
"github.com/jinzhu/copier"
"golang.org/x/time/rate"
)
// 向其他人广播,不含自己
func (s *Space) Broadcast(c common.PlayerI, cmd uint32, data any) {
go s.User.Range(func(k uint32, v common.PlayerI) (stop bool) {
if k != c.GetInfo().UserID {
v.SendPackCmd(cmd, data)
}
return false
})
}
func (s *Space) LeaveMap(c common.PlayerI) {
if c == nil {
return
}
if c.GetInfo() == nil {
return
}
s.UP_ARENA(c, 0) //退出擂台
s.Broadcast(c, 2002, info.LeaveMapOutboundInfo{UserID: c.GetInfo().UserID})
s.User.Delete(c.GetInfo().UserID)
s.UserInfo.Delete(c.GetInfo().UserID)
if s.SuperValue != nil {
atomic.AddInt32(s.SuperValue, -1)
}
}
func (s *Space) EnterMap(c common.PlayerI) {
out := info.NewOutInfo()
copier.CopyWithOption(out, c.GetInfo(), copier.Option{DeepCopy: true})
s.Broadcast(c, 2001, info.LeaveMapOutboundInfo{UserID: c.GetInfo().UserID})
s.User.Store(c.GetInfo().UserID, c)
s.UserInfo.Store(c.GetInfo().UserID, *out)
if s.SuperValue != nil {
atomic.AddInt32(s.SuperValue, 1)
}
}
func (s *Space) GetInfo() []maps.OutInfo {
ret := make([]maps.OutInfo, 0)
s.UserInfo.Range(func(k uint32, v maps.OutInfo) (stop bool) {
ret = append(ret, v)
return len(ret) > 30
})
return ret
}
var limiter = rate.NewLimiter(rate.Limit(10), 5)
func (s *Space) Walk(c common.PlayerI, info info.WalkOutInfo) {
// cool.Limiter.Take()
//r := cool.Limiter.Get("Broadcast"+gconv.String(mapid), rate.Limit(10), 5)
if !limiter.Allow() {
return
}
s.Broadcast(c, 2101, info)
}
func LastFourElements[T any](s []T) []T {
n := len(s)
if n <= 30 {
// 切片长度小于等于4时返回整个切片
return s
}
// 切片长度大于4时返回最后4个元素从n-4索引到末尾
return s[n-30:]
}