基本游戏玩家和登录扩展实现
This commit is contained in:
parent
e584d85231
commit
973ea1fd3d
|
@ -0,0 +1,30 @@
|
|||
package builtin
|
||||
|
||||
import "minotaur/server"
|
||||
|
||||
func NewPlayer[ID comparable](id ID, conn *server.Conn) *Player[ID] {
|
||||
return &Player[ID]{
|
||||
id: id,
|
||||
conn: conn,
|
||||
}
|
||||
}
|
||||
|
||||
// Player 游戏玩家
|
||||
type Player[ID comparable] struct {
|
||||
id ID
|
||||
conn *server.Conn
|
||||
}
|
||||
|
||||
func (slf *Player[ID]) GetID() ID {
|
||||
return slf.id
|
||||
}
|
||||
|
||||
// Send 向该玩家发送数据
|
||||
func (slf *Player[ID]) Send(packet []byte) error {
|
||||
return slf.conn.Write(packet)
|
||||
}
|
||||
|
||||
// Close 关闭玩家
|
||||
func (slf *Player[ID]) Close() {
|
||||
slf.conn.Close()
|
||||
}
|
|
@ -0,0 +1,33 @@
|
|||
package extension
|
||||
|
||||
import (
|
||||
"minotaur/game"
|
||||
"time"
|
||||
)
|
||||
|
||||
type PlayerLoginLauncher[PlayerID comparable] struct {
|
||||
game.Player[PlayerID]
|
||||
loggedTime time.Time // 登录时间
|
||||
logoutTime time.Time // 登出时间
|
||||
}
|
||||
|
||||
func (slf *PlayerLoginLauncher[PlayerID]) HasLogged() bool {
|
||||
return !slf.loggedTime.IsZero()
|
||||
}
|
||||
|
||||
func (slf *PlayerLoginLauncher[PlayerID]) Logged() {
|
||||
slf.loggedTime = time.Now()
|
||||
}
|
||||
|
||||
func (slf *PlayerLoginLauncher[PlayerID]) Logout() {
|
||||
slf.logoutTime = time.Now()
|
||||
slf.loggedTime = time.Time{}
|
||||
}
|
||||
|
||||
func (slf *PlayerLoginLauncher[PlayerID]) GetLoggedTime() time.Time {
|
||||
return slf.loggedTime
|
||||
}
|
||||
|
||||
func (slf *PlayerLoginLauncher[PlayerID]) GetLogoutTime() time.Time {
|
||||
return slf.logoutTime
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
package game
|
||||
|
||||
// Player 玩家
|
||||
type Player[ID comparable] interface {
|
||||
// GetID 用户玩家ID
|
||||
GetID() ID
|
||||
// Send 发送数据包
|
||||
Send(packet []byte) error
|
||||
// Close 关闭玩家并且释放其资源
|
||||
Close()
|
||||
}
|
Loading…
Reference in New Issue