const StarApi = require('../net/StarApi');
const GameModule = require('../utils/GameModule');
const {GameNotificationKey, StarType} = require('../utils/GameEnum');
const AlertManager = require('../utils/AlertManager');
var Promise = require('../lib/es6-promise').Promise;
cc.Class({
extends: cc.Component,
properties: {
content: cc.Node,
//
starItemPrefab: cc.Prefab,
scrollView: cc.ScrollView,
starLayout: cc.Layout,
//
progressBar: cc.ProgressBar,
countLabel: cc.Label,
percentRichText: cc.RichText,
//
descNode: cc.Node,
//底部信息
discountRichText: cc.RichText,
discount50Label: cc.Label,
discount30Label: cc.Label,
tab: [cc.Node],
tabIndex: 0,
recallAllNode: cc.Node
},
// LIFE-CYCLE CALLBACKS:
onLoad () {
this.isFirst = true;
if (GameGlobal.winSize.height <= 1000) {
this.content.height = 800;
}
GameEvent.on('show_star_desc', this, (starInfo) => {
this.showDescView(starInfo);
});
this.isRecalling = false;
this.showGiftBag = _.debounce((array) => {
AlertManager.showStarGiftBag(array);
}, 1000, true);
},
onDestroy() {
GameModule.audioMng.playClickButton();
GameEvent.off('show_star_desc', this);
},
start () {
this.getAllStars().then((respondData) => {
// console.log(respondData);
this.configData(respondData);
}).catch((code) => {
console.log(code);
});
},
/// 网络请求
getAllStars() {
return new Promise((resolve, reject) => {
// 获取目标用户的建筑
StarApi.getAllStars((respondData) => {
resolve(respondData);
}, (errCode, errMsg) => {
reject(code);
});
})
},
configData(respondData) {
this.respondData = respondData;
if (respondData.userStars.length > 0) {
// var isUnlockedCount = 0;
// for (let i = 0; i < respondData.userStars.length; ++i) {
// let star = respondData.userStars[i];
// if (star.starCount > 0) {
// isUnlockedCount++;
// }
// }
//
// let totalCount = respondData.userStars.length;
// this.countLabel.string = `${isUnlockedCount}/${totalCount} 已收集的明星`;
// let percent = (isUnlockedCount/totalCount * 100).toFixed(2);
// this.percentRichText.string = `${percent}%`;
// this.progressBar.progress = isUnlockedCount/totalCount;
this._sortStarData();
this._initTab();
}
let percent = parseInt(this.respondData.starZk * 100);
this.discountRichText.string = `${percent}%`;
//遍历礼包折扣
this.discount50Array = [];
this.discount30Array = [];
this.discount50 = 0;
this.discount30 = 0;
if (this.respondData.userShops.length > 0) {
for (let i = 0; i < this.respondData.userShops.length; ++i) {
let shop = this.respondData.userShops[i];
if (shop.zk == 0.5) {
this.discount50 += 1;
this.discount50Array.push(shop);
} else if (shop.zk == 0.3) {
this.discount30 += 1;
this.discount30Array.push(shop);
}
}
}
this.discount50Label.string = `x ${this.discount50}`;
this.discount30Label.string = `x ${this.discount30}`;
},
//重新筛选数据
_sortStarData() {
this.commonStars = [];
this.drawStars = [];
this.giftStars = [];
this.commonUnlocked = 0;
this.drawUnlocked = 0;
this.giftUnlocked = 0;
for (let i = 0; i < this.respondData.userStars.length; ++i) {
let star = this.respondData.userStars[i];
switch (star.type) {
case StarType.Common:
this.commonStars.push(star);
if (star.starCount > 0) {
this.commonUnlocked++;
}
break;
case StarType.Draw:
this.drawStars.push(star);
if (star.starCount > 0) {
this.drawUnlocked++;
}
break;
case StarType.Gift:
this.giftStars.push(star);
if (star.starCount > 0) {
this.giftUnlocked++;
}
break;
default:
break;
}
}
this.commonTabText = `普通明星\n${this.commonUnlocked}/${this.commonStars.length}`;
this.drawTabText = `抽奖明星\n${this.drawUnlocked}/${this.drawStars.length}`;
this.giftTabText = `礼包明星\n${this.giftUnlocked}/${this.giftStars.length}`;
},
/**
* 初始化Tab
*/
_initTab () {
for(let i = 0; i < this.tab.length; i++) {
this.tab[i] = this.tab[i].getComponent('StarHandbookTypeTab');
if (i == 0) {
this.tab[i].init(this.commonTabText);
} else if (i == 1) {
this.tab[i].init(this.drawTabText);
} else {
this.tab[i].init(this.giftTabText);
}
}
this.handleTab(null, this.tabIndex);
},
handleTab(event, tabIndex) {
this.tabIndex = parseInt(tabIndex);
this.tab.forEach((item, index) => {
if(this.tabIndex == index) {
item.show();
if (this.isFirst) {
this.isFirst = false
} else {
GameModule.audioMng.playClickButton();
}
this._refreshListData();
} else {
item.hide();
}
});
},
_refreshListData() {
this.starLayout.node.removeAllChildren();
switch (this.tabIndex) {
case 0:
for (let i = 0; i < this.commonStars.length; ++i) {
let star = this.commonStars[i];
let item = cc.instantiate(this.starItemPrefab);
item = item.getComponent('StarHandbookItem');
item.configData(star);
this.starLayout.node.addChild(item.node);
let totalCount = this.commonStars.length;
this.countLabel.string = `${this.commonUnlocked}/${totalCount} 已收集的明星`;
let percent = (this.commonUnlocked/totalCount * 100).toFixed(2);
this.percentRichText.string = `${percent}%`;
this.progressBar.progress = this.commonUnlocked/totalCount;
}
break;
case 1:
for (let i = 0; i < this.drawStars.length; ++i) {
let star = this.drawStars[i];
let item = cc.instantiate(this.starItemPrefab);
item = item.getComponent('StarHandbookItem');
item.configData(star);
this.starLayout.node.addChild(item.node);
let totalCount = this.drawStars.length;
this.countLabel.string = `${this.drawUnlocked}/${totalCount} 已收集的明星`;
let percent = (this.drawUnlocked/totalCount * 100).toFixed(2);
this.percentRichText.string = `${percent}%`;
this.progressBar.progress = this.drawUnlocked/totalCount;
}
break;
case 2:
for (let i = 0; i < this.giftStars.length; ++i) {
let star = this.giftStars[i];
let item = cc.instantiate(this.starItemPrefab);
item = item.getComponent('StarHandbookItem');
item.configData(star);
this.starLayout.node.addChild(item.node);
let totalCount = this.giftStars.length;
this.countLabel.string = `${this.giftUnlocked}/${totalCount} 已收集的明星`;
let percent = (this.giftUnlocked/totalCount * 100).toFixed(2);
this.percentRichText.string = `${percent}%`;
this.progressBar.progress = this.giftUnlocked/totalCount;
}
break;
default:
break;
}
},
closeView() {
this.node.destroy();
},
//显示描述信息
showDescView(starInfo) {
GameModule.audioMng.playClickButton();
this.descNode.active = true;
this.descNode.getComponent('StarHandbookDesc').configData(starInfo);
},
closeDescView() {
GameModule.audioMng.playClickButton();
this.descNode.active = false;
},
refreshData() {
for (let i = 0; i < this.commonStars.length; ++i) {
let star = this.commonStars[i];
star.starRoomCount = 0;
}
for (let i = 0; i < this.drawStars.length; ++i) {
let star = this.drawStars[i];
star.starRoomCount = 0;
}
for (let i = 0; i < this.giftStars.length; ++i) {
let star = this.giftStars[i];
star.starRoomCount = 0;
}
},
recallAllStar() {
GameModule.audioMng.playClickButton();
if (this.respondData.userStars.length <= 0) {
return;
}
if (this.isRecalling) { return; }
this.recallAllNode.active = true;
},
confirmRecallAll() {
GameModule.audioMng.playClickButton();
this.isRecalling = true;
StarApi.recallAllStar((respondData) => {
this.isRecalling = false;
this.refreshData();
GameEvent.fire(GameNotificationKey.AllStarLeaveRoom);
this.recallAllNode.active = false;
}, (code, msg) => {
this.isRecalling = false;
GameGlobal.commonAlert.showCommonErrorAlert(`收回失败 \n${msg}`);
});
},
closeRecallAllNode() {
GameModule.audioMng.playClickButton();
this.recallAllNode.active = false;
},
//tabIndex:0为5折,1为3折数据
showDiscountGift(event, tabIndex) {
GameModule.audioMng.playClickButton();
if (this.respondData == undefined) {
return;
}
if (tabIndex == 0) {
this.showGiftBag(this.discount50Array);
} else {
this.showGiftBag(this.discount30Array);
}
},
// update (dt) {},
});