diff --git a/modules/hero/api_strengthenUplv.go b/modules/hero/api_strengthenUplv.go index 292bc6fae..f8ed24448 100644 --- a/modules/hero/api_strengthenUplv.go +++ b/modules/hero/api_strengthenUplv.go @@ -22,12 +22,20 @@ func (this *apiComp) StrengthenUplvCheck(session comm.IUserSession, req *pb.Hero func (this *apiComp) StrengthenUplv(session comm.IUserSession, req *pb.HeroStrengthenUplvReq) (code pb.ErrorCode, data proto.Message) { var ( - addExp int32 // 需要增加的经验 - costGold int32 // 需要消耗的资源 - _hero *pb.DBHero // 目标英雄 - - cost []*cfg.Gameatn // 消耗的道具 + addExp int32 // 需要增加的经验 + costGold int32 // 需要消耗的资源 + _hero *pb.DBHero // 目标英雄 + cost []*cfg.Gameatn // 消耗的道具 ) + code = this.StrengthenUplvCheck(session, req) // check + if code != pb.ErrorCode_Success { + return + } + + _hero, code = this.module.GetHeroByObjID(session.GetUserId(), req.HeroObjID) + if code != pb.ErrorCode_Success { + return + } for k, v := range req.Item { if v == 0 { continue @@ -48,15 +56,6 @@ func (this *apiComp) StrengthenUplv(session comm.IUserSession, req *pb.HeroStren T: "gold", N: costGold, }) - code = this.StrengthenUplvCheck(session, req) // check - if code != pb.ErrorCode_Success { - return - } - _hero, code = this.module.GetHeroByObjID(session.GetUserId(), req.HeroObjID) - if code != pb.ErrorCode_Success { - return - } - // 金币消耗判断 if code = this.module.CheckRes(session, cost); code != pb.ErrorCode_Success { return diff --git a/modules/modulebase.go b/modules/modulebase.go index 79aea3105..bc59db64d 100644 --- a/modules/modulebase.go +++ b/modules/modulebase.go @@ -238,26 +238,36 @@ func (this *ModuleBase) SendMsgToCUsers(mainType, subType string, msg proto.Mess // 只校验资源 参数 atn格式 func (this *ModuleBase) CheckRes(session comm.IUserSession, res []*cfg.Gameatn) (code pb.ErrorCode) { - //校验消费资源是否充足 + var ( + items map[string]int32 // 道具背包 批量处理 + attrs map[string]int32 // 属性 + ) for _, v := range res { - if v.A == comm.AttrType { //用户属性资源 - if amount := this.ModuleUser.QueryAttributeValue(session.GetUserId(), v.T); amount < int64(v.N) { - code = pb.ErrorCode_ResNoEnough - this.Warnf("道具不足:A:%s,T:%s,N:%d", v.A, v.T, v.N) - return - } - } else if v.A == comm.ItemType { //道具资源 - // if resID, err = strconv.Atoi(v.T); err != nil { - // code = pb.ErrorCode_ConfigurationException - // return - // } - if amount := this.ModuleItems.QueryItemAmount(session.GetUserId(), v.T); amount < uint32(v.N) { - code = pb.ErrorCode_ResNoEnough - this.Warnf("道具不足:A:%s,T:%s,N:%d", v.A, v.T, v.N) - return - } + switch v.A { + case comm.AttrType: + attrs[v.T] = v.N + case comm.ItemType: + items[v.T] = v.N + default: + this.Errorf("not found res type") // 找不到资源类型 } } + // 校验数量 + for k, v := range attrs { + if this.ModuleUser.QueryAttributeValue(session.GetUserId(), k) < int64(v) { // -v 负负得正 + code = pb.ErrorCode_ResNoEnough + this.Warnf("资源不足", log.Field{Key: "uid", Value: session.GetUserId()}, log.Field{Key: "T", Value: k}, log.Field{Key: "N", Value: v}) + return + } + } + for k, v := range items { //校验消费资源是否充足 + if int32(this.ModuleItems.QueryItemAmount(session.GetUserId(), k)) < v { + code = pb.ErrorCode_ResNoEnough + this.Warnf("道具不足", log.Field{Key: "uid", Value: session.GetUserId()}, log.Field{Key: "T", Value: k}, log.Field{Key: "N", Value: v}) + return + } + } + return } @@ -274,6 +284,7 @@ func (this *ModuleBase) ConsumeRes(session comm.IUserSession, res []*cfg.Gameatn switch v.A { case comm.AttrType: attrs[v.T] -= v.N + case comm.ItemType: items[v.T] -= v.N default: