package robot import ( "errors" "fmt" "go_dreamfactory/comm" "go_dreamfactory/pb" "go_dreamfactory/sys/configure" cfg "go_dreamfactory/sys/configure/structs" "google.golang.org/protobuf/proto" ) //用户模块 机器人 type ModuleRobot_MainLine struct { info *pb.DBMainline group map[int32][]*cfg.GameMainStageData } func (this *ModuleRobot_MainLine) Init() (err error) { var ( confs []*cfg.GameMainStageData ok bool ) this.group = make(map[int32][]*cfg.GameMainStageData) if confs, err = this.getGameMainStageDatas(); err != nil { return } for _, v := range confs { if _, ok = this.group[v.GroupId]; !ok { this.group[v.GroupId] = make([]*cfg.GameMainStageData, 0) } this.group[v.GroupId] = append(this.group[v.GroupId], v) } return } //接收到消息 func (this *ModuleRobot_MainLine) Receive(robot IRobot, stype string, message proto.Message) (err error) { switch stype { case "info": resp := message.(*pb.MainlineInfoResp) this.info = resp.Info if this.info.Level == nil { this.info.Level = make(map[int32]int32) } break case "challengeover": resp := message.(*pb.MainlineChallengeOverResp) this.info.Level[resp.Level] = resp.Star break case "levelpass": resp := message.(*pb.MainlineLevelPassResp) this.info.Level[resp.Level] = resp.Star break } return } func (this *ModuleRobot_MainLine) OncePipeline(robot IRobot) (err error) { var ( errdata *pb.ErrorData ) if _, errdata = robot.SendMessage("mainline", "info", &pb.MainlineInfoReq{}); errdata != nil { err = errors.New(fmt.Sprintf("code:%d message:%s", errdata.Code, errdata.Message)) return } return } //机器人执行流 func (this *ModuleRobot_MainLine) DoPipeline(robot IRobot) (err error) { var ( errdata *pb.ErrorData heromodule *ModuleRobot_Hero conf *cfg.GameMainStageData heros []string resp proto.Message ) if conf, err = this.findcanpasslevel(); err != nil { //找到目标管卡 return } switch conf.Episodetype { case 1, 4, 8: heromodule = robot.GetModule(comm.ModuleHero).(*ModuleRobot_Hero) heros = heromodule.getbattlehero() if resp, errdata = robot.SendMessage("mainline", "challenge", &pb.MainlineChallengeReq{Level: conf.Id, Battle: &pb.BattleFormation{ Format: heros, }}); errdata != nil { err = errors.New(fmt.Sprintf("code:%d message:%s", errdata.Code, errdata.Message)) return } if _, errdata = robot.SendMessage("mainline", "challengeover", &pb.MainlineChallengeOverReq{Level: conf.Id, Report: &pb.BattleReport{ Info: resp.(*pb.MainlineChallengeResp).Info, WinSide: 1, }}); errdata != nil { err = errors.New(fmt.Sprintf("code:%d message:%s", errdata.Code, errdata.Message)) return } break case 0, 2, 3, 5, 6, 7: if _, errdata = robot.SendMessage("mainline", "levelpass", &pb.MainlineLevelPassReq{Level: conf.Id}); errdata != nil { err = errors.New(fmt.Sprintf("code:%d message:%s", errdata.Code, errdata.Message)) return } default: err = fmt.Errorf("no fund mainline id:%d type:%d", conf.Id, conf.Episodetype) } this.info.Level[conf.Id] = 32 return } //做任务 func (this *ModuleRobot_MainLine) DoTask(robot IRobot, taskconf *cfg.GameWorldTaskData, condconf *cfg.GameBuriedCondiData) (err error) { var ( errdata *pb.ErrorData ) switch comm.TaskType(condconf.Type) { case comm.Rtype61: var ( conf *cfg.GameMainStageData ok bool ) if _, ok = this.info.Level[condconf.Filter[0]]; ok { //已通关 return } if conf, err = this.getGameMainStageData(condconf.Filter[0]); err != nil { return } if conf, err = this.findlevel(conf); err != nil { //找到目标管卡 return } switch conf.Episodetype { case 1, 4, 8: var ( heromodule *ModuleRobot_Hero heros []string resp proto.Message ) heromodule = robot.GetModule(comm.ModuleHero).(*ModuleRobot_Hero) heros = heromodule.getbattlehero() if resp, errdata = robot.SendTaskMessage(taskconf.Key, condconf.Id, "mainline", "challenge", &pb.MainlineChallengeReq{Level: conf.Id, Battle: &pb.BattleFormation{ Format: heros, }}); errdata != nil { err = errors.New(fmt.Sprintf("code:%d message:%s", errdata.Code, errdata.Message)) return } if _, errdata = robot.SendTaskMessage(taskconf.Key, condconf.Id, "mainline", "challengeover", &pb.MainlineChallengeOverReq{Level: conf.Id, Report: &pb.BattleReport{ Info: resp.(*pb.MainlineChallengeResp).Info, WinSide: 1, }}); errdata != nil { err = errors.New(fmt.Sprintf("code:%d message:%s", errdata.Code, errdata.Message)) return } break case 3, 7: if _, errdata = robot.SendTaskMessage(taskconf.Key, condconf.Id, "mainline", "levelpass", &pb.MainlineLevelPassReq{Level: conf.Id}); errdata != nil { err = errors.New(fmt.Sprintf("code:%d message:%s", errdata.Code, errdata.Message)) return } default: err = fmt.Errorf("no fund mainline id:%d type:%d", conf.Id, conf.Episodetype) } } return } func (this *ModuleRobot_MainLine) findlevel(conf *cfg.GameMainStageData) (rconf *cfg.GameMainStageData, err error) { var ( confs []*cfg.GameMainStageData nconf *cfg.GameMainStageData ok bool ) //前置组判断 for _, v := range conf.PreviousGroupId { confs = this.group[v] for _, nconf := range confs { if _, ok = this.info.Level[nconf.Id]; !ok { return this.findlevel(nconf) } } } if conf.Previoustage != 0 { if _, ok = this.info.Level[conf.Previoustage]; !ok { if nconf, err = this.getGameMainStageData(conf.Previoustage); err != nil { return } return this.findlevel(nconf) } } //没找到前置了,直接放回 rconf = conf return } // 读取主线关卡表 func (this *ModuleRobot_MainLine) getGameMainStageData(id int32) (conf *cfg.GameMainStageData, err error) { var ( v interface{} ok bool ) if v, err = configure.GetConfigure(game_mainstage); err != nil { return } else { if conf, ok = v.(*cfg.GameMainStage).GetDataMap()[id]; !ok { err = comm.NewNotFoundConfErr("robot", game_mainstage, id) return } } return } // 读取主线关卡表 func (this *ModuleRobot_MainLine) getGameMainStageDatas() (confs []*cfg.GameMainStageData, err error) { var ( v interface{} ) if v, err = configure.GetConfigure(game_mainstage); err != nil { return } else { confs = v.(*cfg.GameMainStage).GetDataList() } return } func (this *ModuleRobot_MainLine) findcanpasslevel() (conf *cfg.GameMainStageData, err error) { var ( v interface{} confs []*cfg.GameMainStageData ok bool ) if v, err = configure.GetConfigure(game_mainstage); err != nil { return } else { for _, tempconf := range v.(*cfg.GameMainStage).GetDataList() { if _, ok = this.info.Level[tempconf.Id]; ok { //已通关 continue } ok = true //前置组判断 for _, v := range tempconf.PreviousGroupId { confs = this.group[v] for _, nconf := range confs { if _, ok = this.info.Level[nconf.Id]; !ok { ok = false break } } if !ok { break } } if !ok { continue } if tempconf.Previoustage != 0 { if _, ok = this.info.Level[tempconf.Previoustage]; !ok { continue } } conf = tempconf return } err = fmt.Errorf("no fund lv") } return }