85 lines
1.7 KiB
Go
85 lines
1.7 KiB
Go
package matchpool
|
|
|
|
import (
|
|
"go_dreamfactory/lego/core"
|
|
"go_dreamfactory/lego/core/cbase"
|
|
"go_dreamfactory/lego/sys/cron"
|
|
"go_dreamfactory/pb"
|
|
"sync"
|
|
)
|
|
|
|
type poolsComp struct {
|
|
cbase.ModuleCompBase
|
|
service core.IService
|
|
module *MatchPool
|
|
lock sync.RWMutex
|
|
pools map[string]*MPool
|
|
}
|
|
|
|
func (this *poolsComp) Init(service core.IService, module core.IModule, comp core.IModuleComp, options core.IModuleOptions) (err error) {
|
|
this.ModuleCompBase.Init(service, module, comp, options)
|
|
this.module = module.(*MatchPool)
|
|
this.service = service
|
|
this.pools = make(map[string]*MPool)
|
|
return
|
|
}
|
|
|
|
func (this *poolsComp) Start() (err error) {
|
|
err = this.ModuleCompBase.Start()
|
|
if _, err = cron.AddFunc("*/1 * * * * ?", this.run); err != nil {
|
|
this.module.Errorf("cron.AddFunc err:%v", err)
|
|
}
|
|
return
|
|
}
|
|
|
|
func (this *poolsComp) joinPools(req *pb.JoinMatchPoolReq) {
|
|
var (
|
|
pool *MPool
|
|
ok bool
|
|
)
|
|
this.lock.RLock()
|
|
pool, ok = this.pools[req.Poolname]
|
|
this.lock.RUnlock()
|
|
if !ok {
|
|
pool = &MPool{
|
|
module: this.module,
|
|
Name: req.Poolname,
|
|
MatchNum: req.Matchnum,
|
|
Timeout: req.Timeout,
|
|
Players: make(map[string]*MatchPlayer),
|
|
State: 1,
|
|
}
|
|
this.lock.Lock()
|
|
this.pools[req.Poolname] = pool
|
|
this.lock.Unlock()
|
|
}
|
|
pool.join(req)
|
|
}
|
|
|
|
func (this *poolsComp) cancelMatch(req *pb.CancelMatchReq) {
|
|
var (
|
|
pool *MPool
|
|
ok bool
|
|
)
|
|
this.lock.RLock()
|
|
pool, ok = this.pools[req.Poolname]
|
|
this.lock.RUnlock()
|
|
if ok {
|
|
pool.cancel(req.Uid)
|
|
}
|
|
}
|
|
|
|
func (this *poolsComp) run() {
|
|
var (
|
|
pools []*MPool = make([]*MPool, 0)
|
|
)
|
|
this.lock.Lock()
|
|
for _, v := range this.pools {
|
|
pools = append(pools, v)
|
|
}
|
|
this.lock.Unlock()
|
|
for _, v := range pools {
|
|
go v.match(1)
|
|
}
|
|
}
|