package entertainment import ( "errors" "fmt" "go_dreamfactory/comm" "go_dreamfactory/lego/core" "go_dreamfactory/lego/core/cbase" "go_dreamfactory/lego/sys/event" "go_dreamfactory/pb" "sync" "google.golang.org/protobuf/proto" ) /* 游戏管理组件 */ type gameMgrComp struct { cbase.ModuleCompBase module *Entertainment lock sync.RWMutex rooms map[string]*Room } func (this *gameMgrComp) Init(service core.IService, module core.IModule, comp core.IModuleComp, options core.IModuleOptions) (err error) { err = this.ModuleCompBase.Init(service, module, comp, options) this.module = module.(*Entertainment) this.rooms = make(map[string]*Room) return } func (this *gameMgrComp) Start() (err error) { event.RegisterGO(comm.EventCloseRoom, this.CloseRoom) return } // 通过房间ID 加入房间 func (this *gameMgrComp) JoinMasterRoom(roomid string, p *pb.PlayerData) (room *Room, err error) { var ( ok bool ) this.lock.Lock() room, ok = this.rooms[roomid] if ok { room, err = room.JoinRoom(this.module, p) } else { err = errors.New("房间不存在") } this.lock.Unlock() return } // 手动创建房间 func (this *gameMgrComp) CreateMasterRoom(p *pb.PlayerData) (room *Room, err error) { room = new(Room) //初始化房间 room, err = room.JoinRoom(this.module, p) this.lock.Lock() this.rooms[room.Id] = room this.lock.Unlock() return } // 创建一个玩法类型的房间(itype :-1 表示随机玩法) func (this *gameMgrComp) CreateRoomByType(p1 *pb.PlayerData, p2 *pb.PlayerData, itype int32) (room *Room, err error) { room = new(Room) //初始化房间 room, err = room.InitRoom(this.module, p1, p2, itype) if err != nil { return } this.lock.Lock() this.rooms[room.Id] = room this.lock.Unlock() return } func (this *gameMgrComp) CloseRoom(id string) (err error) { this.lock.Lock() defer this.lock.Unlock() if _, ok := this.rooms[id]; ok { delete(this.rooms, id) return } err = errors.New(fmt.Sprintf("cant found rooid:%s", id)) return } func (this *gameMgrComp) RoomDistribute(rid string, session comm.IUserSession, stype string, req proto.Message) (errdata *pb.ErrorData) { var ( room *Room ok bool ) this.lock.RLock() room, ok = this.rooms[rid] this.lock.RUnlock() if ok { errdata = room.ReceiveMessage(session, stype, req) } else { errdata = &pb.ErrorData{ Code: pb.ErrorCode_ReqParameterError, Title: pb.ErrorCode_ReqParameterError.String(), Message: fmt.Sprintf("on found room:%s", rid), } } return } // 获取房间信息 func (this *gameMgrComp) GetRoomInfo(id string) (room *Room, err error) { var ( ok bool ) this.lock.Lock() defer this.lock.Unlock() if room, ok = this.rooms[id]; ok { return } err = errors.New(fmt.Sprintf("cant found rooid:%s", id)) return }