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');
var Promise = require('../lib/es6-promise').Promise;
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,
},
// LIFE-CYCLE CALLBACKS:
onLoad () {
GameEvent.on('StarItem_updateUI', this, () => {
this._buyGold = this._listViewAdapter.buyGold;
this._starTypeCount = this._listViewAdapter.starTypeCount;
this.updateData();
});
},
start () {
this._isAcion = false;
},
onDestroy() {
GameEvent.off('StarItem_updateUI', this);
},
update (dt) {
/// 如果是锁住的 那就什么都不做嘛 这里就是更新金币
if (this._isRoomLocked == false && this._isStarLocked == false && this._isAcion == false) {
/// 如果是没反应的 说明金币不够
if (this.starUseBtn.interactable == false) {
if (TapTapTool.compare(GameModule.userInfo.gold, this._buyGold)) {
this.setUpUseBtnBg(true);
}
}
}
},
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 = GameGlobal.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._isRoomLocked = false;
this._isStarLocked = false;
this.subTitleRichText1.string = "";
this.subTitleRichText.string = '';
} else {
let buyStarCount = GameModule.userInfo.buyStarCount;
let roomId = this._starData.signRoomId;
let isUnlocked = GameGlobal.BuildingManager.getRoomIsUnlocked(roomId);
let roomName = GameGlobal.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;
this.setUpUseBtnBg(TapTapTool.compare(GameModule.userInfo.gold, this._buyGold));
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;
}
},
/// 设置签约的button是否能点击
setUpUseBtnBg(isActive) {
this.starUseBtn.interactable = isActive;
this.starUserSprite.spriteFrame = isActive ? this.lightSpriteFrame : this.graySpriteFrame;
},
// 签约明星
signUseBtnAction() {
/// 只能同时买一个
if (this._listViewAdapter.isBuying) {
return;
}
this._listViewAdapter.isBuying = true;
GameModule.audioMng.playClickButton();
this.starUseBtn.interactable = false;
this._isAcion = true;
this.buyStar().then((respondData) => {
this.starUseBtn.interactable = true;
this._isAcion = 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});
GameGlobal._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._isAcion = false;
GameGlobal.commonAlert.showCommonErrorAlert("签约明星失败");
console.error(error);
});
},
buyStar() {
return new Promise((resolve, reject) => {
//// 购买明星
StarApi.buyStar(this._starData.starId, (respondData) => {
resolve(respondData);
}, (code, msg) => {
reject({code, msg});
});
});
},
});