Files
ZYZ/shared/domain/activityField/firstGiftField.ts
2021-05-10 18:39:13 +08:00

78 lines
2.3 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { FIRST_GIFT_STATE } from '../../consts';
import { ActivityModelType } from '../../db/Activity';
import { ActivityFirstGiftModelType } from '../../db/ActivityFirstGift';
import { deltaDays } from '../../pubUtils/util';
import { ActivityBase } from './activityField';
// 首充礼包的数据
export class FirstGiftItem {
index: number; // 第几天从1开始
name: string; //名称
reward: string; //奖励
isReceive: boolean = false; //是否领取过奖励
constructor(data: any) {
this.name = data.name;
this.index = data.index;
this.reward = data.reward;
}
}
// 30天任务活动数据
export class FirstGiftData extends ActivityBase {
state: number = FIRST_GIFT_STATE.NOT_OPEN;//活动状态
list: Array<FirstGiftItem> = [];//奖励
days: number = 0;//展示天数
//全部领取完成
public isComplete() {
for (let i = 0; i < this.list.length; i++) {
if (!this.list[i].isReceive) {
return false
}
}
return true;
}
//奖励内容
public findFirstGiftItem(index: number): FirstGiftItem {
let listIndex = this.list.findIndex(obj => { return obj && obj.index == index });
if (listIndex != -1) {
return this.list[listIndex];
}
return null;
}
//解析玩家任务领取记录
public setPlayerRecords(data: ActivityFirstGiftModelType) {
this.todayIndex = 0;
if (!data) {
return;
}
this.todayIndex = deltaDays(data.createdAt, new Date) + 1;
this.state = (this.todayIndex <= this.days) ? FIRST_GIFT_STATE.OPEN : FIRST_GIFT_STATE.CLOSED;
let daysNum = data.days ? data.days : [];
for (let obj of this.list) {
if (daysNum.indexOf(obj.index) !== -1) {
obj.isReceive = true;
}
}
}
public initData(data: string) {
let dataObj = JSON.parse(data);
this.days = dataObj.days;
let arr = dataObj.data;
for (let obj of arr) {
this.list.push(new FirstGiftItem(obj))
}
}
constructor(activityData: ActivityModelType) {
super(activityData)
this.initData(activityData.data)
}
}