From cc356997a310a036e69686d7cd882acf43962d43 Mon Sep 17 00:00:00 2001 From: wh_zcy Date: Fri, 5 May 2023 09:36:55 +0800 Subject: [PATCH] update --- modules/rtask/model_record.go | 1 + modules/rtask/model_rtask.go | 4 +- modules/rtask/module.go | 95 +++++++++++------ modules/rtask/updateHandle.go | 143 +++++++++++--------------- modules/worldtask/api_battlefinish.go | 21 +++- 5 files changed, 145 insertions(+), 119 deletions(-) diff --git a/modules/rtask/model_record.go b/modules/rtask/model_record.go index e016d35b0..0503a1689 100644 --- a/modules/rtask/model_record.go +++ b/modules/rtask/model_record.go @@ -16,6 +16,7 @@ type ModelRtaskRecord struct { modules.MCompModel moduleRtask *ModuleRtask service core.IService + record *pb.DBRtaskRecord } func (this *ModelRtaskRecord) Init(service core.IService, module core.IModule, comp core.IModuleComp, options core.IModuleOptions) (err error) { diff --git a/modules/rtask/model_rtask.go b/modules/rtask/model_rtask.go index 1bcbc318f..eaf4b0f37 100644 --- a/modules/rtask/model_rtask.go +++ b/modules/rtask/model_rtask.go @@ -89,7 +89,7 @@ func (this *ModelRtask) checkCondi(uid string, condiId int32) (err error, ok boo } //验证限定条件 - var condi *rtaskCondi + var condi *rtaskCondHandle cond, ok := this.moduleRtask.handleMap.Load(condiId) if !ok { rcs := this.moduleRtask.getHandle(comm.TaskType(conf.Type)) @@ -100,7 +100,7 @@ func (this *ModelRtask) checkCondi(uid string, condiId int32) (err error, ok boo } } - if condi, ok = cond.(*rtaskCondi); !ok { + if condi, ok = cond.(*rtaskCondHandle); !ok { err = fmt.Errorf("condiType err") return } diff --git a/modules/rtask/module.go b/modules/rtask/module.go index b9b9e5534..4d7634e8e 100644 --- a/modules/rtask/module.go +++ b/modules/rtask/module.go @@ -12,18 +12,21 @@ import ( "go_dreamfactory/lego/sys/log" "go_dreamfactory/modules" "go_dreamfactory/pb" + "go_dreamfactory/sys/configure" cfg "go_dreamfactory/sys/configure/structs" "go_dreamfactory/sys/db" "go_dreamfactory/utils" "sync" "github.com/pkg/errors" + "go.mongodb.org/mongo-driver/bson/primitive" + "go.mongodb.org/mongo-driver/mongo" ) var _ comm.IRtask = (*ModuleRtask)(nil) // 限定条件 -type rtaskCondi struct { +type rtaskCondHandle struct { condId int32 //任务条件配置ID verify verifyHandle //校验任务条件 find condiFindHandle //检索任务条件 @@ -71,15 +74,15 @@ func (this *ModuleRtask) OnInstallComp() { this.configure = this.RegisterComp(new(configureComp)).(*configureComp) } -func (this *ModuleRtask) registerVerifyHandle(condiId int32, condi *rtaskCondi) { +func (this *ModuleRtask) registerVerifyHandle(condiId int32, condi *rtaskCondHandle) { this.handleMap.Store(condiId, condi) } -func (this *ModuleRtask) getHandle(tt comm.TaskType) (condis []*rtaskCondi) { +func (this *ModuleRtask) getHandle(tt comm.TaskType) (condis []*rtaskCondHandle) { for _, v := range this.configure.getRtaskCondis(int32(tt)) { switch tt { case comm.Rtype1: - condi := &rtaskCondi{ + condi := &rtaskCondHandle{ condId: v.Id, find: this.modelRtaskRecord.equalFirstParam, verify: this.modelRtask.verfiyRtype1, @@ -88,7 +91,7 @@ func (this *ModuleRtask) getHandle(tt comm.TaskType) (condis []*rtaskCondi) { condis = append(condis, condi) this.registerVerifyHandle(v.Id, condi) case comm.Rtype2: - condi := &rtaskCondi{ + condi := &rtaskCondHandle{ condId: v.Id, find: this.modelRtaskRecord.equalFirstParam, verify: this.modelRtask.verifyRtype2, @@ -97,7 +100,7 @@ func (this *ModuleRtask) getHandle(tt comm.TaskType) (condis []*rtaskCondi) { condis = append(condis, condi) this.registerVerifyHandle(v.Id, condi) case comm.Rtype3: - condi := &rtaskCondi{ + condi := &rtaskCondHandle{ condId: v.Id, find: this.modelRtaskRecord.equalFirstParam, verify: this.modelRtask.verifyRtype3, @@ -106,7 +109,7 @@ func (this *ModuleRtask) getHandle(tt comm.TaskType) (condis []*rtaskCondi) { condis = append(condis, condi) this.registerVerifyHandle(v.Id, condi) case comm.Rtype4: - condi := &rtaskCondi{ + condi := &rtaskCondHandle{ condId: v.Id, find: this.modelRtaskRecord.equalFirstParam, verify: this.modelRtask.verifyRtype4, @@ -115,7 +118,7 @@ func (this *ModuleRtask) getHandle(tt comm.TaskType) (condis []*rtaskCondi) { condis = append(condis, condi) this.registerVerifyHandle(v.Id, condi) case comm.Rtype5: - condi := &rtaskCondi{ + condi := &rtaskCondHandle{ condId: v.Id, find: this.modelRtaskRecord.equalFirstParam, verify: this.modelRtask.verifyRtype5, @@ -124,7 +127,7 @@ func (this *ModuleRtask) getHandle(tt comm.TaskType) (condis []*rtaskCondi) { condis = append(condis, condi) this.registerVerifyHandle(v.Id, condi) case comm.Rtype6: - condi := &rtaskCondi{ + condi := &rtaskCondHandle{ condId: v.Id, find: this.modelRtaskRecord.equalFirstParam, verify: this.modelRtask.verifyRtype6, @@ -133,7 +136,7 @@ func (this *ModuleRtask) getHandle(tt comm.TaskType) (condis []*rtaskCondi) { condis = append(condis, condi) this.registerVerifyHandle(v.Id, condi) case comm.Rtype8: - condi := &rtaskCondi{ + condi := &rtaskCondHandle{ condId: v.Id, find: this.modelRtaskRecord.equalFirstParam, verify: this.modelRtask.verfiyRtype8, @@ -142,7 +145,7 @@ func (this *ModuleRtask) getHandle(tt comm.TaskType) (condis []*rtaskCondi) { condis = append(condis, condi) this.registerVerifyHandle(v.Id, condi) case comm.Rtype9: - condi := &rtaskCondi{ + condi := &rtaskCondHandle{ condId: v.Id, find: this.modelRtaskRecord.equalFirstParam, verify: this.modelRtask.verfiyRtype9, @@ -151,7 +154,7 @@ func (this *ModuleRtask) getHandle(tt comm.TaskType) (condis []*rtaskCondi) { condis = append(condis, condi) this.registerVerifyHandle(v.Id, condi) case comm.Rtype10: - condi := &rtaskCondi{ + condi := &rtaskCondHandle{ condId: v.Id, find: this.modelRtaskRecord.equalFirstParam, verify: this.modelRtask.verfiyRtype10, @@ -160,7 +163,7 @@ func (this *ModuleRtask) getHandle(tt comm.TaskType) (condis []*rtaskCondi) { condis = append(condis, condi) this.registerVerifyHandle(v.Id, condi) case comm.Rtype11, comm.Rtype84, comm.Rtype85: - condi := &rtaskCondi{ + condi := &rtaskCondHandle{ condId: v.Id, find: this.modelRtaskRecord.lessEqualFirstParam, verify: this.modelRtaskRecord.verifyFirstGreatEqualParam, @@ -170,7 +173,7 @@ func (this *ModuleRtask) getHandle(tt comm.TaskType) (condis []*rtaskCondi) { condis = append(condis, condi) this.registerVerifyHandle(v.Id, condi) case comm.Rtype18: - condi := &rtaskCondi{ + condi := &rtaskCondHandle{ condId: v.Id, find: this.modelRtaskRecord.greatEqualFirstParam, verify: this.modelRtaskRecord.verifyFirstGreatEqualParam, @@ -187,7 +190,7 @@ func (this *ModuleRtask) getHandle(tt comm.TaskType) (condis []*rtaskCondi) { 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{ + condi := &rtaskCondHandle{ condId: v.Id, find: this.modelRtaskRecord.lessEqualFirstParam, verify: this.modelRtaskRecord.verifyFirstGreatEqualParam, @@ -196,7 +199,7 @@ func (this *ModuleRtask) getHandle(tt comm.TaskType) (condis []*rtaskCondi) { condis = append(condis, condi) this.registerVerifyHandle(v.Id, condi) case comm.Rtype50, comm.Rtype73: - condi := &rtaskCondi{ + condi := &rtaskCondHandle{ condId: v.Id, find: this.modelRtaskRecord.lessEqualFirstParam, verify: this.modelRtaskRecord.verifyFromDb, @@ -205,7 +208,7 @@ func (this *ModuleRtask) getHandle(tt comm.TaskType) (condis []*rtaskCondi) { condis = append(condis, condi) this.registerVerifyHandle(v.Id, condi) case comm.Rtype20: - condi := &rtaskCondi{ + condi := &rtaskCondHandle{ condId: v.Id, find: this.modelRtaskRecord.equalFirstParam, verify: this.modelRtask.verifyRtype20, @@ -215,7 +218,7 @@ func (this *ModuleRtask) getHandle(tt comm.TaskType) (condis []*rtaskCondi) { condis = append(condis, condi) this.registerVerifyHandle(v.Id, condi) case comm.Rtype22, comm.Rtype109: - condi := &rtaskCondi{ + condi := &rtaskCondHandle{ condId: v.Id, find: this.modelRtaskRecord.equalFirstParam, verify: this.modelRtaskRecord.verifyFirstEqualParam, @@ -225,7 +228,7 @@ func (this *ModuleRtask) getHandle(tt comm.TaskType) (condis []*rtaskCondi) { condis = append(condis, condi) this.registerVerifyHandle(v.Id, condi) case comm.Rtype63: - condi := &rtaskCondi{ + condi := &rtaskCondHandle{ condId: v.Id, find: this.modelRtaskRecord.equalFirstParam, verify: this.modelRtask.verifyRtype63, @@ -236,7 +239,7 @@ func (this *ModuleRtask) getHandle(tt comm.TaskType) (condis []*rtaskCondi) { case comm.Rtype16, comm.Rtype17, comm.Rtype35, comm.Rtype44, comm.Rtype59, comm.Rtype61: - condi := &rtaskCondi{ + condi := &rtaskCondHandle{ condId: v.Id, find: this.modelRtaskRecord.equalParams, verify: this.modelRtaskRecord.verifyFromDb, @@ -251,7 +254,7 @@ func (this *ModuleRtask) getHandle(tt comm.TaskType) (condis []*rtaskCondi) { comm.Rtype46, comm.Rtype47, comm.Rtype48, comm.Rtype49, comm.Rtype52, comm.Rtype55, comm.Rtype56, comm.Rtype65, comm.Rtype66, comm.Rtype67, comm.Rtype68, comm.Rtype70, comm.Rtype140: - condi := &rtaskCondi{ + condi := &rtaskCondHandle{ condId: v.Id, find: this.modelRtaskRecord.lessThanParams, verify: this.modelRtaskRecord.verifyFromDb, @@ -269,7 +272,8 @@ func (this *ModuleRtask) getHandle(tt comm.TaskType) (condis []*rtaskCondi) { // 处理触发的任务 func (this *ModuleRtask) processOneTask(session comm.IUserSession, rtaskType comm.TaskType, params ...int32) (code pb.ErrorCode) { uid := session.GetUserId() - var condis []*rtaskCondi + + var handles []*rtaskCondHandle if this.IsCross() { //随机任务 @@ -285,18 +289,18 @@ func (this *ModuleRtask) processOneTask(session comm.IUserSession, rtaskType com return } - condis = this.getHandle(rtaskType) + handles = this.getHandle(rtaskType) // update - for _, v := range condis { - conf, err := this.configure.getRtaskTypeById(v.condId) + for _, handle := range handles { + conf, err := this.configure.getRtaskTypeById(handle.condId) if err != nil { - log.Debug("任务配置未找到", log.Field{Key: "condId", Value: v.condId}) + log.Debug("任务配置未找到", log.Field{Key: "condId", Value: handle.condId}) code = pb.ErrorCode_RtaskCondiNoFound return } - if v.update != nil { - if err := v.update(uid, conf, params...); err != nil { + if handle.update != nil { + if err := handle.update(uid, conf, params...); err != nil { log.Errorf("update task:%v", err) code = pb.ErrorCode_DBError return @@ -349,27 +353,52 @@ func (this *ModuleRtask) processOneTask(session comm.IUserSession, rtaskType com } func (this *ModuleRtask) TriggerTask(uid string, taskParams ...*comm.TaskParam) { - this.Debug("任务处理", - log.Field{Key: "uid", Value: uid}, - log.Field{Key: "params", Value: taskParams}) session, ok := this.GetUserSession(uid) if !ok { return } + lock, _ := this.modelRtask.userlock(uid) err := lock.Lock() if err != nil { this.Error("TriggerTask userlock err!", log.Field{Key: "err", Value: err.Error()}) } defer lock.Unlock() - for _, tp := range taskParams { - if code := this.processOneTask(session, tp.TT, tp.Params...); code != pb.ErrorCode_Success { + record := &pb.DBRtaskRecord{Uid: uid} + if err := this.modelRtaskRecord.Get(uid, record); err != nil { + if err == mongo.ErrNoDocuments { + record.Id = primitive.NewObjectID().Hex() + record.Ctime = configure.Now().Unix() + if err := this.modelRtaskRecord.Add(uid, record); err != nil { + return + } } + } + + this.modelRtaskRecord.record = record + + for _, tp := range taskParams { + this.Debug("任务触发", + log.Field{Key: "uid", Value: uid}, + log.Field{Key: "type", Value: tp.TT}, + log.Field{Key: "params", Value: tp.Params}) + + if code := this.processOneTask(session, tp.TT, tp.Params...); code != pb.ErrorCode_Success { + // this.Error("任务处理", log.Field{Key: "uid", Value: uid}, log.Field{Key: "code", Value: code}) + } + session.Push() comm.PuttaskParam(tp) } + + update := map[string]interface{}{ + "vals": record.Vals, + } + + this.modelRtaskRecord.Change(uid, update) + this.PutUserSession(session) return } diff --git a/modules/rtask/updateHandle.go b/modules/rtask/updateHandle.go index d598854f2..30cd56045 100644 --- a/modules/rtask/updateHandle.go +++ b/modules/rtask/updateHandle.go @@ -2,14 +2,9 @@ package rtask import ( - "go_dreamfactory/lego/sys/log" "go_dreamfactory/pb" "go_dreamfactory/sys/configure" cfg "go_dreamfactory/sys/configure/structs" - - "github.com/pkg/errors" - "go.mongodb.org/mongo-driver/bson/primitive" - "go.mongodb.org/mongo-driver/mongo" ) // 覆盖更新 @@ -19,93 +14,79 @@ func (this *ModelRtaskRecord) overrideUpdate(uid string, cfg *cfg.GameRdtaskCond return err } - record := &pb.DBRtaskRecord{Uid: uid} - if err := this.Get(uid, record); err != nil { - if err == mongo.ErrNoDocuments { - record.Id = primitive.NewObjectID().Hex() - record.Ctime = configure.Now().Unix() - if err := this.Add(uid, record); err != nil { - return errors.Wrapf(err, "创建玩家任务记录 err: %v rtype[%v]", uid, cfg.Id) - } - } else { - return errors.Wrapf(err, "获取玩家任务记录 err: %v rtype[%v]", uid, cfg.Id) - } + // record := &pb.DBRtaskRecord{Uid: uid} + // if err := this.Get(uid, record); err != nil { + // if err == mongo.ErrNoDocuments { + // record.Id = primitive.NewObjectID().Hex() + // record.Ctime = configure.Now().Unix() + // if err := this.Add(uid, record); err != nil { + // return errors.Wrapf(err, "创建玩家任务记录 err: %v rtype[%v]", uid, cfg.Id) + // } + // } else { + // return errors.Wrapf(err, "获取玩家任务记录 err: %v rtype[%v]", uid, cfg.Id) + // } + // } + + if this.record.Vals == nil { + this.record.Vals = make(map[int32]*pb.RtaskData) } - if record.Vals == nil { - record.Vals = make(map[int32]*pb.RtaskData) - } - - if v, ok := record.Vals[cfg.Id]; ok { - this.moduleRtask.Debug("覆盖更新前", - log.Field{Key: "uid", Value: uid}, - log.Field{Key: "v", Value: v}, - log.Field{Key: "paramLen", Value: paramLen}, - log.Field{Key: "vals", Value: vals}, - log.Field{Key: "cfgId", Value: cfg.Id}, - ) + if v, ok := this.record.Vals[cfg.Id]; ok { v.Data = hasUpdateData(paramLen, v, vals...) - this.moduleRtask.Debug("覆盖更新后", - log.Field{Key: "uid", Value: uid}, - log.Field{Key: "v", Value: v.Data}, - log.Field{Key: "paramLen", Value: paramLen}, - log.Field{Key: "vals", Value: vals}, - log.Field{Key: "cfgId", Value: cfg.Id}, - ) - if len(v.Data) > 0 { - update := map[string]interface{}{ - "vals": record.Vals, - } - if err = this.Change(uid, update); err != nil { - this.moduleRtask.Error("更新失败", - log.Field{Key: "uid", Value: uid}, - log.Field{Key: "update", Value: update}) - return - } - } + // if len(v.Data) > 0 { + // update := map[string]interface{}{ + // "vals": record.Vals, + // } + // if err = this.Change(uid, update); err != nil { + // this.moduleRtask.Error("更新失败", + // log.Field{Key: "uid", Value: uid}, + // log.Field{Key: "update", Value: update}) + // return + // } + // } } else { data := &pb.RtaskData{ Rtype: cfg.Type, Data: toMap(vals...), Timestamp: configure.Now().Unix(), } - record.Vals[cfg.Id] = data + this.record.Vals[cfg.Id] = data - update := map[string]interface{}{ - "vals": record.Vals, - } + // update := map[string]interface{}{ + // "vals": record.Vals, + // } - if err = this.Change(uid, update); err != nil { - this.moduleRtask.Error("更新失败", - log.Field{Key: "uid", Value: uid}, - log.Field{Key: "update", Value: update}) - return - } + // if err = this.Change(uid, update); err != nil { + // this.moduleRtask.Error("更新失败", + // log.Field{Key: "uid", Value: uid}, + // log.Field{Key: "update", Value: update}) + // return + // } } return } // 累计更新 - 招募等 func (this *ModelRtaskRecord) addUpdate(uid string, cfg *cfg.GameRdtaskCondiData, vals ...int32) (err error) { - record := &pb.DBRtaskRecord{Uid: uid} - err = this.Get(uid, record) - if err != nil { - if err == mongo.ErrNoDocuments { - record.Id = primitive.NewObjectID().Hex() - record.Ctime = configure.Now().Unix() - if err := this.Add(uid, record); err != nil { - return errors.Wrapf(err, "创建玩家任务记录 err: %v rtype[%v]", uid, cfg.Id) - } - } else { - return errors.Wrapf(err, "获取玩家任务记录 err: %v rtype[%v]", uid, cfg.Id) - } - } + // record := &pb.DBRtaskRecord{Uid: uid} + // err = this.Get(uid, record) + // if err != nil { + // if err == mongo.ErrNoDocuments { + // record.Id = primitive.NewObjectID().Hex() + // record.Ctime = configure.Now().Unix() + // if err := this.Add(uid, record); err != nil { + // return errors.Wrapf(err, "创建玩家任务记录 err: %v rtype[%v]", uid, cfg.Id) + // } + // } else { + // return errors.Wrapf(err, "获取玩家任务记录 err: %v rtype[%v]", uid, cfg.Id) + // } + // } - if record.Vals == nil { - record.Vals = make(map[int32]*pb.RtaskData) + if this.record.Vals == nil { + this.record.Vals = make(map[int32]*pb.RtaskData) } //查找任务数据 - if v, ok := record.Vals[cfg.Id]; ok { + if v, ok := this.record.Vals[cfg.Id]; ok { newArr := make([]int32, len(vals)) copy(newArr, vals) srcCount := v.Data[0] @@ -113,20 +94,20 @@ func (this *ModelRtaskRecord) addUpdate(uid string, cfg *cfg.GameRdtaskCondiData v.Data = toMap(newArr...) v.Timestamp = configure.Now().Unix() - update := map[string]interface{}{ - "vals": record.Vals, - } - err = this.Change(uid, update) + // update := map[string]interface{}{ + // "vals": record.Vals, + // } + // err = this.Change(uid, update) } else { - record.Vals[cfg.Id] = &pb.RtaskData{ + this.record.Vals[cfg.Id] = &pb.RtaskData{ Data: toMap(vals...), Rtype: cfg.Type, Timestamp: configure.Now().Unix(), } - update := map[string]interface{}{ - "vals": record.Vals, - } - err = this.Change(uid, update) + // update := map[string]interface{}{ + // "vals": record.Vals, + // } + // err = this.Change(uid, update) } return } diff --git a/modules/worldtask/api_battlefinish.go b/modules/worldtask/api_battlefinish.go index 9bb14f0c3..6db9533a7 100644 --- a/modules/worldtask/api_battlefinish.go +++ b/modules/worldtask/api_battlefinish.go @@ -11,7 +11,7 @@ import ( // 战斗结束的请求 func (this *apiComp) BattlefinishCheck(session comm.IUserSession, req *pb.WorldtaskBattleFinishReq) (code pb.ErrorCode) { - if req.GroupId <= 0 || req.CondiId <= 0 || req.BattleConfId <= 0 || req.TaskId <= 0 || req.Report == nil { + if req.GroupId <= 0 || req.BattleConfId <= 0 || req.TaskId <= 0 || req.Report == nil { this.module.Error("世界任务战斗结束参数错误", log.Field{Key: "uid", Value: session.GetUserId()}, log.Field{Key: "params", Value: req.String()}, @@ -87,8 +87,17 @@ func (this *apiComp) Battlefinish(session comm.IUserSession, req *pb.WorldtaskBa if userTask.CurrentTask == nil { userTask.CurrentTask = make(map[int32]*pb.Worldtask) } - if _, ok := utils.Findx(userTask.CurrentTask[req.GroupId].CondiIds, req.CondiId); !ok { - userTask.CurrentTask[req.GroupId].CondiIds = append(userTask.CurrentTask[req.GroupId].CondiIds, req.CondiId) + + if req.CondiId != 0 { + if _, ok := utils.Findx(userTask.CurrentTask[req.GroupId].CondiIds, req.CondiId); !ok { + userTask.CurrentTask[req.GroupId].CondiIds = append(userTask.CurrentTask[req.GroupId].CondiIds, req.CondiId) + } + } else { + for _, condId := range taskConf.Completetask { + if _, ok := utils.Findx(userTask.CurrentTask[req.GroupId].CondiIds, condId); !ok { + userTask.CurrentTask[req.GroupId].CondiIds = append(userTask.CurrentTask[req.GroupId].CondiIds, condId) + } + } } update := map[string]interface{}{ @@ -105,6 +114,12 @@ func (this *apiComp) Battlefinish(session comm.IUserSession, req *pb.WorldtaskBa TaskId: req.TaskId, CondiIds: userTask.CurrentTask[req.GroupId].CondiIds, }) + + //结束任务 + if taskConf.DeliverNpc == 0 { + this.module.modelWorldtask.taskFinish(session, req.GroupId, req.TaskId, userTask, taskConf) + this.module.modelWorldtask.taskFinishPush(session, req.GroupId, userTask, taskConf) + } } this.module.Debug("校验战报", log.Field{Key: "uid", Value: session.GetUserId()},