优化基础业务模块基类接口

This commit is contained in:
liwei1dao 2022-06-14 11:40:32 +08:00
parent 84005d7974
commit 0b7aea4610
10 changed files with 89 additions and 31 deletions

View File

@ -14,7 +14,7 @@ import (
type MComp_CacheComp struct { type MComp_CacheComp struct {
cbase.ModuleCompBase cbase.ModuleCompBase
S base.IRPCXService //rpc服务对象 S base.IRPCXService //rpc服务对象
M core.IModule //当前业务模块 M IModule //当前业务模块
Redis redis.ISys Redis redis.ISys
} }
@ -22,7 +22,7 @@ type MComp_CacheComp struct {
func (this *MComp_CacheComp) Init(service core.IService, module core.IModule, comp core.IModuleComp, options core.IModuleOptions) (err error) { func (this *MComp_CacheComp) Init(service core.IService, module core.IModule, comp core.IModuleComp, options core.IModuleOptions) (err error) {
this.ModuleCompBase.Init(service, module, comp, options) this.ModuleCompBase.Init(service, module, comp, options)
this.S = service.(base.IRPCXService) this.S = service.(base.IRPCXService)
this.M = module this.M = module.(IModule)
this.Redis = cache.Redis() this.Redis = cache.Redis()
return return
} }

34
modules/configure_comp.go Normal file
View File

@ -0,0 +1,34 @@
package modules
import (
"go_dreamfactory/lego/base"
"go_dreamfactory/lego/core"
"go_dreamfactory/lego/core/cbase"
"go_dreamfactory/sys/configure"
)
///配置管理基础组件
type MComp_Configure struct {
cbase.ModuleCompBase
S base.IRPCXService //rpc服务对象
M IModule //当前业务模块
}
//组件初始化接口
func (this *MComp_Configure) Init(service core.IService, module core.IModule, comp core.IModuleComp, options core.IModuleOptions) (err error) {
this.ModuleCompBase.Init(service, module, comp, options)
this.S = service.(base.IRPCXService)
this.M = module.(IModule)
return
}
//加载配置文件
func (this *MComp_Configure) LoadConfigure(name string, fn interface{}) (err error) {
configure.RegisterConfigure(name, fn)
return
}
//读取配置数据
func (this *MComp_Configure) GetConfigure(name string) (v interface{}, err error) {
return configure.GetConfigure(name)
}

View File

@ -11,11 +11,24 @@ type (
//业务模块基类接口 定义所有业务模块都可以使用的接口 //业务模块基类接口 定义所有业务模块都可以使用的接口
IModule interface { IModule interface {
core.IModule core.IModule
API() IAPI_Comp
Cache() ICache_Comp
DB() IDB_Comp
///向指定用户发送消息 ///向指定用户发送消息
SendMsgToUser(mainType, subType string, msg proto.Message, user *pb.Cache_UserData) (err error) SendMsgToUser(mainType, subType string, msg proto.Message, user *pb.Cache_UserData) (err error)
///向多个用户发送消息 ///向多个用户发送消息
SendMsgToUsers(mainType, subType string, msg proto.Message, user ...*pb.Cache_UserData) (err error) SendMsgToUsers(mainType, subType string, msg proto.Message, user ...*pb.Cache_UserData) (err error)
} }
IAPI_Comp interface {
}
ICache_Comp interface {
}
IDB_Comp interface {
}
IConfigure_Comp interface {
LoadConfigure(name string, fn interface{}) (err error)
GetConfigure(name string) (v interface{}, err error)
}
) )
type LogHandleType string type LogHandleType string

View File

@ -14,7 +14,7 @@ import (
type MComp_DBComp struct { type MComp_DBComp struct {
cbase.ModuleCompBase cbase.ModuleCompBase
S base.IRPCXService //rpc服务对象 S base.IRPCXService //rpc服务对象
M core.IModule //当前业务模块 M IModule //当前业务模块
DB mgo.ISys DB mgo.ISys
} }
@ -22,7 +22,11 @@ type MComp_DBComp struct {
func (this *MComp_DBComp) Init(service core.IService, module core.IModule, comp core.IModuleComp, options core.IModuleOptions) (err error) { func (this *MComp_DBComp) Init(service core.IService, module core.IModule, comp core.IModuleComp, options core.IModuleOptions) (err error) {
this.ModuleCompBase.Init(service, module, comp, options) this.ModuleCompBase.Init(service, module, comp, options)
this.S = service.(base.IRPCXService) this.S = service.(base.IRPCXService)
this.M = module this.M = module.(IModule)
this.DB = db.Mgo() this.DB = db.Mgo()
return return
} }
func (this *MComp_DBComp) WriteMgoLog(log string) {
}

View File

@ -28,7 +28,7 @@ var typeOfError = reflect.TypeOf((*error)(nil)).Elem()
type MComp_GateComp struct { type MComp_GateComp struct {
cbase.ModuleCompBase cbase.ModuleCompBase
S base.IRPCXService //rpc服务对象 S base.IRPCXService //rpc服务对象
M core.IModule //当前业务模块 M IModule //当前业务模块
comp core.IModuleComp //网关组件自己 comp core.IModuleComp //网关组件自己
scomp comm.ISC_GateRouteComp scomp comm.ISC_GateRouteComp
} }
@ -37,7 +37,7 @@ type MComp_GateComp struct {
func (this *MComp_GateComp) Init(service core.IService, module core.IModule, comp core.IModuleComp, options core.IModuleOptions) (err error) { func (this *MComp_GateComp) Init(service core.IService, module core.IModule, comp core.IModuleComp, options core.IModuleOptions) (err error) {
this.ModuleCompBase.Init(service, module, comp, options) this.ModuleCompBase.Init(service, module, comp, options)
this.S = service.(base.IRPCXService) this.S = service.(base.IRPCXService)
this.M = module this.M = module.(IModule)
this.comp = comp this.comp = comp
return return
} }

View File

@ -21,6 +21,19 @@ import (
type ModuleBase struct { type ModuleBase struct {
cbase.ModuleBase cbase.ModuleBase
service base.IRPCXService service base.IRPCXService
Api_Comp IAPI_Comp
Cache_Comp ICache_Comp
Db_Comp IDB_Comp
}
func (this *ModuleBase) API() IAPI_Comp {
return this.Api_Comp
}
func (this *ModuleBase) Cache() ICache_Comp {
return this.Cache_Comp
}
func (this *ModuleBase) DB() IDB_Comp {
return this.Db_Comp
} }
//模块初始化接口 //模块初始化接口

View File

@ -32,15 +32,15 @@ func (this *Api_Comp) Getlist(ctx context.Context, session comm.IUserSession, re
session.SendMsg(string(this.module.GetType()), GetlistResp, code, &pb.GetlistResp{Grids: grids}) session.SendMsg(string(this.module.GetType()), GetlistResp, code, &pb.GetlistResp{Grids: grids})
if code == pb.ErrorCode_Success { if code == pb.ErrorCode_Success {
go func() { //异步处理修改数据 go func() { //异步处理修改数据
this.module.cache_comp.Pack_UpdateUserPack(session.GetUserId(), modifys...) this.module.Cache_Comp.(*Cache_Comp).Pack_UpdateUserPack(session.GetUserId(), modifys...)
this.module.cache_comp.Pack_DeleteUserPack(session.GetUserId(), dels...) this.module.Cache_Comp.(*Cache_Comp).Pack_DeleteUserPack(session.GetUserId(), dels...)
}() }()
} }
}() }()
if code = this.Getlist_Check(ctx, session, req); code != pb.ErrorCode_Success { if code = this.Getlist_Check(ctx, session, req); code != pb.ErrorCode_Success {
return return
} }
if items, err = this.module.cache_comp.Pack_QueryUserPack(session.GetUserId()); err != nil { if items, err = this.module.Cache_Comp.(*Cache_Comp).Pack_QueryUserPack(session.GetUserId()); err != nil {
log.Errorf("QueryUserPackReq err:%v", err) log.Errorf("QueryUserPackReq err:%v", err)
code = pb.ErrorCode_CacheReadError code = pb.ErrorCode_CacheReadError
return return

View File

@ -15,13 +15,11 @@ import (
///背包缓存数据管理组件 ///背包缓存数据管理组件
type Cache_Comp struct { type Cache_Comp struct {
modules.MComp_CacheComp modules.MComp_CacheComp
module *Pack
} }
//组件初始化接口 //组件初始化接口
func (this *Cache_Comp) Init(service core.IService, module core.IModule, comp core.IModuleComp, options core.IModuleOptions) (err error) { func (this *Cache_Comp) Init(service core.IService, module core.IModule, comp core.IModuleComp, options core.IModuleOptions) (err error) {
this.ModuleCompBase.Init(service, module, comp, options) this.ModuleCompBase.Init(service, module, comp, options)
this.module = module.(*Pack)
return return
} }
@ -38,7 +36,7 @@ func (this *Cache_Comp) Pack_QueryUserPack(uId string) (itmes []*pb.DB_UserItemD
} }
return return
} else if err == redis.RedisNil { } else if err == redis.RedisNil {
if itmes, err = this.module.db_comp.Pack_QueryUserPack(uId); err == nil { if itmes, err = this.M.DB().(*DB_Comp).Pack_QueryUserPack(uId); err == nil {
temp = make(map[string]interface{}) temp = make(map[string]interface{})
for _, v := range itmes { for _, v := range itmes {
temp[v.GridId] = v temp[v.GridId] = v
@ -61,7 +59,7 @@ func (this *Cache_Comp) Pack_QueryUserPackByGridId(uId string, grid string) (itm
if err = this.Redis.HGet(fmt.Sprintf(Redis_PackCache, uId), grid, itme); err == nil { if err = this.Redis.HGet(fmt.Sprintf(Redis_PackCache, uId), grid, itme); err == nil {
return return
} else if err == redis.RedisNil { } else if err == redis.RedisNil {
if itmes, err = this.module.db_comp.Pack_QueryUserPack(uId); err == nil { if itmes, err = this.M.DB().(*DB_Comp).Pack_QueryUserPack(uId); err == nil {
temp = make(map[string]interface{}) temp = make(map[string]interface{})
for _, v := range itmes { for _, v := range itmes {
temp[v.GridId] = v temp[v.GridId] = v
@ -88,7 +86,7 @@ func (this *Cache_Comp) Pack_UpdateUserPack(uId string, itmes ...*pb.DB_UserItem
temp[v.GridId] = v temp[v.GridId] = v
} }
if err = this.Redis.HMSet(fmt.Sprintf(Redis_PackCache, uId), temp); err != nil { if err = this.Redis.HMSet(fmt.Sprintf(Redis_PackCache, uId), temp); err != nil {
this.module.db_comp.Pack_UpdateGridToUserPack(uId, itmes...) this.M.DB().(*DB_Comp).Pack_UpdateGridToUserPack(uId, itmes...)
} }
return return
@ -97,7 +95,7 @@ func (this *Cache_Comp) Pack_UpdateUserPack(uId string, itmes ...*pb.DB_UserItem
//更新用户的背包信息 //更新用户的背包信息
func (this *Cache_Comp) Pack_DeleteUserPack(uId string, gridIds ...string) (err error) { func (this *Cache_Comp) Pack_DeleteUserPack(uId string, gridIds ...string) (err error) {
if err = this.Redis.HDel(fmt.Sprintf(Redis_PackCache, uId), gridIds...); err != nil { if err = this.Redis.HDel(fmt.Sprintf(Redis_PackCache, uId), gridIds...); err != nil {
err = this.module.db_comp.Pack_DeleteGridToUserPack(uId, gridIds...) err = this.M.DB().(*DB_Comp).Pack_DeleteGridToUserPack(uId, gridIds...)
} }
return return
} }

View File

@ -2,12 +2,11 @@ package pack
import ( import (
"fmt" "fmt"
"go_dreamfactory/modules"
"go_dreamfactory/pb" "go_dreamfactory/pb"
"go_dreamfactory/sys/configure"
cfg "go_dreamfactory/sys/configure/structs" cfg "go_dreamfactory/sys/configure/structs"
"go_dreamfactory/lego/core" "go_dreamfactory/lego/core"
"go_dreamfactory/lego/core/cbase"
"go_dreamfactory/lego/sys/log" "go_dreamfactory/lego/sys/log"
) )
@ -17,13 +16,13 @@ const (
///背包配置管理组件 ///背包配置管理组件
type Configure_Comp struct { type Configure_Comp struct {
cbase.ModuleCompBase modules.MComp_Configure
} }
//组件初始化接口 //组件初始化接口
func (this *Configure_Comp) Init(service core.IService, module core.IModule, comp core.IModuleComp, options core.IModuleOptions) (err error) { func (this *Configure_Comp) Init(service core.IService, module core.IModule, comp core.IModuleComp, options core.IModuleOptions) (err error) {
this.ModuleCompBase.Init(service, module, comp, options) this.ModuleCompBase.Init(service, module, comp, options)
configure.RegisterConfigure(game_item, cfg.NewGame_item) this.LoadConfigure(game_item, cfg.NewGame_item)
return return
} }
@ -33,7 +32,7 @@ func (this *Configure_Comp) GetItemConfigure(id int32) (item *cfg.Game_itemData,
v interface{} v interface{}
ok bool ok bool
) )
if v, err = configure.GetConfigure(game_item); err != nil { if v, err = this.GetConfigure(game_item); err != nil {
return return
} else { } else {
if item, ok = v.(*cfg.Game_item).GetDataMap()[id]; !ok { if item, ok = v.(*cfg.Game_item).GetDataMap()[id]; !ok {
@ -55,7 +54,7 @@ func (this *Configure_Comp) GetPackItemByType(itmes []*pb.DB_UserItemData, usety
ok bool ok bool
err error err error
) )
if v, err = configure.GetConfigure(game_item); err != nil { if v, err = this.GetConfigure(game_item); err != nil {
return return
} else { } else {
table = v.(*cfg.Game_item) table = v.(*cfg.Game_item)

View File

@ -20,9 +20,6 @@ func NewModule() core.IModule {
type Pack struct { type Pack struct {
modules.ModuleBase modules.ModuleBase
api_comp *Api_Comp //背包模块 协议处理组件
cache_comp *Cache_Comp //缓存组件
db_comp *DB_Comp //存储组件
configure_comp *Configure_Comp //背包模块 配置相关接口封装组件 configure_comp *Configure_Comp //背包模块 配置相关接口封装组件
} }
@ -40,9 +37,9 @@ func (this *Pack) Init(service core.IService, module core.IModule, options core.
//装备组件 //装备组件
func (this *Pack) OnInstallComp() { func (this *Pack) OnInstallComp() {
this.ModuleBase.OnInstallComp() this.ModuleBase.OnInstallComp()
this.api_comp = this.RegisterComp(new(Api_Comp)).(*Api_Comp) this.Api_Comp = this.RegisterComp(new(Api_Comp)).(*Api_Comp)
this.cache_comp = this.RegisterComp(new(Cache_Comp)).(*Cache_Comp) this.Cache_Comp = this.RegisterComp(new(Cache_Comp)).(*Cache_Comp)
this.db_comp = this.RegisterComp(new(DB_Comp)).(*DB_Comp) this.Db_Comp = this.RegisterComp(new(DB_Comp)).(*DB_Comp)
this.configure_comp = this.RegisterComp(new(Configure_Comp)).(*Configure_Comp) this.configure_comp = this.RegisterComp(new(Configure_Comp)).(*Configure_Comp)
} }
@ -50,14 +47,14 @@ func (this *Pack) OnInstallComp() {
///查询用户背包物品数量 ///查询用户背包物品数量
func (this *Pack) QueryUserPackItemAmount(uId string, itemid int32) (amount uint32) { func (this *Pack) QueryUserPackItemAmount(uId string, itemid int32) (amount uint32) {
defer log.Debugf("获取物品 uId:%s itemid:%d addnum:%d ", uId, itemid, amount) defer log.Debugf("获取物品 uId:%s itemid:%d addnum:%d ", uId, itemid, amount)
amount = this.cache_comp.Pack_QueryUserPackItemAmount(uId, itemid) amount = this.Cache_Comp.(*Cache_Comp).Pack_QueryUserPackItemAmount(uId, itemid)
return return
} }
///添加单个物品到背包 (可以加物品和减物品) ///添加单个物品到背包 (可以加物品和减物品)
func (this *Pack) AddItemToUserPack(uId string, itemid, addnum int32) (err error) { func (this *Pack) AddItemToUserPack(uId string, itemid, addnum int32) (err error) {
defer log.Debugf("给用户添加物品 uId:%s itemid:%d addnum:%d issucc:%v", uId, itemid, addnum, err == nil) defer log.Debugf("给用户添加物品 uId:%s itemid:%d addnum:%d issucc:%v", uId, itemid, addnum, err == nil)
if err = this.cache_comp.Pack_AddItemToUserPack(uId, itemid, addnum); err != nil { if err = this.Cache_Comp.(*Cache_Comp).Pack_AddItemToUserPack(uId, itemid, addnum); err != nil {
log.Errorf("给用户添加物品 uId:%s itemid:%d addnum:%d err:%v", uId, itemid, addnum, err) log.Errorf("给用户添加物品 uId:%s itemid:%d addnum:%d err:%v", uId, itemid, addnum, err)
} }
return return
@ -66,7 +63,7 @@ func (this *Pack) AddItemToUserPack(uId string, itemid, addnum int32) (err error
///添加多个物品到背包 (可以加物品和减物品) ///添加多个物品到背包 (可以加物品和减物品)
func (this *Pack) AddItemsToUserPack(uId string, items map[int32]int32) (err error) { func (this *Pack) AddItemsToUserPack(uId string, items map[int32]int32) (err error) {
defer log.Debugf("给用户添加物品 uId:%s items:%d items:%v", uId, items, err == nil) defer log.Debugf("给用户添加物品 uId:%s items:%d items:%v", uId, items, err == nil)
if err = this.cache_comp.Pack_AddItemsToUserPack(uId, items); err != nil { if err = this.Cache_Comp.(*Cache_Comp).Pack_AddItemsToUserPack(uId, items); err != nil {
log.Errorf("给用户添加物品 uId:%s items:%d err:%v", uId, items, err) log.Errorf("给用户添加物品 uId:%s items:%d err:%v", uId, items, err)
} }
return return