88 lines
2.2 KiB
Go
88 lines
2.2 KiB
Go
package reputation
|
|
|
|
import (
|
|
"go_dreamfactory/comm"
|
|
"go_dreamfactory/lego/core"
|
|
"go_dreamfactory/lego/sys/log"
|
|
"go_dreamfactory/modules"
|
|
"go_dreamfactory/pb"
|
|
cfg "go_dreamfactory/sys/configure/structs"
|
|
|
|
"go.mongodb.org/mongo-driver/mongo"
|
|
)
|
|
|
|
type ModelReputation struct {
|
|
modules.MCompModel
|
|
moduleReputation *Reputation
|
|
service core.IService
|
|
}
|
|
|
|
func (this *ModelReputation) Init(service core.IService, module core.IModule, comp core.IModuleComp, options core.IModuleOptions) (err error) {
|
|
err = this.MCompModel.Init(service, module, comp, options)
|
|
this.TableName = comm.TableReputation
|
|
this.moduleReputation = module.(*Reputation)
|
|
this.service = service
|
|
return
|
|
}
|
|
|
|
// 获取玩家声望数据
|
|
func (this *ModelReputation) getDBReputation(uid string) *pb.DBReputation {
|
|
reputation := &pb.DBReputation{}
|
|
if err := this.Get(uid, reputation); err != nil {
|
|
if err == mongo.ErrNoDocuments {
|
|
reputation.Uid = uid
|
|
reputation.Camps = make(map[int32]*pb.Camp)
|
|
if err2 := this.Add(uid, reputation); err2 == nil {
|
|
return reputation
|
|
}
|
|
}
|
|
this.moduleReputation.Error("getDBReputation",
|
|
log.Field{Key: "uid", Value: uid},
|
|
log.Field{Key: "err", Value: err})
|
|
}
|
|
return reputation
|
|
}
|
|
|
|
// 更新声望数据
|
|
func (this *ModelReputation) updateDBReputation(uid string, data map[string]interface{}) {
|
|
if err := this.Change(uid, data); err != nil {
|
|
this.moduleReputation.Error("updateDBReputation",
|
|
log.Field{Key: "uid", Value: uid},
|
|
log.Field{Key: "err", Value: err})
|
|
}
|
|
}
|
|
|
|
// 阵营属性
|
|
func (this *ModelReputation) computeAttr(attr *pb.CampAttr, cfgs []*cfg.Gameatr) *pb.CampAttr {
|
|
if attr == nil {
|
|
attr = &pb.CampAttr{}
|
|
}
|
|
for _, v := range cfgs {
|
|
switch v.A {
|
|
case comm.Hp:
|
|
attr.Hp += v.N
|
|
case comm.Atk:
|
|
attr.Attack += v.N
|
|
case comm.Def:
|
|
attr.Defense += v.N
|
|
case comm.HpPro:
|
|
attr.HpPer += v.N
|
|
}
|
|
}
|
|
return attr
|
|
}
|
|
|
|
// 合计所有阵营的属性
|
|
func (this *ModelReputation) mergeAttrs(camps map[int32]*pb.Camp) *pb.CampAttr {
|
|
totalAttr := &pb.CampAttr{}
|
|
for _, v := range camps {
|
|
if v.CampAttr != nil {
|
|
totalAttr.Hp += v.CampAttr.Hp
|
|
totalAttr.Attack += v.CampAttr.Attack
|
|
totalAttr.Defense += v.CampAttr.Defense
|
|
totalAttr.HpPer += v.CampAttr.HpPer
|
|
}
|
|
}
|
|
return totalAttr
|
|
}
|