Files
ZYZ/shared/db/ActivityTreasureHuntTask.ts
2022-03-17 20:24:20 +08:00

110 lines
6.0 KiB
TypeScript

import ActivityGrowth from './ActivityGrowth';
import { index, getModelForClass, prop, DocumentType } from '@typegoose/typegoose';
import { UpdateTaskParam } from '../domain/roleField/task';
/**
* 活动系统 - 寻宝骑兵-备战任务
*/
@index({ roleId: 1 })
export default class Activity_Treasure_Hunt_Task extends ActivityGrowth {
@prop({ required: true })
roundIndex: number; // 周期
@prop({ required: true })
isPush: boolean; // 是否推送过
//任务领取记录
public static async receiveReward(serverId: number, activityId: number, roleId: string, roundIndex: number, cellIndex: number, count: number,) {
let result: ActivityTreasureHuntTaskModelType = await ActivityTreasureHuntTaskModel.findOneAndUpdate({ serverId, roleId, activityId, roundIndex, cellIndex },
{ $inc: { receiveRewardCount: count } }, { upsert: true, new: true }).lean(true);
return result;
}
//查询第几天的活动数据
public static async findDataByRoundIndex(serverId: number, activityId: number, roleId: string, roundIndex: number) {
let result: ActivityTreasureHuntTaskModelType[] = await ActivityTreasureHuntTaskModel.find({ serverId, roleId, activityId, roundIndex }).lean(true);
return result;
}
// 更新任务
public static async checkAndUpdateTask(serverId: number, activityId: number, roleId: string, roundIndex: number, cellIndex: number, param: UpdateTaskParam) {
let canSet = true;
if(param.record || param.max || param.min) {
let rec = await this.findDataByCellIndex(serverId, activityId, roleId, roundIndex, cellIndex);
if(rec) {
if(param.record && rec.records.indexOf(param.record) != -1) {
canSet = false;
} else if (param.max && rec.totalCount >= param.max) {
canSet = false;
} else if (param.min && rec.totalCount <= param.min) {
canSet = false;
}
}
}
if(canSet) {
return await this.setOrIncTask(serverId, activityId, roleId, roundIndex, cellIndex, param);
}
}
public static async setOrIncTask(serverId: number, activityId: number, roleId: string, roundIndex: number, cellIndex: number, param: UpdateTaskParam) {
if(param.set) {
return await this.setTaskCount(serverId, activityId, roleId, roundIndex, cellIndex, param.set, param.record);
} else if (param.inc) {
return await this.addTaskCount(serverId, activityId, roleId, roundIndex, cellIndex, param.inc, param.record);
}
}
//根据活动统计完成任务次数
public static async setTaskCount(serverId: number, activityId: number, roleId: string, roundIndex: number, cellIndex: number, count: number, record?: string) {
let result: ActivityTreasureHuntTaskModelType = await ActivityTreasureHuntTaskModel.findOneAndUpdate({ serverId, roleId, activityId, roundIndex, cellIndex },
{ $set: { totalCount: count }, $addToSet: { records: record||'' } }, { upsert: true, new: true }).lean();
return result;
}
//根据活动统计完成任务次数
public static async addTaskCount(serverId: number, activityId: number, roleId: string, roundIndex: number, cellIndex: number, count: number, record?: string) {
let result: ActivityTreasureHuntTaskModelType = await ActivityTreasureHuntTaskModel.findOneAndUpdate({ serverId, roleId, activityId, roundIndex, cellIndex },
{ $inc: { totalCount: count }, $addToSet: { records: record||'' } }, { upsert: true, new: true }).lean();
return result;
}
//查询活动数据
public static async findDataByCellIndex(serverId: number, activityId: number, roleId: string, roundIndex: number, cellIndex: number) {
let result: ActivityTreasureHuntTaskModelType = await ActivityTreasureHuntTaskModel.findOne({ serverId, roleId, activityId, roundIndex, cellIndex }).lean(true);
return result;
}
//根据活动统计完成任务次数
public static async addTreasureHuntTaskCount(serverId: number, activityId: number, roleId: string, roundIndex: number, cellIndex: number, addCount: number) {
let result: ActivityTreasureHuntTaskModelType = await ActivityTreasureHuntTaskModel.findOneAndUpdate({ serverId, roleId, activityId, roundIndex, cellIndex },
{ $inc: { totalCount: addCount } }, { upsert: true, new: true }).lean(true);
return result;
}
//根据活动记录统计数据
public static async addTreasureHuntTaskRecord(serverId: number, activityId: number, roleId: string, roundIndex: number, cellIndex: number, data: string,) {
let result: ActivityTreasureHuntTaskModelType = await ActivityTreasureHuntTaskModel.findOneAndUpdate({ serverId, roleId, activityId, roundIndex, cellIndex },
{ $set: { data: data } }, { upsert: true, new: true }).lean(true);
return result;
}
//推送标记
public static async pushMessage(serverId: number, activityId: number, roleId: string, roundIndex: number, cellIndex: number,) {
let result: ActivityTreasureHuntTaskModelType = await ActivityTreasureHuntTaskModel.findOneAndUpdate({ serverId, roleId, activityId, roundIndex, cellIndex },
{ $set: { isPush: true, } }, { upsert: true, new: true }).lean(true);
return result;
}
//删除活动领取记录
public static async deleteActivity(serverId: number, activityId: number, roleId: string, roundIndex: number, cellIndex: number) {
await ActivityTreasureHuntTaskModel.deleteMany({ serverId, roleId, activityId, roundIndex, cellIndex });
}
}
export const ActivityTreasureHuntTaskModel = getModelForClass(Activity_Treasure_Hunt_Task);
export interface ActivityTreasureHuntTaskModelType extends Pick<DocumentType<Activity_Treasure_Hunt_Task>, keyof Activity_Treasure_Hunt_Task> { }
export type ActivityTreasureHuntTaskModelTypeParam = Partial<ActivityTreasureHuntTaskModelType>; // 将所有字段变成可选项