From 6442e4aff23edb399452ef54143a9acf87631c43 Mon Sep 17 00:00:00 2001 From: liwei1dao Date: Sat, 23 Jul 2022 01:00:57 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E7=89=A9=E5=93=81=E4=BD=BF?= =?UTF-8?q?=E7=94=A8=E7=9A=84=E6=B6=88=E6=81=AF=E6=8E=A8=E9=80=81=E6=9C=BA?= =?UTF-8?q?=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- modules/items/api_useItem.go | 25 ++++++++++++++----------- modules/items/modelitems.go | 17 +++++++---------- modules/items/module.go | 24 ++++++++++++++++++++++++ 3 files changed, 45 insertions(+), 21 deletions(-) diff --git a/modules/items/api_useItem.go b/modules/items/api_useItem.go index 77586d2d3..7c6498def 100644 --- a/modules/items/api_useItem.go +++ b/modules/items/api_useItem.go @@ -54,18 +54,21 @@ func (this *apiComp) Useitem(session comm.IUserSession, req *pb.ItemsUseItemReq) code = pb.ErrorCode_ItemsUseNotSupported return } - item.Amount = item.Amount - req.Amount - if item.Amount == 0 { - if err = this.module.modelItems.DelUserPack(session.GetUserId(), item.GridId); err != nil { - code = pb.ErrorCode_DBError - return - } - } else { - if err = this.module.modelItems.UpdateUserPack(session.GetUserId(), item); err != nil { - code = pb.ErrorCode_DBError - return - } + if code = this.module.AddItemforGrid(&comm.ModuleCallSource{}, session, req.GridId, -1*int32(req.Amount), true); code != pb.ErrorCode_Success { + return } + // item.Amount = item.Amount - req.Amount + // if item.Amount == 0 { + // if err = this.module.modelItems.DelUserPack(session.GetUserId(), item.GridId); err != nil { + // code = pb.ErrorCode_DBError + // return + // } + // } else { + // if err = this.module.modelItems.UpdateUserPack(session.GetUserId(), item); err != nil { + // code = pb.ErrorCode_DBError + // return + // } + // } session.SendMsg(string(this.module.GetType()), "useitem", &pb.ItemsUseItemResp{GridId: req.GridId, Amount: req.Amount, Issucc: true}) return } diff --git a/modules/items/modelitems.go b/modules/items/modelitems.go index 7badcf0c1..040272cb4 100644 --- a/modules/items/modelitems.go +++ b/modules/items/modelitems.go @@ -207,32 +207,28 @@ func (this *ModelItemsComp) AddItemsToUserPack(uId string, items map[int32]int32 } ///修改指定格子的物品数量 -func (this *ModelItemsComp) AddItemToUserPackByGrid(uId string, gridid string, itemId int32, addnum int32) (err error) { +func (this *ModelItemsComp) AddItemToUserPackByGrid(uId string, gridid string, addnum int32) (change []*pb.DB_UserItemData, err error) { var ( conf *cfg.Game_itemData itme *pb.DB_UserItemData - grid *pb.DB_UserItemData num int64 amount int64 ) if addnum == 0 { return } - if conf, err = this.module.configure.GetItemConfigure(itemId); err != nil { - this.module.Errorf("err:%v", err) - return - } - if itme, err = this.QueryUserPackByGridId(uId, gridid); err != nil { this.module.Errorf("err:%v", err) return } - amount = int64(itme.Amount) - if grid.ItemId != itemId { - err = fmt.Errorf("target grid itemid:%d no is %d ", grid.ItemId, itemId) + + if conf, err = this.module.configure.GetItemConfigure(itme.ItemId); err != nil { + this.module.Errorf("err:%v", err) return } + amount = int64(itme.Amount) num = amount + int64(addnum) + change = make([]*pb.DB_UserItemData, 0) if num < 0 { err = ItemNotEnoughError } else { @@ -242,6 +238,7 @@ func (this *ModelItemsComp) AddItemToUserPackByGrid(uId string, gridid string, i } else { itme.Amount = uint32(num) this.UpdateUserPack(uId, itme) + change = append(change, itme) } } return diff --git a/modules/items/module.go b/modules/items/module.go index 5766779f2..ead2dffcb 100644 --- a/modules/items/module.go +++ b/modules/items/module.go @@ -61,6 +61,30 @@ func (this *Items) QueryItemsAmount(source *comm.ModuleCallSource, uId string, i return } +func (this *Items) AddItemforGrid(source *comm.ModuleCallSource, session comm.IUserSession, gridid string, addnum int32, bPush bool) (code pb.ErrorCode) { + var ( + err error + change []*pb.DB_UserItemData + ) + defer this.Debugf("给用户添加物品 uId:%s gridid:%d addnum:%d issucc:%v", session.GetUserId(), gridid, addnum, err == nil) + if change, err = this.modelItems.AddItemToUserPackByGrid(session.GetUserId(), gridid, addnum); err != nil { + this.Errorf("给用户添加物品 uId:%s gridid:%d addnum:%d err:%v", session.GetUserId(), gridid, addnum, err) + if err == ItemNotEnoughError { + code = pb.ErrorCode_ItemsNoEnough + } else if err == PackGridNumUpper { + code = pb.ErrorCode_ItemsGridNumUpper + } else { + code = pb.ErrorCode_Unknown + } + return + } + if bPush { + this.itemsChangePush(session, change) //推送道具背包变化 + } + + return +} + ///添加单个物品到背包 (可以加物品和减物品) func (this *Items) AddItem(source *comm.ModuleCallSource, session comm.IUserSession, itemid, addnum int32, bPush bool) (code pb.ErrorCode) { var (