package entity import ( "blazing/common/core" "github.com/panjf2000/gnet/v2" ) type Player struct { MainConn Conn UserID uint32 //用户ID IsLogin bool //是否登录 //TODO 待实现登录包为第一个包,后续再发其他的包 } // PlayerOption 定义配置 Player 的函数类型 type PlayerOption func(*Player) // WithUserID 设置用户ID的选项函数 func WithUserID(userID uint32) PlayerOption { return func(p *Player) { p.UserID = userID } } func WithConn(c gnet.Conn) PlayerOption { return func(p *Player) { p.MainConn = Conn{MainConn: c} } } // NewPlayer 使用 Options 模式创建 Player 实例 func NewPlayer(opts ...PlayerOption) *Player { p := &Player{} for _, opt := range opts { opt(p) } return p } func (p *Player) GetUserID() uint32 { return p.UserID } func (p *Player) SendPack(b []byte) error { err := p.MainConn.SendPack(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 }