106 lines
2.2 KiB
Go
106 lines
2.2 KiB
Go
package space
|
||
|
||
import (
|
||
"blazing/logic/service/common"
|
||
|
||
"blazing/logic/service/space/info"
|
||
|
||
"sync/atomic"
|
||
|
||
"github.com/jinzhu/copier"
|
||
"github.com/panjf2000/ants/v2"
|
||
"golang.org/x/time/rate"
|
||
)
|
||
|
||
var mappool, _ = ants.NewPool(-1)
|
||
|
||
// 向其他人广播,不含自己
|
||
// 广播是c为空就不特判,发给全体成员广播
|
||
func (s *Space) Broadcast(c common.PlayerI, cmd uint32, data any) {
|
||
mappool.Submit(func() {
|
||
|
||
s.User.Range(func(k uint32, v common.PlayerI) (stop bool) {
|
||
if c != nil {
|
||
if k != c.GetInfo().UserID {
|
||
v.SendPackCmd(cmd, data)
|
||
|
||
}
|
||
} else {
|
||
v.SendPackCmd(cmd, data)
|
||
}
|
||
|
||
return false
|
||
})
|
||
})
|
||
|
||
}
|
||
|
||
func (s *Space) LeaveMap(c common.PlayerI) {
|
||
|
||
if atomic.CompareAndSwapUint32(&s.Owner.UserID, c.GetInfo().UserID, 0) {
|
||
|
||
s.Owner.Reset()
|
||
|
||
s.Broadcast(c, 2419, &s.Owner)
|
||
|
||
}
|
||
s.Broadcast(c, 2002, &info.LeaveMapOutboundInfo{UserID: c.GetInfo().UserID})
|
||
|
||
s.User.Delete(c.GetInfo().UserID)
|
||
s.UserInfo.Delete(c.GetInfo().UserID)
|
||
current, ok := maphot[s.Super]
|
||
if ok && *current > 0 {
|
||
atomic.AddInt32(maphot[s.Super], -1)
|
||
}
|
||
|
||
}
|
||
|
||
func (s *Space) EnterMap(c common.PlayerI) {
|
||
|
||
out := info.NewOutInfo()
|
||
copier.CopyWithOption(out, c.GetInfo(), copier.Option{DeepCopy: true})
|
||
|
||
s.User.Store(c.GetInfo().UserID, c)
|
||
s.UserInfo.Store(c.GetInfo().UserID, *out)
|
||
s.Broadcast(c, 2001, out)
|
||
_, ok := maphot[s.Super]
|
||
if ok {
|
||
atomic.AddInt32(maphot[s.Super], 1)
|
||
}
|
||
|
||
}
|
||
func (s *Space) GetInfo(c common.PlayerI) []info.OutInfo {
|
||
|
||
if atomic.LoadUint32(&s.TimeBoss.Flag) == 1 {
|
||
defer c.SendPackCmd(2022, &s.TimeBoss)
|
||
}
|
||
if s.MapBossInfo.Pos != 200 {
|
||
var t info.MapBossSInfo
|
||
t.INFO = append(t.INFO, s.MapBossInfo)
|
||
s.Broadcast(nil, 2021, &t)
|
||
|
||
}
|
||
defer c.SendPackCmd(50004, &info.S2C_50004{Id: uint32(s.Weather)}) //获取天气
|
||
ret := make([]info.OutInfo, 0)
|
||
s.UserInfo.Range(func(k uint32, v info.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)
|
||
|
||
}
|