- 移除 global.go 文件 - 更新 player.go 中的玩家数据存储方式 - 删除 session.go 文件 - 调整 rpc.go 中的 RPC 客户端方法 - 更新 ServerEvent.go 中的会话管理 - 调整 controller 中的 Maincontroller 结构 - 更新 login.go 中的用户登录逻辑 - 调整 service 中的玩家数据获取方式 - 更新 admin/login.go 和 login.go 中的会话管理
59 lines
1.1 KiB
Go
59 lines
1.1 KiB
Go
package entity
|
|
|
|
import (
|
|
"blazing/cool"
|
|
|
|
"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 {
|
|
|
|
count := 0
|
|
cool.Mainplayer.Range(func(key, value interface{}) bool {
|
|
count++
|
|
return true // 继续遍历
|
|
})
|
|
return count
|
|
//fmt.Println("元素数量:", count) // 输出: 3
|
|
}
|