const DWTool = require("../utils/DWTool");
const GameModule = require("../utils/GameModule");
const TapTapTool = require("../utils/TapTapTool");
const AlertManager = require('../utils/AlertManager');
const WeChat = require('../net/WeChat');
const StoreApi = require('../net/StoreApi');
const {GameNotificationKey, WechatShareType, GameRedDot } = require('../utils/GameEnum');
var Promise = require('../lib/es6-promise').Promise;
cc.Class({
extends: cc.Component,
properties: {
diamondNode: cc.Node,
diamondSprite: cc.Sprite,
diamondDoubleSprite: cc.Sprite,
diamondDiscountLabel: cc.Label,
diamondRichText: cc.RichText,
coinNode: cc.Node,
coinLabel: cc.Label,
coinSprite: cc.Sprite,
coinRicheText: cc.RichText,
adNode: cc.Node,
adSprite: cc.Sprite,
adLabel: cc.Label,
adTitleRichText: cc.RichText,
buyBtn: cc.Button,
buyRichText: cc.RichText,
buySprite: cc.Sprite,
_adState: 0
},
// LIFE-CYCLE CALLBACKS:
// onLoad () {},
start () {
},
// buyType 商品购买类型(1人民币购买,2砖石购买,3看广告获得) 是 [int] 查看
// cdTime 剩余的CD时间,单位毫秒(为0就是没有CD 可以继续购买) 是 [long] 查看
// desc 商品描述 是 [string] 查看
// isBuy 是否还可以继续购买(1为已经购买过,不能在购买了),某些商品只能购买一次 是 [int] 查看
// minuteTime 购买的商品有效时间,单位分钟(为0就是永久性商品) 是 [long] 查看
// name 商品名字 是 [string] 查看
// picId 图片ID 是 [int] 查看
// price 商品的价格(人民币或者砖石) 是 [int] 查看
// shopId 商品ID 是 [string] 查看
// type 商品类型(1推荐,2砖石,3金币,4礼包)
/// tabIndex为种类
init(data, tabIndex, index) {
this._tabIndex = tabIndex;
this._index = index;
this._shopData = data;
this.adNode.active = index == 0;
this.coinNode.active = tabIndex == 3 && index !== 0;
this.diamondNode.active = tabIndex == 2 && index !==0;
/// 说明是广告的样式
if (index == 0) {
/// 是砖石
if (tabIndex == 2) {
/// 是金币
} else {
let path = './textures/quest/altas/quest_coin';
DWTool.loadResSpriteFrame(path).then((spriteFrame) => {
this.adSprite.spriteFrame = spriteFrame;
});
let add = TapTapTool.multiple(GameModule.userInfo.rateGold, GameModule.skill.multiple);
add = TapTapTool.multiple({'n': GameModule.shop.multiple, 'e': 0}, add);
let gold = TapTapTool.multiple(TapTapTool.multiple(add, GameModule.userInfo.perpetualMt), {'e': 0, 'n': 36});
this._getGold = gold;
let coinString = TapTapTool.parseToString(gold);
this.adTitleRichText.string = `${coinString}`;
}
GameEvent.on(GameNotificationKey.AdUpdateStateNotification, this, (adState, callBack) => {
/// 说明是技能的关闭状态
if (adState === 3 && callBack === 'store' + this._tabIndex) {
this.shareActionCallback();
}
if (adState === 0 || adState === 1) {
this.initStoreAd();
}
});
this.initStoreAd();
} else {
/// 如果是砖石
if (tabIndex == 2) {
let path = './textures/store/800' + tabIndex + index + '0';
DWTool.loadResSpriteFrame(path).then((spriteFrame) => {
this.diamondSprite.spriteFrame= spriteFrame;
});
if (this._shopData.isBuyDiamond == 1) {
this.diamondDoubleSprite.node.active = false;
} else {
this.diamondDoubleSprite.node.active = index > 1;
}
if (data.shopId <= 10 && data.shopId >= 6) {
let disCountDiamonds = [100, 800, 3000, 10000];
let diamonds = [200, 1000, 4000, 10000, 20000];
this.diamondRichText.string = `${diamonds[data.shopId - 6]}钻石`;
/// 如果是获得两百个钻石
if (data.shopId === 6) {
this.diamondDiscountLabel.node.active = false;
} else {
this.diamondDiscountLabel.string = `额外送 ${disCountDiamonds[data.shopId - 7]}钻石`;
}
}
} else {
let index = this._shopData.shopId - 11;
let path = './textures/store/800' + tabIndex + index + '0';
DWTool.loadResSpriteFrame(path).then((spriteFrame) => {
this.coinSprite.spriteFrame= spriteFrame;
});
if (data.shopId >= 12 && data.shopId <= 15) {
let coinArr = ['15分钟', '24小时', '72小时', '144小时'];
this.coinRicheText.string = `${coinArr[data.shopId - 12]}`;
}
}
}
this.updateTime();
},
initStoreAd() {
if (!CC_WECHATGAME) {
return;
}
// this.adId = this._tabIndex === 2 ? ADVideo.storeAdDiamand : ADVideo.storeAdCoinId;
//// 说明有广告
if (GameGlobal._adVideoState == 0) {
this._adState = 1;
} else if (GameGlobal._adVideoState === 1) {
this._adState = 0;
}
this.updateNormalTitle();
if (this.buyBtn.interactable) {
this.updateBuyTitle();
}
},
updateNormalTitle() {
if (this._tabIndex == 2) {
if ( this._adState === 0) {
this.adLabel.string = '分享到群获得钻石';
} else {
this.adLabel.string = '播放视频获得钻石';
}
} else {
if ( this._adState === 0) {
this.adLabel.string = '分享到群获得金币';
} else {
this.adLabel.string = '播放视频获得金币';
}
}
},
updateBuyTitle() {
if (this._adState === 0) {
this.buyRichText.string = `分享`;
} else {
this.buyRichText.string = `播放视频`;
}
},
updateTime() {
/// 设置购买样式和文字
/// 说明没有时间限制
let data = this._shopData;
let index = this._index;
let tabIndex = this._tabIndex;
if (data.minuteTime == 0 || data.cdTime == 0) {
if (index != 0) {
if (data.buyType == 1) {
this.buyRichText.string = `¥ ${data.price}`;
} else {
this.buyRichText.string = `${data.price}`;
}
}
//// 钻石购买金币
if (tabIndex == 3 && index == 1) {
this.setupBuyBtn(GameModule.userInfo.diamond >= data.price);
} else {
this.buyBtn.interactable = true;
}
} else {
this.setupTime();
}
},
setupTime() {
this._cdTime = this._shopData.cdTime / 1000;
let timeStr = DWTool.calculateTime(this._cdTime);
this.buyRichText.string = timeStr;
this.setupBuyBtn(false);
this.schedule(this.timeAction, 1);
},
setupBuyBtn(isActive) {
let path = isActive ? './textures/store/store_blue_btn' : './textures/store/store_btn_gray';
DWTool.loadResSpriteFrame(path)
.then((spriteFrame) => {
this.buySprite.spriteFrame = spriteFrame;
});
this.buyBtn.interactable = isActive;
},
timeAction() {
this._cdTime -= 1;
this.buyRichText.string = DWTool.calculateTime(this._cdTime);
if (this._cdTime <= 0) {
/// 如果不是广告样式
if (this._index !== 0) {
if (this._shopData.buyType == 1) {
this.buyRichText.string = `${this._shopData.price}`;
} else {
this.buyRichText.string = `¥ ${this._shopData.price}`;
}
} else {
this.updateBuyTitle();
}
this.setupBuyBtn(true);
this.unschedule(this.timeAction, this);
}
},
buyAction() {
GameModule.audioMng.playClickButton();
/// 是钻石 并且没有买过 显示后面的礼包特惠
if (this._tabIndex === 2 && this._index > 1 && this._shopData.isBuyDiamond == 0) {
//传入选择的index为购买优惠的顺序
let selectIndex = this._index - 2;
AlertManager.showStoreDiamondAlert(selectIndex);
/// 分享广告购买
} else if (this._index == 0) {
if (this._adState === 0) {
this.buyBtn.interactable = false;
GameEvent.on(GameNotificationKey.ShowShareAction, this, (type, isOk) => {
if (type != WechatShareType.StoreGetGift) { return; }
if (isOk) {
this.shareActionCallback();
} else {
this.buyBtn.interactable = true;
}
GameEvent.off(GameNotificationKey.ShowShareAction, this);
});
WeChat.shareAction(WechatShareType.StoreGetGift, () => {
}, () => {
console.log('分享取消或失败');
this.buyBtn.interactable = true;
});
} else {
GameGlobal._adVideo.showVideo('store' + this._tabIndex);
}
} else if (this._tabIndex == 3 && this._index == 1) {
this.buyBtn.interactable = false;
this.buyShop().then(() => {
this.buyBtn.interactable = true;
GameModule.userInfo.diamond -= this._shopData.price;
GameEvent.fire('store_buy_coin_updateDiamond', this._shopData.infoDesc);
let isActive = GameModule.userInfo.diamond >= this._shopData.price;
if (isActive == false) {
this.setupBuyBtn(false);
}
let iconPath = './textures/store/' + this._shopData.picId;
AlertManager.showCommonAlert(iconPath, this._shopData.desc, this._shopData.name);
}).catch( (err, code) => {
console.log(err, code);
this.buyBtn.interactable = false;
})
} else {
WeChat.jumpCustomerServices();
}
},
shareActionCallback() {
let self = this;
this.buyShop().then( () => {
self._shopData.cdTime = self._shopData.minuteTime * 60 * 1000;
self.setupTime();
/// 如果是钻石
let iconPath = '';
let name = '';
let desc = '';
if (this._tabIndex == 2) {
GameModule.userInfo.diamond += 10;
iconPath = './textures/quest/altas/quest_diamond';
name = '获得钻石';
if (this._adState === 0) {
desc = '分享获得钻石10个';
} else {
desc = '观看视频获得钻石10个';
}
TapTapTool.removeRedDot(GameRedDot.storeDiamond);
} else {
GameModule.userInfo.gold = TapTapTool.add(GameModule.userInfo.gold, this._getGold);
iconPath = './textures/quest/altas/quest_coin';
name = '获得金币';
if (this._adState === 0) {
desc = '分享获得金币' + TapTapTool.parseToString(this._getGold);
} else {
desc = '观看视频获得金币' + TapTapTool.parseToString(this._getGold);
}
TapTapTool.removeRedDot(GameRedDot.storeCoin);
}
AlertManager.showCommonAlert(iconPath, desc, name);
}).catch( (code, msg) => {
this.buyBtn.interactable = true;
console.log(err, code);
})
if (this._adState === 0) {
GameEvent.off(GameNotificationKey.ShowShareAction, this);
}
},
onDestroy() {
GameEvent.off(GameNotificationKey.ShowShareAction, this);
GameEvent.off(GameNotificationKey.AdUpdateStateNotification, this);
},
buyShop() {
return new Promise((resolve, reject) => {
StoreApi.buyShop(this._shopData.shopId, (respondData) => {
resolve(respondData);
}, (code, msg) => {
reject({code, msg});
});
});
},
// update (dt) {},
});