From 4b150256d07717d51ff7108f755768fce222d9e9 Mon Sep 17 00:00:00 2001 From: meixiongfeng <766881921@qq.com> Date: Tue, 2 Aug 2022 12:05:00 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B5=84=E6=BA=90=E5=8F=91=E6=94=BE=E6=B6=88?= =?UTF-8?q?=E8=80=97=E6=89=B9=E9=87=8F=E5=A4=84=E7=90=86=EF=BC=8C=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3=E8=BF=94=E5=9B=9E=E5=80=BC=E7=BB=9F=E4=B8=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- comm/imodule.go | 4 +- modules/hero/model_hero.go | 25 ++--- modules/hero/module.go | 18 +++- modules/mail/api_getAttachment.go | 2 +- modules/mail/api_readmail.go | 2 +- modules/modulebase.go | 153 +++++++++++++++--------------- modules/user/module.go | 68 +++++++++++++ 7 files changed, 177 insertions(+), 95 deletions(-) diff --git a/comm/imodule.go b/comm/imodule.go index 74489d394..460cae600 100644 --- a/comm/imodule.go +++ b/comm/imodule.go @@ -40,7 +40,7 @@ type ( //创建指定数量 CreateRepeatHero(uid string, heroCfgId string, num int32) (*pb.DBHero, error) // 批量创建英雄 - CreateRepeatHeros(session IUserSession, items map[string]int32, bPush bool) (code pb.ErrorCode) + CreateRepeatHeros(session IUserSession, heros map[string]int32, bPush bool) (code pb.ErrorCode) // 获取英雄 // heroId 英雄ID GetHeroByObjID(uid, heroId string) (*pb.DBHero, pb.ErrorCode) @@ -64,6 +64,8 @@ type ( QueryAttributeValue(uid string, attr string) (value int32) //添加/减少属性值 第四个参数控制是否推送给前端 AddAttributeValue(session IUserSession, attr string, add int32, bPush bool) (code pb.ErrorCode) + // 批量处理 + AddAttributeValues(session IUserSession, attrs map[string]int32, bPush bool) (code pb.ErrorCode) //用户改变事件 EventUserChanged(session IUserSession) //获取用户expand diff --git a/modules/hero/model_hero.go b/modules/hero/model_hero.go index e03bc604a..c66ca69b3 100644 --- a/modules/hero/model_hero.go +++ b/modules/hero/model_hero.go @@ -112,20 +112,23 @@ func (this *ModelHero) initHeroOverlying(uid string, heroCfgId string, count int hero = this.initHero(uid, heroCfgId) if hero != nil { // 添加图鉴 - if result, err1 := this.moduleHero.ModuleUser.GetUserExpand(uid); err1 == nil { - sz := make(map[string]bool, 0) - for k := range result.GetTujian() { - sz[k] = true - } - if _, ok := result.GetTujian()[heroCfgId]; !ok { - sz[heroCfgId] = true - initUpdate := map[string]interface{}{ - "tujian": sz, + go func(uid, heroCfgId string) { // 携程处理 图鉴数据 + if result, err1 := this.moduleHero.ModuleUser.GetUserExpand(uid); err1 == nil { + sz := make(map[string]bool, 0) + for k := range result.GetTujian() { + sz[k] = true } + if _, ok := result.GetTujian()[heroCfgId]; !ok { + sz[heroCfgId] = true + initUpdate := map[string]interface{}{ + "tujian": sz, + } - this.moduleHero.ModuleUser.ChangeUserExpand(uid, initUpdate) + this.moduleHero.ModuleUser.ChangeUserExpand(uid, initUpdate) + } } - } + }(uid, heroCfgId) + hero.SameCount = count if err = this.moduleHero.modelHero.AddList(uid, hero.Id, hero); err != nil { this.moduleHero.Errorf("%v", err) diff --git a/modules/hero/module.go b/modules/hero/module.go index a64692368..1b2511024 100644 --- a/modules/hero/module.go +++ b/modules/hero/module.go @@ -159,14 +159,22 @@ func (this *Hero) EventUserOffline(session comm.IUserSession) { } // 批量创建多个英雄 -func (this *Hero) CreateRepeatHeros(session comm.IUserSession, items map[string]int32, bPush bool) (code pb.ErrorCode) { - - for heroCfgId, num := range items { - _, err := this.modelHero.createHeroOverlying(session.GetUserId(), heroCfgId, num) +func (this *Hero) CreateRepeatHeros(session comm.IUserSession, heros map[string]int32, bPush bool) (code pb.ErrorCode) { + changeHero := make([]*pb.DBHero, 0) + for heroCfgId, num := range heros { + if num == 0 { // 数量为0 不做处理 + continue + } + hero, err := this.modelHero.createHeroOverlying(session.GetUserId(), heroCfgId, num) if err != nil { code = pb.ErrorCode_HeroCreate - break + return } + changeHero = append(changeHero, hero) + } + + if bPush && len(changeHero) > 0 { //推送 + session.SendMsg("hero", "change", &pb.HeroChangePush{List: changeHero}) } return diff --git a/modules/mail/api_getAttachment.go b/modules/mail/api_getAttachment.go index 80a1a373e..f0f790ff7 100644 --- a/modules/mail/api_getAttachment.go +++ b/modules/mail/api_getAttachment.go @@ -47,7 +47,7 @@ func (this *apiComp) GetUserMailAttachment(session comm.IUserSession, req *pb.Ma if code == pb.ErrorCode_Success { // 修改状态 this.module.modelMail.MailUpdateMailAttachmentState(req.ObjID) - mail.Reward = true + //mail.Reward = true //return } } diff --git a/modules/mail/api_readmail.go b/modules/mail/api_readmail.go index 0b34a01e1..6f0d09609 100644 --- a/modules/mail/api_readmail.go +++ b/modules/mail/api_readmail.go @@ -33,7 +33,7 @@ func (this *apiComp) ReadMail(session comm.IUserSession, req *pb.MailReadMailReq return } this.module.Debugf("read mail %v", mail) - mail.Check = true + session.SendMsg(string(this.module.GetType()), "readmail", &pb.MailReadMailResp{Mail: mail}) return } diff --git a/modules/modulebase.go b/modules/modulebase.go index 62e3a97b4..5df5317c7 100644 --- a/modules/modulebase.go +++ b/modules/modulebase.go @@ -178,71 +178,56 @@ func (this *ModuleBase) CheckRes(session comm.IUserSession, res []*cfg.Game_atn) //消耗资源 func (this *ModuleBase) ConsumeRes(session comm.IUserSession, res []*cfg.Game_atn, bPush bool) (code pb.ErrorCode) { var ( - amount int32 + items map[string]int32 // 道具背包 批量处理 + attrs map[string]int32 // 属性 ) + items = make(map[string]int32, 0) + attrs = make(map[string]int32, 0) + for _, v := range res { + 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") // 找不到资源类型 + } + } source := &comm.ModuleCallSource{ Module: string(this.module.GetType()), FuncName: "CheckConsumeRes", Describe: "消耗资源", } - //校验消费资源是否充足 - for _, v := range res { - if v.A == comm.AttrType { //用户属性资源 - if amount = this.ModuleUser.QueryAttributeValue(session.GetUserId(), v.T); amount < v.N { - code = pb.ErrorCode_ResNoEnough - this.Errorf("道具不足: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 = int32(this.ModuleItems.QueryItemAmount(source, session.GetUserId(), v.T)); amount < v.N { - code = pb.ErrorCode_ResNoEnough - this.Errorf("道具不足: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) < -v { // -v 负负得正 + code = pb.ErrorCode_ResNoEnough + this.Errorf("资源不足: A: attr, T: %s, N: %d", k, v) + return } - // else if v.A == comm.HeroType { //卡片资源 - // if resID, err = strconv.Atoi(v.T); err != nil { - // code = pb.ErrorCode_ConfigurationException - // return - // } - // if amount = int32(this.ModuleHero.QueryHeroAmount(uid, int32(resID))); amount < v.N { - // code = pb.ErrorCode_ResNoEnough - // return - // } - // } - //不存在消耗武器的情况 - // } else if v.A == comm.EquipmentType { - // if resID, err = strconv.Atoi(v.T); err != nil { - // code = pb.ErrorCode_ConfigurationException - // return - // } - // if amount = int32(equipment.QueryEquipmentAmount(source, uid, int32(resID))); amount < v.N { - // code = pb.ErrorCode_ResNoEnough - // return - // } - // } } - - for _, v := range res { - if v.A == comm.AttrType { //用户属性资源 - this.ModuleUser.AddAttributeValue(session, v.T, -1*v.N, bPush) - } else if v.A == comm.ItemType { //道具资源 - //resID, _ = strconv.Atoi(v.T) - this.ModuleItems.AddItem(source, session, v.T, -1*v.N, bPush) + for k, v := range items { + if int32(this.ModuleItems.QueryItemAmount(source, session.GetUserId(), k)) < -v { + code = pb.ErrorCode_ResNoEnough + this.Errorf("道具不足: A: item, T:%s, N:%d", k, v) + return } - // else if v.A == comm.HeroType { //卡片资源 - // resID, _ = strconv.Atoi(v.T) - // this.ModuleHero.ConsumeCard(uid, int32(resID), -1*v.N) - // } - // } else if v.A == comm.EquipmentType { - // resID, _ = strconv.Atoi(v.T) - // equipment.AddNewEquipments(source, uid, resID, -1*v.N) - // } + } + // 真正消耗 + if len(attrs) > 0 { + code = this.ModuleUser.AddAttributeValues(session, attrs, bPush) + if code != pb.ErrorCode_Success { + return + } + this.Debugf("消耗玩家资源: %v", attrs) + } + if len(items) > 0 { + code = this.ModuleItems.AddItems(source, session, items, bPush) + if code != pb.ErrorCode_Success { + return + } + this.Debugf("消耗道具资源: %v", items) } return @@ -251,37 +236,53 @@ func (this *ModuleBase) ConsumeRes(session comm.IUserSession, res []*cfg.Game_at //发放资源 func (this *ModuleBase) DispenseRes(session comm.IUserSession, res []*cfg.Game_atn, bPush bool) (code pb.ErrorCode) { var ( - hero []*pb.DBHero + items map[string]int32 // 道具背包 批量处理 + heros map[string]int32 // 英雄 + attrs map[string]int32 // 属性 + equips map[string]uint32 // 装备 ) + items = make(map[string]int32, 0) + heros = make(map[string]int32, 0) + attrs = make(map[string]int32, 0) + equips = make(map[string]uint32, 0) + source := &comm.ModuleCallSource{ Module: string(this.module.GetType()), FuncName: "DispenseRes", Describe: "发放资源", } for _, v := range res { - this.Debugf("发放资源 DispenseRes:A = %s, T:%s, N:%d", v.A, v.T, v.N) - if v.A == comm.AttrType { //用户属性资源 - code = this.ModuleUser.AddAttributeValue(session, v.T, v.N, bPush) - - } else if v.A == comm.ItemType { //道具资源 - // resID = cast.ToInt(v.T) - // resID, _ = strconv.Atoi(v.T) - code = this.ModuleItems.AddItem(source, session, v.T, v.N, bPush) - } else if v.A == comm.HeroType { //卡片资源 - //resID, _ = strconv.Atoi(v.T) - _hero, err := this.ModuleHero.CreateRepeatHero(session.GetUserId(), v.T, v.N) - if err != nil { - code = pb.ErrorCode_HeroMaxCount - return + switch v.A { + case comm.AttrType: + attrs[v.T] += v.N + case comm.ItemType: + items[v.T] += v.N + case comm.HeroType: + heros[v.T] += v.N + case comm.EquipmentType: + if v.N > 0 { // 不允许减少装备 + equips[v.T] += uint32(v.N) } - hero = append(hero, _hero) - } else if v.A == comm.EquipmentType { - //resID, _ = strconv.Atoi(v.T) - code = this.ModuleEquipment.AddNewEquipments(source, session, map[string]uint32{v.T: uint32(v.N)}, bPush) + default: + this.Errorf("not found res type") // 找不到资源类型 } } - if len(hero) > 0 { - session.SendMsg("hero", "change", &pb.HeroChangePush{List: hero}) + + if len(attrs) > 0 { //用户属性资源 + code = this.ModuleUser.AddAttributeValues(session, attrs, bPush) + this.Debugf("发放用户资源: %v", attrs) + } + if len(items) > 0 { //道具资源 + code = this.ModuleItems.AddItems(source, session, items, bPush) + this.Debugf("发放道具资源: %v", items) + } + if len(heros) > 0 { //卡片资源 + code = this.ModuleHero.CreateRepeatHeros(session, heros, bPush) + this.Debugf("发放英雄资源: %v", heros) + } + if len(equips) > 0 { + code = this.ModuleEquipment.AddNewEquipments(source, session, equips, bPush) + this.Debugf("发放装备资源: %v", equips) } return diff --git a/modules/user/module.go b/modules/user/module.go index 72f1b7fe2..f0fb5e39f 100644 --- a/modules/user/module.go +++ b/modules/user/module.go @@ -161,6 +161,74 @@ func (this *User) AddAttributeValue(session comm.IUserSession, attr string, add return } +//用户资源 +func (this *User) AddAttributeValues(session comm.IUserSession, attrs map[string]int32, bPush bool) (code pb.ErrorCode) { + user := this.GetUser(session.GetUserId()) + if user == nil { + code = pb.ErrorCode_UserSessionNobeing + return + } + _change := &pb.UserResChangePush{ + Gold: user.Gold, + Exp: user.Exp, + Lv: user.Lv, + Vip: user.Vip, + Diamond: user.Diamond, + } + update := make(map[string]interface{}) + for key, add := range attrs { + if add == 0 { + log.Errorf("attr no changed,uid: %s attr: %s add: %d", session.GetUserId(), key, add) + continue + } + switch key { + case comm.ResGold: + if add < 0 { + if user.Gold+add < 0 { + code = pb.ErrorCode_GoldNoEnough + return + } + } + _change.Gold += add + update[comm.ResGold] = user.Gold + add + case comm.ResExp: + if add < 0 { + if user.Exp+add < 0 { + code = pb.ErrorCode_ResNoEnough + return + } + } + _change.Exp += add + update[comm.ResExp] = user.Exp + add + case comm.ResDiamond: + if add < 0 { + if user.Diamond+add < 0 { + code = pb.ErrorCode_ResNoEnough + return + } + } + _change.Diamond += add + update[comm.ResDiamond] = user.Diamond + add + default: + code = pb.ErrorCode_Unknown + } + } + + if len(update) == 0 { + log.Warn("AddAttributeValue param is empty") + return + } + if err := this.modelUser.updateUserAttr(session.GetUserId(), update); err != nil { + log.Errorf("AddAttributeValue err:%v", err) + code = pb.ErrorCode_DBError + } + + if bPush { //推送玩家账号信息变化消息 + session.SendMsg(string(this.GetType()), "reschange", _change) + } + return +} + // 用户事件变化 func (this *User) EventUserChanged(session comm.IUserSession) { ul := new(UserListen)