go_dreamfactory/modules/plunder/model_plunder.go
2024-01-26 22:20:59 +08:00

231 lines
5.6 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_dreamfactory/sys/db"
mgooptions "go.mongodb.org/mongo-driver/mongo/options"
"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)}},
Options: mgooptions.Index().SetUnique(true),
})
this.DB.CreateIndex(core.SqlTable(this.TableName), mongo.IndexModel{
Keys: bsonx.Doc{{Key: "score", Value: bsonx.Int32(1)}},
})
this.DB.CreateIndex(core.SqlTable(this.TableName), mongo.IndexModel{
Keys: bsonx.Doc{{Key: "landid", Value: bsonx.Int32(1)}},
})
return
}
func (this *modelPlunder) initplayerData(uid string) (err error) {
info := &pb.DBPlunder{
Id: primitive.NewObjectID().Hex(),
Uid: uid,
Ctime: configure.Now().Unix(),
Develop: make(map[int32]int32),
}
for i := 0; i < 3; i++ { // 队列固定三条
info.Line = append(info.Line, &pb.TransportLine{})
}
info.Line = append(info.Line, &pb.TransportLine{
Closetime: -1, // 需要手动解锁
})
// 刷新货物信息
info.Source, _ = this.refreshGoodsInfo()
_, err = this.DB.InsertOne(core.SqlTable(comm.TablePlunder), info)
return
}
// 获取基本数据
func (this *modelPlunder) getPlunderData(session comm.IUserSession) (info *pb.DBPlunder, err error) {
var (
user *pb.DBUser
)
info = &pb.DBPlunder{}
if err = this.Get(session.GetUserId(), info); err != nil && err != mgo.MongodbNil {
this.module.Errorln(err)
return
}
if err == mgo.MongodbNil {
user, err = this.module.GetUserForSession(session)
if err != nil {
this.module.Errorln(err)
return
}
info = &pb.DBPlunder{
Id: primitive.NewObjectID().Hex(),
Uid: session.GetUserId(),
Uinfo: comm.GetUserBaseInfo(user),
Ctime: configure.Now().Unix(),
Develop: make(map[int32]int32),
}
for i := 0; i < 3; i++ { // 队列固定三条
info.Line = append(info.Line, &pb.TransportLine{})
}
info.Line = append(info.Line, &pb.TransportLine{
Closetime: -1, // 需要手动解锁
})
// 刷新货物信息
info.Source, _ = this.refreshGoodsInfo()
err = this.Add(session.GetUserId(), 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
}
func (this *modelPlunder) queryUserHeros(uid string, heroids []string) (results []*pb.DBHero, err error) {
var (
model *db.DBModel
ids []string
resultstemp []*pb.DBHero
)
if model, err = this.module.GetDBModelByUid(uid, comm.TableHero); err != nil {
this.module.Errorln(err)
return
}
for _, v := range heroids {
if v != "" {
ids = append(ids, v)
}
}
resultstemp = make([]*pb.DBHero, 0)
if err = model.GetListObjs(uid, ids, &resultstemp); err != nil {
this.module.Errorln(err)
return
}
results = make([]*pb.DBHero, len(heroids))
for i1, v1 := range resultstemp {
for i2, _ := range results {
if i1 == i2 {
results[i2] = v1
}
}
}
return
}
func (this *modelPlunder) queryPlunderInfos(uids []string, landid string) (data []*pb.DBPlunder, err error) {
var (
mp map[string]struct{} // 没找到的数据
results []*pb.DBPlunder
//onfound []string
newdata map[string]interface{} = make(map[string]interface{})
unfound []string
)
results = make([]*pb.DBPlunder, 0)
mp = make(map[string]struct{})
if _, err = this.GetByUids(uids, &results); err != nil {
this.module.Errorln(err)
return
}
for _, v := range results {
mp[v.Uid] = struct{}{}
}
for _, v := range uids {
if _, ok := mp[v]; !ok {
unfound = append(unfound, v)
}
}
for _, v := range unfound {
temp := &pb.DBPlunder{
Id: primitive.NewObjectID().Hex(),
Uid: v,
Ctime: configure.Now().Unix(),
Landid: landid,
Develop: make(map[int32]int32),
}
for i := 0; i < 3; i++ { // 队列固定三条
temp.Line = append(temp.Line, &pb.TransportLine{})
}
temp.Line = append(temp.Line, &pb.TransportLine{
Closetime: -1, // 需要手动解锁
})
newdata[v] = temp
data = append(data, temp)
if len(newdata) >= 20 {
break
}
}
if len(newdata) > 0 {
if err = this.Adds(newdata); err != nil {
this.module.Errorln(err)
return
}
}
for _, v := range results {
if v.Landid == "" {
this.Change(v.Uid, map[string]interface{}{
"landid": landid,
})
data = append(data, v)
if len(newdata) >= 20 {
break
}
}
}
return
}