go_dreamfactory/modules/sys/model_sys.go

142 lines
3.2 KiB
Go

package sys
import (
"go_dreamfactory/comm"
"go_dreamfactory/lego/core"
"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"
)
type ModelSys struct {
modules.MCompModel
moduleSys *ModuleSys
service core.IService
}
func (this *ModelSys) Init(service core.IService, module core.IModule, comp core.IModuleComp, options core.IModuleOptions) (err error) {
this.TableName = comm.TableOpenCond
err = this.MCompModel.Init(service, module, comp, options)
this.moduleSys = module.(*ModuleSys)
this.service = service
return
}
// 是否允许访问功能,条件:玩家等级
func (this *ModelSys) IsAccess(funName string, uid string) (errdata *pb.ErrorData) {
// user := this.moduleSys.ModuleUser.GetUser(uid)
// if user != nil {
// if conf := this.moduleSys.configure.getFuncCfg(funName); conf != nil {
// if conf.ActivateType == 1 { // 已经手动激活过
// list, _ := this.GetOpenCondList(uid)
// if v, ok := list.Cond[funName]; ok && v == 1 {
// return
// }
// } else {
// if id := this.validCond(uid, conf); id != "" { // 条件满足已经激活
// return
// }
// }
// }
// }
//code = pb.ErrorCode_OpenCondErr
return
}
func (this *ModelSys) validCond(uid string, condData *cfg.GameOpencondData) string {
for _, conf := range condData.Main {
switch conf.Key {
case 1: //等级
iuser := this.moduleSys.ModuleUser
user := iuser.GetUser(uid)
if user == nil {
return ""
}
if user.Lv < conf.Param {
return ""
}
case 2: //关卡ID
iuser := this.moduleSys.ModuleUser
ex, err := iuser.GetUserExpand(uid)
if err != nil {
return ""
}
if ex.Uid == "" {
return ""
}
if v, ok := ex.Mline[1]; ok {
if v < conf.Param {
return ""
}
} else {
return ""
}
case 3: //世界任务ID
module, err := this.service.GetModule(comm.ModuleWorldtask)
if err != nil {
this.moduleSys.Debugln(err)
return ""
}
if i, ok := module.(comm.IWorldtask); ok {
d := i.GetMyWorldtask(uid)
bFound := false
for _, taskId := range d.TaskList {
if taskId == conf.Param {
bFound = true
break
}
}
if !bFound {
return ""
}
} else {
return ""
}
case 4:
module, err := this.service.GetModule(comm.ModuleFriend)
if err != nil {
this.moduleSys.Debugln(err)
return ""
}
if v, ok := module.(comm.IFriend); ok {
if v.GetFriendCount(uid) < conf.Param {
return ""
}
}
}
}
return condData.Id
}
func (this *ModelSys) GetOpenCondList(uid string) (result *pb.DBOpenCond, err error) {
result = &pb.DBOpenCond{}
if err = this.Get(uid, result); err != nil {
if mongo.ErrNoDocuments == err { // 创建一条新的数据
result = &pb.DBOpenCond{
Id: primitive.NewObjectID().Hex(),
Uid: uid,
Cond: map[string]int32{},
}
this.Add(uid, result)
err = nil
}
}
return result, err
}
// 修改OpenCond 数据
func (this *ModelSys) ChangeOpenCondData(uid string, value map[string]interface{}) (err error) {
if len(value) == 0 {
return nil
}
return this.Change(uid, value)
}