80 lines
2.6 KiB
TypeScript
80 lines
2.6 KiB
TypeScript
import moment = require('moment');
|
|
import { ActivityModelType } from '../../db/Activity';
|
|
import { ActivityShopModelType } from '../../db/ActivityShop';
|
|
import { ActivityBase } from './activityField';
|
|
|
|
// 每个商品的内容
|
|
export class ShopItem {
|
|
id: number; // 商品id
|
|
reward: string; //任务奖励,格式:1&3&1(类型&id&数量) 类型定义:1.英雄,2.物品
|
|
countMax: number = 0; //可购买的最大次数,0表示不限制
|
|
name: string; //名字
|
|
|
|
buyCount: number = 0; //购买过的次数
|
|
|
|
constructor(data: any) {
|
|
this.id = data.id;
|
|
this.reward = data.reward;
|
|
this.countMax = data.countMax;
|
|
this.name = data.name;
|
|
}
|
|
}
|
|
|
|
// 商店数据
|
|
export class LimitShopData extends ActivityBase {
|
|
name: string = '';//活动名称
|
|
interval: number = 0;//周期间隔(秒)
|
|
list: Array<ShopItem> = [];//商品列表
|
|
nextRefreshTime: number;//下次刷新时间
|
|
roundIndex: number = 1;//周期数从1开始
|
|
|
|
public findItem(id: number) {
|
|
let index = this.list.findIndex(obj => { return obj && obj.id === id });
|
|
return (index != -1) ? this.list[index] : null
|
|
}
|
|
|
|
//全部领取完成
|
|
public isComplete() {
|
|
for (let item of this.list) {
|
|
if (item.countMax == 0 ||
|
|
(item.countMax > 0 && item.buyCount < item.countMax)) {
|
|
return false
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
//解析玩家购买记录
|
|
public setPlayerRecords(data: ActivityShopModelType) {
|
|
if (!data) {
|
|
return;
|
|
}
|
|
let records = data.records ? data.records : [];
|
|
for (let item of this.list) {
|
|
let buyRecords = records.filter(obj => { return obj && obj.id === item.id });
|
|
item.buyCount = buyRecords.length;
|
|
}
|
|
}
|
|
|
|
|
|
public initData(data: string) {
|
|
this.nextRefreshTime = this.endTime;
|
|
let dataObj = JSON.parse(data);
|
|
this.name = dataObj.name;
|
|
this.interval = dataObj.interval;
|
|
if (this.interval > 0) {
|
|
this.roundIndex = Math.ceil((moment(new Date).valueOf() - this.beginTime) / (this.interval * 1000));
|
|
this.nextRefreshTime = moment(this.beginTime).add(this.interval * this.roundIndex, 'second').valueOf();
|
|
}
|
|
console.log(moment(new Date).valueOf(), moment(this.beginTime).valueOf(), this.roundIndex,)
|
|
let arr = dataObj.data;
|
|
for (let obj of arr) {
|
|
this.list.push(new ShopItem(obj))
|
|
}
|
|
}
|
|
|
|
constructor(activityData: ActivityModelType) {
|
|
super(activityData)
|
|
this.initData(activityData.data)
|
|
}
|
|
} |