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 = `¥${(GameGlobal.userData.hb / 100).toFixed(2)}`;
},
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 = '' + 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;
if (this._adState === 0) {
this.normalRichText.string = '分享\n免费获得';
} else if (this._adState === 1) {
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.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 = '' + text + '';
}
},
/// 看广告抽奖
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) {},
});