119 lines
3.2 KiB
Go
119 lines
3.2 KiB
Go
package task
|
|
|
|
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/bson/primitive"
|
|
)
|
|
|
|
const ( //Redis
|
|
TableTaskActive core.SqlTable = "taskactive" //活跃度表
|
|
)
|
|
|
|
type ModelTaskActive struct {
|
|
modules.MCompModel
|
|
moduleTask *ModuleTask
|
|
}
|
|
|
|
func (this *ModelTaskActive) Init(service core.IService, module core.IModule, comp core.IModuleComp, options core.IModuleOptions) (err error) {
|
|
err = this.MCompModel.Init(service, module, comp, options)
|
|
this.moduleTask = module.(*ModuleTask)
|
|
this.TableName = string(TableTaskActive)
|
|
return
|
|
}
|
|
|
|
//初始化活跃度
|
|
func (this *ModelTaskActive) initActiveRewardByTag(uid string, taskTag comm.TaskTag) {
|
|
data, err := this.moduleTask.configure.getTaskActiveByTag(int32(taskTag))
|
|
if err != nil {
|
|
log.Errorf("uid:%v tag:%v initActiveRewardByTag err %v", uid, taskTag, err)
|
|
return
|
|
}
|
|
|
|
for _, conf := range data {
|
|
objId := primitive.NewObjectID().Hex()
|
|
ta := &pb.DBTaskActive{
|
|
Id: objId,
|
|
Uid: uid,
|
|
RId: conf.Key,
|
|
}
|
|
if err := this.moduleTask.modelTaskActive.AddList(swapKey(uid, taskTag), ta.Id, ta); err != nil {
|
|
log.Errorf("uid:%v tag:%v initActiv add err %v", uid, taskTag, err)
|
|
return
|
|
}
|
|
}
|
|
}
|
|
|
|
//获取玩家活跃度列表
|
|
func (this *ModelTaskActive) getUserActiveList(uid string, taskTag comm.TaskTag) []*pb.DBTaskActive {
|
|
al := []*pb.DBTaskActive{}
|
|
if err := this.GetList(swapKey(uid, taskTag), &al); err != nil {
|
|
log.Errorf("getUserActiveList err:%v", err)
|
|
return al
|
|
}
|
|
return al
|
|
}
|
|
|
|
//获取玩家活跃记录 id 唯一ID
|
|
func (this *ModelTaskActive) getUserActive(uid, id string, taskTag comm.TaskTag) *pb.DBTaskActive {
|
|
record := this.getUserActiveList(uid, taskTag)
|
|
for _, v := range record {
|
|
if v.Id == id {
|
|
return v
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (this *ModelTaskActive) updateReceive(uid, id string, taskTag comm.TaskTag, data map[string]interface{}) error {
|
|
return this.moduleTask.modelTaskActive.ChangeList(swapKey(uid, taskTag), id, data)
|
|
}
|
|
|
|
//领取处理
|
|
func (this *ModelTaskActive) receiveHandle(uid, id string, conf *cfg.Game_activeRewardData,
|
|
ua *pb.DBTaskActive, taskTag comm.TaskTag) (code pb.ErrorCode) {
|
|
if conf == nil {
|
|
code = pb.ErrorCode_ConfigNoFound
|
|
return
|
|
}
|
|
//玩家活跃度数
|
|
userActiveTotal := this.moduleTask.modelTask.countActive(uid, taskTag)
|
|
|
|
if ua.Received == 0 { //未领取
|
|
if userActiveTotal >= conf.Active {
|
|
//更新玩家领取状态
|
|
update := map[string]interface{}{
|
|
"received": 1, //标识已领取
|
|
}
|
|
if err := this.updateReceive(uid, ua.Id, taskTag, update); err != nil {
|
|
log.Errorf("updateReceive err %v", err)
|
|
code = pb.ErrorCode_DBError
|
|
return
|
|
}
|
|
} else {
|
|
code = pb.ErrorCode_TaskActiveNoenough
|
|
}
|
|
} else {
|
|
code = pb.ErrorCode_TaskReceived
|
|
}
|
|
return
|
|
}
|
|
|
|
//清空任务
|
|
func (this *ModelTaskActive) clearTask(uid string, taskTag comm.TaskTag) error {
|
|
data := this.getUserActiveList(uid, taskTag)
|
|
for _, v := range data {
|
|
if err := this.moduleTask.modelTask.DelListlds(swapKey(uid, taskTag), v.Id); err != nil {
|
|
log.Errorf("uid: %v taskTag:%v err:%v", uid, taskTag, err)
|
|
return err
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|