上传捕羊匹配逻辑

This commit is contained in:
liwei1dao 2023-10-12 19:13:27 +08:00
parent e47d9cbc27
commit 1cbfd710e3
2 changed files with 56 additions and 52 deletions

View File

@ -162,7 +162,6 @@ func (this *ModelItemsComp) UpdateUserPack(uid string, itmes ...*pb.DB_UserItemD
}) })
} }
} }
return return
} }

View File

@ -20,6 +20,11 @@ import (
"go.mongodb.org/mongo-driver/mongo/options" "go.mongodb.org/mongo-driver/mongo/options"
) )
type ParkourTeam struct {
member []*pb.DBRaceMember
ais []*pb.DBRaceMember
}
/* /*
捕羊大赛 维护服务 捕羊大赛 维护服务
*/ */
@ -31,7 +36,7 @@ type ParkourComp struct {
ulock sync.RWMutex ulock sync.RWMutex
users []*pb.DBRaceMember //推荐用户信息 users []*pb.DBRaceMember //推荐用户信息
tlock sync.RWMutex tlock sync.RWMutex
teams map[string][]*pb.DBRaceMember teams map[string]*ParkourTeam
timerlock int32 timerlock int32
total int32 total int32
} }
@ -42,7 +47,7 @@ func (this *ParkourComp) Init(service core.IService, module core.IModule, comp c
this.service = service.(base.IRPCXService) this.service = service.(base.IRPCXService)
this.module = module.(*Timer) this.module = module.(*Timer)
this.timerlock = 1 this.timerlock = 1
this.teams = make(map[string][]*pb.DBRaceMember) this.teams = make(map[string]*ParkourTeam)
return return
} }
@ -112,7 +117,10 @@ 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.tlock.Lock() this.tlock.Lock()
this.teams[req.Captainid] = req.Member this.teams[req.Captainid] = &ParkourTeam{
member: req.Member,
ais: req.Ais,
}
this.total += int32(len(req.Member)) this.total += int32(len(req.Member))
this.tlock.Unlock() this.tlock.Unlock()
if this.total >= 6 && atomic.LoadInt32(&this.timerlock) == 1 { if this.total >= 6 && atomic.LoadInt32(&this.timerlock) == 1 {
@ -162,13 +170,15 @@ func (this *ParkourComp) match() {
bule []string = make([]string, 0) bule []string = make([]string, 0)
bulenum int = 0 bulenum int = 0
buleuser []*pb.DBRaceMember = make([]*pb.DBRaceMember, 0) buleuser []*pb.DBRaceMember = make([]*pb.DBRaceMember, 0)
aismap map[string]*pb.DBRaceMember = make(map[string]*pb.DBRaceMember)
ais []*pb.DBRaceMember = make([]*pb.DBRaceMember, 0)
order bool = false order bool = false
) )
this.tlock.Lock() this.tlock.Lock()
for k, v := range this.teams { for k, v := range this.teams {
if len(v) == 1 { if len(v.member) == 1 {
teams1 = append(teams1, k) teams1 = append(teams1, k)
} else if len(v) == 2 { } else if len(v.member) == 2 {
teams2 = append(teams2, k) teams2 = append(teams2, k)
} else { } else {
teams3 = append(teams3, k) teams3 = append(teams3, k)
@ -181,7 +191,7 @@ func (this *ParkourComp) match() {
for _, v1 := range this.users { for _, v1 := range this.users {
ok = false ok = false
for _, v := range this.teams { for _, v := range this.teams {
for _, v2 := range v { for _, v2 := range v.member {
if v1.Uid == v2.Uid { if v1.Uid == v2.Uid {
ok = true ok = true
} }
@ -193,9 +203,9 @@ func (this *ParkourComp) match() {
} }
this.ulock.RUnlock() this.ulock.RUnlock()
if len(users)+int(this.total) < 6 { //人员不足 // if len(users)+int(this.total) < 6 { //人员不足
return // return
} // }
var fn = func() { var fn = func() {
if order { if order {
@ -283,50 +293,45 @@ func (this *ParkourComp) match() {
} }
this.tlock.RLock() this.tlock.RLock()
for _, v := range red { for _, v := range red {
reduser = append(reduser, this.teams[v]...) reduser = append(reduser, this.teams[v].member...)
for _, ai := range this.teams[v].ais {
aismap[ai.Name] = ai
}
} }
for _, v := range bule { for _, v := range bule {
buleuser = append(buleuser, this.teams[v]...) buleuser = append(buleuser, this.teams[v].member...)
for _, ai := range this.teams[v].ais {
aismap[ai.Name] = ai
}
}
for _, v := range aismap {
ais = append(ais, v)
} }
this.tlock.RUnlock() this.tlock.RUnlock()
if len(users)+rednum+bulenum < 6 { if len(users)+rednum+bulenum < 6 {
return return
} }
n := len(reduser)
//补充人员 //补充人员
if len(reduser) < 3 { if len(reduser) < 3 {
n := len(reduser) if len(users) > 3-n {
reduser = append(reduser, users[0:(3-len(reduser))]...) reduser = append(reduser, users[0:(3-n)]...)
users = users[(3 - n):] users = users[(3 - n):]
for _, v := range reduser[rednum:3] {
if v.Name == "" { //同步用户数据
if user, err := this.getuser(v.Uid); err != nil {
this.module.Errorln(err)
return
} else { } else {
v.Name = user.Name reduser = append(reduser, ais[0:(3-n)]...)
v.Sex = user.Gender ais = ais[(3 - n):]
v.Lv = user.Lv
}
}
} }
} }
n = len(buleuser)
if len(buleuser) < 3 { if len(buleuser) < 3 {
n := len(buleuser) if len(users) > 3-n {
buleuser = append(buleuser, users[0:(3-len(buleuser))]...) buleuser = append(buleuser, users[0:(3-n)]...)
users = users[(3 - n):] users = users[(3 - n):]
for _, v := range buleuser[bulenum:3] {
if v.Name == "" { //同步用户数据
if user, err := this.getuser(v.Uid); err != nil {
this.module.Errorln(err)
return
} else { } else {
v.Name = user.Name buleuser = append(buleuser, ais[0:(3-n)]...)
v.Sex = user.Gender ais = ais[(3 - n):]
v.Lv = user.Lv
}
}
} }
} }