任务:按条件开启任务

This commit is contained in:
luying
2021-04-18 20:39:33 +08:00
parent 260744e7d2
commit dd6c3355c3
32 changed files with 296 additions and 195 deletions

View File

@@ -2,6 +2,7 @@ import BaseModel from './BaseModel';
import { index, getModelForClass, prop, DocumentType, modelOptions } from '@typegoose/typegoose';
import { TASK_FUN_TYPE } from '../consts';
import { genCode } from '../pubUtils/util';
import { getTodayZeroDate } from '../pubUtils/timeUtil';
/**
* 玩家购买商店记录表,每个商品一条,每次刷新新建一条
@@ -34,19 +35,31 @@ export default class UserTaskRec extends BaseModel {
@prop({ required: true, default: false })
received: boolean; // 是否已领取
private static getRefreshCondition(type: number) {
let today = getTodayZeroDate(5);
if(type == TASK_FUN_TYPE.DAILY) {
return { createdAt: { $gte: today } };
} else {
return {};
}
}
public static async setTaskRec(roleId: string, type: number, taskType: number, group: number, count: number) {
let rec: UserTaskRecType = await UserTaskRecModel.findOneAndUpdate({ roleId, type, group }, { $setOnInsert: { code: genCode(8), taskType, received: false }, $set: { count } }, { new: true, upsert: true }).lean();
let condition = this.getRefreshCondition(type);
let rec: UserTaskRecType = await UserTaskRecModel.findOneAndUpdate({ roleId, type, group, ...condition }, { $setOnInsert: { code: genCode(8), taskType, received: false }, $set: { count } }, { new: true, upsert: true }).lean();
return rec;
}
public static async incTaskRec(roleId: string, type: number, taskType: number, group: number, count: number) {
let rec: UserTaskRecType = await UserTaskRecModel.findOneAndUpdate({ roleId, type, group }, { $setOnInsert: { code: genCode(8), taskType, received: false }, $inc: { count } }, { new: true, upsert: true }).lean();
let condition = this.getRefreshCondition(type);
let rec: UserTaskRecType = await UserTaskRecModel.findOneAndUpdate({ roleId, type, group, ...condition }, { $setOnInsert: { code: genCode(8), taskType, received: false }, $inc: { count } }, { new: true, upsert: true }).lean();
return rec;
}
public static async checkHistoryAndSetTaskRec(roleId: string, type: number, taskType: number, group: number, count: number) {
let rec: UserTaskRecType = await UserTaskRecModel.findOne({ roleId, type, group }).lean();
let condition = this.getRefreshCondition(type);
let rec: UserTaskRecType = await UserTaskRecModel.findOne({ roleId, type, group, ...condition }).lean();
if(rec) {
if(rec.count < count) {
rec = await UserTaskRecModel.setTaskRec(roleId, type, taskType, group, count);