跨服接口任务触发

This commit is contained in:
wh_zcy 2023-05-18 10:35:51 +08:00
parent dbe12ea192
commit a834812fa9
3 changed files with 93 additions and 123 deletions

View File

@ -6,8 +6,11 @@ import (
"go_dreamfactory/lego/sys/log"
"go_dreamfactory/modules"
"go_dreamfactory/pb"
"go_dreamfactory/sys/configure"
"go_dreamfactory/sys/db"
"github.com/pkg/errors"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/x/bsonx"
)
@ -16,7 +19,6 @@ 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) {
@ -45,10 +47,40 @@ func (this *ModelRtaskRecord) GetVerifyData(uid string, condiId int32) (*pb.Rtas
// 配置表: rdtask_condi
func (this *ModelRtaskRecord) getRecord(uid string) *pb.DBRtaskRecord {
record := &pb.DBRtaskRecord{}
if err := this.Get(uid, record); err != nil {
if err != mongo.ErrNoDocuments {
log.Warnf("获取玩家任务条件 uid:%s err:%v", uid, err)
if this.moduleRtask.IsCross() {
var (
stag string
conn *db.DBConn
err error
)
if stag, err = comm.UidToSTag(uid); err != nil {
return nil
}
if stag == this.service.GetTag() {
if conn, err = db.Local(); err != nil {
return nil
}
} else {
if conn, err = db.ServerDBConn(stag); err != nil {
return nil
}
}
model := db.NewDBModel(this.TableName, this.Expired, conn)
if model != nil {
model.Get(uid, record)
}
} else {
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 {
log.Errorln(err)
return nil
}
}
}
}
return record
}

View File

@ -12,15 +12,12 @@ 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)
@ -35,7 +32,7 @@ type rtaskCondHandle struct {
type verifyHandle func(uid string, cfg *cfg.GameRdtaskCondiData) (bool, error)
type condiFindHandle func(cfg *cfg.GameRdtaskCondiData, vals ...int32) (int32, error)
type updateDataHandle func(uid string, cfg *cfg.GameRdtaskCondiData, vals ...int32) error
type updateDataHandle func(uid string, record *pb.DBRtaskRecord, cfg *cfg.GameRdtaskCondiData, vals ...int32) error
type ModuleRtask struct {
modules.ModuleBase
@ -273,7 +270,10 @@ func (this *ModuleRtask) getHandle(tt comm.TaskType) (condis []*rtaskCondHandle)
func (this *ModuleRtask) processOneTask(session comm.IUserSession, rtaskType comm.TaskType, params ...int32) (code pb.ErrorCode) {
uid := session.GetUserId()
var handles []*rtaskCondHandle
var (
handles []*rtaskCondHandle
condIds []int32
)
if this.IsCross() {
//随机任务
@ -282,14 +282,24 @@ func (this *ModuleRtask) processOneTask(session comm.IUserSession, rtaskType com
session.GetServiecTag(),
comm.Service_Worker,
string(comm.Rpc_ModuleRtaskSendTask),
pb.RPCRTaskReq{Uid: uid, TaskType: int32(rtaskType), Param: params},
&pb.RPCRTaskReq{Uid: uid, TaskType: int32(rtaskType), Param: params},
nil); err != nil {
this.Errorln(err)
log.Errorln(err)
}
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()
record := this.modelRtaskRecord.getRecord(uid)
handles = this.getHandle(rtaskType)
// update
for _, handle := range handles {
conf, err := this.configure.getRtaskTypeById(handle.condId)
@ -300,22 +310,31 @@ func (this *ModuleRtask) processOneTask(session comm.IUserSession, rtaskType com
}
if handle.update != nil {
if err := handle.update(uid, conf, params...); err != nil {
if err := handle.update(uid, record, conf, params...); err != nil {
log.Errorf("update task:%v", err)
code = pb.ErrorCode_DBError
return
}
}
condIds = append(condIds, handle.condId)
}
//任务完成则推送
if code = this.CheckCondi(uid, conf.Id); code == pb.ErrorCode_Success {
update := map[string]interface{}{
"vals": record.Vals,
}
this.modelRtaskRecord.Change(uid, update)
for _, condId := range condIds {
if code = this.CheckCondi(uid, condId); code == pb.ErrorCode_Success {
module, err := this.service.GetModule(comm.ModuleWorldtask)
if err == nil {
//世界任务
if worldtask, ok := module.(comm.IWorldtask); ok {
if err := worldtask.TaskCondFinishNotify(session, conf.Id); err != nil {
log.Error("任务条件达成通知",
if err := worldtask.TaskCondFinishNotify(session, condId); err != nil {
log.Error("世界任务条件达成通知",
log.Field{Key: "uid", Value: uid},
log.Field{Key: "condId", Value: conf.Id},
log.Field{Key: "condId", Value: condId},
log.Field{Key: "err", Value: err.Error()},
)
}
@ -332,11 +351,11 @@ func (this *ModuleRtask) processOneTask(session comm.IUserSession, rtaskType com
return
}
if sociaty, ok := sociatyModule.(comm.ISociaty); ok {
if err2 := sociaty.TaskcondNotify(uid, ex.SociatyId, conf.Id); err2 != nil {
if err2 := sociaty.TaskcondNotify(uid, ex.SociatyId, condId); err2 != nil {
log.Error("公会任务条件达成通知",
log.Field{Key: "uid", Value: uid},
log.Field{Key: "sociatyId", Value: ex.SociatyId},
log.Field{Key: "condId", Value: conf.Id},
log.Field{Key: "condId", Value: condId},
log.Field{Key: "err", Value: err2.Error()},
)
}
@ -346,9 +365,7 @@ func (this *ModuleRtask) processOneTask(session comm.IUserSession, rtaskType com
}
}
}
}
return
}
@ -358,46 +375,21 @@ func (this *ModuleRtask) TriggerTask(uid string, taskParams ...*comm.TaskParam)
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()
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})
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})
code := this.processOneTask(session, tp.TT, tp.Params...)
if 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)
session.Push()
this.PutUserSession(session)
return
}
@ -457,10 +449,14 @@ func (this *ModuleRtask) ChangeCondi(uid string, data map[int32]*pb.RtaskData) e
return nil
}
type TaskProcessResp struct {
CondIds []int32
}
// 接收区服worker发起的秘境事件
func (this *ModuleRtask) Rpc_ModuleRtaskSendTask(ctx context.Context, args *pb.RPCRTaskReq, reply *pb.EmptyResp) (err error) {
this.Debug("Rpc_ModuleRtaskSendTask",
log.Field{Key: "args", Value: args.String()},
log.Field{Key: "args", Value: args},
)
if args.Uid == "" {
err = errors.New("参数异常!")

View File

@ -8,106 +8,48 @@ import (
)
// 覆盖更新
func (this *ModelRtaskRecord) overrideUpdate(uid string, cfg *cfg.GameRdtaskCondiData, vals ...int32) (err error) {
func (this *ModelRtaskRecord) overrideUpdate(uid string, record *pb.DBRtaskRecord, cfg *cfg.GameRdtaskCondiData, vals ...int32) (err error) {
var paramLen int
if paramLen, err = lenParam(cfg, vals...); err != nil {
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)
// }
// }
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 := this.record.Vals[cfg.Id]; ok {
if v, ok := record.Vals[cfg.Id]; ok {
v.Data = hasUpdateData(paramLen, v, vals...)
// 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(),
}
this.record.Vals[cfg.Id] = data
// 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
// }
record.Vals[cfg.Id] = data
}
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)
// }
// }
if this.record.Vals == nil {
this.record.Vals = make(map[int32]*pb.RtaskData)
func (this *ModelRtaskRecord) addUpdate(uid string, record *pb.DBRtaskRecord, cfg *cfg.GameRdtaskCondiData, vals ...int32) (err error) {
if record.Vals == nil {
record.Vals = make(map[int32]*pb.RtaskData)
}
//查找任务数据
if v, ok := this.record.Vals[cfg.Id]; ok {
if v, ok := record.Vals[cfg.Id]; ok {
newArr := make([]int32, len(vals))
copy(newArr, vals)
srcCount := v.Data[0]
newArr[0] = srcCount + vals[0]
v.Data = toMap(newArr...)
v.Timestamp = configure.Now().Unix()
// update := map[string]interface{}{
// "vals": record.Vals,
// }
// err = this.Change(uid, update)
} else {
this.record.Vals[cfg.Id] = &pb.RtaskData{
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)
}
return
}