package baseroom import ( "game/common/proto/pb" "github.com/fox/fox/service" ) 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) 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, srv service.IService) (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, srv) if room != nil { s.Add(room) } return room, code }