feat(fight): 优化擂台战斗逻辑与状态管理 - 修改了擂台主人设置逻辑,引入 `Set` 方法统一处理玩家信息更新 - 增加对擂主是否可战斗的判断,防止无效挑战 - 调整连胜计算和广播机制,确保数据一致性 - 修复擂台挑战失败时的状态回滚问题 - 引入错误码替代硬编码返回值,提高代码可读性与维护性 - 统一访问擂台玩家的方式,移除冗余字段
92 lines
1.8 KiB
Go
92 lines
1.8 KiB
Go
package space
|
||
|
||
import (
|
||
"blazing/logic/service/common"
|
||
"blazing/logic/service/maps/info"
|
||
maps "blazing/logic/service/maps/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)
|
||
|
||
}
|
||
}
|
||
|
||
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)
|
||
_, ok := maphot[s.Super]
|
||
if ok {
|
||
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() []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)
|
||
|
||
}
|