重构
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
package space
|
||||
|
||||
import (
|
||||
"blazing/common/data/entity"
|
||||
"blazing/common/data/socket"
|
||||
xml "blazing/common/data/xml/map"
|
||||
"blazing/common/utils"
|
||||
"blazing/modules/blazing/model"
|
||||
@@ -11,7 +11,7 @@ import (
|
||||
// Space 针对Player的并发安全map,键为uint32类型
|
||||
type Space struct {
|
||||
mu sync.RWMutex // 读写锁,读多写少场景更高效
|
||||
data map[uint32]*entity.Player // 存储玩家数据的map,键为玩家ID
|
||||
data map[uint32]*socket.Player // 存储玩家数据的map,键为玩家ID
|
||||
CanRefresh bool //是否能够刷怪
|
||||
ID uint32 // 地图ID
|
||||
Name string //地图名称
|
||||
@@ -22,13 +22,13 @@ type Space struct {
|
||||
// NewSyncMap 创建一个新的玩家同步map
|
||||
func NewSpace() *Space {
|
||||
return &Space{
|
||||
data: make(map[uint32]*entity.Player),
|
||||
data: make(map[uint32]*socket.Player),
|
||||
}
|
||||
}
|
||||
|
||||
// Get 根据玩家ID获取玩家实例
|
||||
// 读操作使用RLock,允许多个goroutine同时读取
|
||||
func (m *Space) Get(playerID uint32) (*entity.Player, bool) {
|
||||
func (m *Space) Get(playerID uint32) (*socket.Player, bool) {
|
||||
m.mu.RLock()
|
||||
defer m.mu.RUnlock()
|
||||
val, exists := m.data[playerID]
|
||||
@@ -37,10 +37,12 @@ func (m *Space) Get(playerID uint32) (*entity.Player, bool) {
|
||||
|
||||
// Set 存储玩家实例(按ID)
|
||||
// 写操作使用Lock,独占锁保证数据一致性
|
||||
func (m *Space) Set(playerID uint32, player *entity.Player) {
|
||||
func (m *Space) Set(playerID uint32, player *socket.Player)*Space {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
m.data[playerID] = player
|
||||
|
||||
return m
|
||||
}
|
||||
|
||||
// Delete 根据玩家ID删除玩家实例
|
||||
@@ -61,7 +63,7 @@ func (m *Space) Len() int {
|
||||
|
||||
// Range 遍历所有玩家并执行回调函数
|
||||
// 读操作使用RLock,遍历过程中不会阻塞其他读操作
|
||||
func (m *Space) Range(f func(playerID uint32, player *entity.Player) bool) {
|
||||
func (m *Space) Range(f func(playerID uint32, player *socket.Player) bool) {
|
||||
m.mu.RLock()
|
||||
defer m.mu.RUnlock()
|
||||
for id, player := range m.data {
|
||||
|
||||
Reference in New Issue
Block a user