120 lines
4.2 KiB
TypeScript
120 lines
4.2 KiB
TypeScript
import moment = require('moment');
|
||
import { REFRESH_TIME } from '../../consts';
|
||
import { ActivityModelType } from '../../db/Activity';
|
||
import { ActivityRefreshShopModelType } from '../../db/ActivityRefreshShop';
|
||
import { ActivityBase } from './activityField';
|
||
|
||
// 每个商品的内容
|
||
export class RefreshShopItem {
|
||
id: number; // 商品id
|
||
reward: string; //任务奖励,格式:1&3&1(类型&id&数量) 类型定义:1.英雄,2.物品
|
||
countMax: number = 0; //可购买的最大次数,0表示不限制
|
||
name: string; //名字
|
||
price: number; //价格
|
||
productID: string; //商品id
|
||
consume: string; //消耗
|
||
imageName: string;
|
||
discount: number; //折扣
|
||
pageIndex: number; // 第几页
|
||
|
||
buyCount: number = 0; //购买过的次数
|
||
|
||
constructor(data: any, pageIndex: number) {
|
||
this.pageIndex = pageIndex;
|
||
this.id = data.id;
|
||
this.reward = data.reward;
|
||
this.countMax = data.countMax;
|
||
this.name = data.name;
|
||
this.price = data.price;
|
||
this.productID = data.productID;
|
||
this.imageName = data.imageName;
|
||
this.discount = data.discount ? data.discount : 0;
|
||
this.consume = data.consume ? data.consume : '';
|
||
this.buyCount = 0;
|
||
}
|
||
}
|
||
|
||
|
||
// 每个商品的内容
|
||
export class RefreshShopPage {
|
||
pageIndex: number; // 第几页
|
||
name: string; //名字
|
||
items: Array<RefreshShopItem> = [];//商品列表
|
||
constructor(data: any) {
|
||
this.pageIndex = data.pageIndex;
|
||
this.name = data.name;
|
||
for (let item of data.items) {
|
||
this.items.push(new RefreshShopItem(item, data.pageIndex))
|
||
}
|
||
}
|
||
}
|
||
|
||
// 商店数据
|
||
export class RefreshShopData extends ActivityBase {
|
||
shopType: number = 0;//商店类型,用于客户端显示使用
|
||
name: string = '';//活动名称
|
||
interval: number = 0;//周期间隔(秒)
|
||
list: Array<RefreshShopPage> = [];//商品列表
|
||
nextRefreshTime: number;//下次刷新时间
|
||
roundIndex: number = 1;//周期数从1开始
|
||
|
||
public findItemByProductID(productID: string) {
|
||
for (let pageData of this.list) {
|
||
let index = pageData.items.findIndex(obj => { return obj && obj.productID === productID });
|
||
if (index != -1) {
|
||
this.list[index]
|
||
}
|
||
}
|
||
return null;
|
||
}
|
||
|
||
public findItem(id: number, pageIndex: number) {
|
||
let index = this.list.findIndex(obj => { return obj && obj.pageIndex === pageIndex });
|
||
if (index != -1) {
|
||
let itemIndex = this.list[index].items.findIndex(obj => { return obj && obj.id == id });
|
||
if (itemIndex != -1) {
|
||
return this.list[index].items[itemIndex];
|
||
}
|
||
}
|
||
return null
|
||
}
|
||
|
||
//解析玩家购买记录
|
||
public setPlayerRecords(data: ActivityRefreshShopModelType) {
|
||
if (!data) {
|
||
return;
|
||
}
|
||
let records = data.records ? data.records : [];
|
||
for (let pageData of this.list) {
|
||
for (let item of pageData.items) {
|
||
let buyRecords = records.filter(obj => { return obj && obj.id === item.id && obj.pageIndex == pageData.pageIndex });
|
||
item.buyCount = buyRecords.length;
|
||
}
|
||
}
|
||
}
|
||
|
||
|
||
public initData(data: string) {
|
||
this.nextRefreshTime = this.endTime;
|
||
let dataObj = JSON.parse(data);
|
||
this.shopType = dataObj.shopType;
|
||
this.name = dataObj.name;
|
||
this.interval = dataObj.interval;
|
||
|
||
this.beginTime = moment(this.beginTime).startOf('d').add(REFRESH_TIME, 'hour').valueOf();
|
||
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 RefreshShopPage(obj))
|
||
}
|
||
}
|
||
|
||
constructor(activityData: ActivityModelType) {
|
||
super(activityData)
|
||
this.initData(activityData.data)
|
||
}
|
||
} |