go_dreamfactory/modules/robot/modulerobot_hero.go
2023-08-25 12:04:26 +08:00

103 lines
2.2 KiB
Go

package robot
import (
"errors"
"fmt"
"go_dreamfactory/comm"
"go_dreamfactory/pb"
cfg "go_dreamfactory/sys/configure/structs"
"sort"
"google.golang.org/protobuf/proto"
)
//用户模块 机器人
type ModuleRobot_Hero struct {
heros map[string]*pb.DBHero
}
func (this *ModuleRobot_Hero) Init() (err error) {
this.heros = make(map[string]*pb.DBHero)
return
}
//接收到消息
func (this *ModuleRobot_Hero) Receive(robot IRobot, stype string, message proto.Message) (err error) {
switch stype {
case "list":
resp := message.(*pb.HeroListResp)
for _, v := range resp.List {
this.heros[v.Id] = v
}
break
case "change":
resp := message.(*pb.HeroChangePush)
for _, v := range resp.List {
this.heros[v.Id] = v
}
break
}
return
}
//机器人执行流
func (this *ModuleRobot_Hero) DoPipeline(robot IRobot) (err error) {
var (
errdata *pb.ErrorData
)
if _, errdata = robot.SendMessage("hero", "list", &pb.HeroListReq{}); errdata != nil {
err = errors.New(fmt.Sprintf("code:%d message:%s", errdata.Code, errdata.Message))
return
}
return
}
//做任务
func (this *ModuleRobot_Hero) DoTask(robot IRobot, taskconf *cfg.GameWorldTaskData, condconf *cfg.GameBuriedCondiData) (err error) {
var (
errdata *pb.ErrorData
)
switch comm.TaskType(condconf.Type) {
case comm.Rtype14:
if _, errdata = robot.SendTaskMessage(taskconf.Key, condconf.Id, "hero", "drawcard", &pb.HeroDrawCardReq{DrawType: 2, DrawCount: 1, Consume: 0}); errdata != nil {
err = errors.New(fmt.Sprintf("code:%d message:%s", errdata.Code, errdata.Message))
return
}
}
return
}
//获取战斗英雄
func (this *ModuleRobot_Hero) getbattlehero() (bheros []string) {
var (
heros []*pb.DBHero = make([]*pb.DBHero, 0, len(this.heros))
)
bheros = make([]string, 5)
for _, v := range this.heros {
heros = append(heros, v)
}
// 使用sort.Slice进行排序
sort.Slice(heros, func(i, j int) bool {
return heros[i].Lv > heros[j].Lv
})
for i, v := range heros {
if i < 5 {
bheros[i] = v.Id
} else {
return
}
}
return
}
//获取武官训练英雄英雄
func (this *ModuleRobot_Hero) getpracticehero() (bheros string) {
for _, v := range this.heros {
if v.Status != pb.HeroType_HeroTypeKongFu {
return v.Id
}
}
return
}