From 0a17fb1517e10d16c9ea48d56aa641a191493af4 Mon Sep 17 00:00:00 2001 From: liwei <2211068034@qq.com> Date: Thu, 13 Jul 2023 11:33:59 +0800 Subject: [PATCH] =?UTF-8?q?=E5=90=8C=E6=AD=A5=E5=95=86=E5=BA=97=E4=BB=A3?= =?UTF-8?q?=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- modules/shop/api_buy.go | 93 ++++++++++++++++++++++--------------- modules/shop/api_getlist.go | 42 +++++++++++------ modules/shop/configure.go | 14 +++--- modules/shop/core.go | 12 ++--- modules/shop/module.go | 8 ++-- modules/shop/module_test.go | 16 ++----- 6 files changed, 105 insertions(+), 80 deletions(-) diff --git a/modules/shop/api_buy.go b/modules/shop/api_buy.go index 83c335577..bbe6b0a86 100644 --- a/modules/shop/api_buy.go +++ b/modules/shop/api_buy.go @@ -1,6 +1,7 @@ package shop import ( + "fmt" "go_dreamfactory/comm" "go_dreamfactory/pb" cfg "go_dreamfactory/sys/configure/structs" @@ -8,9 +9,9 @@ import ( "math" ) -//参数校验 +// 参数校验 func (this *apiComp) BuyCheck(session comm.IUserSession, req *pb.ShopBuyReq) (errdata *pb.ErrorData) { - if req.ShopType == 0 || req.GoodsId == 0 || req.BuyNum <= 0 { + if req.ShopType == 0 || req.Gid == 0 || req.BuyNum <= 0 { errdata = &pb.ErrorData{ Code: pb.ErrorCode_ReqParameterError, Title: pb.ErrorCode_ReqParameterError.ToString(), @@ -19,12 +20,13 @@ func (this *apiComp) BuyCheck(session comm.IUserSession, req *pb.ShopBuyReq) (er return } -///获取用户商品列表 +// /获取用户商品列表 func (this *apiComp) Buy(session comm.IUserSession, req *pb.ShopBuyReq) (errdata *pb.ErrorData) { var ( err error conf *cfg.GameShopitemData shopData *pb.DBShop + good *pb.UserShopGood filed string record *pb.UserShopData need []*cfg.Gameatn @@ -35,14 +37,6 @@ func (this *apiComp) Buy(session comm.IUserSession, req *pb.ShopBuyReq) (errdata return } - if conf, err = this.module.configure.GetShopItemsConfigure(req.GoodsId); err != nil { - errdata = &pb.ErrorData{ - Code: pb.ErrorCode_SystemError, - Title: pb.ErrorCode_SystemError.ToString(), - Message: err.Error(), - } - return - } if shopData, err = this.module.modelShop.QueryUserShopData(session.GetUserId()); err != nil { //没有购买记录 errdata = &pb.ErrorData{ Code: pb.ErrorCode_DBError, @@ -51,23 +45,6 @@ func (this *apiComp) Buy(session comm.IUserSession, req *pb.ShopBuyReq) (errdata } return } - need = make([]*cfg.Gameatn, len(conf.Need)) - for i, v := range conf.Need { - need[i] = &cfg.Gameatn{ - A: v.A, - T: v.T, - N: int32(math.Ceil(float64(v.N)*float64(conf.Sale)/float64(1000))) * req.BuyNum, - } - } - - give = make([]*cfg.Gameatn, len(conf.Iteminfo)) - for i, v := range conf.Iteminfo { - give[i] = &cfg.Gameatn{ - A: v.A, - T: v.T, - N: v.N * req.BuyNum, - } - } switch req.ShopType { case pb.ShopType_GoldShop: @@ -106,18 +83,64 @@ func (this *apiComp) Buy(session comm.IUserSession, req *pb.ShopBuyReq) (errdata return } if record == nil { - record = &pb.UserShopData{ - Buy: map[int32]int32{}, + errdata = &pb.ErrorData{ + Code: pb.ErrorCode_DBError, + Title: pb.ErrorCode_DBError.ToString(), + Message: err.Error(), + } + return + } + + for _, v := range record.Items { + if v.Id == req.Gid { + good = v } } - if conf.Buyminnum-record.Buy[req.GoodsId] < req.BuyNum { + + if good == nil { + errdata = &pb.ErrorData{ + Code: pb.ErrorCode_ReqParameterError, + Title: pb.ErrorCode_ReqParameterError.ToString(), + Message: fmt.Sprintf("no found good:%d", req.Gid), + } + return + } + + if conf, err = this.module.configure.GetShopItemsConfigure(good.Gid); err != nil { + errdata = &pb.ErrorData{ + Code: pb.ErrorCode_SystemError, + Title: pb.ErrorCode_SystemError.ToString(), + Message: err.Error(), + } + return + } + + need = make([]*cfg.Gameatn, len(conf.Need)) + for i, v := range conf.Need { + need[i] = &cfg.Gameatn{ + A: v.A, + T: v.T, + N: int32(math.Ceil(float64(v.N)*float64(conf.Sale)/float64(1000))) * req.BuyNum, + } + } + + give = make([]*cfg.Gameatn, len(conf.Iteminfo)) + for i, v := range conf.Iteminfo { + give[i] = &cfg.Gameatn{ + A: v.A, + T: v.T, + N: v.N * req.BuyNum, + } + } + + if conf.Buyminnum-good.Buy < req.BuyNum { errdata = &pb.ErrorData{ Code: pb.ErrorCode_ShopGoodsIsSoldOut, Title: pb.ErrorCode_ShopGoodsIsSoldOut.ToString(), } return } - record.Buy[req.GoodsId] += req.BuyNum + good.Buy += req.BuyNum if errdata = this.module.ConsumeRes(session, need, true); errdata != nil { return } @@ -127,25 +150,21 @@ func (this *apiComp) Buy(session comm.IUserSession, req *pb.ShopBuyReq) (errdata return } } else { - if errdata = this.module.equip.AddEquipment(session, record.Preview[req.GoodsId]); errdata != nil { + if errdata = this.module.equip.AddEquipment(session, record.Preview[req.Gid]); errdata != nil { return } } //随机任务 tasks = append(tasks, comm.GetBuriedParam(comm.Rtype64, 1)) - // this.module.ModuleRtask.SendToRtask(session, comm.Rtype64, 1) for _, v := range give { if v.A == comm.ItemType { tasks = append(tasks, comm.GetBuriedParam(comm.Rtype65, v.N, utils.ToInt32(v.T))) - // this.module.ModuleRtask.SendToRtask(session, comm.Rtype65, v.N, utils.ToInt32(v.T)) } } tasks = append(tasks, comm.GetBuriedParam(comm.Rtype66, 1, int32(req.ShopType))) - // this.module.ModuleRtask.SendToRtask(session, comm.Rtype66, 1, int32(req.ShopType)) for _, v := range need { if v.A == comm.AttrType { - // tasks = append(tasks, comm.GetBuriedParam(comm.Rtype67, v.N, utils.ToInt32(v.T))) switch v.T { case comm.ResGold: tasks = append(tasks, comm.GetBuriedParam(comm.Rtype67, v.N, 1)) diff --git a/modules/shop/api_getlist.go b/modules/shop/api_getlist.go index 1f29807d0..a78747545 100644 --- a/modules/shop/api_getlist.go +++ b/modules/shop/api_getlist.go @@ -25,6 +25,7 @@ func (this *apiComp) Getlist(session comm.IUserSession, req *pb.ShopGetListReq) var ( err error filed string + refresh int32 shopconf *cfg.GameShopData shopData *pb.DBShop udata *pb.DBUser @@ -66,6 +67,7 @@ func (this *apiComp) Getlist(session comm.IUserSession, req *pb.ShopGetListReq) case pb.ShopType_GoldShop: sdata = shopData.GoldShop filed = "goldShop" + refresh = this.module.privilege.GetCountByPrivilegeId(session.GetUserId(), comm.PrivilegeType2) break case pb.ShopType_DiamondShop: sdata = shopData.DiamondShop @@ -100,7 +102,7 @@ func (this *apiComp) Getlist(session comm.IUserSession, req *pb.ShopGetListReq) } if sdata == nil { sdata = &pb.UserShopData{ - Buy: map[int32]int32{}, + Items: make([]*pb.UserShopGood, 0), } } if shopconf.Rnum > 0 { @@ -127,14 +129,15 @@ func (this *apiComp) Getlist(session comm.IUserSession, req *pb.ShopGetListReq) if req.IsManualRefresh && shopconf.Rnum > 0 { //可以手动刷新 isrefresh := false - refresh := int(this.module.privilege.GetCountByPrivilegeId(session.GetUserId(), comm.PrivilegeType2)) if refresh > 0 { // if time.Unix(shopData.RefreshtimegoldShop, 0).Day() < configure.Now().Day() { shopData.RefreshtimegoldShop = configure.Now().Unix() shopData.RefreshnumgoldShop = 0 } - if int(shopData.RefreshnumgoldShop) < refresh { + if shopData.RefreshnumgoldShop < refresh { isrefresh = true + shopData.RefreshnumgoldShop++ + } } if !isrefresh { @@ -166,15 +169,20 @@ func (this *apiComp) Getlist(session comm.IUserSession, req *pb.ShopGetListReq) } items = append(items, randomGoods(_items)) } - sdata.Buy = make(map[int32]int32) sdata.LastRefreshTime = configure.Now().Unix() sdata.ManualRefreshNum++ - sdata.Items = make([]int32, len(items)) + sdata.Leftfreerefreshnum = refresh - shopData.RefreshnumgoldShop + sdata.Items = make([]*pb.UserShopGood, len(items)) sdata.Preview = make(map[int32]*pb.DB_Equipment) for i, v := range items { - sdata.Items[i] = v.Key + id := v.Key*100 + int32(i) + sdata.Items[i] = &pb.UserShopGood{ + Id: id, + Gid: v.Key, + Buy: 0, + } if v.Preview { //是否预览 - if errdata, sdata.Preview[v.Key] = this.module.equip.NewEquipment(session.GetUserId(), v.Iteminfo[0].T); errdata != nil { + if errdata, sdata.Preview[id] = this.module.equip.NewEquipment(session.GetUserId(), v.Iteminfo[0].T); errdata != nil { return } } @@ -213,14 +221,18 @@ func (this *apiComp) Getlist(session comm.IUserSession, req *pb.ShopGetListReq) } items = append(items, randomGoods(_items)) } - sdata.Buy = make(map[int32]int32) sdata.LastRefreshTime = configure.Now().Unix() - sdata.Items = make([]int32, len(items)) + sdata.Items = make([]*pb.UserShopGood, len(items)) sdata.Preview = make(map[int32]*pb.DB_Equipment) for i, v := range items { - sdata.Items[i] = v.Key + id := v.Key*100 + int32(i) + sdata.Items[i] = &pb.UserShopGood{ + Id: id, + Gid: v.Key, + Buy: 0, + } if v.Preview { //是否预览 - if errdata, sdata.Preview[v.Key] = this.module.equip.NewEquipment(session.GetUserId(), v.Iteminfo[0].T); errdata != nil { + if errdata, sdata.Preview[id] = this.module.equip.NewEquipment(session.GetUserId(), v.Iteminfo[0].T); errdata != nil { return } } @@ -230,7 +242,11 @@ func (this *apiComp) Getlist(session comm.IUserSession, req *pb.ShopGetListReq) // this.module.ModuleRtask.SendToRtask(session, comm.Rtype105, 1) go this.module.ModuleBuried.TriggerBuried(session.GetUserId(), comm.GetBuriedParam(comm.Rtype105, 1)) } else { //返回以前的商品列表 - if items, err = this.module.configure.GetShopItemsConfigureByIds(sdata.Items...); err != nil { + keys := make([]int32, len(sdata.Items)) + for i, v := range sdata.Items { + keys[i] = v.Gid + } + if items, err = this.module.configure.GetShopItemsConfigureByIds(keys...); err != nil { errdata = &pb.ErrorData{ Code: pb.ErrorCode_SystemError, Title: pb.ErrorCode_SystemError.ToString(), @@ -247,6 +263,6 @@ func (this *apiComp) Getlist(session comm.IUserSession, req *pb.ShopGetListReq) } return } - session.SendMsg(string(this.module.GetType()), "getlist", &pb.ShopGetListResp{SType: req.SType, IsManualRefresh: req.IsManualRefresh, Goods: goods, SurplusRefreshNum: leftrefnum, Lastrefreshtime: sdata.LastRefreshTime}) + session.SendMsg(string(this.module.GetType()), "getlist", &pb.ShopGetListResp{SType: req.SType, IsManualRefresh: req.IsManualRefresh, Goods: goods, SurplusRefreshNum: leftrefnum, Lastrefreshtime: sdata.LastRefreshTime, Leftfreerefreshnum: sdata.Leftfreerefreshnum}) return } diff --git a/modules/shop/configure.go b/modules/shop/configure.go index 7df5cf1ab..ebd8e785f 100644 --- a/modules/shop/configure.go +++ b/modules/shop/configure.go @@ -14,13 +14,13 @@ const ( game_shopitem = "game_shopitem.json" ) -///背包配置管理组件 +// /背包配置管理组件 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.MCompConfigure.Init(service, module, comp, options) this.module = module.(*Shop) @@ -29,7 +29,7 @@ func (this *configureComp) Init(service core.IService, module core.IModule, comp return } -//获取装备配置数据 +// 获取装备配置数据 func (this *configureComp) GetShopConfigure(id int32) (configure *cfg.GameShopData, err error) { var ( v interface{} @@ -48,7 +48,7 @@ func (this *configureComp) GetShopConfigure(id int32) (configure *cfg.GameShopDa return } -//读取商品 +// 读取商品 func (this *configureComp) GetShopItemsConfigure(key int32) (result *cfg.GameShopitemData, err error) { var ( v interface{} @@ -67,7 +67,7 @@ func (this *configureComp) GetShopItemsConfigure(key int32) (result *cfg.GameSho return } -//读取商品组 +// 读取商品组 func (this *configureComp) GetShopItemsConfigureByGroups(groupid int32, user *pb.DBUser) (result []*cfg.GameShopitemData, err error) { result = make([]*cfg.GameShopitemData, 0, 10) var ( @@ -92,7 +92,7 @@ func (this *configureComp) GetShopItemsConfigureByGroups(groupid int32, user *pb return } -//读取商品 +// 读取商品 func (this *configureComp) GetShopItemsConfigureByIds(keys ...int32) (result []*cfg.GameShopitemData, err error) { result = make([]*cfg.GameShopitemData, 0, len(keys)) var ( @@ -111,6 +111,8 @@ func (this *configureComp) GetShopItemsConfigureByIds(keys ...int32) (result []* result = append(result, item) } else { this.module.Errorf("no found GetShopItemsConfigureByIds:%d", v) + err = comm.NewNotFoundConfErr(string(this.module.GetType()), game_shopitem, v) + return } } } diff --git a/modules/shop/core.go b/modules/shop/core.go index 1481ba439..a0a38818a 100644 --- a/modules/shop/core.go +++ b/modules/shop/core.go @@ -8,7 +8,7 @@ import ( "math/big" ) -//随机商品列表 +// 随机商品列表 func randomGoods(goods []*cfg.GameShopitemData) (result *cfg.GameShopitemData) { var ( totle int64 @@ -30,20 +30,16 @@ func randomGoods(goods []*cfg.GameShopitemData) (result *cfg.GameShopitemData) { return } -//转换商品对象 +// 转换商品对象 func transGoods(goods []*cfg.GameShopitemData, sdata *pb.UserShopData) (result []*pb.ShopItem) { result = make([]*pb.ShopItem, len(goods)) - // ok := false - // uitem := &pb.DBShopItem{} for i, v := range goods { - // if uitem, ok = ushoputem[v.Key]; !ok { - // uitem = &pb.DBShopItem{} - // } result[i] = &pb.ShopItem{ + Gid: sdata.Items[i].Id, GoodsId: v.Key, Sale: int32(v.Sale), } - result[i].LeftBuyNum = v.Buyminnum - sdata.Buy[v.Key] + result[i].LeftBuyNum = v.Buyminnum - sdata.Items[i].Buy result[i].Items = make([]*pb.UserAssets, len(v.Iteminfo)) for i1, v1 := range v.Iteminfo { result[i].Items[i1] = &pb.UserAssets{ diff --git a/modules/shop/module.go b/modules/shop/module.go index 6395c3ebd..3e7e75dac 100644 --- a/modules/shop/module.go +++ b/modules/shop/module.go @@ -28,12 +28,12 @@ type Shop struct { modelShop *modelShopComp } -//模块名 +// 模块名 func (this *Shop) GetType() core.M_Modules { return comm.ModuleShop } -//模块初始化接口 注册用户创建角色事件 +// 模块初始化接口 注册用户创建角色事件 func (this *Shop) Init(service core.IService, module core.IModule, options core.IModuleOptions) (err error) { err = this.ModuleBase.Init(service, module, options) this.service = service.(base.IRPCXService) @@ -53,7 +53,7 @@ func (this *Shop) Start() (err error) { return } -//装备组件 +// 装备组件 func (this *Shop) OnInstallComp() { this.ModuleBase.OnInstallComp() this.api_comp = this.RegisterComp(new(apiComp)).(*apiComp) @@ -61,7 +61,7 @@ func (this *Shop) OnInstallComp() { this.configure = this.RegisterComp(new(configureComp)).(*configureComp) } -//Event------------------------------------------------------------------------------------------------------------ +// Event------------------------------------------------------------------------------------------------------------ func (this *Shop) EventUserOffline(session comm.IUserSession) { this.modelShop.DelByUId(session.GetUserId(), db.SetDBMgoLog(false)) } diff --git a/modules/shop/module_test.go b/modules/shop/module_test.go index c92fe70d8..edc3b037b 100644 --- a/modules/shop/module_test.go +++ b/modules/shop/module_test.go @@ -30,12 +30,12 @@ func newService(ops ...rpcx.Option) core.IService { return s } -//梦工厂基础服务对象 +// 梦工厂基础服务对象 type TestService struct { rpcx.RPCXService } -//初始化相关系统 +// 初始化相关系统 func (this *TestService) InitSys() { this.RPCXService.InitSys() if err := db.OnInit(this.GetSettings().Sys["db"]); err != nil { @@ -54,7 +54,7 @@ var service core.IService var s_gateComp comm.ISC_GateRouteComp = services.NewGateRouteComp() var module = new(shop.Shop) -//测试环境下初始化db和cache 系统 +// 测试环境下初始化db和cache 系统 func TestMain(m *testing.M) { service = newService( rpcx.SetConfPath("../../bin/conf/worker_1.yaml"), @@ -76,18 +76,10 @@ func TestMain(m *testing.M) { defer os.Exit(m.Run()) } -//测试api_getlist +// 测试api_getlist func Test_Module_APIGetList(t *testing.T) { data, _ := ptypes.MarshalAny(&pb.ShopGetListReq{SType: pb.ShopType_GoldShop, IsManualRefresh: false}) reply := &pb.RPCMessageReply{} s_gateComp.ReceiveMsg(context.Background(), &pb.AgentMessage{UserId: "0_62b16dda909b2f8faeff788d", MainType: "shop", SubType: "getlist", Message: data}, reply) log.Debugf("reply:%v", reply) } - -//测试api_buy -func Test_Module_APIBuy(t *testing.T) { - data, _ := ptypes.MarshalAny(&pb.ShopBuyReq{GoodsId: 7}) - reply := &pb.RPCMessageReply{} - s_gateComp.ReceiveMsg(context.Background(), &pb.AgentMessage{UserId: "0_62b16dda909b2f8faeff788d", MainType: "shop", SubType: "buy", Message: data}, reply) - log.Debugf("reply:%v", reply) -}