refactor(entity): 重构 ClientData 结构体并添加同步锁
- 在 ClientData 中添加 sync.Mutex 以确保并发安全 - 实现 SetPlayer 和 GetPlayer 等方法来封装 player 字段的操作 - 更新相关代码以使用新的 ClientData 结构和方法
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
package entity
|
||||
|
||||
import (
|
||||
"blazing/common/core"
|
||||
|
||||
"github.com/panjf2000/gnet/v2"
|
||||
)
|
||||
|
||||
@@ -43,3 +45,16 @@ func (p *Player) SendPack(b []byte) error {
|
||||
_, err := p.MainConn.Write(b)
|
||||
return err
|
||||
}
|
||||
|
||||
func ConutPlayer() int {
|
||||
|
||||
// v := reflect.ValueOf(&core.Mainplayer).Elem().FieldByName("m").Elem()
|
||||
// return int(v.FieldByName("count").Int())
|
||||
count := 0
|
||||
core.Mainplayer.Range(func(key, value interface{}) bool {
|
||||
count++
|
||||
return true // 继续遍历
|
||||
})
|
||||
return count
|
||||
//fmt.Println("元素数量:", count) // 输出: 3
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user