go_dreamfactory/modules/equipment/api_upgrade.go
2022-07-25 16:23:29 +08:00

146 lines
4.7 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/big"
"go.mongodb.org/mongo-driver/bson/primitive"
"google.golang.org/protobuf/proto"
)
//参数校验
func (this *apiComp) UpgradeCheck(session comm.IUserSession, req *pb.EquipmentUpgradeReq) (code pb.ErrorCode) {
if req.EquipmentId == "" {
log.Errorf("Upgrade 请求参数错误 req:%v", req)
code = pb.ErrorCode_ReqParameterError
return
}
return
}
///英雄挂在装备
func (this *apiComp) Upgrade(session comm.IUserSession, req *pb.EquipmentUpgradeReq) (code pb.ErrorCode, data proto.Message) {
var (
err error
conf *cfg.Game_equipData
intensify *cfg.Game_equipIntensifyData
equipment *pb.DB_Equipment
modifyequipments []*pb.DB_Equipment
hero *pb.DBHero
equipments []*pb.DB_Equipment
issucc bool
)
if code = this.UpgradeCheck(session, req); code != pb.ErrorCode_Success {
return
}
if equipment, err = this.module.modelEquipment.QueryUserEquipmentsById(session.GetUserId(), req.EquipmentId); err != nil {
log.Errorf("Equip_Check err:%v", err)
code = pb.ErrorCode_EquipmentOnFoundEquipment
return
}
if conf, err = this.module.configure.GetEquipmentConfigureById(equipment.CId); err != nil {
log.Errorf("Equip_Check err:%v", err)
code = pb.ErrorCode_EquipmentOnFoundEquipment
return
}
//找到下一个等级的相关配置
if intensify, err = this.module.configure.GetEquipmentIntensifyConfigureById(equipment.Lv); err != nil {
log.Errorf("Equip_Check err:%v", err)
code = pb.ErrorCode_EquipmentLvlimitReached
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
} else {
issucc = false
}
}
if issucc {
if code = this.module.ConsumeRes(session, intensify.Need, true); code != pb.ErrorCode_Success {
return
}
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 {
log.Errorf("Upgrade err:%v", err)
code = pb.ErrorCode_SystemError
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
log.Errorf("Upgrade err:%v", err)
return
}
if err = this.module.modelEquipment.AddList(session.GetUserId(), equipment.Id, equipment); err != nil {
log.Errorf("Upgrade err:%v", err)
code = pb.ErrorCode_SystemError
return
}
} else {
equipment.IsInitialState = false
modifyequipments = append(modifyequipments, equipment)
if err = this.module.modelEquipment.upgradeEquipment(equipment, conf, intensify); err != nil {
code = pb.ErrorCode_SystemError
log.Errorf("Upgrade err:%v", err)
return
}
if err = this.module.modelEquipment.ChangeList(session.GetUserId(), equipment.Id, map[string]interface{}{
"lv": equipment.Lv,
"mainEntry": equipment.MainEntry,
"adverbEntry": equipment.AdverbEntry,
"isInitialState": false,
}); err != nil {
log.Errorf("Upgrade err:%v", err)
code = pb.ErrorCode_SystemError
return
}
}
//已装备 重新计算属性
if equipment.HeroId != "" {
if hero, code = this.module.ModuleHero.GetHeroByObjID(session.GetUserId(), equipment.HeroId); code != pb.ErrorCode_Success {
log.Errorf("Upgrade code:%d", code)
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
}
} else {
equipments[i] = equipment
}
}
}
code = this.module.ModuleHero.UpdateEquipment(session, hero, equipments)
}
}
session.SendMsg(string(this.module.GetType()), "upgrade", &pb.EquipmentUpgradeResp{IsSucc: issucc, Equipment: modifyequipments})
return
}