go_dreamfactory/modules/dragon/model_dragon.go
2023-08-31 16:38:25 +08:00

98 lines
2.6 KiB
Go

package dragon
import (
"errors"
"go_dreamfactory/comm"
"go_dreamfactory/lego/core"
"go_dreamfactory/modules"
"go_dreamfactory/pb"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/x/bsonx"
)
type ModelDragon struct {
modules.MCompModel
module *Dragon
}
func (this *ModelDragon) Init(service core.IService, module core.IModule, comp core.IModuleComp, options core.IModuleOptions) (err error) {
this.TableName = comm.TableDragon
err = this.MCompModel.Init(service, module, comp, options)
this.module = module.(*Dragon)
// 通过uid创建索引
this.DB.CreateIndex(core.SqlTable(this.TableName), mongo.IndexModel{
Keys: bsonx.Doc{{Key: "uid", Value: bsonx.Int32(1)}},
})
return
}
// 获取一个新的坐骑
func (this *ModelDragon) AddDragon(session comm.IUserSession, dragonCfgId string) (dragon *pb.DBDragon, err error) {
dragonList := make([]*pb.DBDragon, 0)
uid := session.GetUserId()
if _, err = this.module.configure.GetDragonConfById(dragonCfgId, 1); err != nil {
err = errors.New("not found dragon dragonCfgId")
this.module.Errorf("not found dragon dragonCfgId:%s", dragonCfgId)
return
}
if this.module.IsCross() {
if dbModel, err1 := this.module.GetDBModelByUid(uid, this.TableName); err1 == nil {
if err = dbModel.GetList(uid, &dragonList); err != nil {
this.module.Errorf("err:%v", err)
return
}
} else {
this.module.Errorln(err) // 获取跨服对象失败
return
}
} else {
if err = this.GetList(uid, &dragonList); err != nil {
this.module.Errorf("err:%v", err)
}
}
for _, obj := range dragonList {
if obj.Dragonid == dragonCfgId { // 重复获得 直接返回
return
}
}
dragon = &pb.DBDragon{
Id: primitive.NewObjectID().Hex(),
Uid: uid,
Dragonid: dragonCfgId,
Lv: 1,
Exp: 0,
Property: map[int32]int32{},
}
if this.module.IsCross() {
if dbModel, err1 := this.module.GetDBModelByUid(uid, this.TableName); err1 == nil {
err = dbModel.AddList(uid, dragonCfgId, dragon)
}
} else {
err = this.AddList(uid, dragonCfgId, dragon)
}
return
}
// 获取坐骑列表
func (this *ModelDragon) GetDragonList(uid string) (dragon []*pb.DBDragon, err error) {
if this.module.IsCross() {
if dbModel, err1 := this.module.GetDBModelByUid(uid, this.TableName); err1 == nil {
if err = dbModel.GetList(uid, &dragon); err != nil {
this.module.Errorf("err:%v", err)
return
}
} else {
this.module.Errorln(err) // 获取跨服对象失败
return
}
} else {
if err = this.GetList(uid, &dragon); err != nil {
this.module.Errorf("err:%v", err)
}
}
return
}