254 lines
8.4 KiB
Go
254 lines
8.4 KiB
Go
package equipment
|
|
|
|
import (
|
|
"crypto/rand"
|
|
"go_dreamfactory/comm"
|
|
"go_dreamfactory/lego/sys/log"
|
|
"go_dreamfactory/pb"
|
|
cfg "go_dreamfactory/sys/configure/structs"
|
|
"math"
|
|
"math/big"
|
|
|
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
|
)
|
|
|
|
//参数校验
|
|
func (this *apiComp) UpgradeCheck(session comm.IUserSession, req *pb.EquipmentUpgradeReq) (code pb.ErrorCode) {
|
|
if req.EquipmentId == "" {
|
|
code = pb.ErrorCode_ReqParameterError
|
|
return
|
|
}
|
|
return
|
|
}
|
|
|
|
///英雄挂在装备
|
|
func (this *apiComp) Upgrade(session comm.IUserSession, req *pb.EquipmentUpgradeReq) (code pb.ErrorCode, data *pb.ErrorData) {
|
|
var (
|
|
err error
|
|
conf *cfg.GameEquipData
|
|
intensify *cfg.GameEquipIntensifyData
|
|
equipment *pb.DB_Equipment
|
|
modifyequipments []*pb.DB_Equipment
|
|
hero *pb.DBHero
|
|
equipments []*pb.DB_Equipment
|
|
confs []*cfg.GameEquipData
|
|
issucc bool
|
|
suite1Lv, suite2Lv int32 = math.MaxInt32, math.MaxInt32
|
|
user *pb.DBUser
|
|
tasks []*pb.BuriedParam = make([]*pb.BuriedParam, 0)
|
|
)
|
|
if code = this.UpgradeCheck(session, req); code != pb.ErrorCode_Success {
|
|
data = &pb.ErrorData{
|
|
Title: code.ToString(),
|
|
Message: req.String(),
|
|
}
|
|
return
|
|
}
|
|
if equipment, err = this.module.modelEquipment.QueryUserEquipmentsById(session.GetUserId(), req.EquipmentId); err != nil {
|
|
this.module.Errorf("Equip_Check err:%v", err)
|
|
code = pb.ErrorCode_EquipmentOnFoundEquipment
|
|
data = &pb.ErrorData{
|
|
Title: code.ToString(),
|
|
Message: err.Error(),
|
|
}
|
|
return
|
|
}
|
|
if conf, err = this.module.configure.GetEquipmentConfigureById(equipment.CId); err != nil {
|
|
this.module.Errorf("Equip_Check err:%v", err)
|
|
code = pb.ErrorCode_EquipmentOnFoundEquipment
|
|
data = &pb.ErrorData{
|
|
Title: code.ToString(),
|
|
Message: err.Error(),
|
|
}
|
|
return
|
|
}
|
|
//找到下一个等级的相关配置
|
|
if intensify, err = this.module.configure.GetEquipmentIntensifyConfigureById(conf.EquipId, conf.Star, equipment.Lv); err != nil || intensify.Need == nil || len(intensify.Need) == 0 {
|
|
this.module.Errorf("Equip_Check err:%v", err)
|
|
code = pb.ErrorCode_EquipmentLvlimitReached
|
|
data = &pb.ErrorData{
|
|
Title: code.ToString(),
|
|
Message: err.Error(),
|
|
}
|
|
return
|
|
}
|
|
if code = this.module.ConsumeRes(session, intensify.Need, true); code != pb.ErrorCode_Success {
|
|
return
|
|
}
|
|
if equipment.KeepFailNum >= intensify.Num { //没有达到保底次数 根据概率随机成功失败
|
|
issucc = true
|
|
} else { //随机 千分比
|
|
n, _ := rand.Int(rand.Reader, big.NewInt(1000))
|
|
if int32(n.Int64()) < intensify.Probability {
|
|
issucc = true
|
|
equipment.KeepFailNum = 0
|
|
} else {
|
|
issucc = false
|
|
equipment.KeepFailNum++
|
|
if err = this.module.modelEquipment.ChangeList(session.GetUserId(), equipment.Id, map[string]interface{}{
|
|
"keepFailNum": equipment.KeepFailNum,
|
|
}); err != nil {
|
|
log.Errorf("Upgrade err:%v", err)
|
|
code = pb.ErrorCode_SystemError
|
|
data = &pb.ErrorData{
|
|
Title: code.ToString(),
|
|
Message: err.Error(),
|
|
}
|
|
return
|
|
}
|
|
}
|
|
}
|
|
if issucc {
|
|
modifyequipments = make([]*pb.DB_Equipment, 0)
|
|
//叠加装备 拆分处理
|
|
if equipment.IsInitialState && equipment.OverlayNum > 1 {
|
|
equipment.OverlayNum--
|
|
if err = this.module.modelEquipment.ChangeList(session.GetUserId(), equipment.Id, map[string]interface{}{
|
|
"overlayNum": equipment.OverlayNum,
|
|
"heroId": "",
|
|
}); err != nil {
|
|
this.module.Errorf("Upgrade err:%v", err)
|
|
code = pb.ErrorCode_SystemError
|
|
data = &pb.ErrorData{
|
|
Title: code.ToString(),
|
|
Message: err.Error(),
|
|
}
|
|
return
|
|
}
|
|
modifyequipments = append(modifyequipments, equipment)
|
|
equipment = CloneEquipment(equipment)
|
|
equipment.Id = primitive.NewObjectID().Hex()
|
|
equipment.IsInitialState = false
|
|
equipment.OverlayNum = 1
|
|
modifyequipments = append(modifyequipments, equipment)
|
|
if err = this.module.modelEquipment.upgradeEquipment(equipment, conf, intensify); err != nil {
|
|
code = pb.ErrorCode_SystemError
|
|
data = &pb.ErrorData{
|
|
Title: code.ToString(),
|
|
Message: err.Error(),
|
|
}
|
|
this.module.Errorf("Upgrade err:%v", err)
|
|
return
|
|
}
|
|
if err = this.module.modelEquipment.AddList(session.GetUserId(), equipment.Id, equipment); err != nil {
|
|
this.module.Errorf("Upgrade err:%v", err)
|
|
code = pb.ErrorCode_SystemError
|
|
data = &pb.ErrorData{
|
|
Title: code.ToString(),
|
|
Message: err.Error(),
|
|
}
|
|
return
|
|
}
|
|
} else {
|
|
equipment.IsInitialState = false
|
|
modifyequipments = append(modifyequipments, equipment)
|
|
if err = this.module.modelEquipment.upgradeEquipment(equipment, conf, intensify); err != nil {
|
|
code = pb.ErrorCode_SystemError
|
|
this.module.Errorf("Upgrade err:%v", err)
|
|
data = &pb.ErrorData{
|
|
Title: code.ToString(),
|
|
Message: err.Error(),
|
|
}
|
|
return
|
|
}
|
|
if err = this.module.modelEquipment.ChangeList(session.GetUserId(), equipment.Id, map[string]interface{}{
|
|
"keepFailNum": equipment.KeepFailNum,
|
|
"lv": equipment.Lv,
|
|
"mainEntry": equipment.MainEntry,
|
|
"adverbEntry": equipment.AdverbEntry,
|
|
"adverbskill": equipment.Adverbskill,
|
|
"isInitialState": false,
|
|
}); err != nil {
|
|
log.Errorf("Upgrade err:%v", err)
|
|
code = pb.ErrorCode_SystemError
|
|
data = &pb.ErrorData{
|
|
Title: code.ToString(),
|
|
Message: err.Error(),
|
|
}
|
|
return
|
|
}
|
|
}
|
|
equipments = make([]*pb.DB_Equipment, 8)
|
|
confs = make([]*cfg.GameEquipData, 8)
|
|
//已装备 重新计算属性
|
|
if equipment.HeroId != "" {
|
|
if hero, code = this.module.ModuleHero.GetHeroByObjID(session.GetUserId(), equipment.HeroId); code != pb.ErrorCode_Success {
|
|
this.module.Errorf("Upgrade code:%d", code)
|
|
data = &pb.ErrorData{
|
|
Title: code.ToString(),
|
|
}
|
|
return
|
|
}
|
|
if conf.Pos >= 8 {
|
|
code = pb.ErrorCode_ConfigurationException
|
|
this.module.Errorf("Upgrade equipment Pos:%d", conf.Pos)
|
|
data = &pb.ErrorData{
|
|
Title: code.ToString(),
|
|
}
|
|
return
|
|
}
|
|
hero.EquipID[conf.Pos] = equipment.Id //拆分后的装备是一个新的
|
|
for i, v := range hero.EquipID {
|
|
if v != "" {
|
|
if v != equipment.Id {
|
|
if equipments[i], err = this.module.modelEquipment.QueryUserEquipmentsById(session.GetUserId(), v); err != nil {
|
|
log.Errorf("Upgrade err:%v", err)
|
|
code = pb.ErrorCode_EquipmentOnFoundEquipment
|
|
return
|
|
}
|
|
confs[i], _ = this.module.configure.GetEquipmentConfigureById(equipments[i].CId)
|
|
} else {
|
|
equipments[i] = equipment
|
|
confs[i] = conf
|
|
}
|
|
if i < 4 {
|
|
if equipments[i].Lv < suite1Lv {
|
|
suite1Lv = equipments[i].Lv
|
|
}
|
|
} else {
|
|
if equipments[i].Lv < suite2Lv {
|
|
suite2Lv = equipments[i].Lv
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
if conf.Pos < 4 && hero.SuiteId != 0 {
|
|
hero.Suite1Lv = suite1Lv
|
|
tasks = append(tasks, comm.GetBuriedParam(comm.Rtype49, 1, hero.Suite1Star, hero.Suite1Lv))
|
|
tasks = append(tasks, comm.GetBuriedParam2(comm.Rtype43, equipment.Id, equipment.Lv))
|
|
tasks = append(tasks, comm.GetBuriedParam2(comm.Rtype45, equipment.Id, conf.Star, hero.SuiteExtId, hero.Suite2Lv))
|
|
}
|
|
if conf.Pos >= 4 && conf.Pos < 6 && hero.SuiteExtId != 0 {
|
|
hero.Suite2Lv = suite2Lv
|
|
tasks = append(tasks, comm.GetBuriedParam(comm.Rtype48, 1, hero.Suite2Star, hero.Suite2Lv))
|
|
tasks = append(tasks, comm.GetBuriedParam2(comm.Rtype45, equipment.Id, conf.Star, hero.SuiteExtId, hero.Suite2Lv))
|
|
}
|
|
code = this.module.ModuleHero.UpdateEquipment(session, hero, equipments)
|
|
}
|
|
//随机任务触发
|
|
tasks = append(tasks, comm.GetBuriedParam2(comm.Rtype43, equipment.Id, equipment.Lv))
|
|
tasks = append(tasks, comm.GetBuriedParam(comm.Rtype47, 1))
|
|
|
|
if conf.Pos == 7 {
|
|
tasks = append(tasks, comm.GetBuriedParam(comm.Rtype102, 1))
|
|
} else if conf.Pos == 6 {
|
|
tasks = append(tasks, comm.GetBuriedParam(comm.Rtype100, 1))
|
|
}
|
|
//聊天系统通知
|
|
if user = this.module.ModuleUser.GetUser(session.GetUserId()); user != nil {
|
|
this.module.chat.SendSysChatToWorld(comm.ChatSystem3, equipment, equipment.Lv, 0, user.Name, conf.Id)
|
|
} else {
|
|
this.module.Errorf("no found userdata uid:%s", session.GetUserId())
|
|
}
|
|
} else {
|
|
tasks = append(tasks, comm.GetBuriedParam(comm.Rtype96, 1))
|
|
}
|
|
tasks = append(tasks, comm.GetBuriedParam(comm.Rtype92, 1))
|
|
if len(tasks) > 0 {
|
|
go this.module.ModuleBuried.TriggerBuried(session.GetUserId(), tasks...)
|
|
}
|
|
session.SendMsg(string(this.module.GetType()), "upgrade", &pb.EquipmentUpgradeResp{IsSucc: issucc, Equipment: modifyequipments})
|
|
return
|
|
}
|