game/common/baseroom/roomMgr.go
2025-06-06 20:02:58 +08:00

92 lines
1.6 KiB
Go

package baseroom
import (
"game/common/proto/pb"
)
type ServiceStatus int
const (
SsWorking ServiceStatus = 0
SsWaitStop ServiceStatus = 1
SsStopped ServiceStatus = 2
)
type RoomMgr struct {
rooms map[int]IRoom
createRoom ICreateRoom
status ServiceStatus // 服务状态
no int
}
func NewRoomMgr(create ICreateRoom) *RoomMgr {
return &RoomMgr{
rooms: make(map[int]IRoom),
createRoom: create,
status: SsWorking,
no: 100000,
}
}
func (s *RoomMgr) Count() int {
return len(s.rooms)
}
func (s *RoomMgr) Init() {
}
func (s *RoomMgr) Add(room IRoom) {
s.rooms[room.Id()] = room
}
func (s *RoomMgr) Del(id int) {
delete(s.rooms, id)
}
func (s *RoomMgr) Len() int {
return len(s.rooms)
}
func (s *RoomMgr) Find(id int) IRoom {
return s.rooms[id]
}
func (s *RoomMgr) Status() ServiceStatus {
return s.status
}
func (s *RoomMgr) SetStatus(ss ServiceStatus) {
s.status = ss
}
// FindWaitRooms 获取所有未开始游戏的房间
func (s *RoomMgr) Filter(predicate func(IRoom) bool) []IRoom {
rooms := make([]IRoom, 0)
for _, room := range s.rooms {
if predicate(room) {
rooms = append(rooms, room)
}
}
return rooms
}
func (s *RoomMgr) makeRoomId() int {
s.no++
return s.no
}
func (s *RoomMgr) CreateRoom(roomType int) (room IRoom, code pb.ErrCode) {
if s.status != SsWorking {
return nil, pb.ErrCode_Maintain
}
roomId := s.makeRoomId()
if roomId < 0 {
return nil, pb.ErrCode_SystemErr
}
room, code = s.createRoom.CreateRoom(roomId, roomType)
if room != nil {
s.Add(room)
}
return room, code
}