go_dreamfactory/modules/privilege/configure.go
2022-12-21 19:42:35 +08:00

94 lines
2.4 KiB
Go

package privilege
import (
"fmt"
"go_dreamfactory/lego/core"
"go_dreamfactory/lego/sys/log"
"go_dreamfactory/modules"
"go_dreamfactory/sys/configure"
cfg "go_dreamfactory/sys/configure/structs"
"sync"
)
const (
game_privilegecard = "game_privilegecard.json"
game_privilege = "game_privilege.json"
)
///背包配置管理组件
type configureComp struct {
modules.MCompConfigure
module *Privilege
hlock sync.RWMutex
_privilegeMap map[int32]map[int32]int32
}
//组件初始化接口
func (this *configureComp) Init(service core.IService, module core.IModule, comp core.IModuleComp, options core.IModuleOptions) (err error) {
this.MCompConfigure.Init(service, module, comp, options)
this.module = module.(*Privilege)
this.LoadConfigure(game_privilegecard, cfg.NewGamePrivilegeCard)
this.LoadConfigure(game_privilege, cfg.NewGamePrivilege)
this._privilegeMap = make(map[int32]map[int32]int32, 0)
configure.RegisterConfigure(game_privilege, cfg.NewGamePrivilege, func() {
if v, err := this.GetConfigure(game_privilege); err == nil {
if configure, ok := v.(*cfg.GamePrivilege); ok {
this.hlock.Lock()
defer this.hlock.Unlock()
for k, v := range configure.GetDataMap() {
if v1, ok := this._privilegeMap[v.PrivilegeType]; ok {
v1[k] = v.PrivilegeParameter
} else {
this._privilegeMap[v.PrivilegeType] = make(map[int32]int32)
}
}
return
}
}
log.Errorf("get game_pagoda conf err:%v", err)
return
})
return
}
func (this *configureComp) GetPrivilegeCard(id string) (configure *cfg.GamePrivilegeCardData, err error) {
var (
v interface{}
ok bool
)
if v, err = this.GetConfigure(game_privilegecard); err != nil {
this.module.Errorf("err:%v", err)
return
} else {
if configure, ok = v.(*cfg.GamePrivilegeCard).GetDataMap()[id]; !ok {
err = fmt.Errorf("GamePrivilegeCardData not found:%s ", id)
this.module.Errorf("err:%v", err)
return
}
}
return
}
// 获取指定特权配置
func (this *configureComp) GetPrivilegeData(id int32) (result *cfg.GamePrivilegeData) {
if v, err := this.GetConfigure(game_privilege); err == nil {
if c, ok := v.(*cfg.GamePrivilege); ok {
result = c.Get(id)
return
}
}
this.module.Errorf("GetPrivilegeData err, id:%d", id)
return nil
}
func (this *configureComp) GetPrivilegeByType(iType int32) (result map[int32]int32) {
result = make(map[int32]int32)
if v, ok := this._privilegeMap[iType]; ok {
result = v
}
return result
}