123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356 |
- const GameModule = require("../utils/GameModule");
- const DWTool = require("../utils/DWTool");
- const StarApi = require('../net/StarApi');
- const GameNotificationKey = require('../utils/GameEnum').GameNotificationKey;
- const TapTapTool = require("../utils/TapTapTool");
- const ArtistManager = require('../utils/ArtistManager');
- const HomeApi = require('../net/HomeApi');
- // const ADVideo = require('../utils/ADVideo');
- cc.Class({
- extends: cc.Component,
- properties: {
- starIcon: cc.Sprite,
- starNameLabel: cc.Label,
- subTitleRichText: cc.RichText,
- subTitleRichText1: cc.RichText,
- starUseTitleRichText: cc.RichText,
- starUseBtn: cc.Button,
- starUserSprite: cc.Sprite,
- countLabel: cc.Label,
- countNode: cc.Node,
- starIconBlackBg: cc.Node,
- lightSpriteFrame: cc.SpriteFrame,
- graySpriteFrame: cc.SpriteFrame,
- lockedSpriteFrame: cc.SpriteFrame,
- /// 0说明是用金币 1 说明是用分享 2 看广告
- _adState: 0,
- //// 当前使用的广告状态
- _useAdState: 0,
-
- },
- // LIFE-CYCLE CALLBACKS:
- onLoad () {
-
- GameEvent.on('StarItem_updateUI', this, () => {
- this._buyGold = this._listViewAdapter.buyGold;
- this._starTypeCount = this._listViewAdapter.starTypeCount;
- this.updateData();
- });
- // GameEvent.on('StarItem_AD_updateState', this, (id) => {
- // if (id < 3) {
- // this._adState = id;
- // this._video = this.listAdapter._video;
- // /// 说明是关闭
- // } else if (id === 3) {
- // this.finishAction();
- // }
- // });
- // GameEvent.on(GameNotificationKey.AdStarStateUpdate, this, () => {
- // this.initStarAd();
- // })
- GameEvent.on(GameNotificationKey.AdUpdateStateNotification, this, (adState, callBack) => {
- /// 说明是技能的关闭状态
- if (adState === 3 && callBack === 'buyStar' + this._starData.starId) {
- this.finishAction();
- }
- if (adState === 0 || adState === 1) {
- this.initStarAd();
- }
- });
- },
- start () {
- this._isAction = false;
- },
- onDestroy() {
- GameEvent.off('StarItem_updateUI', this);
- GameEvent.off(GameNotificationKey.AdUpdateStateNotification, this);
- },
- update (dt) {
- /// 如果是锁住的 那就什么都不做嘛 这里就是更新金币
- if (this._isRoomLocked == false && this._isStarLocked == false && this._isAction == false) {
- /// 如果是没反应的 说明金币不够
- // if (this.starUseBtn.interactable == false) {
- // if () {
- // this.setUpUseBtnBg(true);
- // }
- // }
- let isCanUse = TapTapTool.compare(GameModule.userInfo.gold, this._buyGold);
- let isAd = DWTool.checkIsOldUser() && Global._buyStarInfo.maxCount > Global._buyStarInfo.count && Global._buyStarInfo.cdTime <= 0;
-
- if (isAd && !this._isInitAd) {
- this._isInitAd = true;
- this.initStarAd();
- }
- if (isAd && !isCanUse) {
- if (this._adState != this._useAdState) {
- this._useAdState = this._adState;
- this.updateBuyStarUI();
- this.setUpUseBtnBg(true);
- }
- } else {
- if (this._useAdState != 0) {
- this._useAdState = 0;
- this.updateBuyStarUI();
- }
-
- let inter = this.starUseBtn.interactable;
- if (isCanUse != inter) {
- this.setUpUseBtnBg(!inter);
- }
- }
- }
- },
- initStarAd() {
- if (!CC_WECHATGAME) {
- return;
- }
- //// 说明有广告
- if (Global._adVideoState == 0) {
- this._adState = 2;
- } else if (Global._adVideoState === 1) {
- this._adState = 1;
- }
- },
- finishAction() {
- HomeApi.reportInformation(3, () => {
- Global._buyStarInfo.count += 1;
- /// 时间重置为5分钟
- Global._buyStarInfo.cdTime = 5 * 60 * 1000;
- /// 恢复到正常的状态
- this._adState = 0;
- this._useAdState = 0;
- this._isAcion = false;
- this.buyStarRequest();
- }, (code, msg) => {
- console.log(msg);
- })
- },
- setListViewAdapter (listViewAdapter) {
- this._listViewAdapter = listViewAdapter;
- this._buyGold = listViewAdapter.buyGold;
- this._starTypeCount = listViewAdapter.starTypeCount;
- },
- updateItem(userInfo, itemId) {
- this._itemId = itemId;
- this._starData = userInfo;
- // console.log(this._starData.itemId);
- this._starInfo = Global.BuildingManager.getStarInfo(this._starData.starId);
- this.updateData();
- this.starNameLabel.string = this._starInfo.name;
- let imageId = 50000 + this._starData.starId;
- ArtistManager.loadStarAvatarSpriteFrame(imageId, this.starIcon);
- },
- deleteItem() {
- this._listViewAdapter.removeItem(this);
- },
- /// 实时更新数据
- updateData() {
- this.setUpIsCanSign();
- this.undateStarCount();
- },
- undateStarCount() {
- if (this._starData.starCount > 0) {
- this.countNode.active = true;
- if (this._starData.starRoomCount <= this._starData.starCount) {
- this.countLabel.string = this._starData.starRoomCount + '/' + this._starData.starCount;
- } else {
- this.countLabel.string = this._starData.starCount + '/' + this._starData.starCount;
- }
- } else {
- this.countNode.active = false;
- }
- },
- /// 设置是否能签约
- setUpIsCanSign() {
- if (this._starData.type === 3 || this._starData.type === 4) {
- this._isRoomLocked = false;
- this._isStarLocked = false;
- this.subTitleRichText1.string = "";
- this.subTitleRichText.string = '';
- } else {
- let buyStarCount = GameModule.userInfo.buyStarCount;
- let roomId = this._starData.signRoomId;
- let isUnlocked = Global.BuildingManager.getRoomIsUnlocked(roomId);
- let roomName = Global.BuildingManager.getRoomName(roomId);
-
- this._isRoomLocked = false;
- this._isStarLocked = false;
- /// 需要的房间锁住的。
- if (isUnlocked == 0) {
- this.subTitleRichText.string = "<img src='star_false'/><color=#2E2E2E> 开启 " + roomName + "</c>";
- this._isRoomLocked = true;
- } else {
- this.subTitleRichText.string = "<img src='star_true'/><color=#2E2E2E> 开启 " + roomName + "</c>";
- }
-
- let needCount = this._starData.itemCount;
- if (needCount > 0) {
- /// 如要明星的个数为解锁条件
- if (this._starData.itemId == 101) {
- /// 如果明星数量足够
- let newBuyStarCount = buyStarCount <= needCount ? buyStarCount : needCount;
- if (buyStarCount >= needCount) {
- this.subTitleRichText1.string = "<img src='star_true'/><color=#2E2E2E>" + this._starInfo.desc + " (" + newBuyStarCount + "/" + needCount + ")" + "</c>";
- } else {
- this._isStarLocked = true;
- this.subTitleRichText1.string = "<img src='star_false'/><color=#2E2E2E>" + this._starInfo.desc + " (" + newBuyStarCount + "/" + needCount + ")" + "</c>";
- }
- } else if (this._starData.itemId == 102) {
- let newStarTypeCount = this._starTypeCount <= needCount ? this._starTypeCount : needCount;
- if (this._starTypeCount >= needCount) {
- this.subTitleRichText1.string = "<img src='star_true'/><color=#2E2E2E>" + this._starInfo.desc + " (" + newStarTypeCount + "/" + needCount + ")" + "</c>";
- } else {
- this._isStarLocked = true;
- this.subTitleRichText1.string = "<img src='star_fale'/><color=#2E2E2E>" + this._starInfo.desc + " (" + newStarTypeCount + "/" + needCount + ")" + "</c>";
- }
- } else {
- let newAreaStarCount = this._starData.areaStarCount <= needCount ? this._starData.areaStarCount : needCount;
- if (this._starData.areaStarCount >= needCount) {
- this.subTitleRichText1.string = "<img src='star_true'/><color=#2E2E2E>" + this._starInfo.desc + " (" + newAreaStarCount + "/" + needCount + ")" + "</c>";
- } else {
- this._isStarLocked = true;
- this.subTitleRichText1.string = "<img src='star_false'/><color=#2E2E2E>" + this._starInfo.desc + " (" + newAreaStarCount + "/" + needCount + ")" + "</c>";
- }
- }
- } else {
- this.subTitleRichText1.string = "";
- }
- }
- ///说明满足明星和房间的条件 已经解锁 那么只需要判断金币进行预约了
- if (this._isStarLocked == false && this._isRoomLocked == false) {
- /// 如果金币满足
- this.starUseTitleRichText.node.active = true;
- if (this._useAdState === 0) {
- this.setUpUseBtnBg(TapTapTool.compare(GameModule.userInfo.gold, this._buyGold));
- } else {
- this.setUpUseBtnBg(true);
- }
- this.updateBuyStarUI();
- // this.starUseTitleRichText.string = `<color=#ffffff>签约</c><br/><img src='coin_small'/><color=#ffffff>${TapTapTool.parseToString(this._buyGold)}</c>`;
- this.starIconBlackBg.active = false;
- } else {
- this.starUseTitleRichText.node.active = false;
- this.starUseBtn.interactable = false;
- this.starUserSprite.spriteFrame = this.lockedSpriteFrame;
- this.starIconBlackBg.active = true;
- }
- },
- /// 根据视频 分享 升级更新使用的UI
- updateBuyStarUI() {
- /// 如果是直接使用金币
- if (this._useAdState === 0) {
- this.starUseTitleRichText.string = `<color=#ffffff>签约</c><br/><img src='coin_small'/><color=#ffffff>${TapTapTool.parseToString(this._buyGold)}</c>`;
- /// 如果是分享
- } else if (this._useAdState === 1) {
- this.starUseTitleRichText.string = '<color=#ffffff> 分享\n免费签约' + '</c>';
- } else {
- this.starUseTitleRichText.string = '<color=#ffffff> 看视频\n免费签约' + '</c>';
- }
- },
- /// 设置签约的button是否能点击
- setUpUseBtnBg(isActive) {
- this.starUseBtn.interactable = isActive;
- this.starUserSprite.spriteFrame = isActive ? this.lightSpriteFrame : this.graySpriteFrame;
- },
- // 签约明星
- signUseBtnAction() {
-
- GameModule.audioMng.playClickButton();
- this.starUseBtn.interactable = false;
- this._isAction = true;
- if (CC_WECHATGAME) {
- if (this._useAdState == 0) {
- /// 只能同时买一个
- if (this._listViewAdapter.isBuying) {
- return;
- }
- this._listViewAdapter.isBuying = true;
- this.buyStarRequest();
- /// 说明是分享
- } else if (this._useAdState == 1) {
- GameEvent.on(GameNotificationKey.ShowShareAction, this, (type) => {
- if (type == WechatShareType.BuyStar) {
- this.finishAction();
- }
- });
- WeChat.shareAction(WechatShareType.BuyStar, () => {
-
- }, () => {
- this.starUseBtn.interactable = true;
- this._isAction = false;
- console.log('分享失败或取消');
- });
- /// 如果是看视频
- } else {
- this.starUseBtn.interactable = true;
- this._isAction = false;
- Global._adVideo.showVideo('buyStar' + this._starData.starId);
- }
- } else {
- this.buyStarRequest();
- }
- },
- buyStarRequest() {
- this.buyStar().then((respondData) => {
- this.starUseBtn.interactable = true;
- this._isAction = false;
- GameModule.userInfo.gold = TapTapTool.sub(GameModule.userInfo.gold, this._buyGold);
- /// 本地购买的明星数量加1
- GameModule.userInfo.buyStarCount += 1;
- // this._starData.starRoomCount += 1;
- this._starData.starCount += 1;
- GameModule.userInfo.perpetualClickMt = TapTapTool.multiple(GameModule.userInfo.perpetualClickMt, {'e': 0, 'n': 2});
- GameModule.userInfo.perpetualMt = TapTapTool.multiple(GameModule.userInfo.perpetualMt, {'e': 0, 'n': 2});
- Global._buyStarGold = respondData.buyGold;
- this.undateStarCount();
- this._listViewAdapter.isBuying = false;
- GameEvent.fire(GameNotificationKey.StarEnterRoom, this._starData.starId, respondData.roomId);
- GameEvent.fire("Star_Buy_success", this._starInfo.areaType, respondData.buyGold, respondData.starTypeCount);
- GameEvent.fire('Star_Buy_success_Alert', this._starInfo.starId, this._starInfo.name);
- }).catch((error) => {
- this.starUseBtn.interactable = true;
- this._isAction = false;
- Global.commonAlert.showCommonErrorAlert("签约明星失败");
- console.error(error);
- });
- },
- buyStar() {
- return new Promise((resolve, reject) => {
- //// 购买明星
- StarApi.buyStar(this._starData.starId, (respondData) => {
- resolve(respondData);
- }, (code, msg) => {
- reject({code, msg});
- });
- });
- },
- });
|