go_dreamfactory/modules/exclusive/model.go
2024-03-12 15:32:35 +08:00

262 lines
6.5 KiB
Go

package exclusive
import (
"go_dreamfactory/comm"
"go_dreamfactory/lego/core"
"go_dreamfactory/modules"
"go_dreamfactory/pb"
cfg "go_dreamfactory/sys/configure/structs"
"go_dreamfactory/sys/db"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/x/bsonx"
)
// /装备 数据组件
type modelComp struct {
modules.MCompModel
module *Exclusive
}
// 组件初始化接口
func (this *modelComp) Init(service core.IService, module core.IModule, comp core.IModuleComp, opt core.IModuleOptions) (err error) {
this.TableName = comm.TableExclusive
this.MCompModel.Init(service, module, comp, opt)
this.module = module.(*Exclusive)
//创建uid索引
_, err = this.DB.CreateIndex(core.SqlTable(this.TableName), mongo.IndexModel{
Keys: bsonx.Doc{{Key: "uid", Value: bsonx.Int32(1)}},
})
return
}
func (this *modelComp) Start() (err error) {
this.MCompModel.Start()
return
}
// 更新埋点数据到db中
func (this *modelComp) getburiedModel(uid string) (model *exclusiveModel, err error) {
var m *db.DBModel
if db.IsCross() {
if m, err = this.module.GetDBModelByUid(uid, this.TableName); err != nil {
return
}
model = &exclusiveModel{module: this.module, model: m}
} else {
model = &exclusiveModel{module: this.module, model: this.DBModel}
}
return
}
// /查询用户的武器背包
func (this *modelComp) getExclusives(uId string) (list []*pb.DB_Exclusive, err error) {
var (
model *exclusiveModel
)
if model, err = this.getburiedModel(uId); err != nil {
return
}
list, err = model.getExclusives(uId)
return
}
// /查询用户的武器背包
func (this *modelComp) getExclusivesById(uId, oid string) (info *pb.DB_Exclusive, err error) {
var (
model *exclusiveModel
)
if model, err = this.getburiedModel(uId); err != nil {
return
}
info, err = model.getExclusivesById(uId, oid)
return
}
// /查询用户的武器背包
func (this *modelComp) getExclusivesByIds(uId string, oids []string) (list []*pb.DB_Exclusive, err error) {
var (
model *exclusiveModel
)
if model, err = this.getburiedModel(uId); err != nil {
return
}
list, err = model.getExclusivesByIds(uId, oids)
return
}
func (this *modelComp) addExclusives(uid string, cIds map[string]int32) (change []*pb.DB_Exclusive, err error) {
var (
model *exclusiveModel
)
if model, err = this.getburiedModel(uid); err != nil {
return
}
change, err = model.addExclusives(uid, cIds)
return
}
// 删除装备
func (this *modelComp) delExclusives(uId string, eIds []string) (change []*pb.DB_Exclusive, err error) {
var (
model *exclusiveModel
)
if model, err = this.getburiedModel(uId); err != nil {
return
}
if err = model.delExclusive(uId, eIds); err != nil {
return
}
for _, v := range eIds {
change = append(change, &pb.DB_Exclusive{
Id: v,
UId: uId,
})
}
return
}
// 更新武器挂载信息
func (this *modelComp) updateExclusive(uid string, exclusives ...*pb.DB_Exclusive) (err error) {
var (
model *exclusiveModel
)
if model, err = this.getburiedModel(uid); err != nil {
return
}
err = model.updateExclusive(uid, exclusives)
return
}
// 埋点专属模型 会封装特殊的数据转换接口
type exclusiveModel struct {
module *Exclusive
model *db.DBModel
}
// /查询用户的武器背包
func (this *exclusiveModel) getExclusives(uId string) (list []*pb.DB_Exclusive, err error) {
list = make([]*pb.DB_Exclusive, 0)
if err = this.model.GetList(uId, &list); err != nil {
this.module.Errorf("err:%v", err)
}
return
}
// /查询用户的武器背包
func (this *exclusiveModel) getExclusivesById(uId string, oid string) (info *pb.DB_Exclusive, err error) {
info = &pb.DB_Exclusive{}
if err = this.model.GetListObj(uId, oid, info); err != nil {
this.module.Errorf("err:%v", err)
}
return
}
// /查询用户的武器背包
func (this *exclusiveModel) getExclusivesByIds(uId string, oid []string) (list []*pb.DB_Exclusive, err error) {
list = make([]*pb.DB_Exclusive, 0)
if err = this.model.GetListObjs(uId, oid, &list); err != nil {
this.module.Errorf("err:%v", err)
}
return
}
func (this *exclusiveModel) addExclusives(uId string, cIds map[string]int32) (change []*pb.DB_Exclusive, err error) {
var (
conf *cfg.GameExclusiveWeapon
add map[string]*pb.DB_Exclusive
)
if conf, err = this.module.configure.GetAllGameExclusiveWeapon(); err != nil {
return
}
add = make(map[string]*pb.DB_Exclusive)
change = make([]*pb.DB_Exclusive, 0, 10)
for k, v := range cIds {
if c, ok := conf.GetDataMap()[k]; ok {
for i := int32(0); i < v; i++ {
if equipment, err := this.newEquipment(uId, c); err != nil {
return nil, err
} else {
add[equipment.Id] = equipment
change = append(change, equipment)
}
}
} else {
this.module.Errorf("cfg.Game_equipment not found equip id %s", k)
}
}
if len(add) > 0 {
if err = this.model.AddLists(uId, add); err != nil {
this.module.Errorf("err:%v", err)
return
}
}
return
}
func (this *exclusiveModel) delExclusive(uId string, oid []string) (err error) {
if err = this.model.DelListlds(uId, oid); err != nil {
this.module.Errorf("err:%v", err)
return
}
return
}
func (this *exclusiveModel) updateExclusive(uid string, list []*pb.DB_Exclusive) (err error) {
var (
datas map[string]interface{} = make(map[string]interface{})
)
for _, v := range list {
datas[v.Id] = map[string]interface{}{
"hero": v.Hero,
"star": v.Star,
"lv": v.Lv,
"step": v.Step,
"exp": v.Exp,
"property": v.Property,
"commonskill": v.Commonskill,
"exclusiveskill": v.Exclusiveskill,
"islock": v.Islock,
}
}
err = this.model.ChangeLists(uid, datas)
return
}
func (this *exclusiveModel) newEquipment(uId string, conf *cfg.GameExclusiveWeaponData) (equipment *pb.DB_Exclusive, err error) {
var (
lvconf *cfg.GameExclusiveUpgradeData
starconf *cfg.GameExclusiveStarData
)
equipment = &pb.DB_Exclusive{
Id: primitive.NewObjectID().Hex(),
UId: uId,
CId: conf.Weaponid,
Lv: 1,
Star: 1,
Step: 1,
Property: make(map[int32]int32),
}
if lvconf, err = this.module.configure.GetGameExclusiveUpgradeData(conf.Weaponid, 1); err != nil {
return
}
if starconf, err = this.module.configure.GetGameExclusiveStarData(conf.Weaponid, 1); err != nil {
return
}
for _, v := range lvconf.Attribute {
equipment.Property[v.A] = v.N
}
equipment.Commonskill = starconf.Commonskill
equipment.Exclusiveskill = starconf.Exclusiveskill
return
}