220 lines
5.5 KiB
Go
220 lines
5.5 KiB
Go
package task
|
|
|
|
import (
|
|
"go_dreamfactory/comm"
|
|
"go_dreamfactory/lego/sys/log"
|
|
"go_dreamfactory/pb"
|
|
cfg "go_dreamfactory/sys/configure/structs"
|
|
)
|
|
|
|
//任务奖励领取
|
|
|
|
func (this *apiComp) ReceiveCheck(session comm.IUserSession, req *pb.TaskReceiveReq) (errdata *pb.ErrorData) {
|
|
var code pb.ErrorCode
|
|
if req.Id == 0 {
|
|
code = pb.ErrorCode_TaskIdEmpty
|
|
} else if req.TaskTag == 0 {
|
|
code = pb.ErrorCode_TaskTagEmpty
|
|
}
|
|
errdata = &pb.ErrorData{
|
|
Code: code,
|
|
Title: code.ToString(),
|
|
}
|
|
return
|
|
}
|
|
|
|
func (this *apiComp) Receive(session comm.IUserSession, req *pb.TaskReceiveReq) (errdata *pb.ErrorData) {
|
|
if errdata = this.ReceiveCheck(session, req); errdata != nil {
|
|
return
|
|
}
|
|
uid := session.GetUserId()
|
|
// 获取待领取的任务
|
|
userTask := this.module.modelTask.getUserTask(uid, req.Id)
|
|
|
|
if userTask == nil {
|
|
errdata = &pb.ErrorData{
|
|
Code: pb.ErrorCode_TaskNotFound,
|
|
Title: pb.ErrorCode_TaskNotFound.ToString(),
|
|
}
|
|
return
|
|
}
|
|
|
|
var taskDataList []*pb.TaskData
|
|
|
|
switch req.TaskTag {
|
|
case int32(comm.TASK_DAILY):
|
|
taskDataList = userTask.DayList
|
|
case int32(comm.TASK_WEEKLY):
|
|
taskDataList = userTask.WeekList
|
|
case int32(comm.TASK_ACHIEVE):
|
|
taskDataList = userTask.AchieveList
|
|
}
|
|
|
|
resp := &pb.TaskReceiveResp{}
|
|
var taskData *pb.TaskData
|
|
for _, v := range taskDataList {
|
|
if v.TaskId == req.Id {
|
|
taskData = v
|
|
}
|
|
}
|
|
|
|
//判断是否完成
|
|
if taskData.Status == 0 {
|
|
errdata = &pb.ErrorData{
|
|
Code: pb.ErrorCode_TaskNoFinished,
|
|
Title: pb.ErrorCode_TaskNoFinished.ToString(),
|
|
}
|
|
return
|
|
}
|
|
//判断任务是否领取
|
|
if taskData.Received == 1 {
|
|
errdata = &pb.ErrorData{
|
|
Code: pb.ErrorCode_TaskReceived,
|
|
Title: pb.ErrorCode_TaskReceived.ToString(),
|
|
}
|
|
return
|
|
}
|
|
|
|
// 待领取的任务配置
|
|
conf := this.module.configure.getTaskById(taskData.TaskId)
|
|
if conf == nil {
|
|
errdata = &pb.ErrorData{
|
|
Code: pb.ErrorCode_ConfigNoFound,
|
|
Title: pb.ErrorCode_ConfigNoFound.ToString(),
|
|
}
|
|
return
|
|
}
|
|
|
|
// 更新活跃度
|
|
if conf.Active > 0 {
|
|
//更新活跃度
|
|
ue, err := this.module.ModuleUser.GetUserExpand(session.GetUserId())
|
|
if err != nil {
|
|
errdata = &pb.ErrorData{
|
|
Code: pb.ErrorCode_DBError,
|
|
Title: pb.ErrorCode_DBError.ToString(),
|
|
Message: err.Error(),
|
|
}
|
|
return
|
|
}
|
|
update := make(map[string]interface{})
|
|
if ue != nil {
|
|
if comm.TaskTag(req.TaskTag) == comm.TASK_DAILY {
|
|
ue.Activeday = ue.Activeday + conf.Active
|
|
update["activeday"] = ue.Activeday
|
|
}
|
|
if comm.TaskTag(req.TaskTag) == comm.TASK_WEEKLY {
|
|
ue.Activeweek = ue.Activeweek + conf.Active
|
|
update["activeweek"] = ue.Activeweek
|
|
}
|
|
// 周任务 自动领取奖励
|
|
if comm.TaskTag(req.TaskTag) == comm.TASK_WEEKLY {
|
|
TaskActive := make(map[string]interface{})
|
|
activeList := this.module.modelTaskActive.getActiveList(uid)
|
|
var rewards []*cfg.Gameatn
|
|
var maxTaskId int32
|
|
for _, v := range activeList {
|
|
if v.Received != 1 {
|
|
conf := this.module.configure.getTaskActiveById(v.TaskId)
|
|
if conf == nil {
|
|
errdata = &pb.ErrorData{
|
|
Code: pb.ErrorCode_ConfigNoFound,
|
|
Title: pb.ErrorCode_ConfigNoFound.ToString(),
|
|
Message: err.Error(),
|
|
}
|
|
return
|
|
}
|
|
if ue.Activeweek < conf.Active {
|
|
continue
|
|
}
|
|
v.Received = 1
|
|
rewards = append(rewards, conf.Reword...)
|
|
}
|
|
|
|
maxTaskId = v.TaskId
|
|
|
|
}
|
|
|
|
if len(rewards) > 0 {
|
|
TaskActive["activityList"] = activeList
|
|
|
|
if err := this.module.modelTaskActive.Change(session.GetUserId(), TaskActive); err != nil {
|
|
this.module.Errorf("updateReceive err %v", err)
|
|
errdata = &pb.ErrorData{
|
|
Code: pb.ErrorCode_DBError,
|
|
Title: pb.ErrorCode_DBError.ToString(),
|
|
Message: err.Error(),
|
|
}
|
|
return
|
|
}
|
|
//派发奖励
|
|
if errdata = this.module.DispenseRes(session, rewards, true); errdata != nil {
|
|
this.module.Error("活跃度奖励",
|
|
log.Field{Key: "uid", Value: uid},
|
|
log.Field{Key: "rewards", Value: rewards},
|
|
)
|
|
}
|
|
go this.module.ModuleBuried.TriggerBuried(uid, comm.GetBuriedParam(comm.Rtype171, 1))
|
|
session.SendMsg(string(this.module.GetType()), "activereceive", &pb.TaskActiveReceivePush{
|
|
TaskTag: int32(comm.TASK_WEEKLY),
|
|
Id: maxTaskId,
|
|
})
|
|
|
|
}
|
|
}
|
|
}
|
|
|
|
if len(update) > 0 {
|
|
if err = this.module.ModuleUser.ChangeUserExpand(session.GetUserId(), update); err != nil {
|
|
errdata = &pb.ErrorData{
|
|
Code: pb.ErrorCode_DBError,
|
|
Title: pb.ErrorCode_DBError.ToString(),
|
|
Message: err.Error(),
|
|
}
|
|
return
|
|
}
|
|
}
|
|
}
|
|
|
|
//奖励
|
|
if errdata = this.module.DispenseRes(session, conf.Reword, true); errdata != nil {
|
|
this.module.Error("发送奖励",
|
|
log.Field{Key: "uid", Value: uid},
|
|
log.Field{Key: "rewards", Value: conf.Reword},
|
|
log.Field{Key: "errdata", Value: errdata},
|
|
)
|
|
return
|
|
}
|
|
|
|
//更新用户领取状态
|
|
for _, v := range taskDataList {
|
|
if v.TaskId == req.Id {
|
|
v.Received = 1
|
|
break
|
|
}
|
|
}
|
|
update := map[string]interface{}{}
|
|
switch req.TaskTag {
|
|
case int32(comm.TASK_DAILY):
|
|
update["dayList"] = taskDataList
|
|
case int32(comm.TASK_WEEKLY):
|
|
update["weekList"] = taskDataList
|
|
case int32(comm.TASK_ACHIEVE):
|
|
update["achieveList"] = taskDataList
|
|
}
|
|
|
|
if err := this.module.modelTask.Change(session.GetUserId(), update); err != nil {
|
|
errdata = &pb.ErrorData{
|
|
Code: pb.ErrorCode_DBError,
|
|
Title: pb.ErrorCode_DBError.ToString(),
|
|
Message: err.Error(),
|
|
}
|
|
return
|
|
}
|
|
resp.TaskId = taskData.TaskId
|
|
|
|
session.SendMsg(string(this.module.GetType()), TaskSubTypeReceive, resp)
|
|
|
|
return
|
|
}
|