Files
MatchMaster/assets/Script/module/Config/StaminaPackState.ts
T

41 lines
1.4 KiB
TypeScript

const MINUTE = 60 * 1000;
export interface StaminaPackRecord {
offerEnd: number;
cooldownEnd: number;
}
function storageKey(): string {
return "StaminaPack_" + (cc.fx.GameConfig.GM_INFO.uid || "guest");
}
export function getStaminaPackRecord(): StaminaPackRecord {
return cc.fx.StorageMessage.getStorage(storageKey()) || { offerEnd: 0, cooldownEnd: 0 };
}
export function canShowStaminaPack(now = Date.now()): boolean {
const record = getStaminaPackRecord();
return record.offerEnd > now || record.cooldownEnd <= now;
}
export function beginStaminaPackOffer(now = Date.now()): void {
const record = getStaminaPackRecord();
if (record.offerEnd > now || record.cooldownEnd > now) return;
cc.fx.StorageMessage.setStorage(storageKey(), {
offerEnd: now + 10 * MINUTE,
cooldownEnd: now + 40 * MINUTE,
});
}
export function closeStaminaPackOffer(now = Date.now()): void {
const record = getStaminaPackRecord();
// 发货后 offerEnd 为 0,不能被关闭回调覆盖八小时冷却。
if (record.offerEnd > 0 && now >= record.offerEnd) {
cc.fx.StorageMessage.setStorage(storageKey(), { ...record, cooldownEnd: now + 30 * MINUTE });
}
}
export function recordStaminaPackPurchase(now = Date.now()): void {
cc.fx.StorageMessage.setStorage(storageKey(), { offerEnd: 0, cooldownEnd: now + 8 * 60 * MINUTE });
}