| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247 |
- import { _decorator, Component, Node, Sprite, SpriteFrame, Label, EventHandler, Button, RichText } from 'cc';
- import { RewardVideoSystem } from '../ad/RewardVideoSystem';
- import { DataSystem } from '../core/data/DataSystem';
- import { Http } from '../core/net/Http';
- import { HttpSystem } from '../core/net/HttpSystem';
- import { ResourceLoader } from '../core/resourceManager/ResourceLoader';
- import { WindowOpenMode } from '../core/ui/window/WindowOpenMode';
- import { WindowSystem } from '../core/ui/window/WindowSystem';
- import { HttpErrorCode } from '../data/HttpErrorCode';
- import { ADData } from '../data/jsb/ADData';
- import { RedPointData } from '../main/RedPointData';
- import { ReportThinking } from '../ReportThinking';
- const { ccclass, property, requireComponent } = _decorator;
- @ccclass('ExpropriationGrainItem')
- @requireComponent(ResourceLoader)
- export class ExpropriationGrainItem extends Component {
- @property({ tooltip: "背景图", type: Sprite }) private bg: Sprite;
- @property({ tooltip: "奖励值", type: Label }) private prizeNum: Label;
- @property({ tooltip: "征收按钮", type: Node }) private toGetBtn: Node;
- @property({ tooltip: "征收按钮免费文本", type: Node }) private toGetBtnFree: Node;
- @property({ tooltip: "征收按钮视频文本", type: Node }) private toGetBtnVideo: Node;
- @property({ tooltip: "领取按钮", type: Node }) private getBtn: Node;
- @property({ tooltip: "加速按钮", type: Node }) private quickenBtn: Node;
- @property({ tooltip: "倒计时文本", type: Label }) private timeTxt: Label;
- @property({ tooltip: "配置所需时间文本", type: Node }) private configTime: Node;
- @property({ tooltip: "加速按钮文本", type: Label }) private quickTxt: Label;
- private index = 0;
- private isHadToGet = false;//是否有出征状态
- public initToGetState: EventHandler;
- public timesTxt: RichText;//次数文本
- public finishTime: number = 0;//征粮结束时间
- start() {
- // [3]
- }
- public async initUI(data: { index: number, rewardNum: number, time: number, finishTime?: number, isReceive?: boolean }) {
- this.prizeNum.string = data.rewardNum + "";
- this.configTime.getComponent(Label).string = this.formatTime(data.time * 1000);
- this.index = data.index;
- this.bg.spriteFrame = await this.getComponent(ResourceLoader).load<SpriteFrame>("image/getGrain/banner" + data.index + "/spriteFrame", SpriteFrame);
- if (!data.finishTime) {
- this.initToGetState && this.initToGetState.emit([this.index, 0]);
- return;
- }
- let date = HttpSystem.serverTime / 1000;
- if (date >= data.finishTime) {
- if (!data.isReceive) {
- this.toGetBtn.active = false;
- this.configTime.active = false;
- this.getBtn.active = true;
- this.timeTxt.string = "已完成";
- }
- this.initToGetState && this.initToGetState.emit([this.index, 0]);
- } else {
- this.toGetBtn.active = false;
- this.configTime.active = false;
- this.quickenBtn.active = true;
- this.finishTime = data.finishTime;
- this.initToGetState && this.initToGetState.emit([this.index, 1]);
- }
- }
- public initTogetBtn(isToGet: boolean) {
- this.toGetBtnFree.active = !isToGet;
- this.toGetBtnVideo.active = isToGet;
- this.isHadToGet = isToGet;
- }
- //征粮
- public async toGetGrain() {
- this.toGetBtn.getComponent(Button).interactable = false;
- if (this.isHadToGet) {
- let result = await this.getComponent(Http).send("/api/ad/watchVideoAD");
- if (result && result.code == 0) {
- this.ad_init();
- let adData = await RewardVideoSystem.show();
- if (adData) {//视频观看成功
- this.requestToGetGrain(adData);
- } else {
- //视频观看失败
- WindowSystem.showTips("视频观看时间不足");
- this.toGetBtn.getComponent(Button).interactable = true;
- }
- } else {//申请看视频回复错误信息
- WindowSystem.showTips("视频次数不足");
- this.toGetBtn.getComponent(Button).interactable = true;
- }
- } else {
- this.requestToGetGrain()
- }
- }
- private async requestToGetGrain(adData?: ADData) {
- let data = adData ? { index: this.index, adData: adData.obj } : { index: this.index };
- let resul = await this.getComponent(Http).send("/api/grainLevies/StartGrainLevies", data);
- if (resul && resul.code == 0) {
- adData && this.ad_end();
- !adData && ReportThinking.field();
- this.timesTxt && (this.timesTxt.string = "<b><color=#FFFFFF>剩余次数:</color><color=#DA5E5F>" + resul.data.times + "</color>");
- this.finishTime = resul.data.finishTime;
- //初始化界面
- this.toGetBtn.active = false;
- this.configTime.active = false;
- this.quickenBtn.active = true;
- this.initToGetState && this.initToGetState.emit([this.index, 1]);
- DataSystem.getData(RedPointData).grainLevies.times = resul.data.times;//更新红点数据,剩余出征次数
- DataSystem.getData(RedPointData).grainLevies = DataSystem.getData(RedPointData).grainLevies;
- } else {//服务器返回错误消息
- if (resul && resul.code == HttpErrorCode.NoTimes) {
- WindowSystem.showTips("征粮次数不足");
- } else if (resul && resul.code == HttpErrorCode.VideoADCD) {
- WindowSystem.showTips('视频观看时间不足');
- } else {
- WindowSystem.showTips("征粮失败");
- }
- }
- this.toGetBtn.getComponent(Button).interactable = true;
- }
- //领取征粮粮草
- public async getGrain() {
- this.getBtn.getComponent(Button).interactable = false;
- let resul = await this.getComponent(Http).send("/api/grainLevies/ReceiveGrainLevies", { index: this.index });
- if (resul && resul.code == 0) {//领取粮草
- await WindowSystem.open("prefabs/ui/turntable/turntablePrizePopup", WindowOpenMode.NotCloseAndCover, [{ icon: 4, type: 1, num: resul.data }]);
- //初始化界面
- this.getBtn.active = false;
- this.toGetBtn.active = true;
- this.configTime.active = true;
- this.timeTxt.string = "";
- this.redPointChange(true);
- }
- this.getBtn.getComponent(Button).interactable = true;
- }
- //cd减半
- public async quicken() {
- this.quickenBtn.getComponent(Button).interactable = false;
- let result = await this.getComponent(Http).send("/api/ad/watchVideoAD");
- if (result && result.code == 0) {
- let adData = await RewardVideoSystem.show();
- this.ad_init(true);
- if (adData) {//视频观看成功
- let _result = await this.getComponent(Http).send("/api/grainLevies/HalfGrainLevies", { index: this.index, adData: adData.obj });
- if (_result && _result.code == 0) {
- this.ad_end(true);
- this.finishTime = _result.data;
- } else if (_result && _result.code == HttpErrorCode.VideoADCD) {
- WindowSystem.showTips("视频观看时间不足");
- }
- } else {
- WindowSystem.showTips("视频观看时间不足");
- }
- } else {
- WindowSystem.showTips("视频次数不足");
- }
- this.quickenBtn.getComponent(Button).interactable = true;
- }
- /**
- * 格式化倒计时
- */
- public formatCountDown(time: number): string {
- let ss = Math.floor(time / 1000);
- let day = Math.floor(ss / 3600 / 24);
- let h = Math.floor(ss / 3600 % 24) < 10 ? "0" + Math.floor(ss / 3600 % 24) : Math.floor(ss / 3600 % 24);
- let m = Math.floor(ss / 60 % 60) < 10 ? "0" + Math.floor(ss / 60 % 60) : Math.floor(ss / 60 % 60);
- let s = Math.floor(ss % 60) < 10 ? "0" + Math.floor(ss % 60) : Math.floor(ss % 60);
- let result = "";
- if (day > 0) {
- result = day + "天" + h + ":" + m + ":" + s;
- } else {
- result = h + ":" + m + ":" + s;
- }
- return result;
- }
- /**
- * 时间格式化
- */
- public formatTime(time: number): string {
- let ss = Math.floor(time / 1000);
- let day = Math.floor(ss / 3600 / 24);
- let h = Math.floor(ss / 3600 % 24);
- let m = Math.floor(ss / 60 % 60);
- let s = Math.floor(ss % 60);
- let result = "";
- day > 0 && (result = day + "天");
- h > 0 && (result = result + h + "小时");
- m > 0 && (result = result + m + "分钟");
- s > 0 && (result = result + s + "秒");
- return result;
- }
- update() {
- let _time = Math.round(HttpSystem.serverTime / 1000);
- if (_time <= this.finishTime) {
- this.timeTxt.string = this.formatCountDown((this.finishTime - _time) * 1000);
- if (this.quickTxt.string != "-50%时间" && (this.finishTime - _time > (60 * 30))) {
- this.quickTxt.string = "-50%时间";
- } else if (this.quickTxt.string != "立即完成" && (this.finishTime - _time <= (60 * 30))) {
- this.quickTxt.string = "立即完成";
- }
- } else {
- if (this.timeTxt.string != "已完成" && this.timeTxt.string != "") {
- this.timeTxt.string = "已完成";
- this.quickenBtn.active = false;
- this.getBtn.active = true;
- this.initToGetState && this.initToGetState.emit([this.index, 0]);
- this.redPointChange(false);
- }
- }
- }
- //修改红点已领取数据
- private redPointChange(isReceive: boolean) {
- for (let i = 0; i < DataSystem.getData(RedPointData).grainLevies.leviesDatas.length; i++) {
- if (DataSystem.getData(RedPointData).grainLevies.leviesDatas[i].index == this.index) {
- DataSystem.getData(RedPointData).grainLevies.leviesDatas[i].isReceive = isReceive;
- DataSystem.getData(RedPointData).grainLevies = DataSystem.getData(RedPointData).grainLevies;
- break;
- }
- }
- }
- /**上报数数 ad_init*/
- private ad_init(isCd: boolean = false) {
- let p_name = this.getName(isCd);
- ReportThinking.ad_init(p_name);
- }
- /**上报数数 ad_end*/
- private ad_end(isCd: boolean = false) {
- let p_name = this.getName(isCd);
- ReportThinking.ad_end(p_name);
- }
- private getName(isCd: boolean) {
- let p_name = '';
- switch (this.index) {
- case 0: p_name = isCd ? 'skip_5min' : 'collect_5min'; break;
- case 1: p_name = isCd ? 'skip_15min' : 'collect_15min'; break;
- case 2: p_name = isCd ? 'skip_1hour' : 'collect_1hour'; break;
- case 3: p_name = isCd ? 'skip_12hour' : 'collect_12hour'; break;
- }
- return p_name;
- }
- }
|