今日挑战互动接口

This commit is contained in:
qiaoxin
2021-04-25 17:46:01 +08:00
parent 7bb445693d
commit 8cb2ba2a8b
13 changed files with 493 additions and 156 deletions

View File

@@ -7,4 +7,5 @@
export enum ACTIVITY_TYPE {
SEVEN_DAYS = 1, // 七天乐活动
TASK_GROWTH = 2, // 成长任务活动
TASK_DAILY_CHALLENGES = 3, // 今日挑战活动
}

View File

@@ -338,6 +338,7 @@ export const STATUS = {
ACTIVITY_TASK_UNCOMPLETED: { code: 50002, simStr: '任务还未完成' },
ACTIVITY_NO_POINT: { code: 50003, simStr: '奖章不足' },
ACTIVITY_REWARDED: { code: 50004, simStr: '已经领取过' },
ACTIVITY_TIME_ERROR: { code: 50004, simStr: '时间错误' },
// GM后台相关状态 60000 - 69999
GM_ERR_PASSWORD: { code: 60001, simStr: '账号或密码错误' },
GM_MISS_API: { code: 60002, simStr: '未找到该接口' },

View File

@@ -0,0 +1,82 @@
import BaseModel from './BaseModel';
import { index, getModelForClass, prop, DocumentType } from '@typegoose/typegoose';
/**
* 活动系统 - 今日挑战活动
*/
@index({ roleId: 1 })
export default class ActivityDailyChallenges extends BaseModel {
@prop({ required: true })
acvitityId: number; // 活动Id
@prop({ required: true })
roleId: string; // 用户Id
@prop({ required: true })
dayIndex: number; // 第几天
@prop({ required: true })
cellIndex: number; // 第几天的第几个奖励
@prop({ required: true })
type: number; // 任务类型
@prop({ required: true })
totalCount: number; // 累计达成次数
@prop({ required: true })
receiveRewardCount: number; // 领取奖励次数
//任务领取记录
public static async addCellRecord(acvitityId: number, roleId: string, dayIndex: number, cellIndex: number, type: number, count: number, lean = true) {
let result: ActivityDailyChallengesModelType = await ActivityDailyChallengesModel.findOneAndUpdate({ roleId, acvitityId, dayIndex, cellIndex, type },
{ $inc: { receiveRewardCount: count } }, { upsert: true, new: true }).lean(lean);
return result;
}
//根据活动统计完成任务次数
public static async setTaskCount(acvitityId: number, roleId: string, dayIndex: number, cellIndex: number, type: number, count: number, lean = true) {
let result: ActivityDailyChallengesModelType = await ActivityDailyChallengesModel.findOneAndUpdate({ roleId, acvitityId, dayIndex, cellIndex, type },
{ $set: { totalCount: count } }, { upsert: true, new: true }).lean(lean);
return result;
}
//根据活动统计完成任务次数
public static async addTaskCount(acvitityId: number, roleId: string, dayIndex: number, cellIndex: number, type: number, addCount: number, lean = true) {
let result: ActivityDailyChallengesModelType = await ActivityDailyChallengesModel.findOneAndUpdate({ roleId, acvitityId, dayIndex, cellIndex, type },
{ $inc: { totalCount: addCount } }, { upsert: true, new: true }).lean(lean);
return result;
}
//根据活动id查询活动数据
public static async findData(acvitityId: number, roleId: string, lean = true) {
let result: ActivityDailyChallengesModelType[] = await ActivityDailyChallengesModel.find({ roleId, acvitityId }).lean(lean);
return result;
}
//查询第几天的活动数据
public static async findDataByDayIndex(acvitityId: number, roleId: string, dayIndex: number, lean = true) {
let result: ActivityDailyChallengesModelType[] = await ActivityDailyChallengesModel.find({ roleId, acvitityId, dayIndex }).lean(lean);
return result;
}
//查询第几天某个的活动数据
public static async findDataByCellIndex(acvitityId: number, roleId: string, dayIndex: number, cellIndex: number, lean = true) {
let result: ActivityDailyChallengesModelType[] = await ActivityDailyChallengesModel.find({ roleId, acvitityId, dayIndex, cellIndex }).lean(lean);
return result;
}
//新增领取记录
public static async addData(acvitityId: number, roleId: string, dayIndex: number, cellIndex: number, count: number, lean = true) {
let result: ActivityDailyChallengesModelType = await ActivityDailyChallengesModel.findOneAndUpdate({ roleId, acvitityId, dayIndex, cellIndex },
{ $: { count: count } },
{ upsert: true, new: true }).lean(lean);
return result;
}
//删除活动领取记录
public static async deleteActivity(acvitityId: number, roleId: string, dayIndex: number, cellIndex: number, lean = true) {
let result = await ActivityDailyChallengesModel.deleteMany({ roleId, acvitityId, dayIndex, cellIndex });
return result;
}
}
export const ActivityDailyChallengesModel = getModelForClass(ActivityDailyChallenges);
export interface ActivityDailyChallengesModelType extends Pick<DocumentType<ActivityDailyChallenges>, keyof ActivityDailyChallenges> { }
export type ActivityDailyChallengesModelTypeParam = Partial<ActivityDailyChallengesModelType>; // 将所有字段变成可选项

View File

@@ -1,4 +1,5 @@
import { ActivityModelType } from '../../db/Activity';
import { deltaDays } from '../../pubUtils/util';
// 活动数据
export abstract class ActivityBase {
@@ -6,9 +7,15 @@ export abstract class ActivityBase {
beginTime: Date = null;
endTime: Date = null;
type: number = 0;
todayIndex: number = 0;//从1开始
abstract initData(data: string): void;
//今天是活动第几天
public today(): number {
return this.todayIndex;
}
constructor(activityData: ActivityModelType) {
this.activityId = activityData.acvitityId;
this.beginTime = activityData.beginTime;
@@ -16,5 +23,7 @@ export abstract class ActivityBase {
this.type = activityData.type;
// this.data = activityData.data;
this.initData(activityData.data);
this.todayIndex = deltaDays(activityData.beginTime, new Date) + 1;
console.log('今天是活动第几天', activityData.beginTime, new Date, this.todayIndex)
}
}

View File

@@ -0,0 +1,142 @@
import { TASK_TYPE } from '../../consts';
import { ActivityModelType } from '../../db/Activity';
import { ActivityDailyChallengesModelType } from '../../db/ActivityDailyChallenges';
import { RewardInter } from '../../pubUtils/interface';
import { parseGoodStrWithType, parseHeroStrWithType, splitString } from '../../pubUtils/util';
import { CreateHeroParam } from '../roleField/hero';
import { ActivityBase } from './activityField';
// 今日挑战的每日配置数据
export class DailyItem {
dayIndex: number; // 第几天,从1开始
cellIndex: number; // 当天第几行从1开始
name: string; // 任务名称
taskType: number; // 任务类型 dic_zyz_taskType.json
taskParam: string; //任务数据 dic_zyz_taskType.json
taskParamArray: Array<number>; //任务数据 dic_zyz_taskType.json
reward: string; // 任务奖励,格式:1&3&1(类型&id&数量) 类型定义:1.英雄2.物品
totalCount: number = 0; //完成任务累计次数
receiveRewardCount: number = 0; //领取奖励次数
constructor(data: any) {
this.dayIndex = data.dayIndex;
this.cellIndex = data.cellIndex;
this.name = data.name;
this.taskType = data.taskType;
this.taskParam = data.taskParam;
this.reward = data.reward;
this.taskParamArray = splitString(data.taskParam, '&')
}
public heroReward(): CreateHeroParam[] {
let rewardArray = [];
let rewardData = this.reward.split('|').filter(obj => { return obj && obj != '' });
for (let objStr of rewardData) {
let reward = parseHeroStrWithType(objStr);
rewardArray.push(reward);
}
return rewardArray.find(obj => { return obj && obj.type == 1 })
}
public goodReward(): RewardInter[] {
let rewardArray = [];
let rewardData = this.reward.split('|').filter(obj => { return obj && obj != '' });
for (let objStr of rewardData) {
let reward = parseGoodStrWithType(objStr);
rewardArray.push(reward);
}
return rewardArray.find(obj => { return obj && obj.type == 2 })
}
public canReceive(): boolean {
return this.receiveRewardCount != 0;
}
public isComplete(): boolean {
let complete = false;
switch (this.taskType) {
case TASK_TYPE.ROLE_LV:
complete = this.totalCount >= this.taskParamArray[0];
break;
case TASK_TYPE.GUILD_JOIN:
complete = this.totalCount >= this.taskParamArray[0];
break;
case TASK_TYPE.LOGIN_SUM:
complete = this.totalCount >= this.taskParamArray[0];
break;
case TASK_TYPE.HERO_NUM:
complete = this.totalCount >= this.taskParamArray[0];
break;
case TASK_TYPE.ROLE_TITLE:
complete = this.totalCount >= this.taskParamArray[0];
break;
case TASK_TYPE.GASHA:
complete = this.totalCount >= this.taskParamArray[0];
break;
case TASK_TYPE.EQUIP_STRENGTHEN:
complete = this.totalCount >= this.taskParamArray[0];
break;
case TASK_TYPE.BATTLE_MAIN:
complete = this.totalCount >= this.taskParamArray[0];
break;
case TASK_TYPE.EQUIP_JEWEL_SUM:
complete = this.totalCount >= this.taskParamArray[0];
break;
case TASK_TYPE.GUILD_TRAIN:
complete = this.totalCount >= this.taskParamArray[0];
break;
case TASK_TYPE.ROLE_SCHOOL_PUT_HERO:
complete = this.totalCount >= this.taskParamArray[0];
break;
case TASK_TYPE.GUILD_ACTIVITY:
complete = this.totalCount >= this.taskParamArray[0];
break;
default:
complete = false;
break;
}
return complete;
}
}
// 今日挑战活动数据
export class DailyChallengesData extends ActivityBase {
list: Array<DailyItem> = [];
public findDailyChallengesItem(dayIndex: number, cellIndex: number, type: number) {
let index = this.list.findIndex(obj => { return obj && obj.dayIndex == dayIndex && obj.cellIndex == cellIndex && obj.taskType == type })
return (index != -1) ? this.list[index] : null;
}
public findTaskByType(type: TASK_TYPE, dayIndex: number) {
return this.list.filter(obj => {
return obj && obj.taskType == type && obj.dayIndex == dayIndex;
})
}
//解析玩家领取记录
public setPlayerRecords(data: ActivityDailyChallengesModelType[]) {
for (let obj of this.list) {
let index = data.findIndex(record => { return obj.dayIndex == record.dayIndex && obj.cellIndex == record.cellIndex })
if (index != -1) {
obj.totalCount = data[index].totalCount;
obj.receiveRewardCount = data[index].receiveRewardCount;
}
}
}
public initData(data: string) {
let arr = JSON.parse(data);
for (let obj of arr) {
this.list.push(new DailyItem(obj))
}
}
constructor(activityData: ActivityModelType) {
super(activityData)
}
}

View File

@@ -2,7 +2,8 @@ import { TASK_TYPE } from '../../consts';
import { ActivityModelType } from '../../db/Activity';
import { ActivityGrowthModelType } from '../../db/ActivityGrowth';
import { RewardInter } from '../../pubUtils/interface';
import { parseGoodStrWithType, splitString } from '../../pubUtils/util';
import { parseGoodStrWithType, parseHeroStrWithType, splitString } from '../../pubUtils/util';
import { CreateHeroParam } from '../roleField/hero';
import { ActivityBase } from './activityField';
@@ -38,11 +39,11 @@ export class GrowthItem {
this.taskParamArray = splitString(data.taskParam, '&')
}
public heroReward(): RewardInter[] {
public heroReward(): CreateHeroParam[] {
let rewardArray = [];
let rewardData = this.reward.split('|').filter(obj => { return obj && obj != '' });
for (let objStr of rewardData) {
let reward = parseGoodStrWithType(objStr);
let reward = parseHeroStrWithType(objStr);
rewardArray.push(reward);
}
return rewardArray.find(obj => { return obj && obj.type == 1 })

View File

@@ -2,4 +2,5 @@
import { HeroUpdate } from '../../db/Hero';
export interface CreateHeroParam extends HeroUpdate {
hid: number;
count: number;
}

View File

@@ -9,10 +9,14 @@ import { HeroType } from '../db/Hero';
import { EquipType, EquipModel } from '../db/Equip';
import { ItemInter } from './interface';
import { GrowthData } from '../domain/activityField/growthField';
import { DailyChallengesData } from '../domain/activityField/dailyChallengesField';
import { splitString } from './util';
import { ActivityModel, ActivityModelType } from '../db/Activity';
import { ACTIVITY_TYPE } from '../consts/constModules/activityConst';
import { ActivityGrowthModel } from '../db/ActivityGrowth';
import { ActivityDailyChallengesModel } from '../db/ActivityDailyChallenges';
export async function checkTaskWithRoles(taskType: number, roles: RoleType[], funcs?: number[]) {
let pushMessage = new Array<TaskListReturn>();
@@ -473,6 +477,7 @@ function checkRecResult(rec: UserTaskRecType, id: number, condition: number) {
*
*/
export async function accomplishTask(roleId: string, taskType: TASK_TYPE, count: number, parma?: any) {
//成长活动统计
let allActivity: ActivityModelType[] = await ActivityModel.findOpenActivityByType(ACTIVITY_TYPE.TASK_GROWTH, new Date());
for (let activity of allActivity) {
let growthActivity = new GrowthData(activity);
@@ -487,8 +492,25 @@ export async function accomplishTask(roleId: string, taskType: TASK_TYPE, count:
}
}
}
}
//今日挑战统计
allActivity = await ActivityModel.findOpenActivityByType(ACTIVITY_TYPE.TASK_DAILY_CHALLENGES, new Date());
for (let activity of allActivity) {
let growthActivity = new DailyChallengesData(activity);
let taskArray = growthActivity.findTaskByType(taskType, growthActivity.today());
for (let task of taskArray) {
let addCount = isComplete(roleId, task.taskType, task.taskParam, count, parma);
if (addCount) {
if (taskType == TASK_TYPE.ROLE_LV || taskType == TASK_TYPE.ROLE_TITLE) {
await ActivityDailyChallengesModel.setTaskCount(growthActivity.activityId, roleId, task.dayIndex, task.cellIndex, task.taskType, addCount);
} else {
await ActivityDailyChallengesModel.addTaskCount(growthActivity.activityId, roleId, task.dayIndex, task.cellIndex, task.taskType, addCount);
}
}
}
}
}
/**

View File

@@ -540,6 +540,21 @@ export function parseGoodStrWithType(str: string) {
return result
}
// 根据类型解析物品 {"type":number, "hid": number, "count": number} 格式
//type 1.英雄2.物品
export function parseHeroStrWithType(str: string) {
let result = new Array<{ type: number, hid: number, count: number }>();
if (!str) return result;
let decodeArr = decodeArrayListStr(str);
for (let [type, hid, count] of decodeArr) {
if (isNaN(parseInt(type)) || isNaN(parseInt(hid)) || isNaN(parseInt(count))) {
throw new Error('data table format wrong');
}
result.push({ type: parseInt(type), hid: parseInt(hid), count: parseInt(count) });
}
return result
}
// 数字列表
export function parseNumberList(str: string) {
let res = new Array<number>();