go_dreamfactory/modules/mainline/model_mainline.go
2023-07-27 11:51:33 +08:00

158 lines
4.3 KiB
Go

package mainline
import (
"fmt"
"go_dreamfactory/comm"
"go_dreamfactory/lego/core"
"go_dreamfactory/lego/sys/log"
"go_dreamfactory/lego/sys/mgo"
"go_dreamfactory/modules"
"go_dreamfactory/pb"
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 ModelMline struct {
modules.MCompModel
module *Mainline
}
func (this *ModelMline) Init(service core.IService, module core.IModule, comp core.IModuleComp, options core.IModuleOptions) (err error) {
this.TableName = comm.TableMainline
err = this.MCompModel.Init(service, module, comp, options)
this.module = module.(*Mainline)
//创建uid索引
this.DB.CreateIndex(core.SqlTable(this.TableName), mongo.IndexModel{
Keys: bsonx.Doc{{Key: "uid", Value: bsonx.Int32(1)}},
})
return
}
// 获取用户全部的埋点数据
func (this *ModelMline) getMainlineData(uid string) (results *pb.DBMainline, err error) {
results = &pb.DBMainline{}
if err = this.Get(uid, results); err != nil && err != mgo.MongodbNil {
this.module.Errorln(err)
return
}
if err == mgo.MongodbNil {
err = nil
results = &pb.DBMainline{
Id: primitive.NewObjectID().Hex(),
Uid: uid,
Level: make(map[int32]int32),
Chapteraward: make(map[int32]*pb.DBMainlineAward),
Exploreaward: make(map[int32]*pb.DBMainlineAward),
Groupaward: make(map[int32]*pb.DBMainlineAward),
Ps: make(map[int32]int32),
}
err = this.Add(uid, results)
}
return
}
func (this *ModelMline) updateMainlineData(uid string, data *pb.DBMainline) (err error) {
if err = this.Change(uid, map[string]interface{}{
"Level": data.Level,
"chapteraward": data.Chapteraward,
"exploreaward": data.Exploreaward,
"groupaward": data.Groupaward,
"ps": data.Ps,
}); err != nil {
this.module.Error("更新用户任务数据 错误!", log.Field{Key: "err", Value: err.Error()})
return
}
return
}
// 更新主线进度
func (this *ModelMline) updateprogress(data *pb.DBMainline) {
var (
conf *cfg.GameMainStageData
chapteraward map[int32]*pb.DBMainlineAward = make(map[int32]*pb.DBMainlineAward)
exploreaward map[int32]*pb.DBMainlineAward = make(map[int32]*pb.DBMainlineAward)
groupaward map[int32]*pb.DBMainlineAward = make(map[int32]*pb.DBMainlineAward)
err error
ok bool
)
for k, v := range data.Level {
if conf, err = this.module.configure.GetMainStageConf(k); err != nil { // 配置文件校验
this.module.Errorln(err)
return
}
if conf.Progress == 1 {
if _, ok = chapteraward[conf.Chapterid]; !ok {
chapteraward[conf.Chapterid] = &pb.DBMainlineAward{}
}
chapteraward[conf.Chapterid].Stage++
}
if _, ok = exploreaward[conf.Chapterid]; !ok && conf.Episodetype != 8 {
exploreaward[conf.Chapterid] = &pb.DBMainlineAward{}
}
exploreaward[conf.Chapterid].Stage++
if _, ok = groupaward[conf.GroupId]; !ok {
groupaward[conf.GroupId] = &pb.DBMainlineAward{}
}
for i := 0; i < len(conf.Star); i++ {
if (1<<i)&v > 0 {
groupaward[conf.GroupId].Stage++
}
}
}
for k, v := range chapteraward {
if _, ok = data.Chapteraward[k]; !ok {
data.Chapteraward[k] = v
} else {
data.Chapteraward[k].Stage = v.Stage
}
}
for k, v := range exploreaward {
if _, ok = data.Exploreaward[k]; !ok {
data.Exploreaward[k] = v
} else {
data.Exploreaward[k].Stage = v.Stage
}
}
for k, v := range groupaward {
if _, ok = data.Groupaward[k]; !ok {
data.Groupaward[k] = v
} else {
data.Groupaward[k].Stage = v.Stage
}
}
}
// 校验管卡是否触发
func (this *ModelMline) checklevel(level int32, data *pb.DBMainline) (err error) {
var (
conf *cfg.GameMainStageData
ok bool
)
if conf, err = this.module.configure.GetMainStageConf(level); err != nil { // 配置文件校验
this.module.Errorln(err)
return
}
if conf.Previoustage != 0 {
if _, ok = data.Level[conf.Previoustage]; !ok {
err = fmt.Errorf("previoustage:%d no pass", conf.Previoustage)
return
}
}
if conf.PreviousGroupId != nil && len(conf.PreviousGroupId) > 0 {
for _, gid := range conf.PreviousGroupId {
if _, ok = data.Groupaward[gid]; !ok {
err = fmt.Errorf("PreviousGroupId:%d no pass", gid)
return
}
}
}
return
}