import { SHOP_REFRESH_TYPE } from "../../consts"; import { ActivityModelType } from "../../db/Activity"; import { UserShopType } from "../../db/UserShop"; import { UserShopTypeType } from "../../db/UserShopType"; import { gameData } from "../../pubUtils/data"; import { nowSeconds } from "../../pubUtils/timeUtil"; import { ActivityBase } from "../activityField/activityField"; interface ShopInDb { shop: number; type: number; list: ShopItemInDb[]; } interface ShopItemInDb { id: number; goodId: number; goodName: string; guildLvLimit: number; lvLimit: number; purchaseLimit: number; vipPurchaseLimit: number; refreshType: number; money: number; price: string; productID: string; chosen: number; indirectId: number; createTime: number; beginTime: number; endTime: number; } export class ShopData extends ActivityBase { shop: number; type: number; list: ShopDicData[] = []; constructor(activityData: ActivityModelType, createTime: number, serverTime: number) { super(activityData, createTime, serverTime) this.initData(activityData.data) } public initData(data: string) { let result: ShopInDb = JSON.parse(data); if(result) { this.shop = result.shop; this.type = result.type; for (let obj of result.list) { this.list.push(new ShopDicData(obj)) } } } public findByItemId(itemId: number) { let item = this.list.find(cur => cur.id == itemId); if(!item) return null; return {...item, shop: this.shop, type: this.type}; } public findByProductID(productID: string) { let item = this.list.find(cur => cur.productID == productID); if(!item) return null; return {...item, shop: this.shop, type: this.type}; } } export class ShopDicData { isNew: boolean = false; id: number; goodId: number; goodName: string; guildLvLimit: number; lvLimit: number; purchaseLimit: number; vipPurchaseLimit: number; refreshType: SHOP_REFRESH_TYPE; money: number; price: string; productID: string = ''; chosen: number; indirectId: number; createTime: number; beginTime: number; endTime: number; constructor(obj: ShopItemInDb) { this.id = obj.id; this.goodId = obj.goodId; this.goodName = obj.goodName; this.guildLvLimit = obj.guildLvLimit; this.lvLimit = obj.lvLimit; this.purchaseLimit = obj.purchaseLimit; this.vipPurchaseLimit = obj.vipPurchaseLimit; this.refreshType = obj.refreshType; this.money = obj.money; this.price = obj.price; this.productID = obj.productID; this.chosen = obj.chosen; this.indirectId = obj.indirectId; this.createTime = obj.createTime; this.beginTime = obj.beginTime; this.endTime = obj.endTime; } public setReadRecord(record: UserShopTypeType) { let now = nowSeconds(); if(this.beginTime <= now && this.endTime >= now) { let readTime = record?.readTime||0; if(this.createTime > readTime) this.isNew = true; } } } export interface ShopDicExtendData extends ShopDicData { shop: number; type: number; } export class ShopReturnData { activityId: number = 0; shop: number; type: number; dic: ShopDicData[] = []; records: ShopReturnRecordData[] = []; constructor(shop: number, type: number) { this.shop = shop; this.type = type; } public setDicByActivity(activityData: ShopData) { if(!activityData || activityData.shop != this.shop || activityData.type != this.type) return; this.activityId = activityData.activityId; for(let obj of activityData.list) { let newDic = new ShopDicData(obj); this.dic.push(newDic); } } public addUserRecords(record: UserShopType) { if(!record || record.shop != this.shop || record.type != this.type) return; if(this.activityId > 0) { let dic = this.dic.find(cur => cur.id == record.itemId); if(!dic || (dic.createTime && dic.createTime != record.createTime)) return; } else { if(!gameData.shopItem.has(record.itemId)) return; } let recordResult = new ShopReturnRecordData(record); if(recordResult && recordResult.shopItemId && recordResult.buyCount) this.records.push(recordResult) } public setReadRecord(record: UserShopTypeType) { for(let obj of this.dic) { obj.setReadRecord(record); } } } export class ShopReturnRecordData { shopItemId: number; buyCount: number; constructor(record: UserShopType) { if(!record) return; this.shopItemId = record.itemId; this.buyCount = record.count; } }