go_dreamfactory/modules/whackamole/model.go
2023-12-27 10:23:22 +08:00

72 lines
1.9 KiB
Go

package whackamole
import (
"go_dreamfactory/comm"
"go_dreamfactory/lego/core"
"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 modelComp struct {
modules.MCompModel
module *Whackamole
}
func (this *modelComp) 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.TableWhackamole
this.DB.CreateIndex(core.SqlTable(this.TableName), mongo.IndexModel{
Keys: bsonx.Doc{{Key: "uid", Value: bsonx.Int32(1)}},
})
return
}
// 获取用户全部的埋点数据
func (this *modelComp) getModel(uid string) (info *pb.DBWhackamole, err error) {
info = &pb.DBWhackamole{}
if err = this.Get(uid, info); err != nil && err != mgo.MongodbNil {
this.module.Errorln(err)
return
}
if err == mgo.MongodbNil {
var confs []*cfg.GameTDHeroData
info = &pb.DBWhackamole{
Id: primitive.NewObjectID().Hex(),
Uid: uid,
Levels: make(map[int32]int32),
Fields: make([]*pb.HeroField, 0),
Heros: make([]*pb.DBWHHero, 0),
Lasttime: 0,
}
info.Fields = append(info.Fields, &pb.HeroField{Ftype: pb.HeroFieldType_Type1}, &pb.HeroField{Index: 1, Ftype: pb.HeroFieldType_Type2}, &pb.HeroField{Index: 2, Ftype: pb.HeroFieldType_Type3})
if confs, err = this.module.configure.getGameTDHeroDatas(); err != nil {
return
}
for _, conf := range confs {
this.addhero(info, conf)
}
err = this.Add(uid, info)
}
return
}
///添加英雄
func (this *modelComp) addhero(info *pb.DBWhackamole, conf *cfg.GameTDHeroData) {
var (
tdhero *pb.DBWHHero
)
tdhero = &pb.DBWHHero{
Id: primitive.NewObjectID().Hex(),
Cid: conf.Id,
Wake: false,
}
info.Heros = append(info.Heros, tdhero)
return
}