feat(space): 替换并发安全map实现以提升性能 将原来基于`utils.ConcurrentMap`的玩家存储结构替换为 `github.com/mhmtszr/concurrent-swiss-map`提供的`CsMap`, 以获得更高效的并发读写能力。 同时修改了相关API调用方式: - `Set` 改为 `Store` - `Remove` 改为 `Delete` - `IterCb` 改为 `Range`,并支持提前终止迭代 - `Items()` 不再使用 此外,调整了部分业务逻辑中对玩家列表遍历的方式, 确保在发送网络包后及时跳出循环,避免不必要的操作。 新增战斗类型处理函数`PET_King`用于处理宠物王相关的 战斗请求,并修复了`PET_MELEE`方法中的逻辑问题。 更新了go.mod和go.sum引入新的依赖库。 ```
72 lines
1.8 KiB
Go
72 lines
1.8 KiB
Go
package space
|
||
|
||
import (
|
||
"blazing/common/data/xmlres"
|
||
"blazing/common/utils"
|
||
"blazing/logic/service/common"
|
||
|
||
csmap "github.com/mhmtszr/concurrent-swiss-map"
|
||
)
|
||
|
||
// Space 针对Player的并发安全map,键为uint32类型
|
||
type Space struct {
|
||
User *csmap.CsMap[uint32, common.PlayerI] // 存储玩家数据的map,键为玩家ID
|
||
CanRefresh bool //是否能够刷怪
|
||
//ID uint32 // 地图ID
|
||
Name string //地图名称
|
||
// DefaultPos model.Pos //默认位置DefaultPos
|
||
|
||
//Positions map[uint32]model.Pos //从上一个地图跳转后默认位置 无任何写操作
|
||
}
|
||
|
||
// NewSyncMap 创建一个新的玩家同步map
|
||
func NewSpace() *Space {
|
||
return &Space{
|
||
User: csmap.New[uint32, common.PlayerI](
|
||
// set the number of map shards. the default value is 32.
|
||
csmap.WithShardCount[uint32, common.PlayerI](32),
|
||
|
||
// // if don't set custom hasher, use the built-in maphash.
|
||
// csmap.WithCustomHasher[string, int](func(key string) uint64 {
|
||
// hash := fnv.New64a()
|
||
// hash.Write([]byte(key))
|
||
// return hash.Sum64()
|
||
// }),
|
||
|
||
// set the total capacity, every shard map has total capacity/shard count capacity. the default value is 0.
|
||
// csmap.WithSize[string, int](1000),
|
||
),
|
||
}
|
||
}
|
||
|
||
// 获取星球
|
||
func GetSpace(id uint32) *Space {
|
||
|
||
planet, ok := planetmap.Load(id)
|
||
if ok {
|
||
return planet
|
||
}
|
||
t := NewSpace()
|
||
|
||
if id < 10000 { //说明是玩家地图GetSpace
|
||
|
||
for _, v := range xmlres.MapConfig.Maps {
|
||
if v.ID == int(id) { //找到这个地图
|
||
t := NewSpace()
|
||
|
||
t.Name = v.Name
|
||
|
||
}
|
||
|
||
}
|
||
}
|
||
planetmap.Store(id, t)
|
||
return t
|
||
}
|
||
|
||
var planetmap = &utils.SyncMap[uint32, *Space]{} //玩家数据
|
||
type LeaveMapOutboundInfo struct {
|
||
// 米米号
|
||
UserID uint32 `struc:"uint32" fieldDesc:"米米号" json:"user_id"`
|
||
}
|