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 = " 开启 " + roomName + "";
this._isRoomLocked = true;
} else {
this.subTitleRichText.string = " 开启 " + roomName + "";
}
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 = "" + this._starInfo.desc + " (" + newBuyStarCount + "/" + needCount + ")" + "";
} else {
this._isStarLocked = true;
this.subTitleRichText1.string = "" + this._starInfo.desc + " (" + newBuyStarCount + "/" + needCount + ")" + "";
}
} else if (this._starData.itemId == 102) {
let newStarTypeCount = this._starTypeCount <= needCount ? this._starTypeCount : needCount;
if (this._starTypeCount >= needCount) {
this.subTitleRichText1.string = "" + this._starInfo.desc + " (" + newStarTypeCount + "/" + needCount + ")" + "";
} else {
this._isStarLocked = true;
this.subTitleRichText1.string = "" + this._starInfo.desc + " (" + newStarTypeCount + "/" + needCount + ")" + "";
}
} else {
let newAreaStarCount = this._starData.areaStarCount <= needCount ? this._starData.areaStarCount : needCount;
if (this._starData.areaStarCount >= needCount) {
this.subTitleRichText1.string = "" + this._starInfo.desc + " (" + newAreaStarCount + "/" + needCount + ")" + "";
} else {
this._isStarLocked = true;
this.subTitleRichText1.string = "" + this._starInfo.desc + " (" + newAreaStarCount + "/" + needCount + ")" + "";
}
}
} 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 = `签约
${TapTapTool.parseToString(this._buyGold)}`;
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 = `签约
${TapTapTool.parseToString(this._buyGold)}`;
/// 如果是分享
} else if (this._useAdState === 1) {
this.starUseTitleRichText.string = ' 分享\n免费签约' + '';
} else {
this.starUseTitleRichText.string = ' 看视频\n免费签约' + '';
}
},
/// 设置签约的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});
});
});
},
});