73 lines
1.8 KiB
Go
73 lines
1.8 KiB
Go
package space
|
||
|
||
import (
|
||
"blazing/common/data/xmlres"
|
||
"blazing/common/utils"
|
||
|
||
"blazing/logic/service/common"
|
||
)
|
||
|
||
// Space 针对Player的并发安全map,键为uint32类型
|
||
type Space struct {
|
||
User utils.ConcurrentMap[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: utils.NewWithCustomShardingFunction[uint32, common.PlayerI](func(key uint32) uint32 {
|
||
return key
|
||
}),
|
||
}
|
||
}
|
||
|
||
// // 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"`
|
||
}
|