go_dreamfactory/modules/robot/modulerobot_wtask.go
2024-01-09 18:14:05 +08:00

346 lines
10 KiB
Go

package robot
import (
"errors"
"fmt"
"go_dreamfactory/comm"
"go_dreamfactory/lego/core"
"go_dreamfactory/lego/sys/log"
"go_dreamfactory/pb"
"go_dreamfactory/sys/configure"
cfg "go_dreamfactory/sys/configure/structs"
"time"
"google.golang.org/protobuf/proto"
)
//用户模块 机器人
type ModuleRobot_WTask struct {
info *pb.DBWTask
progress []*pb.DBWTaskItem //任务进度
state int32
change chan bool
}
func (this *ModuleRobot_WTask) Init() (err error) {
this.change = make(chan bool)
return
}
//接收到消息
func (this *ModuleRobot_WTask) Receive(robot IRobot, stype string, message proto.Message) (err error) {
// log.Debug("[机器人 WTask]", log.Field{Key: "Account", Value: robot.Account()}, log.Field{Key: "message", Value: message})
switch stype {
case "info":
resp := message.(*pb.WTaskInfoResp)
this.info = resp.Info
// this.progress = resp.Accepts
break
case "accept":
resp := message.(*pb.WTaskAcceptResp)
// this.progress = resp.Accepts
if this.info != nil {
this.info.Activations = resp.Activations
this.info.Accepts = make([]int32, 0)
for _, v := range this.progress {
this.info.Accepts = append(this.info.Accepts, v.Tid)
}
}
break
case "completecondi":
resp := message.(*pb.WTaskCompleteCondiResp)
for i, v := range this.progress {
if v.Tid == resp.TaskId {
this.progress[i] = resp.Progress
break
}
}
for _, v := range this.progress {
if v.Tid == resp.Progress.Tid {
v.Conlds = resp.Progress.Conlds
return
}
}
this.progress = append(this.progress, resp.Progress)
break
case "finish":
resp := message.(*pb.WTaskFinishResp)
if this.info != nil && this.info.Accepts != nil {
for i, v := range this.info.Accepts {
if v == resp.Tid {
this.info.Accepts = append(this.info.Accepts[0:i], this.info.Accepts[i+1:]...)
break
}
}
}
break
case "activationschange":
resp := message.(*pb.WTaskActivationsChangePush)
if this.info != nil {
this.info.Activations = resp.Activations
}
break
// case "acceptchange":
// resp := message.(*pb.WTaskAcceptChangePush)
// // log.Debug("[机器人 WTask-AcceptChange]", log.Field{Key: "Account", Value: robot.Account()}, log.Field{Key: "Resp", Value: resp.String()})
// this.progress = resp.Accepts
// if this.info != nil {
// this.info.Accepts = make([]int32, 0)
// for _, v := range this.progress {
// this.info.Accepts = append(this.info.Accepts, v.Tid)
// }
// }
// break
case "boxchange":
resp := message.(*pb.WTaskBoxChangePush)
this.info.Boxs = resp.Boxs
break
}
return
}
func (this *ModuleRobot_WTask) OncePipeline(robot IRobot) (err error) {
var (
errdata *pb.ErrorData
)
//获取任务列表
if _, errdata = robot.SendMessage("wtask", "info", &pb.WTaskInfoReq{}); errdata != nil {
err = errors.New(fmt.Sprintf("code:%d message:%s", errdata.Code, errdata.Message))
return
}
return
}
//机器人执行流
func (this *ModuleRobot_WTask) DoPipeline(robot IRobot) (err error) {
var (
errdata *pb.ErrorData
tconf *cfg.GameWorldTaskData
cconf *cfg.GameBuriedCondiData
module core.M_Modules
)
locp:
for {
//寻找可做任务
tconf = this.findtask()
if tconf == nil {
break locp
}
if this.checkaccept(tconf) { //是否需要接收任务
if _, errdata = robot.SendMessage("wtask", "accept", &pb.WTaskAcceptReq{Tid: tconf.Key}); errdata != nil {
err = errors.New(fmt.Sprintf("code:%d message:%s", errdata.Code, errdata.Message))
return
}
continue
}
//找到能做的任务
cconf, err = this.findconlds(tconf)
if err != nil {
break locp
}
//有问完成的子任务
if cconf != nil {
switch comm.TaskType(cconf.Type) {
case comm.Rtype1, comm.Rtype14:
module = comm.ModuleHero
case comm.Rtype20001, comm.Rtype70, comm.Rtype205, comm.Rtype227:
module = comm.ModuleWtask
case comm.Rtype149, comm.Rtype152:
module = comm.ModulePractice
case comm.Rtype61:
module = comm.ModuleMainline
case comm.Rtype5, comm.Rtype41, comm.Rtype43, comm.Rtype92, comm.Rtype94, comm.Rtype95, comm.Rtype96, comm.Rtype169:
module = comm.ModuleEquipment
case comm.Rtype128, comm.Rtype129, comm.Rtype131:
module = comm.ModuleArena
case comm.Rtype168:
module = comm.ModulePagoda
case comm.Rtype73:
module = comm.ModuleViking
case comm.Rtype199:
module = comm.ModuleHero
case comm.Rtype62:
module = comm.ModuleChat
case comm.Rtype104:
module = comm.ModuleShop
default:
err = fmt.Errorf("[Robot DoTask] ctype:%d conld:%d Not Achieved !", cconf.Type, cconf.Id)
// log.Error("[Robot DoTask]", log.Field{Key: "ctype", Value: cconf.Type}, log.Field{Key: "conld", Value: cconf.Id}, log.Field{Key: "err", Value: "Not Achieved !"})
break locp
}
if err = robot.DoTask(tconf, cconf, module); err != nil {
// log.Error("[Robot DoTask]", log.Field{Key: "task", Value: tconf.Key}, log.Field{Key: "conld", Value: cconf.Id}, log.Field{Key: "err", Value: err.Error()})
break locp
}
time.Sleep(time.Second * 2)
continue
} else { //任务已完成直接完成
if _, errdata = robot.SendMessage("wtask", "finish", &pb.WTaskFinishReq{Tid: tconf.Key}); errdata != nil {
err = errors.New(fmt.Sprintf("code:%d message:%s", errdata.Code, errdata.Message))
return
}
}
}
return
}
//做任务
func (this *ModuleRobot_WTask) DoTask(robot IRobot, taskconf *cfg.GameWorldTaskData, condconf *cfg.GameBuriedCondiData) (err error) {
var (
errdata *pb.ErrorData
)
switch comm.TaskType(condconf.Type) {
case comm.Rtype20001: //完成对话
if _, errdata = robot.SendTaskMessage(taskconf.Key, condconf.Id, "wtask", "completecondi", &pb.WTaskCompleteCondiReq{TaskId: taskconf.Key, CondiId: condconf.Id}); errdata != nil {
err = errors.New(fmt.Sprintf("code:%d message:%s", errdata.Code, errdata.Message))
return
}
break
case comm.Rtype70: //完成世界任务战斗
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, "wtask", "battlestart", &pb.WTaskBattleStartReq{BattleConfId: condconf.Filter[0], 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, "wtask", "battlefinish", &pb.WTaskBattleFinishReq{BattleConfId: condconf.Filter[0], Report: &pb.BattleReport{
Info: resp.(*pb.WTaskBattleStartResp).Info,
WinSide: 1,
}}); errdata != nil {
err = errors.New(fmt.Sprintf("code:%d message:%s", errdata.Code, errdata.Message))
return
}
break
case comm.Rtype227: //完成世界任务战斗 失败
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, "wtask", "battlestart", &pb.WTaskBattleStartReq{BattleConfId: condconf.Filter[0], 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, "wtask", "battlefinish", &pb.WTaskBattleFinishReq{BattleConfId: condconf.Filter[0], Report: &pb.BattleReport{
Info: resp.(*pb.WTaskBattleStartResp).Info,
WinSide: 2,
}}); errdata != nil {
err = errors.New(fmt.Sprintf("code:%d message:%s", errdata.Code, errdata.Message))
return
}
break
case comm.Rtype205: //拾取宝箱
boxid := int32(0)
for k, v := range this.info.Boxs[taskconf.Key].Boxs {
if v == 0 {
boxid = k
break
}
}
if _, errdata = robot.SendTaskMessage(taskconf.Key, condconf.Id, "wtask", "boxreceive", &pb.WTaskBoxReceiveReq{
Tid: taskconf.Key,
Boxid: boxid,
}); errdata != nil {
err = errors.New(fmt.Sprintf("code:%d message:%s", errdata.Code, errdata.Message))
return
}
break
}
return
}
func (this *ModuleRobot_WTask) findtask() (conf *cfg.GameWorldTaskData) {
var (
confs []*cfg.GameWorldTaskData
err error
)
for _, v := range this.info.Accepts {
if conf, err = this.getGameWorldTaskData(v); err != nil {
log.Error("[Robot 世界任务配置未找到]", log.Field{Key: "task", Value: v}, log.Field{Key: "err", Value: err.Error()})
}
if conf.Des == 2 { //优先找到主线任务
return
}
confs = append(confs, conf)
}
for _, v := range this.info.Activations { //可接取任务列表
if conf, err = this.getGameWorldTaskData(v); err != nil {
log.Error("[Robot 世界任务配置未找到]", log.Field{Key: "task", Value: v}, log.Field{Key: "err", Value: err.Error()})
return
}
if conf.Des == 2 { //优先找到主线任务
return
}
}
return
}
func (this *ModuleRobot_WTask) findconlds(conf *cfg.GameWorldTaskData) (cconf *cfg.GameBuriedCondiData, err error) {
for _, tp := range this.progress {
if tp.Tid == conf.Key {
for _, v := range tp.Conlds {
if v.State == pb.BuriedItemFinishState_buried_unfinish { //未完成
if cconf, err = this.getburiedcondidata(v.Conid); err != nil {
log.Error("[Robot 埋点任务配置未找到]", log.Field{Key: "conld", Value: v.Conid}, log.Field{Key: "err", Value: err.Error()})
return
}
return
}
}
}
}
return
}
func (this *ModuleRobot_WTask) checkaccept(conf *cfg.GameWorldTaskData) bool {
for _, v := range this.info.Accepts {
if v == conf.Key {
return false
}
}
return true
}
//获取任务配置
func (this *ModuleRobot_WTask) getGameWorldTaskData(tid int32) (conf *cfg.GameWorldTaskData, err error) {
var (
v interface{}
ok bool
)
if v, err = configure.GetConfigure(gameWorldTask); err != nil {
return
} else {
if conf, ok = v.(*cfg.GameWorldTask).GetDataMap()[tid]; !ok {
err = comm.NewNotFoundConfErr("robot", gameWorldTask, tid)
return
}
}
return
}
// 读取条件任务id配置
func (this *ModuleRobot_WTask) getburiedcondidata(cid int32) (conf *cfg.GameBuriedCondiData, err error) {
var (
v interface{}
ok bool
)
if v, err = configure.GetConfigure(game_buriedcondi); err != nil {
return
} else {
if conf, ok = v.(*cfg.GameBuriedCondi).GetDataMap()[cid]; !ok {
err = comm.NewNotFoundConfErr("robot", game_buriedcondi, cid)
return
}
}
return
}