123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311 |
- const AlertManager = require('../utils/AlertManager');
- const DrawApi = require("../net/DrawApi");
- const DWTool = require("../utils/DWTool");
- const GameModule = require("../utils/GameModule");
- const WeChat = require('../net/WeChat');
- const {GameNotificationKey, WechatShareType, GameRedDot } = require('../utils/GameEnum');
- const TapTapTool = require("../utils/TapTapTool");
- var Promise = require('../lib/es6-promise').Promise;
- cc.Class({
- extends: cc.Component,
- properties: {
- moreRichText: cc.RichText,
- bestRichText: cc.RichText,
- normalRichText: cc.RichText,
- normalDrawButton: cc.Button,
- bestChanceButton: cc.Button,
- moreChanceButton: cc.Button,
- normalDrawButtonSprite: cc.Sprite,
- bestChanceButtonSprite: cc.Sprite,
- moreChanceButtonSprite: cc.Sprite,
- content: cc.Node,
- shareActionNode: cc.Node,
- drawMoneyLabel: cc.RichText,
- drawRecordButton: cc.Button,
- /// 1视频广告 0普通分享
- _adState: 0
- },
- // LIFE-CYCLE CALLBACKS:
- onLoad () {
- ///头条隐藏提现
- if (window.tt !== undefined) {
- this.drawRecordButton.node.active = false;
- }
- this.setUpUIIsShow(false);
- if (GameGlobal.isCheck) {
- this.shareActionNode.active = false;
- }
- this.setupNotification();
- this.getDrawState().then((respondData) => {
- this.setUpUIIsShow(true);
- this.respondData = respondData;
- this.setUpUI();
- this.initDrawAd();
- }).catch(({code, msg}) => {
- console.log(code, msg);
- });
- this.drawMoneyLabel.string = `<b><color=#fec808>¥${(GameGlobal.userData.hb / 100).toFixed(2)}</c></b>`;
- },
- onDestroy() {
- GameEvent.off(GameNotificationKey.GameShowNotificationKey, this);
- GameEvent.off(GameNotificationKey.ShowShareAction, this);
- GameEvent.off(GameNotificationKey.AdUpdateStateNotification, this);
- },
- updatelData() {
- // cdTime 已经用了的CD时间 如果 如果为0或者大于和等于总时间 说明可以免费抽奖
- // cdTotalTime cd总时间 ,目前是60分钟
- this.getDrawState().then((respondData) => {
- this.respondData = respondData;
- this.updateUI();
- }).catch(({code, msg}) => {
- console.log(code, msg);
- });
- },
- start () {
- },
- setupNotification() {
- GameEvent.on(GameNotificationKey.AdUpdateStateNotification, this, (adState, callBack) => {
- /// 说明是技能的关闭状态
- if (adState === 3 && callBack === 'draw') {
- this.shareActionCallback();
- }
- if (adState === 0 || adState === 1) {
- this.initDrawAd();
- }
- });
- GameEvent.on(GameNotificationKey.GameShowNotificationKey, this, this.updatelData);
- },
- setUpUIIsShow(isShow) {
- this.normalDrawButton.node.active = isShow;
- this.bestChanceButton.node.active = isShow;
- this.moreChanceButton.node.active = isShow;
- },
- updateUI() {
- /// 抽奖冷却结束
- if (this.respondData.cdTime >= this.respondData.cdTotalTime) {
- this.setUpNormalButton();
- } else {
- this.setUpUseBtnBg(false, 1);
- let text = DWTool.calculateTime((this.respondData.cdTotalTime - this.respondData.cdTime) / 1000);
- this.normalRichText.string = '<b><color=#ffffff>' + text + '</c></b>';
- }
- },
- setUpUI() {
- /// 抽奖冷却结束
- if (this.respondData.cdTime >= this.respondData.cdTotalTime) {
- this.setUpNormalButton();
- } else {
- this.setUpUseBtnBg(false, 1);
- let text = DWTool.calculateTime((this.respondData.cdTotalTime - this.respondData.cdTime) / 1000);
- this.normalRichText.string = '<b><color=#ffffff>' + text + '</c></b>';
- this.schedule(this.updateCD, 1);
- }
- this.moreRichText.string = `<img src='skill_diamond'/><b><color=#ffffff> ${this.respondData.diamond2}</c></b>`;
- this.bestRichText.string = `<img src='skill_diamond'/><b><color=#ffffff> ${this.respondData.diamond3}</c></b>`;
-
- let hasDiamod = GameModule.userInfo.diamond;
- if (hasDiamod < this.respondData.diamond2) {
- this.setUpUseBtnBg(false, 2);
- }
-
- if (hasDiamod < this.respondData.diamond3) {
- this.setUpUseBtnBg(false, 3);
- }
- },
- setUpNormalButton() {
- this.normalDrawButton.interactable = true;
- if (this._adState === 0) {
- this.normalRichText.string = '<b><color=#ffffff>分享\n免费获得</c></b>';
- } else if (this._adState === 1) {
- this.normalRichText.string = '<b><color=#ffffff>看视频\n免费获得</c></b>';
- }
- },
- /// 网络请求
- getDrawState(success, fail) {
-
- return new Promise((resolve, reject) => {
- DrawApi.getLotteryInfo((respondData) => {
- resolve(respondData);
- }, (code, msg) => {
- reject({code, msg});
- });
- })
- },
- startDrawRequest(typeId, diamond = 0) {
- return new Promise((resolve, reject) => {
- ///开始抽奖
- DrawApi.startNewLottery(typeId, diamond, (respondData) => {
- resolve(respondData);
- }, (code, msg) => {
- reject({code, msg});
- });
- })
- },
- startDraw(typeId, completion) {
- this.startDrawRequest(typeId, 0).then((respondData) => {
- /// 把抽奖状态 以及抽奖成功之后的数据加上去
- this.respondData.drawSuccessData = respondData;
- // this.respondData.propId = respondData.propId;
- // this.respondData.propName = respondData.propName;
- // this.respondData.roomId = respondData.roomId;
- this.closeNodeAction();
- completion(true);
- AlertManager.showDrawScrollAlert(this.respondData, typeId);
- }).catch(({code, msg}) => {
- console.log(code, msg);
- completion(false);
- GameGlobal.commonAlert.showCommonErrorAlert(msg);
- });
- },
- updateCD() {
- this.respondData.cdTime += 1000;
- if (this.respondData.cdTime >= this.respondData.cdTotalTime) {
- this.setUpNormalButton();
- this.setUpUseBtnBg(true, 1);
- this.unschedule(this.updateCD, this);
- } else {
- let text = DWTool.calculateTime((this.respondData.cdTotalTime - this.respondData.cdTime) / 1000);
- this.normalRichText.string = '<b><color=#ffffff>' + text + '</c></b>';
- }
- },
- /// 看广告抽奖
- adNodeAction() {
- GameModule.audioMng.playClickButton();
- if (CC_WECHATGAME || CC_QQPLAY) {
- if (this._adState === 0) {
- GameEvent.on(GameNotificationKey.ShowShareAction, this, (type, isOk) => {
- if (type != WechatShareType.DrawLottery) { return; }
- if (isOk) {
- this.shareActionCallback();
- } else {
- this.normalDrawButton.interactable = true;
- }
- GameEvent.off(GameNotificationKey.ShowShareAction, this);
- });
- this.normalDrawButton.interactable = false;
- WeChat.shareAction(WechatShareType.DrawLottery, () => {
-
- }, () => {
- this.normalDrawButton.interactable = true;
- console.log('分享失败或取消');
- });
- } else {
- GameGlobal._adVideo.showVideo('draw');
- }
- }
- },
- shareActionCallback() {
- this.normalDrawButton.interactable = false;
- this.startDraw(1, (isSuccess) => {
- if (isSuccess) {
- TapTapTool.removeRedDot(GameRedDot.draw);
- } else {
- this.normalDrawButton.interactable = true;
- }
- });
- if (this._adState === 0) {
- GameEvent.off(GameNotificationKey.ShowShareAction, this);
- }
- },
- ///更多砖石抽奖
- moreChanceNodeAction() {
- GameModule.audioMng.playClickButton();
- this.moreChanceButton.interactable = false;
- this.startDraw(2, (isSuccess) => {
- if (isSuccess) {
- GameModule.userInfo.diamond -= this.respondData.diamond2;
- } else {
- this.moreChanceButton.interactable = true;
- }
- });
- },
-
- ///最大概率抽奖
- bestChanceNodeAction () {
- GameModule.audioMng.playClickButton();
- this.bestChanceButton.interactable = false;
- this.startDraw(3, (isSuccess) => {
- if (isSuccess) {
- GameModule.userInfo.diamond -= this.respondData.diamond3;
- } else {
- this.bestChanceButton.interactable = true;
- }
- });
- },
- closeNodeAction() {
- // let finish = cc.callFunc(() => {
- // this.node.destroy();
- // }, this);
- // let sequence = cc.sequence(cc.scaleTo(0.2, 0, 0).easing(cc.easeBackIn()), finish)
- // this.content.runAction(sequence);
- this.node.destroy();
- GameModule.audioMng.playClickButton();
- },
- /// 设置button是否能点击 type 123
- setUpUseBtnBg(isActive, type) {
- let path = isActive ? './textures/draw/draw_green_btn_bg' : './textures/draw/draw_gray_btn_bg';
- DWTool.loadResSpriteFrame(path)
- .then((spriteFrame) => {
- if (type == 1) {
- this.normalDrawButtonSprite.spriteFrame = spriteFrame;
- this.normalDrawButton.interactable = isActive;
- } else if (type == 2) {
- this.moreChanceButtonSprite.spriteFrame = spriteFrame;
- this.moreChanceButton.interactable = isActive;
- } else {
- this.bestChanceButtonSprite.spriteFrame = spriteFrame;
- this.bestChanceButton.interactable = isActive;
- }
- });
- },
- showDrawRedAction() {
- AlertManager.showDrawRedRecordAlert();
- },
-
- initDrawAd() {
- if (!CC_WECHATGAME) {
- return;
- }
- //// 说明有广告
- if (GameGlobal._adVideoState == 0) {
- this._adState = 1;
- } else if (GameGlobal._adVideoState === 1) {
- this._adState = 0;
- }
- /// 如果时间到了 那么更新显示的UI
- if (this.respondData.cdTime >= this.respondData.cdTotalTime) {
- this.setUpNormalButton();
- }
- },
- // update (dt) {},
- });
|