import { _decorator, Component, Node, Label, Animation, Button } from 'cc'; import { ADHelper } from '../ad/ADHelper'; import { RewardVideoSystem } from '../ad/RewardVideoSystem'; import { DataSystem } from '../core/data/DataSystem'; import { Http, HttpResponseCode } from '../core/net/Http'; import { HttpSystem } from '../core/net/HttpSystem'; import { ResourceLoader } from '../core/resourceManager/ResourceLoader'; import { Sound } from '../core/sound/Sound'; import { WindowOpenMode } from '../core/ui/window/WindowOpenMode'; import { WindowSystem } from '../core/ui/window/WindowSystem'; import { HttpErrorCode } from '../data/HttpErrorCode'; import { platform } from '../data/jsb/platform'; import { UserData } from '../data/UserData'; import { NoviceGuideData } from '../guide/NoviceGuideData'; import { ReportThinking } from '../ReportThinking'; import { Task } from '../task/Task'; import { TaskData } from '../task/TaskData'; import { LevelBonusData } from './LevelBonusData'; const { ccclass, property, requireComponent } = _decorator; @ccclass('RedEnvelopes') @requireComponent(ResourceLoader) export class RedEnvelopes extends Component { @property({ tooltip: "奖励金额", type: Label }) private prizeNum: Label; @property({ tooltip: "普通领取", type: Node }) private toGet: Node; @property({ tooltip: "视频领取", type: Node }) private videoGet: Node; @property({ tooltip: "视频icon", type: Node }) private videoIcon: Node; @property({ tooltip: "视频按钮文本", type: Label }) private videoBtnTxt: Label; @property({ tooltip: "任务红包普通领取按钮", type: Node }) private txtGetBtn: Node; @property({ tooltip: "插屏广告", type: ADHelper }) private adNode: ADHelper; @property({ tooltip: "普通红包文本(开封前)", type: Node }) private envelopesTxt: Node; @property({ tooltip: "新手红包文本(开封前)", type: Node }) private newEnvelopesTxt: Node; @property({ tooltip: "新手红包图标", type: Node }) private newIcon: Node; @property({ tooltip: "开红包动画", type: Animation }) private openAnim: Animation; @property({ tooltip: "背景", type: Node }) private bg: Node; @property({ tooltip: "关卡红包关闭按钮", type: Node }) private closeBtn: Node; @property({ tooltip: "手指动画", type: Node }) private handAnim: Node; @property({ tooltip: "自动领取关卡红包倒计时", type: Label }) private countDownTxt: Label; private prizenum = 0;//奖励数值 private prizeType = 0;//奖励类型 private activeId = 0;//任务红包id private closeTime = 10 * 1000;//倒计时自动关闭时间,单位毫秒 private isCountDown = false;//是否倒计时 private endTime = 0;//倒计时结束实际点 start() { } /** * 窗口打开回调 * @param data 数据,type:(0:直接领取,1:任务红包普通领取,2:任务红包二倍领取,3:任务红包三倍领取,4:新手红包,5:关卡红包掉落直接弹窗),num:红包币数目,isShowBtn:任务红包普通领取按钮是否显示 */ public setData(data: { type: number, num: number, id: number, isShowBtn: boolean }) { data.id && (this.activeId = data.id); this.prizeNum.string = "X" + data.num; this.prizeType = data.type; switch (data.type) { case 0://关卡红包 this.toGet.active = true; this.prizenum = data.num; this.openAnim.play(); break; case 1: this.videoGet.active = true; this.videoIcon.active = false; this.videoBtnTxt.string = "领取奖励"; this.openAnim.play(); break; case 2: this.videoGet.active = true; this.videoBtnTxt.string = "双倍领取"; this.openAnim.play(); break; case 3: this.videoGet.active = true; this.videoBtnTxt.string = "三倍领取"; this.openAnim.play(); break; case 4://新手红包 this.toGet.active = true; this.envelopesTxt.active = false; this.newEnvelopesTxt.active = true; this.newIcon.active = true; break; case 5: this.toGet.active = true; // this.bg.getComponent(Button).interactable = true; this.closeBtn.active = true; this.handAnim.active = true; this.handAnim.getComponent(Animation).play(); //倒计时 this.startCountDown(); break; default: WindowSystem.close(this.node); break; } data.isShowBtn && (this.txtGetBtn.active = data.isShowBtn); } private isPlay = false;//是否正在播放开红包动画 //手动打开红包 public async openRed() { if (this.prizeType == 4 && !this.isPlay) { platform.reportThinking("guide", JSON.stringify({ guide_id: 'first_gift' })); this.isPlay = true; this.openAnim && this.openAnim.play(); } if (this.prizeType == 5 && !this.isPlay) { this.isCountDown = false; this.isPlay = true; this.closeBtn.active = false; // this.bg.getComponent(Button).interactable = false; let result = await this.getComponent(Http).send("/api/ad/watchVideoAD"); if (result && result.code == 0) { ReportThinking.ad_init('red_envelope'); let adData = await RewardVideoSystem.show(); if (adData) {//观看视频成功 let _result = await this.getComponent(Http).send("/api/battle/receiveBonus", { adData: adData.obj }); if (_result && _result.code == 0) {//打开红包成功 ReportThinking.ad_end('red_envelope'); this.prizeNum.string = "X" + _result.data; this.prizenum = _result.data; this.prizeType = 0; this.handAnim.getComponent(Animation).stop(); this.handAnim.active = false; this.openAnim && this.openAnim.play(); this.countDownTxt.string = ""; } else if (_result && _result.code == HttpErrorCode.VideoADCD) { WindowSystem.showTips('视频观看时间不足'); this.closeBtn.active = true; // this.bg.getComponent(Button).interactable = true; this.isPlay = false; this.startCountDown(); } else { this.closeBtn.active = true; // this.bg.getComponent(Button).interactable = true; this.isPlay = false; this.startCountDown(); WindowSystem.showTips('红包打开失败'); } } else {//观看视频失败 WindowSystem.showTips('视频观看时间不足'); this.closeBtn.active = true; // this.bg.getComponent(Button).interactable = true; this.isPlay = false; this.startCountDown(); } } else {//返回错误信息,无法观看视频 WindowSystem.showTips('视频次数不足'); this.closeBtn.active = true; // this.bg.getComponent(Button).interactable = true; this.isPlay = false; this.startCountDown(); } } } //视频双倍领取 public async openVideo() { let result = await this.getComponent(Http).send("/api/ad/watchVideoAD"); if (result && result.code == 0) { let userData = DataSystem.getData(UserData); ReportThinking.ad_init('taskredpackt_get'); let adData = await RewardVideoSystem.show(); if (adData) {//观看视频成功 this.onGetGift(true, adData.obj); } else {//观看视频失败 WindowSystem.showTips('视频观看时间不足'); } } } //普通领取 public async toGetPrize() { this.onGetGift(); } //背景点击事件 private bgOnTouch() { if (this.prizeType == 1 || this.prizeType == 2 || this.prizeType == 3) { WindowSystem.close(this.node); return; } this.onGetGift(); } private async onGetGift(isDouble = false, adData?: any) { this.isCountDown = false;//停止倒计时 if (this.prizeType && this.prizeType != 4 && this.prizeType != 5) { let data; if (adData) { data = { id: this.activeId, isDouble: isDouble, adData: adData }; } else { data = { id: this.activeId, isDouble: isDouble }; } let result = await this.getComponent(Http).send('/api/task/ReceiveTaskActive', data); if (result && result.code == HttpResponseCode.Success) { if (result.data.rewardType == 'bonus') { this.prizenum = result.data.rewardNum; let userData = DataSystem.getData(UserData); adData && ReportThinking.ad_end('taskredpackt_get', this.prizenum); let activitys = DataSystem.getData(TaskData).activitys; for (let i = 0; i < activitys.length; i++) { if (activitys[i].id == this.activeId) { !isDouble && (activitys[i].isReceiveSingle = true); isDouble && (activitys[i].isReceiveDouble = true); } } DataSystem.getData(TaskData).getActiveBoxGiftNum++; } WindowSystem.close(this.node); } } else { if (this.prizeType == 4) { let result = await this.getComponent(Http).send("/api/user/ReceiveNewUserBonus"); if (result && result.code == 0) { platform.reportThinking("guide", JSON.stringify({ guide_id: 'first_claim' })); this.prizenum = result.data; await WindowSystem.open("prefabs/ui/firstHero/firstHero", WindowOpenMode.NotCloseAndAdd); } else if (result && result.code == HttpErrorCode.HadReceive) { WindowSystem.showTips('已领取'); await WindowSystem.open("prefabs/ui/firstHero/firstHero", WindowOpenMode.NotCloseAndAdd); } else { return; } } if (this.prizeType == 5) { DataSystem.getData(LevelBonusData).saveTimes++; adData && ReportThinking.ad_end('taskredpackt_get', this.prizenum); await WindowSystem.open("prefabs/ui/turntable/prizeFly", WindowOpenMode.NotCloseAndAdd, [{ type: 4, num: 1 }]); } WindowSystem.close(this.node); } } async onDestroy() { this.prizenum && await WindowSystem.open("prefabs/ui/turntable/prizeFly", WindowOpenMode.NotCloseAndAdd, [{ type: 3, num: this.prizenum }]); !this.prizeType && (Math.random() * 100) <= 20 && this.adNode.show(); } update() { if (this.isCountDown) { let time = Date.now(); if (time < this.endTime) { this.closeTime = this.endTime - time; this.countDownTxt.string = Math.round(this.closeTime / 1000) + "秒自动关闭"; } else if (this.isCountDown) { this.isCountDown = false; this.countDownTxt.string = "自动关闭"; this.toGetPrize(); } //新手引导自动关闭红包 if (DataSystem.watch(NoviceGuideData, "isShow")) { DataSystem.getData(NoviceGuideData).isShow && this.toGetPrize(); } } //新手引导自动关闭红包 if (this.bg.getComponent(Button).interactable && DataSystem.watch(NoviceGuideData, "isShow")) { DataSystem.getData(NoviceGuideData).isShow && this.bgOnTouch(); } } /** * 开始倒计时 */ private startCountDown() { this.endTime = Date.now() + this.closeTime; this.isCountDown = true; } }