123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232 |
- 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 = require('../utils/GameEnum').GameNotificationKey;
- const GameRedDot = require('../utils/GameEnum').GameRedDot;
- const TapTapTool = require("../utils/TapTapTool");
- 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,
- },
- // LIFE-CYCLE CALLBACKS:
- onLoad () {
- this.setUpUIIsShow(false);
- if (Global.isCheck) {
- this.shareActionNode.active = false;
- }
- this.getDrawState().then((respondData) => {
- this.setUpUIIsShow(true);
- this.respondData = respondData;
- this.setUpUI();
- }).catch(({code, msg}) => {
- console.log(code, msg);
- });
-
- GameEvent.on(GameNotificationKey.GameShowNotificationKey, this, this.updatelData);
- },
- onDestroy() {
- GameEvent.off(GameNotificationKey.GameShowNotificationKey, 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 () {
- },
- 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;
- this.normalRichText.string = '<b><color=#ffffff>分享\n免费获得</c></b>';
- },
- /// 网络请求
- getDrawState() {
- 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.startLottery(typeId, diamond, (respondData) => {
- resolve(respondData);
- }, (code, msg) => {
- reject({code, msg});
- });
- })
- },
- startDraw(typeId, completion) {
- this.startDrawRequest(typeId, 0).then((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);
- Global.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);
- }
- 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) {
- this.normalDrawButton.interactable = false;
- WeChat.shareAction( () => {
- this.startDraw(1, () => {
- this.normalDrawButton.interactable = true;
- TapTapTool.removeRedDot(GameRedDot.draw);
- });
- }, () => {
- this.normalDrawButton.interactable = true;
- console.log('分享失败或取消');
- });
- }
- },
- ///更多砖石抽奖
- moreChanceNodeAction() {
- GameModule.audioMng.playClickButton();
- this.moreChanceButton.interactable = false;
- this.startDraw(2, (isSuccess) => {
- this.moreChanceButton.interactable = true;
- if (isSuccess) {
- GameModule.userInfo.diamond -= this.respondData.diamond2;
- }
- });
- },
-
- ///最大概率抽奖
- bestChanceNodeAction () {
- GameModule.audioMng.playClickButton();
- this.bestChanceButton.interactable = false;
- this.startDraw(3, (isSuccess) => {
- this.bestChanceButton.interactable = true;
- if (isSuccess) {
- GameModule.userInfo.diamond -= this.respondData.diamond3;
- }
- });
- },
- 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;
- }
- });
- },
- // update (dt) {},
- });
|