From e4d543647dfd753cc8bfc6331116ecd5725ee2e8 Mon Sep 17 00:00:00 2001 From: meixiongfeng <766881921@qq.com> Date: Tue, 28 Jun 2022 12:07:12 +0800 Subject: [PATCH] =?UTF-8?q?=E8=8B=B1=E9=9B=84=E5=8D=87=E7=BA=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- modules/dbservice/db_comp.go | 25 ++++++------ modules/hero/api.go | 10 +++++ modules/hero/api_heroStrengthen.go | 55 +++++++++++++++++++++++---- modules/hero/module.go | 61 +++++++++++++++++++++++++++--- 4 files changed, 126 insertions(+), 25 deletions(-) diff --git a/modules/dbservice/db_comp.go b/modules/dbservice/db_comp.go index 24383fe2f..e01d253d0 100644 --- a/modules/dbservice/db_comp.go +++ b/modules/dbservice/db_comp.go @@ -85,13 +85,13 @@ func (this *DB_Comp) Model_UpdateDBByLog(uid string) (err error) { log.Errorf("parameter len _id : %s,uid : %s d.len:%v", data.ID, data.UID, len(data.D)) continue } - _obj := bson.M{} - for _, v := range data.D[1].(bson.D) { - _obj[v.Key] = v.Value + _obj := make(bson.A, len(data.D[1].(bson.A))) + for i, v := range data.D[1].(bson.A) { + _obj[i] = v } _key := data.D[0].(string) - _, err := this.DB.InsertOne(core.SqlTable(_key), _obj) + _, err := this.DB.InsertMany(core.SqlTable(_key), _obj) if err != nil { log.Errorf("insert %s db err:%v", (core.SqlTable(_key)), err) ErrorLogCount[data.ID]++ @@ -138,16 +138,17 @@ func (this *DB_Comp) Model_UpdateDBByLog(uid string) (err error) { } _tableName := data.D[0].(string) //表名 - Where := bson.M{} - Query := bson.M{} + Where := data.D[1].(bson.M) + Query := data.D[2].(bson.M) - for _, v := range data.D[1].(bson.D) { - Where[v.Key] = v.Value - } - for _, v := range data.D[2].(bson.D) { - Query[v.Key] = v.Value + // for key, v := range data.D[1].(bson.M) { + // //Where[v.Key] = v.Value + // Where = v + // } + // for _, v := range data.D[2].(bson.M) { + // Query[v.Key] = v.Value - } + // } _, err = this.DB.UpdateMany(core.SqlTable(_tableName), Where, bson.M{"$set": Query}) if err != nil { log.Errorf("Update %s db err:%v", core.SqlTable(_tableName), err) diff --git a/modules/hero/api.go b/modules/hero/api.go index b8759bf0c..88ca46318 100644 --- a/modules/hero/api.go +++ b/modules/hero/api.go @@ -1,6 +1,7 @@ package hero import ( + "go_dreamfactory/comm" "go_dreamfactory/lego/core" "go_dreamfactory/modules" ) @@ -9,6 +10,7 @@ type Api_Comp struct { modules.MComp_GateComp service core.IService module *Hero + user comm.IUser } const ( //消息回复的头名称 @@ -27,5 +29,13 @@ func (this *Api_Comp) Init(service core.IService, module core.IModule, comp core func (this *Api_Comp) Start() (err error) { err = this.MComp_GateComp.Start() + + var module core.IModule + + if module, err = this.service.GetModule(comm.SM_UserModule); err != nil { + return + } + this.user = module.(comm.IUser) + return } diff --git a/modules/hero/api_heroStrengthen.go b/modules/hero/api_heroStrengthen.go index 1528ef679..1a7bec85c 100644 --- a/modules/hero/api_heroStrengthen.go +++ b/modules/hero/api_heroStrengthen.go @@ -2,6 +2,7 @@ package hero import ( "go_dreamfactory/comm" + "go_dreamfactory/lego/sys/log" "go_dreamfactory/pb" ) @@ -11,12 +12,20 @@ func (this *Api_Comp) StrengthenUplv_Check(session comm.IUserSession, req *pb.He code.Code = pb.ErrorCode_ReqParameterError return } - _hero, err := this.module.model_hero.moduleHero.GetHeroInfoByObjID(req.HeroObjID) // 校验升级的对象是否存在 + var ( + curLv int32 + curExp int32 // 当前英雄的经验 + costGold int32 // 当前需要消耗金币的数量 + addExp int32 // 需要增加的经验 + //atn = map[string]interface{}{} + ) + + _hero, err := this.module.model_hero.moduleHero.GetHero(session.GetUserId(), req.HeroObjID) // 校验升级的对象是否存在 if err != 0 { code.Code = pb.ErrorCode_HeroNoExist return } - _expHero, err := this.module.model_hero.moduleHero.GetHeroInfoByObjID(req.ExpCardID) // 校验需要消耗经验卡牌的对象是否存在 + _expHero, err := this.module.model_hero.moduleHero.GetHero(session.GetUserId(), req.ExpCardID) // 校验需要消耗经验卡牌的对象是否存在 if err != 0 { code.Code = pb.ErrorCode_HeroNoExist return @@ -25,10 +34,9 @@ func (this *Api_Comp) StrengthenUplv_Check(session comm.IUserSession, req *pb.He code.Code = pb.ErrorCode_HeroNoExist return } - curLv := _hero.Lv - curExp := _hero.Exp // 当前英雄的经验 - var costGold int32 // 当前需要消耗金币的数量 - var addExp int32 // 需要增加的经验 + curLv = _hero.Lv + curExp = _hero.Exp // 当前英雄的经验 + // 查询 本次消耗会获得多少经验 expConf := this.module.configure_comp.GetHeroExp(_expHero.HeroID) if expConf != nil { @@ -44,7 +52,12 @@ func (this *Api_Comp) StrengthenUplv_Check(session comm.IUserSession, req *pb.He code.Code = pb.ErrorCode_HeroMaxLv return } + if _data := this.module.configure_comp.GetHeroLv(curLv); _data != nil { + // atn["a"] = _data.Gold[0].A + // atn["t"] = _data.Gold[0].T + // atn["n"] = _data.Gold[0].N + costGold += _data.Gold[0].N curExp += addExp // 先把经验加上 // 当前升级需要消耗的经验 @@ -74,18 +87,46 @@ func (this *Api_Comp) StrengthenUplv_Check(session comm.IUserSession, req *pb.He "costGold": costGold, "curExp": curExp, "curLv": curLv, + "addExp": addExp, + //"atn": atn, } return } /// 英雄升级 func (this *Api_Comp) StrengthenUplv(session comm.IUserSession, agrs map[string]interface{}, req *pb.Hero_StrengthenUplv_Req) (code pb.ErrorCode) { - + var ( + curLv int32 + curExp int32 // 当前英雄的经验 + costGold int32 // 当前需要消耗金币的数量 + addExp int32 // 需要增加的经验 + //atn = map[string]interface{}{} + ) defer func() { if code == pb.ErrorCode_Success { session.SendMsg(string(this.module.GetType()), StrengthenUplv, &pb.Hero_StrengthenUplv_Resp{}) } }() + costGold = agrs["costGold"].(int32) + curLv = agrs["curLv"].(int32) + curExp = agrs["curExp"].(int32) + addExp = agrs["addExp"].(int32) + log.Debugf("升级后当前等级: %d,经验: %d,需要消耗的金币: %d,增加的经验: %d", curLv, curExp, costGold, addExp) + // 执行升级逻辑 + code = this.module.model_hero.moduleHero.AddCardExp(session.GetUserId(), req.HeroObjID, addExp) // 加经验 + if code != pb.ErrorCode_Success { + return + } + // 消耗道具 + code = this.user.AddAttributeValue(session.GetUserId(), "gold", -costGold) // 减少金币 + if code != pb.ErrorCode_Success { + return + } + // 删除经验卡 + code = this.module.model_hero.moduleHero.DelCard(req.ExpCardID, req.Amount) + if code != pb.ErrorCode_Success { + return + } return } diff --git a/modules/hero/module.go b/modules/hero/module.go index b0115b709..34b5ec11d 100644 --- a/modules/hero/module.go +++ b/modules/hero/module.go @@ -17,6 +17,7 @@ type Hero struct { api_comp *Api_Comp configure_comp *Configure_Comp model_hero *ModelHero + items comm.IItems } //模块名 @@ -38,12 +39,6 @@ func (this *Hero) OnInstallComp() { this.configure_comp = this.RegisterComp(new(Configure_Comp)).(*Configure_Comp) } -//通过唯一对象获取英雄信息 -func (this *Hero) GetHeroInfoByObjID(id string) (*pb.DB_HeroData, pb.ErrorCode) { - - return nil, pb.ErrorCode_HeroNoExist -} - //创建新英雄 func (this *Hero) CreatMultiHero(uid string, heroCfgId ...int32) error { return this.model_hero.createMultiHero(uid, heroCfgId...) @@ -89,4 +84,58 @@ func (this *Hero) QueryCardAmount(uId string, cardId int32) (amount uint32) { return 0 } +// 给指定英雄加经验 +func (this *Hero) AddCardExp(uid string, cardid string, exp int32) (code pb.ErrorCode) { + var ( + curExp int32 + curLv int32 + ) + _hero, err := this.GetHero(uid, cardid) // 获取英雄信息 + if err != 0 { + code = pb.ErrorCode_HeroNoExist + return + } + curExp = _hero.Exp + curLv = _hero.Lv + curExp += exp // 先把经验加上 + if _data := this.configure_comp.GetHeroLv(curLv); _data != nil { + + for { // 死循环判断一键升级 + // 校验等级达到上限 + var maxLv int32 + maxLv = _hero.Star * comm.HeroStarLvRatio + if maxLv >= _hero.Lv && _hero.Exp >= curExp { // 设置最大经验和等级 + curLv = maxLv + curExp = _data.Heroexp[0].N + break + } + if _data.Heroexp[0].N <= curExp { // 升级操作 + curExp = _data.Heroexp[0].N + if curExp >= 0 { // 大于下一级经验 + curLv += 1 // 经验够了 那么等级+1 + if _data := this.configure_comp.GetHeroLv(curLv); _data != nil { + if _data.Heroexp[0].N > curExp { // 经验不足则 直接返回 + break + } + } else { + break + } + } + } else { + break + } + } + } + _hero.Lv = curLv + _hero.Exp = curExp + // 校验是否达到最大等级 + + this.ModifyHero(_hero) // 修改英雄数据 + return +} + +// 删除指定卡牌 +func (this *Hero) DelCard(cardid string, amount int32) (code pb.ErrorCode) { + return +}