go_dreamfactory/modules/reputation/module.go

134 lines
3.1 KiB
Go

package reputation
import (
"go_dreamfactory/comm"
"go_dreamfactory/lego/base"
"go_dreamfactory/lego/core"
"go_dreamfactory/lego/sys/log"
"go_dreamfactory/modules"
"go_dreamfactory/pb"
cfg "go_dreamfactory/sys/configure/structs"
)
var _ comm.IReputation = (*Reputation)(nil)
type Reputation struct {
modules.ModuleBase
api *apiComp
service base.IRPCXService
configure *configureComp
modelReputation *ModelReputation
}
func NewModule() core.IModule {
return &Reputation{}
}
func (this *Reputation) Init(service core.IService, module core.IModule, options core.IModuleOptions) (err error) {
if err = this.ModuleBase.Init(service, module, options); err != nil {
return
}
this.service = service.(base.IRPCXService)
return
}
func (this *Reputation) OnInstallComp() {
this.ModuleBase.OnInstallComp()
this.api = this.RegisterComp(new(apiComp)).(*apiComp)
this.configure = this.RegisterComp(new(configureComp)).(*configureComp)
this.modelReputation = this.RegisterComp(new(ModelReputation)).(*ModelReputation)
}
func (this *Reputation) GetType() core.M_Modules {
return comm.ModuleReputation
}
func (this *Reputation) Start() (err error) {
if err = this.ModuleBase.Start(); err != nil {
return
}
return
}
// 获取阵营最大等级
// raceType 阵营类型
func (this *Reputation) getCampLvMax(raceType int32) (maxLv int32) {
campLvConf, err := this.configure.getCampCfg()
if err != nil {
return
}
arr := campLvConf.GetDataList()
n := len(arr)
if n > 0 {
maxLv = arr[n-1].ReputationLv
} else {
maxLv = -1
}
return
}
// 声望升级
// raceType 阵营 fv 阵营好感度累计值
func (this *Reputation) Upgrade(session comm.IUserSession, raceType int32, fv int32) int32 {
uid := session.GetUserId()
reputation := this.modelReputation.getDBReputation(uid)
if reputation == nil {
return 0
}
c, ok := reputation.Camps[raceType]
if !ok {
reputation.Camps[raceType] = &pb.Camp{}
c = reputation.Camps[raceType]
}
maxLv := this.getCampLvMax(raceType)
if c.ReputationLv == maxLv {
this.Debug("已达到声望最大等级",
log.Field{Key: "uid", Value: uid},
log.Field{Key: "maxLv", Value: c.ReputationLv})
return c.ReputationLv
}
campConf, err := this.configure.getCampCfg()
if err != nil {
return 0
}
var (
reward *cfg.Gameatn
flag bool
)
for _, cnf := range campConf.GetDataList() {
if cnf.RaceType == raceType {
nextCampConf := this.configure.getCampBy(raceType, c.ReputationLv+1)
if nextCampConf == nil {
return cnf.ReputationLv
} else {
if fv >= nextCampConf.ReputationExp {
c.ReputationLv = nextCampConf.ReputationLv
reward = cnf.Reward
flag = true
}
break
}
}
}
if flag {
update := map[string]interface{}{
"camps": reputation.Camps,
}
if err := this.modelReputation.updateDBReputation(uid, update); err == nil {
this.DispenseRes(session, []*cfg.Gameatn{reward}, false)
}
go this.AsynHandleSession(session.Clone(), func(session comm.IUserSession) {
this.WriteUserLog(session.GetUserId(), 0, "ReputationUpgrade", reward)
})
}
return c.ReputationLv
}