Files
bl/logic/service/space/space.go

73 lines
1.8 KiB
Go
Raw Normal View History

package space
import (
"blazing/common/data/xmlres"
"blazing/common/utils"
"blazing/logic/service/common"
)
// Space 针对Player的并发安全map键为uint32类型
type Space struct {
2025-10-10 04:49:23 +00:00
User utils.ConcurrentMap[uint32, common.PlayerI] // 存储玩家数据的map键为玩家ID
2025-10-10 04:31:52 +00:00
CanRefresh bool //是否能够刷怪
//ID uint32 // 地图ID
Name string //地图名称
// DefaultPos model.Pos //默认位置DefaultPos
2025-10-10 04:31:52 +00:00
//Positions map[uint32]model.Pos //从上一个地图跳转后默认位置 无任何写操作
}
// NewSyncMap 创建一个新的玩家同步map
func NewSpace() *Space {
return &Space{
2025-10-10 04:49:23 +00:00
User: utils.NewWithCustomShardingFunction[uint32, common.PlayerI](func(key uint32) uint32 {
return key
}),
}
}
2025-10-10 04:49:23 +00:00
// // Range 遍历所有玩家并执行回调函数
// // 读操作使用RLock遍历过程中不会阻塞其他读操作
// func (m *Space) Range(f func(playerID uint32, player common.PlayerI) bool) {
// m.mu.RLock()
// defer m.mu.RUnlock()
// for id, player := range m.User {
// // 若回调返回false则停止遍历
// if !f(id, player) {
// break
// }
// }
// }
// 获取星球
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"`
}