克隆英雄对象

This commit is contained in:
meixiongfeng 2022-07-25 18:13:27 +08:00
parent 97beb8c48d
commit 10fb331b52
3 changed files with 23 additions and 24 deletions

View File

@ -97,6 +97,13 @@ func (this *ModelHero) createOneHero(uid string, heroCfgId string) (hero *pb.DBH
return return
} }
func (this *ModelHero) CloneNewHero(hero *pb.DBHero) (newHero *pb.DBHero) {
temp := *hero
newHero = &temp
newHero.Id = primitive.NewObjectID().Hex()
return
}
//初始化可叠加的英雄 //初始化可叠加的英雄
func (this *ModelHero) initHeroOverlying(uid string, heroCfgId string, count int32) (hero *pb.DBHero, err error) { func (this *ModelHero) initHeroOverlying(uid string, heroCfgId string, count int32) (hero *pb.DBHero, err error) {
hero = this.initHero(uid, heroCfgId) hero = this.initHero(uid, heroCfgId)
@ -259,18 +266,16 @@ func (this *ModelHero) getHeroList(uid string) []*pb.DBHero {
// 设置装备属性 // 设置装备属性
func (this *ModelHero) setEquipProperty(hero *pb.DBHero, equip []*pb.DB_Equipment) { func (this *ModelHero) setEquipProperty(hero *pb.DBHero, equip []*pb.DB_Equipment) {
//property := make(map[string]int32) //主属性
addProperty := make(map[string]int32) //副属性 addProperty := make(map[string]int32) //副属性
for i, v := range equip { for i, v := range equip {
if v == nil { if v == nil {
continue continue
} }
hero.EquipID[i] = v.Id hero.EquipID[i] = v.Id
//主属性 addProperty[v.MainEntry.AttrName] += v.MainEntry.Value //主属性
//property[v.MainEntry.AttrName] = v.MainEntry.Value
//附加属性
for _, v := range v.AdverbEntry { for _, v := range v.AdverbEntry {
addProperty[v.AttrName] += v.Value addProperty[v.AttrName] += v.Value //附加属性
} }
} }
@ -302,11 +307,12 @@ func (this *ModelHero) setEquipment(hero *pb.DBHero) (newHero *pb.DBHero, err er
return return
} }
//创建新卡 //创建新卡
newHero, err = this.createOneHero(hero.Uid, hero.HeroID) newHero = this.CloneNewHero(hero)
if err != nil { // newHero, err = this.createOneHero(hero.Uid, hero.HeroID)
this.moduleHero.Errorf("%v", err) // if err != nil {
return // this.moduleHero.Errorf("%v", err)
} // return
// }
update1 := make(map[string]interface{}) update1 := make(map[string]interface{})
update1["sameCount"] = curSameCount update1["sameCount"] = curSameCount

View File

@ -59,14 +59,12 @@ func (this *Hero) GetHeroByObjID(uid, heroId string) (*pb.DBHero, pb.ErrorCode)
//佩戴装备 //佩戴装备
func (this *Hero) UpdateEquipment(session comm.IUserSession, hero *pb.DBHero, equip []*pb.DB_Equipment) (code pb.ErrorCode) { func (this *Hero) UpdateEquipment(session comm.IUserSession, hero *pb.DBHero, equip []*pb.DB_Equipment) (code pb.ErrorCode) {
var (
tagHero *pb.DBHero // 目标英雄 有可能是创建了新的英雄对象
)
if hero == nil { if hero == nil {
code = pb.ErrorCode_HeroNoExist code = pb.ErrorCode_HeroNoExist
return return
} }
tagHero = hero
list := make([]*pb.DBHero, 0) list := make([]*pb.DBHero, 0)
if newHero, err := this.modelHero.setEquipment(hero); err != nil { if newHero, err := this.modelHero.setEquipment(hero); err != nil {
code = pb.ErrorCode_HeroEquipUpdate code = pb.ErrorCode_HeroEquipUpdate
@ -74,20 +72,13 @@ func (this *Hero) UpdateEquipment(session comm.IUserSession, hero *pb.DBHero, eq
} else { } else {
if newHero != nil { if newHero != nil {
list = append(list, newHero) list = append(list, newHero)
tagHero = newHero
} }
} }
list = append(list, hero) list = append(list, hero)
m, err1 := this.modelHero.PushHeroProperty(session, tagHero.Id) // 推送属性变化 this.modelHero.setEquipProperty(hero, equip)
if err1 != nil {
code = pb.ErrorCode_Unknown
this.Errorf("PushHeroProperty err!")
}
tagHero.Property = m
session.SendMsg("hero", "change", &pb.HeroChangePush{List: list}) session.SendMsg("hero", "change", &pb.HeroChangePush{List: list})
this.modelHero.setEquipProperty(tagHero, equip)
return return
} }

View File

@ -40,11 +40,13 @@ func (this *DB_Comp) Start() (err error) {
} }
func (this *DB_Comp) run() { func (this *DB_Comp) run() {
timer := time.NewTimer(time.Second * 2)
defer timer.Stop()
for { for {
select { select {
case v := <-this.task: case v := <-this.task:
this.Model_UpdateDBByLog(v) this.Model_UpdateDBByLog(v)
case <-time.After(time.Second * 2): case <-timer.C:
this.Model_UpdateDBByLog("") this.Model_UpdateDBByLog("")
} }
if !this.isInit && this.Model_TotalCount() <= 0 { if !this.isInit && this.Model_TotalCount() <= 0 {