From 2bb353817ee421a78c03dbd8357692dd050cb444 Mon Sep 17 00:00:00 2001 From: liwei1dao Date: Wed, 13 Jul 2022 15:06:48 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=8A=E4=BC=A0=E6=97=A5=E5=BF=97=E4=BC=98?= =?UTF-8?q?=E5=8C=96=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- comm/const.go | 1 + lego/sys/log/core.go | 5 +++- modules/gm/options.go | 5 ++++ modules/items/api_getlist.go | 3 +-- modules/items/configure.go | 13 +++++----- modules/items/modelitems.go | 28 ++++++++++++++++++---- modules/items/module.go | 11 ++++----- modules/modulebase.go | 39 ++++++++++++++++++++++++++++++ modules/notify/configure.go | 23 ------------------ modules/notify/modelNotify.go | 5 ++-- modules/notify/module.go | 2 -- modules/options.go | 42 +++++++++++++++++++++++++++++++++ modules/shop/configure.go | 17 ++++++------- modules/shop/model_shopitems.go | 4 ++-- 14 files changed, 141 insertions(+), 57 deletions(-) delete mode 100644 modules/notify/configure.go create mode 100644 modules/options.go diff --git a/comm/const.go b/comm/const.go index ca296aefd..c234e0d5c 100644 --- a/comm/const.go +++ b/comm/const.go @@ -42,6 +42,7 @@ const ( ModuleShop core.M_Modules = "shop" //商店模块 ModuleTask core.M_Modules = "task" //任务模块 ModuleStory core.M_Modules = "story" //主线模块 + ModuleNotify core.M_Modules = "notify" //公告模块 ) //RPC服务接口定义处 diff --git a/lego/sys/log/core.go b/lego/sys/log/core.go index 63f23324d..a9024acfe 100644 --- a/lego/sys/log/core.go +++ b/lego/sys/log/core.go @@ -50,7 +50,10 @@ func NewSys(option ...Option) (sys ILog, err error) { } func Clone(option ...Option) ILog { - return defsys.Clone(option...) + if defsys != nil { + return defsys.Clone(option...) + } + return nil } func Debug(msg string, fields ...Field) { defsys.Debug(msg, fields...) } diff --git a/modules/gm/options.go b/modules/gm/options.go index 7dabbd4fd..014d10431 100644 --- a/modules/gm/options.go +++ b/modules/gm/options.go @@ -2,10 +2,12 @@ package gm import ( "go_dreamfactory/lego/utils/mapstructure" + "go_dreamfactory/modules" ) type ( Options struct { + modules.Options Port int Key string } @@ -13,6 +15,9 @@ type ( func (this *Options) LoadConfig(settings map[string]interface{}) (err error) { if settings != nil { + if err = this.Options.LoadConfig(settings); err != nil { + return + } err = mapstructure.Decode(settings, this) } return diff --git a/modules/items/api_getlist.go b/modules/items/api_getlist.go index b6db05998..b93e45ab3 100644 --- a/modules/items/api_getlist.go +++ b/modules/items/api_getlist.go @@ -2,7 +2,6 @@ package items import ( "go_dreamfactory/comm" - "go_dreamfactory/lego/sys/log" "go_dreamfactory/pb" "time" @@ -37,7 +36,7 @@ func (this *apiComp) Getlist(session comm.IUserSession, req *pb.ItemsGetlistReq) }() if items, err = this.module.modelItems.Pack_QueryUserPack(session.GetUserId()); err != nil { - log.Errorf("QueryUserPackReq err:%v", err) + this.module.Errorf("QueryUserPackReq err:%v", err) code = pb.ErrorCode_CacheReadError return } else { diff --git a/modules/items/configure.go b/modules/items/configure.go index 9eb3972f2..e4b686cc3 100644 --- a/modules/items/configure.go +++ b/modules/items/configure.go @@ -7,7 +7,6 @@ import ( cfg "go_dreamfactory/sys/configure/structs" "go_dreamfactory/lego/core" - "go_dreamfactory/lego/sys/log" ) const ( @@ -17,11 +16,13 @@ const ( ///背包配置管理组件 type ConfigureComp struct { modules.MCompConfigure + module *Items } //组件初始化接口 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.(*Items) err = this.LoadConfigure(game_item, cfg.NewGame_item) return } @@ -32,7 +33,7 @@ func (this *ConfigureComp) GetItemsConfigure() (items map[int32]*cfg.Game_itemDa v interface{} ) if v, err = this.GetConfigure(game_item); err != nil { - log.Errorf("err:%v", err) + this.module.Errorf("err:%v", err) return } else { items = v.(*cfg.Game_item).GetDataMap() @@ -47,12 +48,12 @@ func (this *ConfigureComp) GetItemConfigure(id int32) (item *cfg.Game_itemData, ok bool ) if v, err = this.GetConfigure(game_item); err != nil { - log.Errorf("err:%v", err) + this.module.Errorf("err:%v", err) return } else { if item, ok = v.(*cfg.Game_item).GetDataMap()[id]; !ok { err = fmt.Errorf("no found item:%d configure", id) - log.Errorf("err:%v", err) + this.module.Errorf("err:%v", err) return } } @@ -70,7 +71,7 @@ func (this *ConfigureComp) GetPackItemByType(itmes []*pb.DB_UserItemData, usetyp err error ) if v, err = this.GetConfigure(game_item); err != nil { - log.Errorf("err:%v", err) + this.module.Errorf("err:%v", err) return } else { table = v.(*cfg.Game_item) @@ -80,7 +81,7 @@ func (this *ConfigureComp) GetPackItemByType(itmes []*pb.DB_UserItemData, usetyp result = append(result, v) } } else { - log.Errorf("no found itemConfigure:%d", v.ItemId) + this.module.Errorf("no found itemConfigure:%d", v.ItemId) } } } diff --git a/modules/items/modelitems.go b/modules/items/modelitems.go index 05bea94f6..d1a1b8e68 100644 --- a/modules/items/modelitems.go +++ b/modules/items/modelitems.go @@ -34,14 +34,18 @@ func (this *ModelItemsComp) Init(service core.IService, module core.IModule, com ///查询用户背包数据 func (this *ModelItemsComp) Pack_QueryUserPack(uId string) (itmes []*pb.DB_UserItemData, err error) { itmes = make([]*pb.DB_UserItemData, 0) - err = this.GetList(uId, &itmes) + if err = this.GetList(uId, &itmes); err != nil { + this.module.Errorf("err:%v", err) + } return } ///查询用户指定格子的物品数据 func (this *ModelItemsComp) Pack_QueryUserPackByGridId(uId string, grid string) (itme *pb.DB_UserItemData, err error) { itme = &pb.DB_UserItemData{} - err = this.GetListObj(uId, grid, itme) + if err = this.GetListObj(uId, grid, itme); err != nil { + this.module.Errorf("err:%v", err) + } return } @@ -55,7 +59,9 @@ func (this *ModelItemsComp) Pack_AddUserPack(uId string, itmes ...*pb.DB_UserIte //更新用户的背包信息 func (this *ModelItemsComp) Pack_DelUserPack(uId string, ids ...string) (err error) { - err = this.DelListlds(uId, ids...) + if err = this.DelListlds(uId, ids...); err != nil { + this.module.Errorf("err:%v", err) + } return } @@ -71,7 +77,10 @@ func (this *ModelItemsComp) Pack_UpdateUserPack(uId string, itmes ...*pb.DB_User //更新用户的背包信息 func (this *ModelItemsComp) Pack_DeleteUserPack(uId string, gridIds ...string) (err error) { - err = this.DelListlds(uId, gridIds...) + if err = this.DelListlds(uId, gridIds...); err != nil { + this.module.Errorf("err:%v", err) + return + } return } @@ -82,6 +91,7 @@ func (this *ModelItemsComp) Pack_QueryUserPackItemsAmount(uId string, itemid ... err error ) if itmes, err = this.Pack_QueryUserPack(uId); err != nil { + this.module.Errorf("err:%v", err) return } result = map[int32]uint32{} @@ -108,6 +118,7 @@ func (this *ModelItemsComp) Pack_AddItemToUserPack(uId string, itemId int32, add return } if itmes, err = this.Pack_QueryUserPack(uId); err != nil { + this.module.Errorf("err:%v", err) return } add, update, del, leftnum = this.pack_addItemToUserPack(itmes, itemId, addnum) @@ -120,16 +131,19 @@ func (this *ModelItemsComp) Pack_AddItemToUserPack(uId string, itemId int32, add } if len(add) > 0 { if err = this.Pack_AddUserPack(uId, add...); err != nil { + this.module.Errorf("err:%v", err) return } } if len(del) > 0 { if err = this.Pack_DeleteUserPack(uId, del...); err != nil { + this.module.Errorf("err:%v", err) return } } if len(update) > 0 { if err = this.Pack_UpdateUserPack(uId, update...); err != nil { + this.module.Errorf("err:%v", err) return } } @@ -147,6 +161,7 @@ func (this *ModelItemsComp) Pack_AddItemsToUserPack(uId string, items map[int32] leftnum int64 ) if itmes, err = this.Pack_QueryUserPack(uId); err != nil { + this.module.Errorf("err:%v", err) return } for k, v := range items { @@ -160,16 +175,19 @@ func (this *ModelItemsComp) Pack_AddItemsToUserPack(uId string, items map[int32] } if len(add) > 0 { if err = this.Pack_AddUserPack(uId, add...); err != nil { + this.module.Errorf("err:%v", err) return } } if len(del) > 0 { if err = this.Pack_DeleteUserPack(uId, del...); err != nil { + this.module.Errorf("err:%v", err) return } } if len(update) > 0 { if err = this.Pack_UpdateUserPack(uId, update...); err != nil { + this.module.Errorf("err:%v", err) return } } @@ -190,10 +208,12 @@ func (this *ModelItemsComp) Pack_AddItemToUserPackByGrid(uId string, gridid stri return } if conf, err = this.module.configure.GetItemConfigure(itemId); err != nil { + this.module.Errorf("err:%v", err) return } if itme, err = this.Pack_QueryUserPackByGridId(uId, gridid); err != nil { + this.module.Errorf("err:%v", err) return } amount = int64(itme.Amount) diff --git a/modules/items/module.go b/modules/items/module.go index 7958944d2..27a809184 100644 --- a/modules/items/module.go +++ b/modules/items/module.go @@ -6,7 +6,6 @@ import ( "go_dreamfactory/pb" "go_dreamfactory/lego/core" - "go_dreamfactory/lego/sys/log" ) /* @@ -48,7 +47,7 @@ func (this *Items) OnInstallComp() { //IItems------------------------------------------------------------------------------------------------------------------------------- ///查询用户背包物品数量 func (this *Items) QueryItemAmount(source *comm.ModuleCallSource, uId string, itemid int32) (amount uint32) { - defer log.Debugf("获取物品 uId:%s itemid:%d addnum:%d ", uId, itemid, amount) + defer this.Debugf("获取物品 uId:%s itemid:%d addnum:%d ", uId, itemid, amount) amount = 0 if result := this.modelItems.Pack_QueryUserPackItemsAmount(uId, itemid); result != nil && len(result) > 0 { return result[itemid] @@ -65,9 +64,9 @@ func (this *Items) QueryItemsAmount(source *comm.ModuleCallSource, uId string, i ///添加单个物品到背包 (可以加物品和减物品) func (this *Items) AddItem(source *comm.ModuleCallSource, uId string, itemid, addnum int32) (code pb.ErrorCode) { var err error - defer log.Debugf("给用户添加物品 uId:%s itemid:%d addnum:%d issucc:%v", uId, itemid, addnum, err == nil) + defer this.Debugf("给用户添加物品 uId:%s itemid:%d addnum:%d issucc:%v", uId, itemid, addnum, err == nil) if err = this.modelItems.Pack_AddItemToUserPack(uId, itemid, addnum); err != nil { - log.Errorf("给用户添加物品 uId:%s itemid:%d addnum:%d err:%v", uId, itemid, addnum, err) + this.Errorf("给用户添加物品 uId:%s itemid:%d addnum:%d err:%v", uId, itemid, addnum, err) if err == ItemNotEnoughError { code = pb.ErrorCode_ItemsNoEnough } else if err == PackGridNumUpper { @@ -82,9 +81,9 @@ func (this *Items) AddItem(source *comm.ModuleCallSource, uId string, itemid, ad ///添加多个物品到背包 (可以加物品和减物品) func (this *Items) AddItems(source *comm.ModuleCallSource, uId string, items map[int32]int32) (code pb.ErrorCode) { var err error - defer log.Debugf("给用户添加物品 uId:%s items:%d items:%v", uId, items, err == nil) + defer this.Debugf("给用户添加物品 uId:%s items:%d items:%v", uId, items, err == nil) if err = this.modelItems.Pack_AddItemsToUserPack(uId, items); err != nil { - log.Errorf("给用户添加物品 uId:%s items:%d err:%v", uId, items, err) + this.Errorf("给用户添加物品 uId:%s items:%d err:%v", uId, items, err) if err == ItemNotEnoughError { code = pb.ErrorCode_ItemsNoEnough } else if err == PackGridNumUpper { diff --git a/modules/modulebase.go b/modules/modulebase.go index 21c5d6d87..1694957f5 100644 --- a/modules/modulebase.go +++ b/modules/modulebase.go @@ -23,6 +23,7 @@ type ModuleBase struct { cbase.ModuleBase module core.IModule service base.IRPCXService + options IOptions scomp comm.ISC_GateRouteComp //网关服务组件 //常用的一些通用模块 在底层注册好 ModuleUser comm.IUser //用户模块 @@ -32,11 +33,17 @@ type ModuleBase struct { ModuleTask comm.ITask //任务 } +//重构模块配置对象 +func (this *ModuleBase) NewOptions() (options core.IModuleOptions) { + return new(Options) +} + //模块初始化接口 func (this *ModuleBase) Init(service core.IService, module core.IModule, options core.IModuleOptions) (err error) { err = this.ModuleBase.Init(service, module, options) this.service = service.(base.IRPCXService) this.module = module + this.options = options.(IOptions) return } @@ -228,3 +235,35 @@ func (this *ModuleBase) DispenseRes(uid string, res []*cfg.Game_atn) (code pb.Er } return } + +//日志接口 +func (this *ModuleBase) Debugf(format string, a ...interface{}) { + if this.options.GetDebug() { + this.options.GetLog().Debugf(fmt.Sprintf("[Module:%s] ", this.module.GetType())+format, a...) + } +} +func (this *ModuleBase) Infof(format string, a ...interface{}) { + if this.options.GetDebug() { + this.options.GetLog().Infof(fmt.Sprintf("[Module:%s] ", this.module.GetType())+format, a...) + } +} +func (this *ModuleBase) Warnf(format string, a ...interface{}) { + if this.options.GetDebug() { + this.options.GetLog().Warnf(fmt.Sprintf("[Module:%s] ", this.module.GetType())+format, a...) + } +} +func (this *ModuleBase) Errorf(format string, a ...interface{}) { + if this.options.GetDebug() { + this.options.GetLog().Errorf(fmt.Sprintf("[Module:%s] ", this.module.GetType())+format, a...) + } +} +func (this *ModuleBase) Panicf(format string, a ...interface{}) { + if this.options.GetDebug() { + this.options.GetLog().Panicf(fmt.Sprintf("[Module:%s] ", this.module.GetType())+format, a...) + } +} +func (this *ModuleBase) Fatalf(format string, a ...interface{}) { + if this.options.GetDebug() { + this.options.GetLog().Fatalf(fmt.Sprintf("[Module:%s] ", this.module.GetType())+format, a...) + } +} diff --git a/modules/notify/configure.go b/modules/notify/configure.go deleted file mode 100644 index 32b009bf2..000000000 --- a/modules/notify/configure.go +++ /dev/null @@ -1,23 +0,0 @@ -package notify - -import ( - "go_dreamfactory/modules" - - "go_dreamfactory/lego/core" -) - -const ( - game_equipment = "game_equipment.json" -) - -///背包配置管理组件 -type configureComp struct { - modules.MCompConfigure -} - -//组件初始化接口 -func (this *configureComp) Init(service core.IService, module core.IModule, comp core.IModuleComp, options core.IModuleOptions) (err error) { - this.ModuleCompBase.Init(service, module, comp, options) - - return -} diff --git a/modules/notify/modelNotify.go b/modules/notify/modelNotify.go index 4c690b8f5..dcb684ae4 100644 --- a/modules/notify/modelNotify.go +++ b/modules/notify/modelNotify.go @@ -3,7 +3,6 @@ package notify import ( "context" "go_dreamfactory/lego/core" - "go_dreamfactory/lego/sys/log" "go_dreamfactory/modules" "go_dreamfactory/pb" "time" @@ -43,14 +42,14 @@ func (this *modelNotifyComp) GetFullNotify() (result []*pb.DBSystemNotify, err e for c.Next(context.Background()) { notify := &pb.DBSystemNotify{} if err = c.Decode(notify); err != nil { - log.Errorf("GetFullNotify err:%v", err) + this.module.Errorf("GetFullNotify err:%v", err) break } notifys[notify.Id] = notify } if len(notifys) > 0 { if err = this.Redis.HMSet(this.TableName, notifys); err != nil { - log.Errorf("GetFullNotify err:%v", err) + this.module.Errorf("GetFullNotify err:%v", err) } } } diff --git a/modules/notify/module.go b/modules/notify/module.go index 62e73ee61..2b857732a 100644 --- a/modules/notify/module.go +++ b/modules/notify/module.go @@ -20,7 +20,6 @@ func NewModule() core.IModule { type Notification struct { modules.ModuleBase api_comp *apiComp - configure *configureComp modelNotify *modelNotifyComp } @@ -40,5 +39,4 @@ func (this *Notification) OnInstallComp() { this.ModuleBase.OnInstallComp() this.api_comp = this.RegisterComp(new(apiComp)).(*apiComp) this.modelNotify = this.RegisterComp(new(modelNotifyComp)).(*modelNotifyComp) - this.configure = this.RegisterComp(new(configureComp)).(*configureComp) } diff --git a/modules/options.go b/modules/options.go new file mode 100644 index 000000000..a24bca798 --- /dev/null +++ b/modules/options.go @@ -0,0 +1,42 @@ +package modules + +import ( + "fmt" + "go_dreamfactory/lego/core" + "go_dreamfactory/lego/sys/log" + "go_dreamfactory/lego/utils/mapstructure" +) + +type ( + IOptions interface { + core.IModuleOptions + GetDebug() bool + GetLog() log.ILog + } + Options struct { + Debug bool //日志是否开启 + Log log.ILog + } +) + +func (this *Options) GetDebug() bool { + return this.Debug +} + +func (this *Options) GetLog() log.ILog { + return this.Log +} + +func (this *Options) LoadConfig(settings map[string]interface{}) (err error) { + if settings != nil { + err = mapstructure.Decode(settings, this) + } + if this.Debug && this.Log == nil { + this.Log = log.Clone() + if this.Log == nil { + err = fmt.Errorf("Log is nil") + return + } + } + return +} diff --git a/modules/shop/configure.go b/modules/shop/configure.go index a646a6807..2fc1d3a0c 100644 --- a/modules/shop/configure.go +++ b/modules/shop/configure.go @@ -3,7 +3,6 @@ package shop import ( "fmt" "go_dreamfactory/lego/core" - "go_dreamfactory/lego/sys/log" "go_dreamfactory/modules" "go_dreamfactory/pb" cfg "go_dreamfactory/sys/configure/structs" @@ -17,11 +16,13 @@ const ( ///背包配置管理组件 type configureComp struct { modules.MCompConfigure + module *Shop } //组件初始化接口 func (this *configureComp) 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.(*Shop) this.LoadConfigure(game_shop, cfg.NewGame_shop) this.LoadConfigure(game_shopitem, cfg.NewGame_shopitem) return @@ -34,12 +35,12 @@ func (this *configureComp) GetShopConfigure(id int32) (configure *cfg.Game_shopD ok bool ) if v, err = this.GetConfigure(game_shop); err != nil { - log.Errorf("err:%v", err) + this.module.Errorf("err:%v", err) return } else { if configure, ok = v.(*cfg.Game_shop).GetDataMap()[id]; !ok { err = fmt.Errorf("ShopConfigure not found:%d ", id) - log.Errorf("err:%v", err) + this.module.Errorf("err:%v", err) return } } @@ -53,12 +54,12 @@ func (this *configureComp) GetShopItemsConfigure(key int32) (result *cfg.Game_sh ok bool ) if v, err = this.GetConfigure(game_shopitem); err != nil { - log.Errorf("err:%v", err) + this.module.Errorf("err:%v", err) return } else { if result, ok = v.(*cfg.Game_shopitem).GetDataMap()[key]; !ok { err = fmt.Errorf("ShopConfigure not found:%d ", key) - log.Errorf("err:%v", err) + this.module.Errorf("err:%v", err) return } } @@ -73,7 +74,7 @@ func (this *configureComp) GetShopItemsConfigureByGroups(groupid int32, user *pb table *cfg.Game_shopitem ) if v, err = this.GetConfigure(game_shopitem); err != nil { - log.Errorf("err:%v", err) + this.module.Errorf("err:%v", err) return } else { table = v.(*cfg.Game_shopitem) @@ -99,7 +100,7 @@ func (this *configureComp) GetShopItemsConfigureByIds(keys ...int32) (result []* ok bool ) if v, err = this.GetConfigure(game_shopitem); err != nil { - log.Errorf("err:%v", err) + this.module.Errorf("err:%v", err) return } else { table = v.(*cfg.Game_shopitem) @@ -107,7 +108,7 @@ func (this *configureComp) GetShopItemsConfigureByIds(keys ...int32) (result []* if item, ok = table.GetDataMap()[v]; ok { result = append(result, item) } else { - log.Errorf("no found GetShopItemsConfigureByIds:%d", v) + this.module.Errorf("no found GetShopItemsConfigureByIds:%d", v) } } } diff --git a/modules/shop/model_shopitems.go b/modules/shop/model_shopitems.go index 363067ebb..7f8b106d0 100644 --- a/modules/shop/model_shopitems.go +++ b/modules/shop/model_shopitems.go @@ -2,7 +2,6 @@ package shop import ( "go_dreamfactory/lego/core" - "go_dreamfactory/lego/sys/log" "go_dreamfactory/lego/sys/mgo" "go_dreamfactory/modules" "go_dreamfactory/pb" @@ -35,6 +34,7 @@ func (this *modelShopItemsComp) QueryUserShopData(uId string) (result map[int32] result = make(map[int32]*pb.DBShopItem) data := make([]*pb.DBShopItem, 0) if err = this.GetList(uId, &data); err != nil && err != mgo.MongodbNil { + err = nil return } err = nil @@ -52,7 +52,7 @@ func (this *modelShopItemsComp) QueryUserShopDataByGoodId(uId string, goodId int ) data = make([]*pb.DBShopItem, 0) if err = this.GetList(uId, &data); err != nil { - log.Errorf("err%v", err) + this.module.Errorf("err%v", err) return } for _, v := range data {