423 lines
11 KiB
Go
423 lines
11 KiB
Go
package robot
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"go_dreamfactory/comm"
|
|
"go_dreamfactory/pb"
|
|
"go_dreamfactory/sys/configure"
|
|
cfg "go_dreamfactory/sys/configure/structs"
|
|
"sort"
|
|
|
|
"google.golang.org/protobuf/proto"
|
|
)
|
|
|
|
//用户模块 机器人
|
|
type ModuleRobot_Equipment struct {
|
|
equipments map[string]*pb.DB_Equipment
|
|
}
|
|
|
|
func (this *ModuleRobot_Equipment) Init() (err error) {
|
|
this.equipments = make(map[string]*pb.DB_Equipment)
|
|
return
|
|
}
|
|
|
|
//接收到消息
|
|
func (this *ModuleRobot_Equipment) Receive(robot IRobot, stype string, message proto.Message) (err error) {
|
|
switch stype {
|
|
case "getlist":
|
|
resp := message.(*pb.EquipmentGetListResp)
|
|
for _, v := range resp.Equipments {
|
|
this.equipments[v.Id] = v
|
|
}
|
|
break
|
|
case "upgrade":
|
|
resp := message.(*pb.EquipmentUpgradeResp)
|
|
for _, v := range resp.Equipment {
|
|
this.equipments[v.Id] = v
|
|
}
|
|
return
|
|
case "change":
|
|
resp := message.(*pb.EquipmentChangePush)
|
|
for _, v := range resp.Equipments {
|
|
this.equipments[v.Id] = v
|
|
}
|
|
break
|
|
}
|
|
return
|
|
}
|
|
func (this *ModuleRobot_Equipment) OncePipeline(robot IRobot) (err error) {
|
|
var (
|
|
errdata *pb.ErrorData
|
|
)
|
|
if _, errdata = robot.SendMessage("equipment", "getlist", &pb.EquipmentGetListReq{}); errdata != nil {
|
|
err = errors.New(fmt.Sprintf("code:%d message:%s", errdata.Code, errdata.Message))
|
|
return
|
|
}
|
|
return
|
|
}
|
|
|
|
//机器人执行流
|
|
func (this *ModuleRobot_Equipment) DoPipeline(robot IRobot) (err error) {
|
|
var (
|
|
errdata *pb.ErrorData
|
|
equipment *pb.DB_Equipment
|
|
lv int32 = 15
|
|
)
|
|
|
|
if equipment, err = this.findcanupgrade(lv); err != nil {
|
|
return
|
|
}
|
|
if _, errdata = robot.SendMessage("equipment", "upgrade", &pb.EquipmentUpgradeReq{
|
|
EquipmentId: equipment.Id,
|
|
}); errdata != nil {
|
|
if errdata.Code == pb.ErrorCode_EquipmentLvlimitReached {
|
|
err = nil
|
|
return
|
|
} else {
|
|
err = errors.New(fmt.Sprintf("code:%d message:%s", errdata.Code, errdata.Message))
|
|
}
|
|
return
|
|
}
|
|
return
|
|
}
|
|
|
|
//做任务
|
|
func (this *ModuleRobot_Equipment) DoTask(robot IRobot, taskconf *cfg.GameWorldTaskData, condconf *cfg.GameBuriedCondiData) (err error) {
|
|
var (
|
|
errdata *pb.ErrorData
|
|
)
|
|
switch comm.TaskType(condconf.Type) {
|
|
case comm.Rtype5: //穿戴
|
|
var (
|
|
heromodule *ModuleRobot_Hero
|
|
heros map[string]*pb.DBHero
|
|
equipments []string
|
|
hero *pb.DBHero
|
|
change bool
|
|
)
|
|
heromodule = robot.GetModule(comm.ModuleHero).(*ModuleRobot_Hero)
|
|
heros = heromodule.heros
|
|
for _, hero = range heros {
|
|
if equipments, change, err = this.findcanEquipEquipment(hero, 0); err != nil {
|
|
return
|
|
}
|
|
if change {
|
|
break
|
|
}
|
|
}
|
|
if change {
|
|
if _, errdata = robot.SendTaskMessage(taskconf.Key, condconf.Id, "equipment", "equip", &pb.EquipmentEquipReq{
|
|
HeroCardId: hero.Id,
|
|
EquipmentId: equipments,
|
|
}); errdata != nil {
|
|
err = errors.New(fmt.Sprintf("code:%d message:%s", errdata.Code, errdata.Message))
|
|
return
|
|
}
|
|
} else {
|
|
err = fmt.Errorf("no fund can use equipments")
|
|
}
|
|
case comm.Rtype41: //穿戴
|
|
var (
|
|
heromodule *ModuleRobot_Hero
|
|
heros map[string]*pb.DBHero
|
|
equipments []string
|
|
hero *pb.DBHero
|
|
change bool
|
|
)
|
|
heromodule = robot.GetModule(comm.ModuleHero).(*ModuleRobot_Hero)
|
|
heros = heromodule.heros
|
|
for _, hero = range heros {
|
|
if equipments, change, err = this.findcanEquipEquipment(hero, condconf.Filter[0]); err != nil {
|
|
return
|
|
}
|
|
if change {
|
|
break
|
|
}
|
|
}
|
|
if change {
|
|
if _, errdata = robot.SendTaskMessage(taskconf.Key, condconf.Id, "equipment", "equip", &pb.EquipmentEquipReq{
|
|
HeroCardId: hero.Id,
|
|
EquipmentId: equipments,
|
|
}); errdata != nil {
|
|
err = errors.New(fmt.Sprintf("code:%d message:%s", errdata.Code, errdata.Message))
|
|
return
|
|
}
|
|
} else {
|
|
err = fmt.Errorf("no fund can use equipments")
|
|
}
|
|
case comm.Rtype169: //穿戴套装
|
|
var (
|
|
heromodule *ModuleRobot_Hero
|
|
hero string
|
|
equipments []string
|
|
)
|
|
heromodule = robot.GetModule(comm.ModuleHero).(*ModuleRobot_Hero)
|
|
hero = heromodule.getbattlehero()[0]
|
|
|
|
if equipments, err = this.findsuitEquipment(condconf.Filter[0]); err != nil {
|
|
return
|
|
}
|
|
|
|
if _, errdata = robot.SendTaskMessage(taskconf.Key, condconf.Id, "equipment", "equip", &pb.EquipmentEquipReq{
|
|
HeroCardId: hero,
|
|
EquipmentId: equipments,
|
|
}); errdata != nil {
|
|
err = errors.New(fmt.Sprintf("code:%d message:%s", errdata.Code, errdata.Message))
|
|
return
|
|
}
|
|
break
|
|
|
|
case comm.Rtype43, comm.Rtype92, comm.Rtype96: //强化
|
|
var (
|
|
equipment *pb.DB_Equipment
|
|
lv int32 = 15
|
|
)
|
|
if comm.TaskType(condconf.Type) == comm.Rtype43 {
|
|
lv = condconf.Filter[0]
|
|
}
|
|
if equipment, err = this.findcanupgrade(lv); err != nil {
|
|
return
|
|
}
|
|
if _, errdata = robot.SendTaskMessage(taskconf.Key, condconf.Id, "equipment", "upgrade", &pb.EquipmentUpgradeReq{
|
|
EquipmentId: equipment.Id,
|
|
}); errdata != nil {
|
|
if errdata.Code == pb.ErrorCode_EquipmentLvlimitReached {
|
|
err = nil
|
|
return
|
|
} else {
|
|
err = errors.New(fmt.Sprintf("code:%d message:%s", errdata.Code, errdata.Message))
|
|
}
|
|
return
|
|
}
|
|
case comm.Rtype94: //附魔
|
|
var (
|
|
equipment *pb.DB_Equipment
|
|
itemmodule *ModuleRobot_Item
|
|
items map[string]*pb.DB_UserItemData
|
|
confs []*cfg.GameEquipEnchantingData
|
|
item *pb.DB_UserItemData
|
|
)
|
|
if equipment, err = this.findmaxlvEquipment(); err != nil {
|
|
return
|
|
}
|
|
|
|
itemmodule = robot.GetModule(comm.ModuleItems).(*ModuleRobot_Item)
|
|
items = itemmodule.items
|
|
if confs, err = this.getEquipenchantings(); err != nil {
|
|
return
|
|
}
|
|
for _, conf := range confs {
|
|
for _, v := range items {
|
|
if conf.Item == v.ItemId { //找到消耗道具
|
|
item = v
|
|
break
|
|
}
|
|
}
|
|
}
|
|
if item != nil {
|
|
if _, errdata = robot.SendTaskMessage(taskconf.Key, condconf.Id, "equipment", "ench", &pb.EquipmentEnchReq{
|
|
Eid: equipment.Id,
|
|
Itemid: item.GridId,
|
|
Index: 0,
|
|
}); errdata != nil {
|
|
err = errors.New(fmt.Sprintf("code:%d message:%s", errdata.Code, errdata.Message))
|
|
return
|
|
}
|
|
}
|
|
case comm.Rtype95: //洗练
|
|
var (
|
|
equipment *pb.DB_Equipment
|
|
resp proto.Message
|
|
pids []int32
|
|
)
|
|
if equipment, err = this.findmaxlvEquipment(); err != nil {
|
|
return
|
|
}
|
|
if resp, errdata = robot.SendTaskMessage(taskconf.Key, condconf.Id, "equipment", "wash", &pb.EquipmentWashReq{
|
|
Eid: equipment.Id,
|
|
}); errdata != nil {
|
|
err = errors.New(fmt.Sprintf("code:%d message:%s", errdata.Code, errdata.Message))
|
|
return
|
|
}
|
|
pids = make([]int32, 0)
|
|
for _, v := range resp.(*pb.EquipmentWashResp).AdverbEntry {
|
|
pids = append(pids, v.Libraryid)
|
|
}
|
|
if _, errdata = robot.SendTaskMessage(taskconf.Key, condconf.Id, "equipment", "washconfirm", &pb.EquipmentWashConfirmReq{
|
|
Eid: equipment.Id,
|
|
Pids: pids,
|
|
}); errdata != nil {
|
|
err = errors.New(fmt.Sprintf("code:%d message:%s", errdata.Code, errdata.Message))
|
|
return
|
|
}
|
|
}
|
|
return
|
|
}
|
|
|
|
//查询套装装备
|
|
func (this *ModuleRobot_Equipment) findsuitEquipment(sid int32) (equipments []string, err error) {
|
|
var (
|
|
econf *cfg.GameEquipData
|
|
)
|
|
equipments = make([]string, 8)
|
|
for _, equipment := range this.equipments {
|
|
if equipment.HeroId != "" {
|
|
continue
|
|
}
|
|
if econf, err = this.getGameEquipData(equipment.CId); err != nil {
|
|
return
|
|
}
|
|
if econf.Suittype == sid {
|
|
equipments[econf.Pos] = equipment.Id
|
|
}
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
//查询能穿戴的装备
|
|
func (this *ModuleRobot_Equipment) findcanEquipEquipment(hero *pb.DBHero, minstar int32) (equipments []string, change bool, err error) {
|
|
var (
|
|
conf *cfg.GameEquipData
|
|
)
|
|
equipments = hero.EquipID
|
|
for i, v := range hero.EquipID {
|
|
if v == "" {
|
|
for _, equipment := range this.equipments {
|
|
if equipment.HeroId == "" {
|
|
if conf, err = this.getGameEquipData(equipment.CId); err != nil {
|
|
return
|
|
}
|
|
if conf.Color < minstar {
|
|
continue
|
|
}
|
|
if conf.Pos == int32(i) { //找到匹配装备
|
|
equipments[i] = equipment.Id
|
|
change = true
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return
|
|
}
|
|
|
|
//查询能强化的装备
|
|
func (this *ModuleRobot_Equipment) findcanupgrade(lv int32) (equipment *pb.DB_Equipment, err error) {
|
|
var (
|
|
equipments []*pb.DB_Equipment = make([]*pb.DB_Equipment, 0, len(this.equipments))
|
|
conf *cfg.GameEquipData
|
|
upconf *cfg.GameEquipIntensifyData
|
|
)
|
|
var ()
|
|
for _, v := range this.equipments {
|
|
if v.Lv < lv {
|
|
equipments = append(equipments, v)
|
|
}
|
|
}
|
|
sort.Slice(equipments, func(i, j int) bool {
|
|
return equipments[i].Lv > equipments[j].Lv
|
|
})
|
|
for _, equipment = range equipments {
|
|
if conf, err = this.getGameEquipData(equipment.CId); err != nil {
|
|
return
|
|
}
|
|
if upconf, err = this.getEquipmentMaxIntensifyConfigure(conf.EquipId, conf.Color); err == nil && upconf.Level >= equipment.Lv {
|
|
return
|
|
}
|
|
}
|
|
equipment = nil
|
|
err = fmt.Errorf("no fund can upgrade equipment")
|
|
return
|
|
}
|
|
|
|
//查询能强化的装备
|
|
func (this *ModuleRobot_Equipment) findmaxlvEquipment() (equipment *pb.DB_Equipment, err error) {
|
|
var (
|
|
equipments []*pb.DB_Equipment = make([]*pb.DB_Equipment, 0, len(this.equipments))
|
|
)
|
|
if len(this.equipments) == 0 {
|
|
err = fmt.Errorf("no found can use equipment")
|
|
return
|
|
}
|
|
for _, v := range this.equipments {
|
|
equipments = append(equipments, v)
|
|
}
|
|
sort.Slice(equipments, func(i, j int) bool {
|
|
return equipments[i].Lv > equipments[j].Lv
|
|
})
|
|
equipment = equipments[0]
|
|
return
|
|
}
|
|
|
|
// 读取条件任务id配置
|
|
func (this *ModuleRobot_Equipment) getGameEquipData(cid string) (conf *cfg.GameEquipData, err error) {
|
|
var (
|
|
v interface{}
|
|
ok bool
|
|
)
|
|
if v, err = configure.GetConfigure(game_equip); err != nil {
|
|
return
|
|
} else {
|
|
if conf, ok = v.(*cfg.GameEquip).GetDataMap()[cid]; !ok {
|
|
err = comm.NewNotFoundConfErr("robot", game_buriedcondi, cid)
|
|
return
|
|
}
|
|
}
|
|
return
|
|
}
|
|
|
|
// 读取条件任务id配置
|
|
func (this *ModuleRobot_Equipment) getEquipmentMaxIntensifyConfigure(etype, star int32) (conf *cfg.GameEquipIntensifyData, err error) {
|
|
var (
|
|
v interface{}
|
|
lv int32
|
|
)
|
|
if v, err = configure.GetConfigure(game_equipintensify); err != nil {
|
|
return
|
|
} else {
|
|
for _, v1 := range v.(*cfg.GameEquipIntensify).GetDataList() {
|
|
if v1.TypeId == etype && v1.Star == star && len(v1.Need) > 0 && v1.Level > lv {
|
|
lv = v1.Level
|
|
conf = v1
|
|
}
|
|
}
|
|
if conf == nil {
|
|
err = fmt.Errorf("GetEquipmentMaxIntensifyConfigure not found star :%d lv:%d", star, lv)
|
|
return
|
|
}
|
|
}
|
|
return
|
|
}
|
|
|
|
// 获取附魔数据
|
|
func (this *ModuleRobot_Equipment) getEquipenchantings() (confs []*cfg.GameEquipEnchantingData, err error) {
|
|
var (
|
|
v interface{}
|
|
)
|
|
if v, err = configure.GetConfigure(game_equipenchanting); err != nil {
|
|
return
|
|
} else {
|
|
confs = v.(*cfg.GameEquipEnchanting).GetDataList()
|
|
}
|
|
return
|
|
}
|
|
|
|
// 获取装备套装配置
|
|
func (this *ModuleRobot_Equipment) getGameEquipSuitData(sid int32) (conf *cfg.GameEquipSuitData, err error) {
|
|
var (
|
|
v interface{}
|
|
ok bool
|
|
)
|
|
if v, err = configure.GetConfigure(equip_suit); err != nil {
|
|
return
|
|
} else {
|
|
if conf, ok = v.(*cfg.GameEquipSuit).GetDataMap()[sid]; !ok {
|
|
err = comm.NewNotFoundConfErr("robot", equip_suit, sid)
|
|
return
|
|
}
|
|
}
|
|
return
|
|
}
|