451 lines
13 KiB
Go
451 lines
13 KiB
Go
package equipment
|
|
|
|
import (
|
|
"crypto/rand"
|
|
"fmt"
|
|
"go_dreamfactory/comm"
|
|
"go_dreamfactory/lego/core"
|
|
"go_dreamfactory/modules"
|
|
"go_dreamfactory/pb"
|
|
cfg "go_dreamfactory/sys/configure/structs"
|
|
"go_dreamfactory/sys/db"
|
|
"math"
|
|
"math/big"
|
|
|
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
|
"go.mongodb.org/mongo-driver/mongo"
|
|
"go.mongodb.org/mongo-driver/x/bsonx"
|
|
)
|
|
|
|
// /装备 数据组件
|
|
type modelEquipmentComp struct {
|
|
modules.MCompModel
|
|
module *Equipment
|
|
}
|
|
|
|
// 组件初始化接口
|
|
func (this *modelEquipmentComp) Init(service core.IService, module core.IModule, comp core.IModuleComp, opt core.IModuleOptions) (err error) {
|
|
this.TableName = comm.TableEquipment
|
|
this.MCompModel.Init(service, module, comp, opt)
|
|
this.module = module.(*Equipment)
|
|
//创建uid索引
|
|
_, err = this.DB.CreateIndex(core.SqlTable(this.TableName), mongo.IndexModel{
|
|
Keys: bsonx.Doc{{Key: "uid", Value: bsonx.Int32(1)}},
|
|
})
|
|
return
|
|
}
|
|
|
|
// 查询用户装备数据
|
|
func (this *modelEquipmentComp) QueryUserEquipmentsById(uId, id string) (equipment *pb.DB_Equipment, err error) {
|
|
equipment = &pb.DB_Equipment{}
|
|
err = this.GetListObj(uId, id, equipment)
|
|
return
|
|
}
|
|
|
|
// 查询用户装备数据
|
|
func (this *modelEquipmentComp) QueryUserEquipmentsByIds(uId string, ids []string) (equipments []*pb.DB_Equipment, err error) {
|
|
equipments = []*pb.DB_Equipment{}
|
|
if err = this.GetListObjs(uId, ids, &equipments); err != nil {
|
|
this.module.Errorf("err:%v", err)
|
|
}
|
|
return
|
|
}
|
|
|
|
// /查询用户的武器背包
|
|
func (this *modelEquipmentComp) QueryUserEquipments(uId string) (equipments []*pb.DB_Equipment, err error) {
|
|
var (
|
|
model *db.DBModel
|
|
)
|
|
equipments = make([]*pb.DB_Equipment, 0)
|
|
if this.module.IsCross() {
|
|
if model, err = this.module.GetDBModelByUid(uId, this.TableName); err != nil {
|
|
this.module.Errorln(err)
|
|
} else {
|
|
if err = model.GetList(uId, &equipments); err != nil {
|
|
this.module.Errorf("err:%v", err)
|
|
}
|
|
}
|
|
} else {
|
|
if err = this.GetList(uId, &equipments); err != nil {
|
|
this.module.Errorf("err:%v", err)
|
|
}
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
// /查询目标卡片数量
|
|
func (this *modelEquipmentComp) QueryEquipmentAmount(uid string, equipmentId string) (amount uint32) {
|
|
var (
|
|
equipments []*pb.DB_Equipment
|
|
err error
|
|
)
|
|
amount = 0
|
|
if equipments, err = this.QueryUserEquipments(uid); err != nil {
|
|
return
|
|
}
|
|
for _, v := range equipments {
|
|
if v.CId == equipmentId {
|
|
amount += v.OverlayNum
|
|
}
|
|
}
|
|
return
|
|
}
|
|
|
|
// 添加装备
|
|
func (this *modelEquipmentComp) AddEquipments(session comm.IUserSession, cIds map[string]uint32) (change []*pb.DB_Equipment, err error) {
|
|
var (
|
|
configure *cfg.GameEquip
|
|
add map[string]*pb.DB_Equipment
|
|
uId string = session.GetUserId()
|
|
tasks []*pb.BuriedParam = make([]*pb.BuriedParam, 0)
|
|
)
|
|
if configure, err = this.module.configure.GetEquipmentConfigure(); err != nil {
|
|
return
|
|
}
|
|
add = make(map[string]*pb.DB_Equipment)
|
|
change = make([]*pb.DB_Equipment, 0, 10)
|
|
for k, v := range cIds {
|
|
if c, ok := configure.GetDataMap()[k]; ok {
|
|
for i := uint32(0); i < v; i++ {
|
|
if equipment, err := this.newEquipment(uId, c, nil, false); err != nil {
|
|
return nil, err
|
|
} else {
|
|
//随机任务
|
|
tasks = append(tasks, comm.GetBuriedParam(comm.Rtype50, 1, c.Color))
|
|
tasks = append(tasks, comm.GetBuriedParam(comm.Rtype97, 1, c.Suittype, c.Color))
|
|
unm := int32(1)
|
|
if equipment.AdverbEntry != nil {
|
|
unm += int32(len(equipment.AdverbEntry))
|
|
}
|
|
if equipment.Adverbskill != nil {
|
|
unm += int32(len(equipment.Adverbskill))
|
|
}
|
|
tasks = append(tasks, comm.GetBuriedParam(comm.Rtype99, 1, unm, c.Color))
|
|
if c.Pos == 7 {
|
|
tasks = append(tasks, comm.GetBuriedParam(comm.Rtype103, 1, c.Color))
|
|
} else if c.Pos == 6 {
|
|
tasks = append(tasks, comm.GetBuriedParam(comm.Rtype101, 1, c.Color))
|
|
}
|
|
add[equipment.Id] = equipment
|
|
change = append(change, equipment)
|
|
}
|
|
}
|
|
} else {
|
|
//err = fmt.Errorf("cfg.Game_equipment not found equip id %s", k) // 太多地方配置无效装备 这地方暂时只打印错误日志
|
|
this.module.Errorf("cfg.Game_equipment not found equip id %s", k)
|
|
}
|
|
}
|
|
|
|
//异步出发任务启动
|
|
if len(tasks) > 0 {
|
|
go this.module.ModuleBuried.TriggerBuried(session.GetUserId(), tasks...)
|
|
}
|
|
|
|
if len(add) > 0 {
|
|
var (
|
|
model *db.DBModel
|
|
)
|
|
if this.module.IsCross() {
|
|
if model, err = this.module.GetDBModelByUid(uId, this.TableName); err != nil {
|
|
this.module.Errorln(err)
|
|
} else {
|
|
if err = model.AddLists(uId, add); err != nil {
|
|
this.module.Errorf("err:%v", err)
|
|
return
|
|
}
|
|
}
|
|
} else {
|
|
if err = this.AddLists(uId, add); err != nil {
|
|
this.module.Errorf("err:%v", err)
|
|
return
|
|
}
|
|
}
|
|
}
|
|
return
|
|
}
|
|
|
|
func (this *modelEquipmentComp) addEquipment(equip *pb.DB_Equipment) (err error) {
|
|
var (
|
|
model *db.DBModel
|
|
)
|
|
if this.module.IsCross() {
|
|
if model, err = this.module.GetDBModelByUid(equip.UId, this.TableName); err != nil {
|
|
this.module.Errorln(err)
|
|
} else {
|
|
if err = model.AddList(equip.UId, equip.Id, equip); err != nil {
|
|
this.module.Errorf("err:%v", err)
|
|
return
|
|
}
|
|
}
|
|
} else {
|
|
if err = this.AddList(equip.UId, equip.Id, equip); err != nil {
|
|
this.module.Errorf("err:%v", err)
|
|
return
|
|
}
|
|
}
|
|
return
|
|
}
|
|
|
|
// 删除装备
|
|
func (this *modelEquipmentComp) DelEquipments(uId string, eIds []string) (change []*pb.DB_Equipment, err error) {
|
|
var (
|
|
model *db.DBModel
|
|
)
|
|
change = make([]*pb.DB_Equipment, 0)
|
|
if this.module.IsCross() {
|
|
if model, err = this.module.GetDBModelByUid(uId, this.TableName); err != nil {
|
|
this.module.Errorln(err)
|
|
} else {
|
|
if err = model.DelListlds(uId, eIds); err != nil {
|
|
this.module.Errorln(err)
|
|
return
|
|
}
|
|
}
|
|
} else {
|
|
if err = this.DelListlds(uId, eIds); err != nil {
|
|
this.module.Errorln(err)
|
|
return
|
|
}
|
|
}
|
|
for _, v := range eIds {
|
|
change = append(change, &pb.DB_Equipment{
|
|
Id: v,
|
|
UId: uId,
|
|
OverlayNum: 0,
|
|
})
|
|
}
|
|
return
|
|
}
|
|
|
|
// 更新武器挂载信息
|
|
func (this *modelEquipmentComp) UpdateByHeroId(uid string, equipments ...*pb.DB_Equipment) (err error) {
|
|
var (
|
|
model *db.DBModel
|
|
)
|
|
if this.module.IsCross() {
|
|
if model, err = this.module.GetDBModelByUid(uid, this.TableName); err != nil {
|
|
this.module.Errorln(err)
|
|
} else {
|
|
for _, v := range equipments {
|
|
if err = model.ChangeList(uid, v.Id, map[string]interface{}{
|
|
"heroId": v.HeroId,
|
|
}); err != nil {
|
|
this.module.Errorf("err:%v", err)
|
|
return
|
|
}
|
|
}
|
|
}
|
|
} else {
|
|
for _, v := range equipments {
|
|
if err = this.ChangeList(uid, v.Id, map[string]interface{}{
|
|
"heroId": v.HeroId,
|
|
}); err != nil {
|
|
this.module.Errorf("err:%v", err)
|
|
return
|
|
}
|
|
}
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
// 创建新的武器对象
|
|
func (this *modelEquipmentComp) newEquipment(uid string, conf *cfg.GameEquipData, dyweight []int32, isepic bool) (equipment *pb.DB_Equipment, err error) {
|
|
var (
|
|
mattr []*cfg.GameEquipAttrlibrarySData
|
|
sattr []*cfg.GameEquipAttrlibrarySData
|
|
equipatt *cfg.GameEquipAttributeData
|
|
weight []int32
|
|
total int64
|
|
maxindex int
|
|
satterNum int32
|
|
)
|
|
equipment = &pb.DB_Equipment{
|
|
Id: primitive.NewObjectID().Hex(),
|
|
CId: conf.Id,
|
|
Lv: 1,
|
|
UId: uid,
|
|
OverlayNum: 1,
|
|
IsInitialState: false,
|
|
}
|
|
if mattr, err = this.module.configure.GetEquipmentAttrlibraryConfigureById(conf.Leadlibrary); err != nil || len(mattr) == 0 {
|
|
err = fmt.Errorf("no found mattr%d", conf.Leadlibrary)
|
|
this.module.Errorln(err)
|
|
return
|
|
}
|
|
equipment.MainEntry = &pb.EquipmentAttributeEntry{
|
|
Id: mattr[0].Key,
|
|
Libraryid: mattr[0].Libraryid,
|
|
Lv: 1,
|
|
AttrName: mattr[0].Attrkey,
|
|
Value: mattr[0].Attrvar,
|
|
BaseValue: mattr[0].Attrvar,
|
|
}
|
|
if sattr, err = this.module.configure.GetEquipmentAttrlibraryConfigureById(conf.Addlibrary); err != nil || len(mattr) == 0 {
|
|
return
|
|
}
|
|
|
|
for i, v := range sattr { //移除主属性
|
|
if v.Attrkey == equipment.MainEntry.AttrName {
|
|
sattr = append(sattr[0:i], sattr[i+1:]...)
|
|
break
|
|
}
|
|
}
|
|
if !isepic {
|
|
weight = make([]int32, len(conf.Addattrnump))
|
|
for i, v := range conf.Addattrnump {
|
|
weight[i] = v
|
|
}
|
|
if dyweight != nil && len(dyweight) >= len(weight) {
|
|
for i, _ := range weight {
|
|
weight[i] += dyweight[i]
|
|
}
|
|
}
|
|
for _, v := range weight {
|
|
total += int64(v)
|
|
}
|
|
result, _ := rand.Int(rand.Reader, big.NewInt(total))
|
|
for i, v := range weight {
|
|
if int32(result.Int64()) <= v {
|
|
satterNum = conf.Addattrnum[i]
|
|
break
|
|
}
|
|
}
|
|
if satterNum > int32(len(sattr)) {
|
|
satterNum = int32(len(sattr))
|
|
}
|
|
} else {
|
|
maxindex = 0
|
|
for i, v := range conf.Addattrnump {
|
|
if v > 0 && i > maxindex {
|
|
maxindex = i
|
|
}
|
|
}
|
|
satterNum = conf.Addattrnum[maxindex]
|
|
if satterNum > int32(len(sattr)) {
|
|
satterNum = int32(len(sattr))
|
|
}
|
|
}
|
|
|
|
if satterNum > 0 && satterNum <= 4 {
|
|
equipment.Star = satterNum
|
|
|
|
if conf.EquipId == 1 {
|
|
equipment.AdverbEntry = make([]*pb.EquipmentAttributeEntry, 0)
|
|
for _, v := range comm.RandShuffle(len(sattr))[:satterNum] {
|
|
equipment.AdverbEntry = append(equipment.AdverbEntry, &pb.EquipmentAttributeEntry{
|
|
Id: sattr[v].Key,
|
|
Libraryid: sattr[v].Libraryid,
|
|
Lv: 1,
|
|
AttrName: sattr[v].Attrkey,
|
|
Value: sattr[v].Attrvar,
|
|
BaseValue: sattr[v].Attrvar,
|
|
})
|
|
}
|
|
} else {
|
|
equipment.Adverbskill = make([]*pb.EquipmentSkillEntry, 0)
|
|
for _, v := range comm.RandShuffle(len(sattr))[:satterNum] {
|
|
if equipatt, err = this.module.configure.getEquipAttribute(sattr[v].Attrkey); err != nil {
|
|
return
|
|
}
|
|
equipment.Adverbskill = append(equipment.Adverbskill, &pb.EquipmentSkillEntry{
|
|
Id: sattr[v].Key,
|
|
Libraryid: sattr[v].Libraryid,
|
|
AttrName: sattr[v].Attrkey,
|
|
SkillId: equipatt.SkillId,
|
|
Lv: 1,
|
|
})
|
|
}
|
|
}
|
|
}
|
|
return
|
|
}
|
|
|
|
// 升级武器
|
|
func (this *modelEquipmentComp) upgradeEquipment(equipment *pb.DB_Equipment, equip *cfg.GameEquipData, intensify *cfg.GameEquipIntensifyData) (err error) {
|
|
equipment.Lv++
|
|
equipment.MainEntry.Lv++
|
|
// var mainconfigure *cfg.GameEquipAttrlibrarySData
|
|
// if mainconfigure, err = this.module.configure.GetEquipmentAttrlibraryConfigureByKey(equipment.MainEntry.Id); err != nil {
|
|
// this.module.Errorf("升级服务错误 读取主词条配置错误!")
|
|
// return
|
|
// }
|
|
equipment.MainEntry.Value = equipment.MainEntry.BaseValue + int32(math.Floor(float64(equipment.MainEntry.BaseValue*intensify.Bonus)/1000.0))
|
|
if intensify.Activation { //不触发副词条变化
|
|
if len(equipment.AdverbEntry) < 4 { //去随机副词条
|
|
var (
|
|
temp []*cfg.GameEquipAttrlibrarySData
|
|
sattr []*cfg.GameEquipAttrlibrarySData
|
|
equipatt *cfg.GameEquipAttributeData
|
|
)
|
|
if temp, err = this.module.configure.GetEquipmentAttrlibraryConfigureById(equip.Addlibrary); err != nil {
|
|
this.module.Errorf("升级服务错误 读取副词条配置错误!")
|
|
return
|
|
}
|
|
//检索出未使用的词条
|
|
for _, v := range temp {
|
|
iskeep := false
|
|
for _, v1 := range equipment.AdverbEntry {
|
|
if v.Attrkey == v1.AttrName {
|
|
iskeep = true
|
|
}
|
|
}
|
|
if v.Attrkey == equipment.MainEntry.AttrName {
|
|
iskeep = true
|
|
}
|
|
if !iskeep {
|
|
sattr = append(sattr, v)
|
|
}
|
|
}
|
|
if len(sattr) > 0 {
|
|
index := comm.RandShuffle(len(sattr))[0]
|
|
if equip.EquipId == 1 {
|
|
equipment.AdverbEntry = append(equipment.AdverbEntry, &pb.EquipmentAttributeEntry{
|
|
Id: sattr[index].Key,
|
|
Libraryid: sattr[index].Libraryid,
|
|
Lv: 1,
|
|
AttrName: sattr[index].Attrkey,
|
|
Value: sattr[index].Attrvar,
|
|
BaseValue: sattr[index].Attrvar,
|
|
})
|
|
return
|
|
} else {
|
|
if equipatt, err = this.module.configure.getEquipAttribute(sattr[index].Attrkey); err != nil {
|
|
return
|
|
}
|
|
equipment.Adverbskill = append(equipment.Adverbskill, &pb.EquipmentSkillEntry{
|
|
Id: sattr[index].Key,
|
|
Libraryid: sattr[index].Libraryid,
|
|
AttrName: sattr[index].Attrkey,
|
|
SkillId: equipatt.SkillId,
|
|
Lv: 1,
|
|
})
|
|
return
|
|
}
|
|
}
|
|
return
|
|
}
|
|
|
|
if equip.EquipId == 1 {
|
|
if len(equipment.AdverbEntry) <= 0 {
|
|
return
|
|
}
|
|
var attrlibrary *cfg.GameEquipAttrlibrarySData
|
|
index := comm.RandShuffle(len(equipment.AdverbEntry))[0]
|
|
if attrlibrary, err = this.module.configure.GetEquipmentAttrlibraryConfigureByKey(equipment.AdverbEntry[index].Id); err != nil {
|
|
return
|
|
}
|
|
value := equipment.AdverbEntry[index].BaseValue + int32(float64(attrlibrary.Addition[equipment.AdverbEntry[index].Lv-1])/1000.0*float64(equipment.AdverbEntry[index].BaseValue))
|
|
if equipment.AdverbEntry[index].Value < value {
|
|
equipment.AdverbEntry[index].Value = value
|
|
}
|
|
equipment.AdverbEntry[index].Lv++
|
|
} else {
|
|
index := comm.RandShuffle(len(equipment.Adverbskill))[0]
|
|
equipment.Adverbskill[index].Lv++
|
|
}
|
|
}
|
|
return
|
|
}
|