- 引入 idgenerator-go 库,实现全局唯一 ID 生成 - 重构 Pack 函数,使用接口参数提高灵活性 - 修改 Player 结构,增加 MainConn 字段用于主连接 - 更新 SocketHandler_Tomee 中的 Data 字段标记 - 优化 Recv 函数中的数据解包和参数处理逻辑
41 lines
908 B
Go
41 lines
908 B
Go
package entity
|
|
|
|
import (
|
|
"github.com/panjf2000/gnet/v2"
|
|
)
|
|
|
|
type Player struct {
|
|
MainConn gnet.Conn `struc:"[0]pad"` //TODO 不序列化,,序列化下面的作为blob存数据库
|
|
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(Conn gnet.Conn) PlayerOption {
|
|
return func(p *Player) {
|
|
p.MainConn = Conn
|
|
}
|
|
}
|
|
|
|
// 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
|
|
}
|