You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
AdLike2048/assets/script/ui/buyPower/UIbuyPower.ts

131 lines
4.0 KiB

3 weeks ago

//*********************
// create by 流云
// time: Fri Nov 28 2025
// desc: 补充体力:广告 / 分享
//*********************
import { _decorator, find, Label } from 'cc';
import { Auto_buyPower } from './Auto_buyPower';
import { ItemNumType, StatisticsType } from '../../game/GameData';
import MTools from '../../../mx/tools/MTools';
const { ccclass } = _decorator;
/** 观看广告领取体力 */
const VIDEO_VIGOUR = 15;
/** 分享领取体力 */
const SHARE_VIGOUR = 5;
/** 分享冷却(毫秒) */
const SHARE_COOLDOWN_MS = 20 * 60 * 1000;
@ccclass('UIbuyPower')
export class UIbuyPower extends Auto_buyPower {
/** ui_shareTime/time 倒计时标签 */
private _shareTimeLab: Label | null = null;
onLoad(): void {
super.onLoad();
this._shareTimeLab = find('ui_center/ui_shareTime/time', this.node)?.getComponent(Label) ?? null;
if (!this._shareTimeLab) {
console.log('[UIbuyPower] ui_shareTime/time Label 缺失');
}
}
onEnable(): void {
super.onEnable();
this._refreshShareCooldownView();
this.schedule(this._tickShareCooldown, 1);
}
onDisable(): void {
this.unschedule(this._tickShareCooldown);
super.onDisable();
}
onInit(): void {
this._refreshShareCooldownView();
}
click_bgMask(): void {
this.close(2);
}
click_btnClose(): void {
this.close(2);
}
/** 观看广告领取 10 体力 */
click_btnVideo(): void {
gg.sdk.requestSubscribeVigourRecovery();
gg.sdk.showVideoAd(this._onVideoAdResult.bind(this));
}
/** 分享领取 5 体力(20 分钟冷却) */
click_btnShare(): void {
if (!this._canShareNow()) {
gg.ui.showToast('分享冷却中,请稍后再试');
this._refreshShareCooldownView();
return;
}
gg.sdk.requestSubscribeVigourRecovery();
gg.sdk.share(2, this._onShareResult.bind(this));
}
/** 广告回调 */
private _onVideoAdResult(res: boolean): void {
if (!res) {
gg.ui.showToast('观看视频失败');
return;
}
gg.sdk.reportDY('inLevel', `体力-AD-广告获得体力`);
gg.data.addItemNum(ItemNumType.vigour, VIDEO_VIGOUR);
gg.ui.showToast(gg.lang.get('体力增加{1}', [VIDEO_VIGOUR]), false);
}
/** 分享回调 */
private _onShareResult(res: boolean): void {
if (!res) {
gg.ui.showToast('分享失败');
return;
}
gg.sdk.reportDY('inLevel', '体力-分享获得体力');
/** 复用 buyPowerMoneyCount 存上次分享时间戳(毫秒) */
gg.data.setStatistics(StatisticsType.buyPowerMoneyCount, Date.now());
gg.data.addItemNum(ItemNumType.vigour, SHARE_VIGOUR);
gg.ui.showToast(gg.lang.get('体力增加{1}', [SHARE_VIGOUR]), false);
this._refreshShareCooldownView();
}
/** 每秒刷新分享冷却展示 */
private _tickShareCooldown(): void {
this._refreshShareCooldownView();
}
/** 刷新 ui_shareTime 显隐与 mm:ss 倒计时 */
private _refreshShareCooldownView(): void {
const remainMs = this._getShareCooldownRemainMs();
const show = remainMs > 0;
if (this.ui_shareTime?.isValid) {
this.ui_shareTime.active = show;
}
if (!show) return;
if (this._shareTimeLab?.isValid) {
this._shareTimeLab.string = MTools.formatTimeString(Math.ceil(remainMs / 1000), 'mm:ss');
}
}
/** 是否已过分享冷却 */
private _canShareNow(): boolean {
return this._getShareCooldownRemainMs() <= 0;
}
/** 分享冷却剩余毫秒;0 表示可分享 */
private _getShareCooldownRemainMs(): number {
const lastShareTs = gg.data.getStatistics(StatisticsType.buyPowerMoneyCount);
if (!(lastShareTs > 1_000_000_000_000)) return 0;
const elapsed = Date.now() - lastShareTs;
if (elapsed >= SHARE_COOLDOWN_MS) return 0;
return SHARE_COOLDOWN_MS - elapsed;
}
}