房间实现

This commit is contained in:
kercylan98 2023-05-05 17:25:45 +08:00
parent e0e43c6749
commit f084613aa9
3 changed files with 141 additions and 0 deletions

92
game/builtin/room.go Normal file
View File

@ -0,0 +1,92 @@
package builtin
import (
"go.uber.org/zap"
"minotaur/game"
"minotaur/utils/log"
"minotaur/utils/synchronization"
)
func NewRoom[PlayerID comparable, Player game.Player[PlayerID]](guid int64) *Room[PlayerID, Player] {
return &Room[PlayerID, Player]{
guid: guid,
playersConn: synchronization.NewMap[string, Player](),
players: synchronization.NewMap[PlayerID, Player](),
}
}
type Room[PlayerID comparable, Player game.Player[PlayerID]] struct {
guid int64
playerLimit int
playersConn *synchronization.Map[string, Player]
players *synchronization.Map[PlayerID, Player]
playerJoinRoomEventHandles []game.PlayerJoinRoomEventHandle[PlayerID, Player]
playerLeaveRoomEventHandles []game.PlayerLeaveRoomEventHandle[PlayerID, Player]
}
func (slf *Room[PlayerID, Player]) GetGuid() int64 {
return slf.guid
}
func (slf *Room[PlayerID, Player]) GetPlayerLimit() int {
return slf.playerLimit
}
func (slf *Room[PlayerID, Player]) GetPlayerWithConnID(id string) Player {
return slf.playersConn.Get(id)
}
func (slf *Room[PlayerID, Player]) GetPlayer(id PlayerID) Player {
return slf.players.Get(id)
}
func (slf *Room[PlayerID, Player]) GetPlayers() synchronization.MapReadonly[PlayerID, Player] {
return slf.players
}
func (slf *Room[PlayerID, Player]) IsExistPlayer(id PlayerID) bool {
return slf.players.Exist(id)
}
func (slf *Room[PlayerID, Player]) Join(player Player) error {
if slf.players.Size() >= slf.playerLimit && slf.playerLimit > 0 {
return ErrWorldPlayerLimit
}
log.Debug("Room.Join", zap.Any("guid", slf.GetGuid()), zap.Any("player", player.GetID()), zap.String("conn", player.GetConnID()))
slf.players.Set(player.GetID(), player)
slf.playersConn.Set(player.GetConnID(), player)
slf.OnPlayerJoinRoomEvent(player)
return nil
}
func (slf *Room[PlayerID, Player]) Leave(id PlayerID) {
player, exist := slf.players.GetExist(id)
if !exist {
return
}
log.Debug("Room.Leave", zap.Any("guid", slf.GetGuid()), zap.Any("player", player.GetID()), zap.String("conn", player.GetConnID()))
slf.OnPlayerLeaveRoomEvent(player)
slf.players.Delete(player.GetID())
slf.playersConn.Delete(player.GetConnID())
}
func (slf *Room[PlayerID, Player]) RegPlayerJoinRoomEvent(handle game.PlayerJoinRoomEventHandle[PlayerID, Player]) {
slf.playerJoinRoomEventHandles = append(slf.playerJoinRoomEventHandles, handle)
}
func (slf *Room[PlayerID, Player]) OnPlayerJoinRoomEvent(player Player) {
for _, handle := range slf.playerJoinRoomEventHandles {
handle(slf, player)
}
}
func (slf *Room[PlayerID, Player]) RegPlayerLeaveRoomEvent(handle game.PlayerLeaveRoomEventHandle[PlayerID, Player]) {
slf.playerLeaveRoomEventHandles = append(slf.playerLeaveRoomEventHandles, handle)
}
func (slf *Room[PlayerID, Player]) OnPlayerLeaveRoomEvent(player Player) {
for _, handle := range slf.playerLeaveRoomEventHandles {
handle(slf, player)
}
}

View File

@ -0,0 +1,13 @@
package builtin
import "minotaur/game"
// RoomOption 房间构建可选项
type RoomOption[PlayerID comparable, Player game.Player[PlayerID]] func(room *Room[PlayerID, Player])
// WithRoomPlayerLimit 限制房间的玩家数量上限
func WithRoomPlayerLimit[PlayerID comparable, Player game.Player[PlayerID]](playerLimit int) RoomOption[PlayerID, Player] {
return func(room *Room[PlayerID, Player]) {
room.playerLimit = playerLimit
}
}

36
game/room.go Normal file
View File

@ -0,0 +1,36 @@
package game
import "minotaur/utils/synchronization"
// Room 房间类似于简版的游戏世界,不过没有游戏实体
type Room[PlayerID comparable, P Player[PlayerID]] interface {
// GetGuid 获取房间的唯一标识符
GetGuid() int64
// GetPlayerLimit 获取玩家人数上限
GetPlayerLimit() int
// GetPlayerWithConnID 根据连接ID获取玩家
GetPlayerWithConnID(id string) P
// GetPlayer 根据玩家id获取玩家
GetPlayer(id PlayerID) P
// GetPlayers 获取房间中的所有玩家
GetPlayers() synchronization.MapReadonly[PlayerID, P]
// IsExistPlayer 检查房间中是否存在特定玩家
IsExistPlayer(id PlayerID) bool
// Join 使特定玩家加入房间
Join(player P) error
// Leave 使特定玩家离开房间
Leave(id PlayerID)
// RegPlayerJoinRoomEvent 玩家进入房间时将立即执行被注册的事件处理函数
RegPlayerJoinRoomEvent(handle PlayerJoinRoomEventHandle[PlayerID, P])
OnPlayerJoinRoomEvent(player P)
// RegPlayerLeaveRoomEvent 玩家离开房间时将立即执行被注册的事件处理函数
RegPlayerLeaveRoomEvent(handle PlayerLeaveRoomEventHandle[PlayerID, P])
OnPlayerLeaveRoomEvent(player P)
}
type (
PlayerJoinRoomEventHandle[ID comparable, P Player[ID]] func(room Room[ID, P], player P)
PlayerLeaveRoomEventHandle[ID comparable, P Player[ID]] func(room Room[ID, P], player P)
)