优化捕羊大赛匹配程序
This commit is contained in:
parent
75c234a274
commit
f75130e947
@ -7,6 +7,7 @@ import (
|
|||||||
"go_dreamfactory/pb"
|
"go_dreamfactory/pb"
|
||||||
"go_dreamfactory/sys/db"
|
"go_dreamfactory/sys/db"
|
||||||
"sync"
|
"sync"
|
||||||
|
"sync/atomic"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"go_dreamfactory/lego/base"
|
"go_dreamfactory/lego/base"
|
||||||
@ -29,8 +30,9 @@ type ParkourComp struct {
|
|||||||
refresh time.Time //上一次刷新时间
|
refresh time.Time //上一次刷新时间
|
||||||
ulock sync.RWMutex
|
ulock sync.RWMutex
|
||||||
users []*pb.DBRaceMember //推荐用户信息
|
users []*pb.DBRaceMember //推荐用户信息
|
||||||
lock sync.RWMutex
|
tlock sync.RWMutex
|
||||||
teams map[string][]*pb.DBRaceMember
|
teams map[string][]*pb.DBRaceMember
|
||||||
|
timerlock int32
|
||||||
total int32
|
total int32
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -39,6 +41,7 @@ func (this *ParkourComp) Init(service core.IService, module core.IModule, comp c
|
|||||||
this.MCompConfigure.Init(service, module, comp, options)
|
this.MCompConfigure.Init(service, module, comp, options)
|
||||||
this.service = service.(base.IRPCXService)
|
this.service = service.(base.IRPCXService)
|
||||||
this.module = module.(*Timer)
|
this.module = module.(*Timer)
|
||||||
|
this.timerlock = 1
|
||||||
this.teams = make(map[string][]*pb.DBRaceMember)
|
this.teams = make(map[string][]*pb.DBRaceMember)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -50,7 +53,7 @@ func (this *ParkourComp) Start() (err error) {
|
|||||||
this.refreshlist()
|
this.refreshlist()
|
||||||
this.service.RegisterFunctionName(string(comm.RPC_ParkourJoinMatch), this.join)
|
this.service.RegisterFunctionName(string(comm.RPC_ParkourJoinMatch), this.join)
|
||||||
this.service.RegisterFunctionName(string(comm.RPC_ParkourCancelMatch), this.cancel)
|
this.service.RegisterFunctionName(string(comm.RPC_ParkourCancelMatch), this.cancel)
|
||||||
if _, err = cron.AddFunc("5 * * * * ?", this.timer); err != nil {
|
if _, err = cron.AddFunc("*/10 * * * * ?", this.match); err != nil {
|
||||||
this.module.Errorf("cron.AddFunc err:%v", err)
|
this.module.Errorf("cron.AddFunc err:%v", err)
|
||||||
}
|
}
|
||||||
if _, err = cron.AddFunc("1 1 * * * ?", this.refreshlist); err != nil {
|
if _, err = cron.AddFunc("1 1 * * * ?", this.refreshlist); err != nil {
|
||||||
@ -102,110 +105,38 @@ func (this *ParkourComp) refreshlist() {
|
|||||||
|
|
||||||
//加入匹配中
|
//加入匹配中
|
||||||
func (this *ParkourComp) join(ctx context.Context, req *pb.RPCParkourJoinMatchReq, resp *pb.RPCParkourJoinMatchResp) (err error) {
|
func (this *ParkourComp) join(ctx context.Context, req *pb.RPCParkourJoinMatchReq, resp *pb.RPCParkourJoinMatchResp) (err error) {
|
||||||
this.lock.Lock()
|
this.tlock.Lock()
|
||||||
defer this.lock.Unlock()
|
|
||||||
this.teams[req.Captainid] = req.Member
|
this.teams[req.Captainid] = req.Member
|
||||||
this.total += int32(len(req.Member))
|
this.total += int32(len(req.Member))
|
||||||
if this.total >= 6 {
|
this.tlock.Unlock()
|
||||||
var (
|
if this.total >= 6 && atomic.LoadInt32(&this.timerlock) == 1 {
|
||||||
teams1 []string = make([]string, 0)
|
this.match()
|
||||||
teams2 []string = make([]string, 0)
|
|
||||||
teams3 []string = make([]string, 0)
|
|
||||||
red []string = make([]string, 0)
|
|
||||||
reduser []*pb.DBRaceMember = make([]*pb.DBRaceMember, 0)
|
|
||||||
bule []string = make([]string, 0)
|
|
||||||
buleuser []*pb.DBRaceMember = make([]*pb.DBRaceMember, 0)
|
|
||||||
)
|
|
||||||
for k, v := range this.teams {
|
|
||||||
if len(v) == 1 {
|
|
||||||
teams1 = append(teams1, k)
|
|
||||||
} else if len(v) == 2 {
|
|
||||||
teams2 = append(teams2, k)
|
|
||||||
} else {
|
|
||||||
teams3 = append(teams3, k)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//找红队
|
|
||||||
if len(teams3) > 0 {
|
|
||||||
red = append(red, teams3[0])
|
|
||||||
teams3 = teams3[1:]
|
|
||||||
} else if len(teams2) > 0 && len(teams1) > 0 {
|
|
||||||
red = append(red, teams2[0], teams1[0])
|
|
||||||
teams2 = teams2[1:]
|
|
||||||
teams1 = teams1[1:]
|
|
||||||
} else if len(teams1) >= 3 {
|
|
||||||
red = append(red, teams1[0], teams1[1], teams1[2])
|
|
||||||
teams1 = teams1[3:]
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(red) == 0 {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
//找蓝队
|
|
||||||
if len(teams3) > 0 {
|
|
||||||
bule = append(bule, teams3[0])
|
|
||||||
teams3 = teams3[1:]
|
|
||||||
} else if len(teams2) > 0 && len(teams1) > 0 {
|
|
||||||
bule = append(bule, teams2[0], teams1[0])
|
|
||||||
teams2 = teams2[1:]
|
|
||||||
teams1 = teams1[1:]
|
|
||||||
} else if len(teams1) >= 3 {
|
|
||||||
bule = append(bule, teams1[0], teams1[1], teams1[2])
|
|
||||||
teams1 = teams1[3:]
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(bule) == 0 {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
for _, v := range red {
|
|
||||||
reduser = append(reduser, this.teams[v]...)
|
|
||||||
}
|
|
||||||
for _, v := range bule {
|
|
||||||
buleuser = append(reduser, this.teams[v]...)
|
|
||||||
}
|
|
||||||
|
|
||||||
ctx, _ := context.WithTimeout(context.Background(), time.Second*2)
|
|
||||||
if err = this.service.RpcCall(ctx,
|
|
||||||
comm.Service_Worker,
|
|
||||||
string(comm.RPC_ParkourMatchSucc),
|
|
||||||
&pb.RPCParkourMatchSuccReq{Red: reduser, Bule: buleuser},
|
|
||||||
&pb.RPCParkourMatchSuccResp{},
|
|
||||||
); err != nil {
|
|
||||||
this.module.Errorln(err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, v := range red {
|
|
||||||
delete(this.teams, v)
|
|
||||||
}
|
|
||||||
for _, v := range bule {
|
|
||||||
delete(this.teams, v)
|
|
||||||
}
|
|
||||||
this.total -= 6
|
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
//加入匹配中
|
//加入匹配中
|
||||||
func (this *ParkourComp) cancel(ctx context.Context, req *pb.RPCParkourCancelMatchReq, resp *pb.RPCParkourCancelMatchResp) (err error) {
|
func (this *ParkourComp) cancel(ctx context.Context, req *pb.RPCParkourCancelMatchReq, resp *pb.RPCParkourCancelMatchResp) (err error) {
|
||||||
this.lock.Lock()
|
this.tlock.Lock()
|
||||||
delete(this.teams, req.Captainid)
|
delete(this.teams, req.Captainid)
|
||||||
this.lock.Unlock()
|
this.tlock.Unlock()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
//定时匹配处理
|
//定时匹配处理
|
||||||
func (this *ParkourComp) timer() {
|
func (this *ParkourComp) match() {
|
||||||
this.module.Errorf("捕羊大赛 定时匹配,%d", this.total)
|
// this.module.Debug("执行一次匹配!")
|
||||||
|
if !atomic.CompareAndSwapInt32(&this.timerlock, 1, 2) { //正在执行,就不要在进来了
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
defer atomic.StoreInt32(&this.timerlock, 1) //执行完毕释放
|
||||||
|
|
||||||
|
// this.module.Errorf("捕羊大赛 定时匹配,%d", this.total)
|
||||||
if this.total == 0 {
|
if this.total == 0 {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
this.ulock.RLock()
|
|
||||||
this.ulock.RUnlock()
|
|
||||||
this.lock.Lock()
|
|
||||||
defer this.lock.Unlock()
|
|
||||||
var (
|
var (
|
||||||
ok bool
|
ok bool
|
||||||
err error
|
err error
|
||||||
@ -222,6 +153,7 @@ func (this *ParkourComp) timer() {
|
|||||||
buleuser []*pb.DBRaceMember = make([]*pb.DBRaceMember, 0)
|
buleuser []*pb.DBRaceMember = make([]*pb.DBRaceMember, 0)
|
||||||
order bool = false
|
order bool = false
|
||||||
)
|
)
|
||||||
|
this.tlock.Lock()
|
||||||
for k, v := range this.teams {
|
for k, v := range this.teams {
|
||||||
if len(v) == 1 {
|
if len(v) == 1 {
|
||||||
teams1 = append(teams1, k)
|
teams1 = append(teams1, k)
|
||||||
@ -231,7 +163,9 @@ func (this *ParkourComp) timer() {
|
|||||||
teams3 = append(teams3, k)
|
teams3 = append(teams3, k)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
this.ulock.Lock()
|
this.tlock.Unlock()
|
||||||
|
|
||||||
|
this.ulock.RLock()
|
||||||
users = make([]*pb.DBRaceMember, 0, len(this.users))
|
users = make([]*pb.DBRaceMember, 0, len(this.users))
|
||||||
for _, v1 := range this.users {
|
for _, v1 := range this.users {
|
||||||
ok = false
|
ok = false
|
||||||
@ -246,7 +180,7 @@ func (this *ParkourComp) timer() {
|
|||||||
users = append(users, v1)
|
users = append(users, v1)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
this.ulock.Unlock()
|
this.ulock.RUnlock()
|
||||||
|
|
||||||
if len(users)+int(this.total) < 6 { //人员不足
|
if len(users)+int(this.total) < 6 { //人员不足
|
||||||
return
|
return
|
||||||
@ -336,12 +270,14 @@ func (this *ParkourComp) timer() {
|
|||||||
fn() //做多执行三次即可
|
fn() //做多执行三次即可
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
this.tlock.RLock()
|
||||||
for _, v := range red {
|
for _, v := range red {
|
||||||
reduser = append(reduser, this.teams[v]...)
|
reduser = append(reduser, this.teams[v]...)
|
||||||
}
|
}
|
||||||
for _, v := range bule {
|
for _, v := range bule {
|
||||||
buleuser = append(buleuser, this.teams[v]...)
|
buleuser = append(buleuser, this.teams[v]...)
|
||||||
}
|
}
|
||||||
|
this.tlock.RLock()
|
||||||
|
|
||||||
if len(users)+rednum+bulenum < 6 {
|
if len(users)+rednum+bulenum < 6 {
|
||||||
return
|
return
|
||||||
@ -393,7 +329,7 @@ func (this *ParkourComp) timer() {
|
|||||||
this.module.Errorln(err)
|
this.module.Errorln(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
this.tlock.Lock()
|
||||||
for _, v := range red {
|
for _, v := range red {
|
||||||
delete(this.teams, v)
|
delete(this.teams, v)
|
||||||
}
|
}
|
||||||
@ -402,6 +338,7 @@ func (this *ParkourComp) timer() {
|
|||||||
}
|
}
|
||||||
this.total -= int32(rednum)
|
this.total -= int32(rednum)
|
||||||
this.total -= int32(bulenum)
|
this.total -= int32(bulenum)
|
||||||
|
this.tlock.Lock()
|
||||||
}
|
}
|
||||||
|
|
||||||
// 从远程库查询用户
|
// 从远程库查询用户
|
||||||
|
@ -20,9 +20,9 @@ const (
|
|||||||
|
|
||||||
///配置管理基础组件
|
///配置管理基础组件
|
||||||
type configureComp struct {
|
type configureComp struct {
|
||||||
hlock sync.RWMutex
|
|
||||||
modules.MCompConfigure
|
modules.MCompConfigure
|
||||||
module *User
|
module *User
|
||||||
|
hlock sync.RWMutex
|
||||||
_sign map[int32]*cfg.GameSignData
|
_sign map[int32]*cfg.GameSignData
|
||||||
_signExtra map[int32]*cfg.GameSignExtraData
|
_signExtra map[int32]*cfg.GameSignExtraData
|
||||||
}
|
}
|
||||||
@ -30,7 +30,7 @@ type configureComp struct {
|
|||||||
//组件初始化接口
|
//组件初始化接口
|
||||||
func (this *configureComp) Init(service core.IService, module core.IModule, comp core.IModuleComp, options core.IModuleOptions) (err error) {
|
func (this *configureComp) Init(service core.IService, module core.IModule, comp core.IModuleComp, options core.IModuleOptions) (err error) {
|
||||||
err = this.MCompConfigure.Init(service, module, comp, options)
|
err = this.MCompConfigure.Init(service, module, comp, options)
|
||||||
module = module.(*User)
|
this.module = module.(*User)
|
||||||
err = this.LoadConfigure(game_initial, cfg.NewGameInitial)
|
err = this.LoadConfigure(game_initial, cfg.NewGameInitial)
|
||||||
this._sign = make(map[int32]*cfg.GameSignData, 0)
|
this._sign = make(map[int32]*cfg.GameSignData, 0)
|
||||||
configure.RegisterConfigure(game_sign, cfg.NewGameSign, this.LoadSignData)
|
configure.RegisterConfigure(game_sign, cfg.NewGameSign, this.LoadSignData)
|
||||||
|
Loading…
Reference in New Issue
Block a user