优化基础业务模块基类接口
This commit is contained in:
parent
84005d7974
commit
0b7aea4610
@ -14,7 +14,7 @@ import (
|
||||
type MComp_CacheComp struct {
|
||||
cbase.ModuleCompBase
|
||||
S base.IRPCXService //rpc服务对象
|
||||
M core.IModule //当前业务模块
|
||||
M IModule //当前业务模块
|
||||
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) {
|
||||
this.ModuleCompBase.Init(service, module, comp, options)
|
||||
this.S = service.(base.IRPCXService)
|
||||
this.M = module
|
||||
this.M = module.(IModule)
|
||||
this.Redis = cache.Redis()
|
||||
return
|
||||
}
|
||||
|
34
modules/configure_comp.go
Normal file
34
modules/configure_comp.go
Normal 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)
|
||||
}
|
@ -11,11 +11,24 @@ type (
|
||||
//业务模块基类接口 定义所有业务模块都可以使用的接口
|
||||
IModule interface {
|
||||
core.IModule
|
||||
API() IAPI_Comp
|
||||
Cache() ICache_Comp
|
||||
DB() IDB_Comp
|
||||
///向指定用户发送消息
|
||||
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)
|
||||
}
|
||||
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
|
||||
|
@ -14,7 +14,7 @@ import (
|
||||
type MComp_DBComp struct {
|
||||
cbase.ModuleCompBase
|
||||
S base.IRPCXService //rpc服务对象
|
||||
M core.IModule //当前业务模块
|
||||
M IModule //当前业务模块
|
||||
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) {
|
||||
this.ModuleCompBase.Init(service, module, comp, options)
|
||||
this.S = service.(base.IRPCXService)
|
||||
this.M = module
|
||||
this.M = module.(IModule)
|
||||
this.DB = db.Mgo()
|
||||
return
|
||||
}
|
||||
|
||||
func (this *MComp_DBComp) WriteMgoLog(log string) {
|
||||
|
||||
}
|
||||
|
@ -28,7 +28,7 @@ var typeOfError = reflect.TypeOf((*error)(nil)).Elem()
|
||||
type MComp_GateComp struct {
|
||||
cbase.ModuleCompBase
|
||||
S base.IRPCXService //rpc服务对象
|
||||
M core.IModule //当前业务模块
|
||||
M IModule //当前业务模块
|
||||
comp core.IModuleComp //网关组件自己
|
||||
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) {
|
||||
this.ModuleCompBase.Init(service, module, comp, options)
|
||||
this.S = service.(base.IRPCXService)
|
||||
this.M = module
|
||||
this.M = module.(IModule)
|
||||
this.comp = comp
|
||||
return
|
||||
}
|
||||
|
@ -20,7 +20,20 @@ import (
|
||||
*/
|
||||
type ModuleBase struct {
|
||||
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
|
||||
}
|
||||
|
||||
//模块初始化接口
|
||||
|
@ -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})
|
||||
if code == pb.ErrorCode_Success {
|
||||
go func() { //异步处理修改数据
|
||||
this.module.cache_comp.Pack_UpdateUserPack(session.GetUserId(), modifys...)
|
||||
this.module.cache_comp.Pack_DeleteUserPack(session.GetUserId(), dels...)
|
||||
this.module.Cache_Comp.(*Cache_Comp).Pack_UpdateUserPack(session.GetUserId(), modifys...)
|
||||
this.module.Cache_Comp.(*Cache_Comp).Pack_DeleteUserPack(session.GetUserId(), dels...)
|
||||
}()
|
||||
}
|
||||
}()
|
||||
if code = this.Getlist_Check(ctx, session, req); code != pb.ErrorCode_Success {
|
||||
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)
|
||||
code = pb.ErrorCode_CacheReadError
|
||||
return
|
||||
|
@ -15,13 +15,11 @@ import (
|
||||
///背包缓存数据管理组件
|
||||
type Cache_Comp struct {
|
||||
modules.MComp_CacheComp
|
||||
module *Pack
|
||||
}
|
||||
|
||||
//组件初始化接口
|
||||
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.module = module.(*Pack)
|
||||
return
|
||||
}
|
||||
|
||||
@ -38,7 +36,7 @@ func (this *Cache_Comp) Pack_QueryUserPack(uId string) (itmes []*pb.DB_UserItemD
|
||||
}
|
||||
return
|
||||
} 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{})
|
||||
for _, v := range itmes {
|
||||
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 {
|
||||
return
|
||||
} 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{})
|
||||
for _, v := range itmes {
|
||||
temp[v.GridId] = v
|
||||
@ -88,7 +86,7 @@ func (this *Cache_Comp) Pack_UpdateUserPack(uId string, itmes ...*pb.DB_UserItem
|
||||
temp[v.GridId] = v
|
||||
}
|
||||
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
|
||||
@ -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) {
|
||||
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
|
||||
}
|
||||
|
@ -2,12 +2,11 @@ package pack
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"go_dreamfactory/modules"
|
||||
"go_dreamfactory/pb"
|
||||
"go_dreamfactory/sys/configure"
|
||||
cfg "go_dreamfactory/sys/configure/structs"
|
||||
|
||||
"go_dreamfactory/lego/core"
|
||||
"go_dreamfactory/lego/core/cbase"
|
||||
"go_dreamfactory/lego/sys/log"
|
||||
)
|
||||
|
||||
@ -17,13 +16,13 @@ const (
|
||||
|
||||
///背包配置管理组件
|
||||
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) {
|
||||
this.ModuleCompBase.Init(service, module, comp, options)
|
||||
configure.RegisterConfigure(game_item, cfg.NewGame_item)
|
||||
this.LoadConfigure(game_item, cfg.NewGame_item)
|
||||
return
|
||||
}
|
||||
|
||||
@ -33,7 +32,7 @@ func (this *Configure_Comp) GetItemConfigure(id int32) (item *cfg.Game_itemData,
|
||||
v interface{}
|
||||
ok bool
|
||||
)
|
||||
if v, err = configure.GetConfigure(game_item); err != nil {
|
||||
if v, err = this.GetConfigure(game_item); err != nil {
|
||||
return
|
||||
} else {
|
||||
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
|
||||
err error
|
||||
)
|
||||
if v, err = configure.GetConfigure(game_item); err != nil {
|
||||
if v, err = this.GetConfigure(game_item); err != nil {
|
||||
return
|
||||
} else {
|
||||
table = v.(*cfg.Game_item)
|
||||
|
@ -20,9 +20,6 @@ func NewModule() core.IModule {
|
||||
|
||||
type Pack struct {
|
||||
modules.ModuleBase
|
||||
api_comp *Api_Comp //背包模块 协议处理组件
|
||||
cache_comp *Cache_Comp //缓存组件
|
||||
db_comp *DB_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() {
|
||||
this.ModuleBase.OnInstallComp()
|
||||
this.api_comp = this.RegisterComp(new(Api_Comp)).(*Api_Comp)
|
||||
this.cache_comp = this.RegisterComp(new(Cache_Comp)).(*Cache_Comp)
|
||||
this.db_comp = this.RegisterComp(new(DB_Comp)).(*DB_Comp)
|
||||
this.Api_Comp = this.RegisterComp(new(Api_Comp)).(*Api_Comp)
|
||||
this.Cache_Comp = this.RegisterComp(new(Cache_Comp)).(*Cache_Comp)
|
||||
this.Db_Comp = this.RegisterComp(new(DB_Comp)).(*DB_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) {
|
||||
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
|
||||
}
|
||||
|
||||
///添加单个物品到背包 (可以加物品和减物品)
|
||||
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)
|
||||
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)
|
||||
}
|
||||
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) {
|
||||
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)
|
||||
}
|
||||
return
|
||||
|
Loading…
Reference in New Issue
Block a user