2025-06-23 10:15:22 +08:00
|
|
|
package entity
|
|
|
|
|
|
2025-06-24 22:09:05 +08:00
|
|
|
import (
|
2025-07-17 06:09:52 +08:00
|
|
|
"context"
|
|
|
|
|
"fmt"
|
|
|
|
|
"sync"
|
|
|
|
|
"time"
|
2025-06-24 22:09:05 +08:00
|
|
|
)
|
|
|
|
|
|
2025-06-23 10:15:22 +08:00
|
|
|
type Player struct {
|
2025-07-14 11:07:13 +08:00
|
|
|
MainConn Conn
|
|
|
|
|
UserID uint32 //用户ID
|
|
|
|
|
IsLogin bool //是否登录 //TODO 待实现登录包为第一个包,后续再发其他的包
|
2025-07-17 06:09:52 +08:00
|
|
|
mu sync.Mutex
|
2025-06-27 22:40:49 +08:00
|
|
|
|
2025-07-17 06:09:52 +08:00
|
|
|
loginChan chan struct{} // 登录完成通知通道
|
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
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-07-26 02:14:54 +00:00
|
|
|
func WithConn(c Conn) PlayerOption {
|
2025-06-24 22:09:05 +08:00
|
|
|
return func(p *Player) {
|
2025-07-26 02:14:54 +00:00
|
|
|
p.MainConn = c
|
2025-06-24 22:09:05 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// NewPlayer 使用 Options 模式创建 Player 实例
|
|
|
|
|
func NewPlayer(opts ...PlayerOption) *Player {
|
2025-07-17 06:09:52 +08:00
|
|
|
p := &Player{
|
|
|
|
|
loginChan: make(chan struct{}),
|
|
|
|
|
}
|
2025-06-24 22:09:05 +08:00
|
|
|
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 {
|
2025-07-14 11:07:13 +08:00
|
|
|
err := p.MainConn.SendPack(b)
|
2025-07-06 19:31:30 +08:00
|
|
|
return err
|
|
|
|
|
}
|
2025-07-06 22:58:39 +08:00
|
|
|
|
2025-07-17 06:09:52 +08:00
|
|
|
// IsLoggedIn 检查是否已登录
|
|
|
|
|
func (lw *Player) IsLoggedIn() bool {
|
|
|
|
|
lw.mu.Lock()
|
|
|
|
|
defer lw.mu.Unlock()
|
|
|
|
|
return lw.IsLogin
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// WaitForLogin 等待登录完成,无超时
|
|
|
|
|
func (lw *Player) WaitForLogin() error {
|
|
|
|
|
if lw.IsLoggedIn() {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 阻塞等待登录完成
|
|
|
|
|
<-lw.loginChan
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// WaitForLoginWithTimeout 带超时的登录等待
|
|
|
|
|
func (lw *Player) WaitForLoginWithTimeout(timeout time.Duration) error {
|
|
|
|
|
if lw.IsLoggedIn() {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 使用定时器实现超时
|
|
|
|
|
timer := time.NewTimer(timeout)
|
|
|
|
|
defer timer.Stop()
|
|
|
|
|
|
|
|
|
|
select {
|
|
|
|
|
case <-lw.loginChan:
|
|
|
|
|
return nil
|
|
|
|
|
case <-timer.C:
|
|
|
|
|
return fmt.Errorf("登录等待超时: %v", timeout)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// WaitForLoginWithCtx 带上下文的登录等待
|
|
|
|
|
func (lw *Player) WaitForLoginWithCtx(ctx context.Context) error {
|
|
|
|
|
if lw.IsLoggedIn() {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
select {
|
|
|
|
|
case <-lw.loginChan:
|
|
|
|
|
return nil
|
|
|
|
|
case <-ctx.Done():
|
|
|
|
|
return ctx.Err() // 上下文取消或超时
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// CompleteLogin 标记登录完成并通知等待者
|
|
|
|
|
func (lw *Player) CompleteLogin() {
|
|
|
|
|
lw.mu.Lock()
|
|
|
|
|
defer lw.mu.Unlock()
|
|
|
|
|
|
|
|
|
|
if !lw.IsLogin {
|
|
|
|
|
lw.IsLogin = true
|
|
|
|
|
close(lw.loginChan) // 关闭通道以通知所有等待者
|
|
|
|
|
}
|
|
|
|
|
}
|