go_dreamfactory/modules/catchbugs/rooms.go
2023-12-08 17:51:54 +08:00

67 lines
1.3 KiB
Go

package catchbugs
import (
"fmt"
"go_dreamfactory/comm"
"go_dreamfactory/lego/core"
"go_dreamfactory/lego/core/cbase"
"go_dreamfactory/pb"
"sync"
)
type roomsComp struct {
cbase.ModuleCompBase
module *CatchBugs
lock sync.RWMutex
rooms map[string]*Room
}
func (this *roomsComp) 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.(*CatchBugs)
this.rooms = make(map[string]*Room)
return
}
func (this *roomsComp) queryRoom(rid string) (room *Room, err error) {
var (
ok bool
)
this.lock.RLock()
room, ok = this.rooms[rid]
this.lock.RUnlock()
if !ok {
err = fmt.Errorf("no found room:%s", rid)
return
}
return
}
func (this *roomsComp) newRoom(data *pb.DBCatchBugsRoom, session []comm.IUserSession) (room *Room, err error) {
room = &Room{
module: this.module,
data: data,
sessions: session,
round: 1,
}
this.lock.Lock()
this.rooms[data.Rid] = room
this.lock.Unlock()
return
}
func (this *roomsComp) removeRoom(rid string) {
var (
ok bool
room *Room
)
this.lock.Lock()
room, ok = this.rooms[rid]
delete(this.rooms, rid)
this.lock.Unlock()
if ok {
go this.module.gameInvite.GameInviteEnd(4, room.data.Red.Info.Uid)
}
}