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,
},
// LIFE-CYCLE CALLBACKS:
onLoad () {
this.setUpUIIsShow(false);
if (GameGlobal.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);
GameEvent.off(GameNotificationKey.ShowShareAction, 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 = '' + text + '';
}
},
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 = '' + text + '';
this.schedule(this.updateCD, 1);
}
this.moreRichText.string = ` ${this.respondData.diamond2}`;
this.bestRichText.string = ` ${this.respondData.diamond3}`;
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 = '分享\n免费获得';
},
/// 网络请求
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.startLottery(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);
this.normalDrawButton.interactable = true;
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);
}
let text = DWTool.calculateTime((this.respondData.cdTotalTime - this.respondData.cdTime) / 1000);
this.normalRichText.string = '' + text + '';
},
/// 看广告抽奖
adNodeAction() {
GameModule.audioMng.playClickButton();
if (CC_WECHATGAME || CC_QQPLAY) {
GameEvent.on(GameNotificationKey.ShowShareAction, this, (type) => {
this.shareActionCallback(type);
});
this.normalDrawButton.interactable = false;
WeChat.shareAction(WechatShareType.DrawLottery, () => {
}, () => {
this.normalDrawButton.interactable = true;
console.log('分享失败或取消');
});
}
},
shareActionCallback(type) {
if (type != WechatShareType.DrawLottery) { return; }
this.startDraw(1, () => {
this.normalDrawButton.interactable = true;
TapTapTool.removeRedDot(GameRedDot.draw);
});
GameEvent.off(GameNotificationKey.ShowShareAction, this);
},
///更多砖石抽奖
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;
}
});
},
showDrawRedAction() {
AlertManager.showDrawRedRecordAlert();
}
// update (dt) {},
});