package parkour import ( "context" "fmt" "go_dreamfactory/comm" "go_dreamfactory/lego/base" "go_dreamfactory/lego/core" "go_dreamfactory/lego/sys/log" "go_dreamfactory/modules" "go_dreamfactory/pb" "sync" "go.mongodb.org/mongo-driver/bson/primitive" ) /* 模块名:跑酷系统 描述:捕羊大赛 开发:李伟 */ func NewModule() core.IModule { m := new(Parkour) return m } type Parkour struct { modules.ModuleBase service base.IRPCXService api *apiComp configure *configureComp teamComp *ModelTeamComp parkourComp *ModelParkourComp raceComp *ModelRaceComp lock sync.RWMutex battles map[string]*RaceItem } //模块名 func (this *Parkour) GetType() core.M_Modules { return comm.ModuleParkour } //模块初始化 func (this *Parkour) Init(service core.IService, module core.IModule, options core.IModuleOptions) (err error) { err = this.ModuleBase.Init(service, module, options) this.service = service.(base.IRPCXService) this.battles = map[string]*RaceItem{} return } func (this *Parkour) Start() (err error) { err = this.ModuleBase.Start() this.service.RegisterFunctionName(string(comm.RPC_ParkourMatchSucc), this.createbattle) return } //装备组件 func (this *Parkour) OnInstallComp() { this.ModuleBase.OnInstallComp() this.api = this.RegisterComp(new(apiComp)).(*apiComp) this.configure = this.RegisterComp(new(configureComp)).(*configureComp) this.teamComp = this.RegisterComp(new(ModelTeamComp)).(*ModelTeamComp) this.parkourComp = this.RegisterComp(new(ModelParkourComp)).(*ModelParkourComp) } //添加坐骑资源 func (this *Parkour) AddMounts(session comm.IUserSession, mounts map[string]int32, bPush bool) (code pb.ErrorCode) { var ( result *pb.DBParkour err error ) if result, err = this.parkourComp.addUserMounts(session.GetUserId(), mounts); err != nil { code = pb.ErrorCode_DBError return } if bPush { session.SendMsg(string(this.GetType()), "infochange", &pb.ParkourInfoChangePush{Info: result}) } return } //匹配 func (this *Parkour) match(team *pb.DBParkour) (err error) { err = this.service.RpcCall( context.Background(), comm.Service_Mainte, string(comm.RPC_ParkourJoinMatch), &pb.RPCParkourJoinMatchReq{Captainid: team.Captainid, Member: team.Member}, &pb.RPCParkourJoinMatchResp{}) if err != nil { this.Errorln(err) return } return } //匹配成功 创建战斗 func (this *Parkour) createbattle(ctx context.Context, req *pb.RPCParkourMatchSuccReq, resp *pb.RPCParkourMatchSuccResp) (err error) { var ( race *pb.DBRace battle *RaceItem sessions []comm.IUserSession = make([]comm.IUserSession, 0) teams []*pb.DBParkour users []string = make([]string, 0) ) this.Debug("createbattle", log.Field{Key: "req", Value: req.String()}) race = &pb.DBRace{ Id: primitive.NewObjectID().Hex(), ServicePath: fmt.Sprintf("%s/%s", this.service.GetType(), this.service.GetId()), Redmember: req.Red, Bulemember: req.Bule, } battle = &RaceItem{ Id: race.Id, RedSession: make([]comm.IUserSession, 0), BuleSession: make([]comm.IUserSession, 0), } for _, v := range req.Red { users = append(users, v.Uid) session, online := this.GetUserSession(v.Uid) v.Isoff = !online if online { battle.RedSession = append(battle.RedSession, session) } } battle.RedMember = req.Red for _, v := range req.Bule { users = append(users, v.Uid) session, online := this.GetUserSession(v.Uid) v.Isoff = !online if online { battle.BuleSession = append(battle.BuleSession, session) } } battle.BuleMember = req.Bule if teams, err = this.teamComp.queryteams(users); err != nil { return } for _, v := range teams { v.State = pb.RaceTeamState_raceing } if err = this.raceComp.addrace(race); err != nil { return } this.lock.Lock() this.battles[race.Id] = battle this.lock.Unlock() if err = this.SendMsgToSession(string(comm.ModulePvp), "racematchsucc", &pb.ParkourRaceMatchSuccPush{ Race: race, }, sessions...); err != nil { this.Errorln(err) } return } func (this *Parkour) startbattle(id string) { this.Debug("startbattle", log.Field{Key: "id", Value: id}) var ( battle *RaceItem ok bool sessions []comm.IUserSession = make([]comm.IUserSession, 0) err error ) this.lock.RLock() battle, ok = this.battles[id] this.lock.RUnlock() if ok { sessions = append(sessions, battle.RedSession...) sessions = append(sessions, battle.BuleSession...) if err = this.SendMsgToSession(string(comm.ModulePvp), "finish", &pb.ParkourRaceStartPush{ Countdown: 3, }, sessions...); err != nil { this.Errorln(err) } } } func (this *Parkour) shot(id string, uid string) { this.Debug("shot", log.Field{Key: "id", Value: id}) var ( battle *RaceItem ok bool sessions []comm.IUserSession = make([]comm.IUserSession, 0) err error ) this.lock.RLock() battle, ok = this.battles[id] this.lock.RUnlock() if ok { sessions = append(sessions, battle.RedSession...) sessions = append(sessions, battle.BuleSession...) if err = this.SendMsgToSession(string(comm.ModulePvp), "finish", &pb.ParkourRaceStartPush{ Countdown: 3, }, sessions...); err != nil { this.Errorln(err) } } } func (this *Parkour) avoid(id string, uid string, dis int32) { this.Debug("shot", log.Field{Key: "id", Value: id}) var ( battle *RaceItem ok bool sessions []comm.IUserSession = make([]comm.IUserSession, 0) err error ) this.lock.RLock() battle, ok = this.battles[id] this.lock.RUnlock() if ok { sessions = append(sessions, battle.RedSession...) sessions = append(sessions, battle.BuleSession...) if err = this.SendMsgToSession(string(comm.ModulePvp), "finish", &pb.ParkourRaceStartPush{ Countdown: 3, }, sessions...); err != nil { this.Errorln(err) } } }