任务条件优化

This commit is contained in:
wh_zcy 2023-03-14 18:19:31 +08:00
parent 4a3ca9166e
commit 413362e21a
4 changed files with 237 additions and 91 deletions

View File

@ -623,8 +623,8 @@
{ {
"id": 170, "id": 170,
"type": 1, "type": 1,
"data1": 44006, "data1": 1,
"data2": 0, "data2": 44006,
"data3": 0, "data3": 0,
"data4": 0, "data4": 0,
"data5": 0 "data5": 0

View File

@ -15,6 +15,7 @@ import (
cfg "go_dreamfactory/sys/configure/structs" cfg "go_dreamfactory/sys/configure/structs"
"go_dreamfactory/sys/db" "go_dreamfactory/sys/db"
"go_dreamfactory/utils" "go_dreamfactory/utils"
"sync"
"github.com/pkg/errors" "github.com/pkg/errors"
) )
@ -40,8 +41,8 @@ type ModuleRtask struct {
modelRtaskRecord *ModelRtaskRecord modelRtaskRecord *ModelRtaskRecord
api *apiComp api *apiComp
configure *configureComp configure *configureComp
lock sync.Mutex
handleMap map[int32]*rtaskCondi //任务校验处理器 handleMap map[int32]*rtaskCondi //任务校验处理器
} }
func NewModule() core.IModule { func NewModule() core.IModule {
@ -57,7 +58,7 @@ func (this *ModuleRtask) GetType() core.M_Modules {
func (this *ModuleRtask) Init(service core.IService, module core.IModule, options core.IModuleOptions) (err error) { func (this *ModuleRtask) Init(service core.IService, module core.IModule, options core.IModuleOptions) (err error) {
err = this.ModuleBase.Init(service, module, options) err = this.ModuleBase.Init(service, module, options)
this.service = service.(base.IRPCXService) this.service = service.(base.IRPCXService)
this.initRtaskVerifyHandle() // this.initRtaskVerifyHandle()
return return
} }
func (this *ModuleRtask) Start() (err error) { func (this *ModuleRtask) Start() (err error) {
@ -75,12 +76,198 @@ func (this *ModuleRtask) OnInstallComp() {
} }
func (this *ModuleRtask) registerVerifyHandle(condiId int32, condi *rtaskCondi) { func (this *ModuleRtask) registerVerifyHandle(condiId int32, condi *rtaskCondi) {
if _, ok := this.handleMap[condiId]; !ok { // if _, ok := this.handleMap[condiId]; !ok {
this.handleMap[condiId] = condi // this.handleMap[condiId] = condi
// }
this.lock.Lock()
defer this.lock.Unlock()
this.handleMap[condiId] = condi
}
func (this *ModuleRtask) getHandle(tt comm.TaskType) (condis []*rtaskCondi) {
for _, v := range this.configure.getRtaskCondis(int32(tt)) {
switch tt {
case comm.Rtype1:
condi := &rtaskCondi{
condId: v.Id,
find: this.modelRtaskRecord.equalFirstParam,
verify: this.modelRtask.verfiyRtype1,
update: this.modelRtaskRecord.addUpdate,
}
condis = append(condis, condi)
this.registerVerifyHandle(v.Id, condi)
case comm.Rtype2:
condi := &rtaskCondi{
condId: v.Id,
find: this.modelRtaskRecord.equalFirstParam,
verify: this.modelRtask.verifyRtype2,
update: this.modelRtaskRecord.overrideUpdate,
}
condis = append(condis, condi)
this.registerVerifyHandle(v.Id, condi)
case comm.Rtype3:
condi := &rtaskCondi{
condId: v.Id,
find: this.modelRtaskRecord.equalFirstParam,
verify: this.modelRtask.verifyRtype3,
update: this.modelRtaskRecord.overrideUpdate,
}
condis = append(condis, condi)
this.registerVerifyHandle(v.Id, condi)
case comm.Rtype4:
condi := &rtaskCondi{
condId: v.Id,
find: this.modelRtaskRecord.equalFirstParam,
verify: this.modelRtask.verifyRtype4,
update: this.modelRtaskRecord.overrideUpdate,
}
condis = append(condis, condi)
this.registerVerifyHandle(v.Id, condi)
case comm.Rtype5:
condi := &rtaskCondi{
condId: v.Id,
find: this.modelRtaskRecord.equalFirstParam,
verify: this.modelRtask.verifyRtype5,
update: this.modelRtaskRecord.overrideUpdate,
}
condis = append(condis, condi)
this.registerVerifyHandle(v.Id, condi)
case comm.Rtype6:
condi := &rtaskCondi{
condId: v.Id,
find: this.modelRtaskRecord.equalFirstParam,
verify: this.modelRtask.verifyRtype6,
update: this.modelRtaskRecord.overrideUpdate,
}
condis = append(condis, condi)
this.registerVerifyHandle(v.Id, condi)
case comm.Rtype8:
condi := &rtaskCondi{
condId: v.Id,
find: this.modelRtaskRecord.equalFirstParam,
verify: this.modelRtask.verfiyRtype8,
update: this.modelRtaskRecord.overrideUpdate,
}
condis = append(condis, condi)
this.registerVerifyHandle(v.Id, condi)
case comm.Rtype9:
condi := &rtaskCondi{
condId: v.Id,
find: this.modelRtaskRecord.equalFirstParam,
verify: this.modelRtask.verfiyRtype9,
update: this.modelRtaskRecord.overrideUpdate,
}
condis = append(condis, condi)
this.registerVerifyHandle(v.Id, condi)
case comm.Rtype10:
condi := &rtaskCondi{
condId: v.Id,
find: this.modelRtaskRecord.equalFirstParam,
verify: this.modelRtask.verfiyRtype10,
update: this.modelRtaskRecord.overrideUpdate,
}
condis = append(condis, condi)
this.registerVerifyHandle(v.Id, condi)
case comm.Rtype11, comm.Rtype84, comm.Rtype85:
condi := &rtaskCondi{
condId: v.Id,
find: this.modelRtaskRecord.lessEqualFirstParam,
verify: this.modelRtaskRecord.verifyFirstGreatEqualParam,
update: this.modelRtaskRecord.overrideUpdate,
}
condis = append(condis, condi)
this.registerVerifyHandle(v.Id, condi)
case comm.Rtype18:
condi := &rtaskCondi{
condId: v.Id,
find: this.modelRtaskRecord.greatEqualFirstParam,
verify: this.modelRtaskRecord.verifyFirstGreatEqualParam,
update: this.modelRtaskRecord.addUpdate,
}
condis = append(condis, condi)
this.registerVerifyHandle(v.Id, condi)
case comm.Rtype7, comm.Rtype12, comm.Rtype13, comm.Rtype14, comm.Rtype15,
comm.Rtype19, comm.Rtype21, comm.Rtype24,
comm.Rtype26, comm.Rtype27, comm.Rtype28, comm.Rtype38,
comm.Rtype39, comm.Rtype50, comm.Rtype51, comm.Rtype53,
comm.Rtype54, comm.Rtype57, comm.Rtype58, comm.Rtype60,
comm.Rtype62, comm.Rtype64, comm.Rtype69, comm.Rtype72, comm.Rtype88, comm.Rtype104,
comm.Rtype96, comm.Rtype105, comm.Rtype128, comm.Rtype130, comm.Rtype131,
comm.Rtype141, comm.Rtype142, comm.Rtype143, comm.Rtype144, comm.Rtype145, comm.Rtype146,
comm.Rtype147, comm.Rtype149, comm.Rtype153, comm.Rtype154, comm.Rtype155, comm.Rtype156:
condi := &rtaskCondi{
condId: v.Id,
find: this.modelRtaskRecord.lessEqualFirstParam,
verify: this.modelRtaskRecord.verifyFirstGreatEqualParam,
update: this.modelRtaskRecord.addUpdate,
}
condis = append(condis, condi)
this.registerVerifyHandle(v.Id, condi)
case comm.Rtype20:
condi := &rtaskCondi{
condId: v.Id,
find: this.modelRtaskRecord.equalFirstParam,
verify: this.modelRtask.verifyRtype20,
update: this.modelRtaskRecord.overrideUpdate,
}
condis = append(condis, condi)
this.registerVerifyHandle(v.Id, condi)
case comm.Rtype22, comm.Rtype109:
condi := &rtaskCondi{
condId: v.Id,
find: this.modelRtaskRecord.equalFirstParam,
verify: this.modelRtaskRecord.verifyFirstEqualParam,
update: this.modelRtaskRecord.overrideUpdate,
}
condis = append(condis, condi)
this.registerVerifyHandle(v.Id, condi)
case comm.Rtype63:
condi := &rtaskCondi{
condId: v.Id,
find: this.modelRtaskRecord.equalFirstParam,
verify: this.modelRtask.verifyRtype63,
update: this.modelRtaskRecord.addUpdate,
}
condis = append(condis, condi)
this.registerVerifyHandle(v.Id, condi)
case comm.Rtype16, comm.Rtype17,
comm.Rtype35, comm.Rtype44,
comm.Rtype59, comm.Rtype61, comm.Rtype70:
condi := &rtaskCondi{
condId: v.Id,
find: this.modelRtaskRecord.equalParams,
verify: this.modelRtaskRecord.verifyFromDb,
update: this.modelRtaskRecord.overrideUpdate,
}
condis = append(condis, condi)
this.registerVerifyHandle(v.Id, condi)
case comm.Rtype23, comm.Rtype25, comm.Rtype29, comm.Rtype30, comm.Rtype31,
comm.Rtype32, comm.Rtype33, comm.Rtype34, comm.Rtype36,
comm.Rtype37, comm.Rtype40, comm.Rtype41,
comm.Rtype42, comm.Rtype43, comm.Rtype45,
comm.Rtype46, comm.Rtype47, comm.Rtype48, comm.Rtype49,
comm.Rtype52, comm.Rtype55, comm.Rtype56,
comm.Rtype65, comm.Rtype66, comm.Rtype67, comm.Rtype68, comm.Rtype140:
condi := &rtaskCondi{
condId: v.Id,
find: this.modelRtaskRecord.lessThanParams,
verify: this.modelRtaskRecord.verifyFromDb,
update: this.modelRtaskRecord.addUpdate,
}
condis = append(condis, condi)
this.registerVerifyHandle(v.Id, condi)
default:
log.Warnf("rtaskType[%v] not register", tt)
}
} }
return
} }
// 条件校验初始 // 条件校验初始
// Deprecated
func (this *ModuleRtask) initRtaskVerifyHandle() { func (this *ModuleRtask) initRtaskVerifyHandle() {
conf, err := this.configure.getRtaskCondiCfg() conf, err := this.configure.getRtaskCondiCfg()
if err != nil { if err != nil {
@ -236,36 +423,13 @@ func (this *ModuleRtask) processOneTask(session comm.IUserSession, rtaskType com
} }
return return
} }
for _, codiConf := range this.configure.getRtaskCondis(int32(rtaskType)) {
v, ok := this.handleMap[codiConf.Id]
if !ok {
this.Warn("未注册事件处理器",
log.Field{Key: "uid", Value: uid},
log.Field{Key: "condiId", Value: codiConf.Id},
)
code = pb.ErrorCode_RtaskCondiNoFound
return
}
if v.find == nil {
this.Warn("未设置find Handle",
log.Field{Key: "uid", Value: uid},
log.Field{Key: "condiId", Value: codiConf.Id},
)
return
}
if condiId, _ := v.find(codiConf, params...); condiId != 0 {
v.condId = codiConf.Id
condis = append(condis, v)
}
}
condis = this.getHandle(rtaskType)
// update // update
for _, v := range condis { for _, v := range condis {
conf, err := this.configure.getRtaskTypeById(v.condId) conf, err := this.configure.getRtaskTypeById(v.condId)
if err != nil { if err != nil {
log.Errorf("get condId conf err:%v", err) log.Debug("任务配置未找到", log.Field{Key: "condId", Value: v.condId})
code = pb.ErrorCode_RtaskCondiNoFound code = pb.ErrorCode_RtaskCondiNoFound
return return
} }
@ -274,6 +438,7 @@ func (this *ModuleRtask) processOneTask(session comm.IUserSession, rtaskType com
if err := v.update(uid, conf, params...); err != nil { if err := v.update(uid, conf, params...); err != nil {
log.Errorf("update task:%v", err) log.Errorf("update task:%v", err)
code = pb.ErrorCode_DBError code = pb.ErrorCode_DBError
return
} }
} }
@ -281,13 +446,6 @@ func (this *ModuleRtask) processOneTask(session comm.IUserSession, rtaskType com
if code = this.CheckCondi(uid, conf.Id); code == pb.ErrorCode_Success { if code = this.CheckCondi(uid, conf.Id); code == pb.ErrorCode_Success {
module, err := this.service.GetModule(comm.ModuleWorldtask) module, err := this.service.GetModule(comm.ModuleWorldtask)
if err == nil { if err == nil {
// go func() {
// defer func() {
// if r := recover(); r != nil {
// log.Errorf("[worldtask ] err:%v ", r)
// }
// }()
if worldtask, ok := module.(comm.IWorldtask); ok { if worldtask, ok := module.(comm.IWorldtask); ok {
if err := worldtask.TaskcondNotify(session, conf.Id); err != nil { if err := worldtask.TaskcondNotify(session, conf.Id); err != nil {
log.Error("任务条件达成通知", log.Error("任务条件达成通知",
@ -297,16 +455,9 @@ func (this *ModuleRtask) processOneTask(session comm.IUserSession, rtaskType com
) )
} }
} }
// }()
} }
userModule, err := this.service.GetModule(comm.ModuleUser) userModule, err := this.service.GetModule(comm.ModuleUser)
if err == nil { if err == nil {
// go func() {
// defer func() { //程序异常 收集异常信息传递给前端显示
// if r := recover(); r != nil {
// log.Errorf("[sociatytask ] err:%v ", r)
// }
// }()
// 公会 // 公会
if user, ok := userModule.(comm.IUser); ok { if user, ok := userModule.(comm.IUser); ok {
ex, err := user.GetUserExpand(uid) ex, err := user.GetUserExpand(uid)
@ -325,15 +476,9 @@ func (this *ModuleRtask) processOneTask(session comm.IUserSession, rtaskType com
) )
} }
this.processOneTask(session, comm.Rtype156, 1) this.processOneTask(session, comm.Rtype156, 1)
// if module, err := this.service.GetModule(comm.ModuleRtask); err == nil {
// if iRtask, ok := module.(comm.IRtask); ok {
// iRtask.SendToRtask(session, comm.Rtype156, 1)
// }
// }
} }
} }
} }
// }()
} }
} }
@ -368,30 +513,32 @@ func (this *ModuleRtask) SendToRtask(session comm.IUserSession, rtaskType comm.T
condis []*rtaskCondi condis []*rtaskCondi
) )
for _, codiConf := range this.configure.getRtaskCondis(int32(rtaskType)) { condis = this.getHandle(rtaskType)
v, ok := this.handleMap[codiConf.Id]
if !ok {
this.Warn("未注册事件处理器",
log.Field{Key: "uid", Value: uid},
log.Field{Key: "condiId", Value: codiConf.Id},
)
code = pb.ErrorCode_RtaskCondiNoFound
return
}
if v.find == nil { // for _, codiConf := range this.configure.getRtaskCondis(int32(rtaskType)) {
this.Warn("未设置find Handle", // v, ok := this.handleMap[codiConf.Id]
log.Field{Key: "uid", Value: uid}, // if !ok {
log.Field{Key: "condiId", Value: codiConf.Id}, // this.Warn("未注册事件处理器",
) // log.Field{Key: "uid", Value: uid},
return // log.Field{Key: "condiId", Value: codiConf.Id},
} // )
// code = pb.ErrorCode_RtaskCondiNoFound
// return
// }
if condiId, _ := v.find(codiConf, params...); condiId != 0 { // if v.find == nil {
v.condId = codiConf.Id // this.Warn("未设置find Handle",
condis = append(condis, v) // log.Field{Key: "uid", Value: uid},
} // log.Field{Key: "condiId", Value: codiConf.Id},
} // )
// return
// }
// if condiId, _ := v.find(codiConf, params...); condiId != 0 {
// v.condId = codiConf.Id
// condis = append(condis, v)
// }
// }
// update // update
for _, v := range condis { for _, v := range condis {
@ -496,10 +643,11 @@ func (this *ModuleRtask) TriggerTask(uid string, taskParams ...*comm.TaskParam)
} }
for _, tp := range taskParams { for _, tp := range taskParams {
if code := this.processOneTask(session, tp.TT, tp.Params...); code != pb.ErrorCode_Success { if code := this.processOneTask(session, tp.TT, tp.Params...); code != pb.ErrorCode_Success {
this.Debug("任务处理", // this.Debug("任务处理",
log.Field{Key: "uid", Value: uid}, // log.Field{Key: "uid", Value: uid},
log.Field{Key: "taskType", Value: tp.TT}, // log.Field{Key: "taskType", Value: tp.TT},
log.Field{Key: "params", Value: tp.Params}) // log.Field{Key: "params", Value: tp.Params},
// log.Field{Key: "code", Value: code})
} }
session.Push() session.Push()
comm.PuttaskParam(tp) comm.PuttaskParam(tp)
@ -511,6 +659,7 @@ func (this *ModuleRtask) TriggerTask(uid string, taskParams ...*comm.TaskParam)
// 任务条件校验 // 任务条件校验
func (this *ModuleRtask) CheckCondi(uid string, condiId int32) (code pb.ErrorCode) { func (this *ModuleRtask) CheckCondi(uid string, condiId int32) (code pb.ErrorCode) {
if _, ok := this.modelRtask.checkCondi(uid, condiId); !ok { if _, ok := this.modelRtask.checkCondi(uid, condiId); !ok {
// this.Debug("任务条件未达到", log.Field{Key: "uid", Value: uid}, log.Field{Key: "condiId", Value: condiId})
code = pb.ErrorCode_RtaskCondiNoReach code = pb.ErrorCode_RtaskCondiNoReach
} }
return return

View File

@ -2,6 +2,7 @@
package rtask package rtask
import ( import (
"go_dreamfactory/lego/sys/log"
"go_dreamfactory/pb" "go_dreamfactory/pb"
"go_dreamfactory/sys/configure" "go_dreamfactory/sys/configure"
cfg "go_dreamfactory/sys/configure/structs" cfg "go_dreamfactory/sys/configure/structs"
@ -37,6 +38,11 @@ func (this *ModelRtaskRecord) overrideUpdate(uid string, cfg *cfg.GameRdtaskCond
if v, ok := record.Vals[cfg.Id]; ok { if v, ok := record.Vals[cfg.Id]; ok {
v.Data = hasUpdateData(paramLen, v, vals...) v.Data = hasUpdateData(paramLen, v, vals...)
if len(v.Data) > 0 { if len(v.Data) > 0 {
this.moduleRtask.Debug("打印V",
log.Field{Key: "v", Value: v.Data},
log.Field{Key: "paramLen", Value: paramLen},
log.Field{Key: "vals", Value: vals},
)
update := map[string]interface{}{ update := map[string]interface{}{
"vals": v, "vals": v,
} }
@ -60,8 +66,6 @@ func (this *ModelRtaskRecord) overrideUpdate(uid string, cfg *cfg.GameRdtaskCond
return return
} }
} }
// log.Debug("覆盖数值更新", log.Fields{"uid": uid, "condiId": cfg.Id, "params": vals, "updated": record.Vals[cfg.Id]})
// this.listenTask(uid, cfg.Id)
return return
} }
@ -107,12 +111,5 @@ func (this *ModelRtaskRecord) addUpdate(uid string, cfg *cfg.GameRdtaskCondiData
} }
err = this.Change(uid, update) err = this.Change(uid, update)
} }
// log.Debug("累计次数更新",
// log.Field{Key: "uid", Value: uid},
// log.Field{Key: "condiId", Value: cfg.Id},
// log.Field{Key: "params", Value: vals},
// log.Field{Key: "updated", Value: record.Vals[cfg.Id]},
// )
return return
} }

View File

@ -240,10 +240,10 @@ type TaskParams struct {
// 任务条件达成通知 // 任务条件达成通知
func (this *Sociaty) TaskcondNotify(uid, sociatyId string, condId int32) error { func (this *Sociaty) TaskcondNotify(uid, sociatyId string, condId int32) error {
log.Debug("公会任务", // log.Debug("公会任务",
log.Field{Key: "uid", Value: uid}, // log.Field{Key: "uid", Value: uid},
log.Field{Key: "sociatyId", Value: sociatyId}, // log.Field{Key: "sociatyId", Value: sociatyId},
log.Field{Key: "condId", Value: condId}) // log.Field{Key: "condId", Value: condId})
dt := &pb.DBSociatyTask{} dt := &pb.DBSociatyTask{}
err := this.service.AcrossClusterRpcCall(context.Background(), this.GetCrossTag(), err := this.service.AcrossClusterRpcCall(context.Background(), this.GetCrossTag(),