diff --git a/game/builtin/player.go b/game/builtin/player.go new file mode 100644 index 0000000..24e85e4 --- /dev/null +++ b/game/builtin/player.go @@ -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() +} diff --git a/game/extension/player_login_launcher.go b/game/extension/player_login_launcher.go new file mode 100644 index 0000000..7eeb851 --- /dev/null +++ b/game/extension/player_login_launcher.go @@ -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 +} diff --git a/game/player.go b/game/player.go new file mode 100644 index 0000000..1762a84 --- /dev/null +++ b/game/player.go @@ -0,0 +1,11 @@ +package game + +// Player 玩家 +type Player[ID comparable] interface { + // GetID 用户玩家ID + GetID() ID + // Send 发送数据包 + Send(packet []byte) error + // Close 关闭玩家并且释放其资源 + Close() +}