资源校验整合

This commit is contained in:
meixiongfeng 2023-02-06 13:39:13 +08:00
parent d01d24912a
commit 59f978937a
2 changed files with 41 additions and 31 deletions

View File

@ -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) { func (this *apiComp) StrengthenUplv(session comm.IUserSession, req *pb.HeroStrengthenUplvReq) (code pb.ErrorCode, data proto.Message) {
var ( var (
addExp int32 // 需要增加的经验 addExp int32 // 需要增加的经验
costGold int32 // 需要消耗的资源 costGold int32 // 需要消耗的资源
_hero *pb.DBHero // 目标英雄 _hero *pb.DBHero // 目标英雄
cost []*cfg.Gameatn // 消耗的道具
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 { for k, v := range req.Item {
if v == 0 { if v == 0 {
continue continue
@ -48,15 +56,6 @@ func (this *apiComp) StrengthenUplv(session comm.IUserSession, req *pb.HeroStren
T: "gold", T: "gold",
N: costGold, 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 { if code = this.module.CheckRes(session, cost); code != pb.ErrorCode_Success {
return return

View File

@ -238,26 +238,36 @@ func (this *ModuleBase) SendMsgToCUsers(mainType, subType string, msg proto.Mess
// 只校验资源 参数 atn格式 // 只校验资源 参数 atn格式
func (this *ModuleBase) CheckRes(session comm.IUserSession, res []*cfg.Gameatn) (code pb.ErrorCode) { 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 { for _, v := range res {
if v.A == comm.AttrType { //用户属性资源 switch v.A {
if amount := this.ModuleUser.QueryAttributeValue(session.GetUserId(), v.T); amount < int64(v.N) { case comm.AttrType:
code = pb.ErrorCode_ResNoEnough attrs[v.T] = v.N
this.Warnf("道具不足:A:%s,T:%s,N:%d", v.A, v.T, v.N) case comm.ItemType:
return items[v.T] = v.N
} default:
} else if v.A == comm.ItemType { //道具资源 this.Errorf("not found res type") // 找不到资源类型
// 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
}
} }
} }
// 校验数量
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 return
} }
@ -274,6 +284,7 @@ func (this *ModuleBase) ConsumeRes(session comm.IUserSession, res []*cfg.Gameatn
switch v.A { switch v.A {
case comm.AttrType: case comm.AttrType:
attrs[v.T] -= v.N attrs[v.T] -= v.N
case comm.ItemType: case comm.ItemType:
items[v.T] -= v.N items[v.T] -= v.N
default: default: