Files
bl/logic/service/space/space.go
昔念 13b37b432a ```
refactor(controller): 替换用户遍历逻辑为迭代回调方式

将多个控制器中使用的 `Items()` 方法遍历用户列表的方式,
统一修改为通过 `IterCb()` 回调函数方式进行处理,
提升代码一致性与可维护性。

同时引入 `blazing/logic/service/common` 包以支持 PlayerI 接口调用。
此外,移除了未使用的 `model.Pos` 类型及相关注释,精简结构体定义。
```
2025-10-10 20:46:16 +08:00

85 lines
2.2 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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
}
if id > 10000 { //说明是玩家地图GetSpace
t := NewSpace()
planetmap.Store(id, t)
return t
}
//如果不ok,说明星球未创建,那就新建星球
for _, v := range xmlres.MapConfig.Maps {
if v.ID == int(id) { //找到这个地图
t := NewSpace()
//t.DefaultPos = model.Pos{X: uint32(v.X), Y: uint32(v.Y)}
t.ID = uint32(v.ID)
t.Name = v.Name
// t.Positions = make(map[uint32]model.Pos)
// for _, v := range v.Entries.Entries { //添加地图入口
// t.Positions[uint32(v.FromMap)] = model.Pos{X: uint32(v.PosX), Y: uint32(v.PosY)}
// }
planetmap.Store(id, t)
return t
}
}
return nil
}
var planetmap = &utils.SyncMap[uint32, *Space]{} //玩家数据
type LeaveMapOutboundInfo struct {
// 米米号
UserID uint32 `struc:"uint32" fieldDesc:"米米号" json:"user_id"`
}