2025-06-23 10:15:22 +08:00
|
|
|
package entity
|
|
|
|
|
|
2025-06-24 22:09:05 +08:00
|
|
|
import (
|
2025-07-06 22:58:39 +08:00
|
|
|
"blazing/common/core"
|
|
|
|
|
|
2025-06-24 22:09:05 +08:00
|
|
|
"github.com/panjf2000/gnet/v2"
|
|
|
|
|
)
|
|
|
|
|
|
2025-06-23 10:15:22 +08:00
|
|
|
type Player struct {
|
2025-06-27 22:40:49 +08:00
|
|
|
MainConn gnet.Conn `struc:"[0]pad"` //TODO 不序列化,,序列化下面的作为blob存数据库
|
|
|
|
|
UserID uint32 //用户ID
|
|
|
|
|
IsLogin bool //是否登录 //TODO 待实现登录包为第一个包,后续再发其他的包
|
|
|
|
|
|
2025-06-24 22:09:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// PlayerOption 定义配置 Player 的函数类型
|
|
|
|
|
type PlayerOption func(*Player)
|
|
|
|
|
|
|
|
|
|
// WithUserID 设置用户ID的选项函数
|
|
|
|
|
func WithUserID(userID uint32) PlayerOption {
|
|
|
|
|
return func(p *Player) {
|
|
|
|
|
p.UserID = userID
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
func WithConn(Conn gnet.Conn) PlayerOption {
|
|
|
|
|
return func(p *Player) {
|
2025-06-27 22:40:49 +08:00
|
|
|
p.MainConn = Conn
|
2025-06-24 22:09:05 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
}
|
2025-07-06 19:31:30 +08:00
|
|
|
|
|
|
|
|
func (p *Player) SendPack(b []byte) error {
|
|
|
|
|
_, err := p.MainConn.Write(b)
|
|
|
|
|
return err
|
|
|
|
|
}
|
2025-07-06 22:58:39 +08:00
|
|
|
|
|
|
|
|
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
|
|
|
|
|
}
|