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');
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 = 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;
this.countLabel.string = this._starData.starRoomCount + '/' + 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 = 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) {
/// 如果明星数量足够
if (buyStarCount >= needCount) {
this.subTitleRichText1.string = "" + this._starInfo.desc + " (" + buyStarCount + "/" + needCount + ")" + "";
} else {
this._isStarLocked = true;
this.subTitleRichText1.string = "" + this._starInfo.desc + " (" + buyStarCount + "/" + needCount + ")" + "";
}
} else if (this._starData.itemId == 102) {
if (this._starTypeCount >= needCount) {
this.subTitleRichText1.string = "" + this._starInfo.desc + " (" + this._starTypeCount + "/" + needCount + ")" + "";
} else {
this._isStarLocked = true;
this.subTitleRichText1.string = "" + this._starInfo.desc + " (" + this._starTypeCount + "/" + needCount + ")" + "";
}
} else {
if (this._starData.areaStarCount >= needCount) {
this.subTitleRichText1.string = "" + this._starInfo.desc + " (" + this._starData.areaStarCount + "/" + needCount + ")" + "";
} else {
this._isStarLocked = true;
this.subTitleRichText1.string = "" + this._starInfo.desc + " (" + this._starData.areaStarCount + "/" + 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 = respondData.clickMt;
GameModule.userInfo.perpetualMt = respondData.mt;
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._isAcion = 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});
});
});
},
});