go_dreamfactory/modules/plunder/model_plunder.go

92 lines
2.3 KiB
Go

package plunder
import (
"go_dreamfactory/comm"
"go_dreamfactory/lego/core"
"go_dreamfactory/lego/sys/mgo"
"go_dreamfactory/modules"
"go_dreamfactory/pb"
"go_dreamfactory/sys/configure"
cfg "go_dreamfactory/sys/configure/structs"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/x/bsonx"
)
type modelPlunder struct {
modules.MCompModel
module *Plunder
}
func (this *modelPlunder) Init(service core.IService, module core.IModule, comp core.IModuleComp, options core.IModuleOptions) (err error) {
err = this.MCompModel.Init(service, module, comp, options)
this.TableName = comm.TablePlunder
this.module = module.(*Plunder)
this.DB.CreateIndex(core.SqlTable(this.TableName), mongo.IndexModel{
Keys: bsonx.Doc{{Key: "uid", Value: bsonx.Int32(1)}},
})
return
}
// 获取基本数据
func (this *modelPlunder) getPlunderData(uid string) (info *pb.DBPlunder, err error) {
info = &pb.DBPlunder{}
if err = this.Get(uid, info); err != nil && err != mgo.MongodbNil {
this.module.Errorln(err)
return
}
if err == mgo.MongodbNil {
info = &pb.DBPlunder{
Id: primitive.NewObjectID().Hex(),
Uid: uid,
Ctime: configure.Now().Unix(),
}
for i := 0; i < 3; i++ { // 队列固定三条
info.Line = append(info.Line, &pb.PlunderLine{})
}
info.Line = append(info.Line, &pb.PlunderLine{
Closetime: -1, // 需要手动解锁
})
// 刷新货物信息
info.Source, _ = this.refreshGoodsInfo()
err = this.Add(uid, info)
}
return
}
func (this *modelPlunder) changePlunderData(uid string, update map[string]interface{}) (err error) {
err = this.Change(uid, update)
return
}
// 批量查询
func (this *modelPlunder) getPlunderDataByUids(uid []string) (info []*pb.DBPlunder, err error) {
if _, err = this.GetByUids(uid, &info); err != nil {
this.module.Errorln(err)
return
}
return
}
// 刷新货物信息
func (this *modelPlunder) refreshGoodsInfo() (cids []int32, err error) {
var (
result []*cfg.GamePlunderData
szW []int32
szIndex []int // 随机索引值
)
if result, err = this.module.configure.getGamePlunderData(); err != nil {
return
}
for _, v := range result {
szW = append(szW, v.Weight)
}
szIndex = comm.GetRandWs(szW, 6)
for _, v := range szIndex {
cids = append(cids, result[v].Id)
}
return
}