package timer import ( "context" "go_dreamfactory/comm" "go_dreamfactory/modules" "go_dreamfactory/pb" "go_dreamfactory/sys/db" "sync" "time" "go_dreamfactory/lego/base" "go_dreamfactory/lego/core" "go_dreamfactory/lego/sys/cron" ) /* 捕羊大赛 维护服务 */ type ParkourComp struct { modules.MCompConfigure service base.IRPCXService module *Timer lock sync.RWMutex teams map[string][]*pb.DBRaceMember total int32 } //组件初始化接口 func (this *ParkourComp) Init(service core.IService, module core.IModule, comp core.IModuleComp, options core.IModuleOptions) (err error) { this.MCompConfigure.Init(service, module, comp, options) this.service = service.(base.IRPCXService) this.module = module.(*Timer) this.teams = make(map[string][]*pb.DBRaceMember) return } //自由跨服环境下开启此功能 func (this *ParkourComp) Start() (err error) { err = this.MCompConfigure.Start() if db.IsCross() { this.service.RegisterFunctionName(string(comm.RPC_ParkourJoinMatch), this.join) if _, err = cron.AddFunc("0 59 23 ? * SAT", this.timer); err != nil { this.module.Errorf("cron.AddFunc err:%v", err) } } return } //加入匹配中 func (this *ParkourComp) join(ctx context.Context, req *pb.RPCParkourJoinMatchReq, resp *pb.RPCParkourJoinMatchResp) (err error) { this.lock.Lock() defer this.lock.Unlock() this.teams[req.Captainid] = req.Member this.total += int32(len(req.Member)) if this.total >= 6 { var ( teams1 []string = make([]string, 0) 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 } //定时匹配处理 func (this *ParkourComp) timer() { }