StarHandbook.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. const StarApi = require('../net/StarApi');
  2. const GameModule = require('../utils/GameModule');
  3. const {GameNotificationKey, StarType} = require('../utils/GameEnum');
  4. const AlertManager = require('../utils/AlertManager');
  5. var Promise = require('../lib/es6-promise').Promise;
  6. cc.Class({
  7. extends: cc.Component,
  8. properties: {
  9. content: cc.Node,
  10. //
  11. starItemPrefab: cc.Prefab,
  12. scrollView: cc.ScrollView,
  13. starLayout: cc.Layout,
  14. //
  15. progressBar: cc.ProgressBar,
  16. countLabel: cc.Label,
  17. percentRichText: cc.RichText,
  18. //
  19. descNode: cc.Node,
  20. //底部信息
  21. discountRichText: cc.RichText,
  22. discount50Label: cc.Label,
  23. discount30Label: cc.Label,
  24. tab: [cc.Node],
  25. tabIndex: 0,
  26. recallAllNode: cc.Node
  27. },
  28. // LIFE-CYCLE CALLBACKS:
  29. onLoad () {
  30. this.isFirst = true;
  31. if (GameGlobal.winSize.height <= 1000) {
  32. this.content.height = 800;
  33. }
  34. GameEvent.on('show_star_desc', this, (starInfo) => {
  35. this.showDescView(starInfo);
  36. });
  37. this.isRecalling = false;
  38. this.showGiftBag = _.debounce((array) => {
  39. AlertManager.showStarGiftBag(array);
  40. }, 1000, true);
  41. },
  42. onDestroy() {
  43. GameModule.audioMng.playClickButton();
  44. GameEvent.off('show_star_desc', this);
  45. },
  46. start () {
  47. this.getAllStars().then((respondData) => {
  48. // console.log(respondData);
  49. this.configData(respondData);
  50. }).catch((code) => {
  51. console.log(code);
  52. });
  53. },
  54. /// 网络请求
  55. getAllStars() {
  56. return new Promise((resolve, reject) => {
  57. // 获取目标用户的建筑
  58. StarApi.getAllStars((respondData) => {
  59. resolve(respondData);
  60. }, (errCode, errMsg) => {
  61. reject(code);
  62. });
  63. })
  64. },
  65. configData(respondData) {
  66. this.respondData = respondData;
  67. if (respondData.userStars.length > 0) {
  68. // var isUnlockedCount = 0;
  69. // for (let i = 0; i < respondData.userStars.length; ++i) {
  70. // let star = respondData.userStars[i];
  71. // if (star.starCount > 0) {
  72. // isUnlockedCount++;
  73. // }
  74. // }
  75. //
  76. // let totalCount = respondData.userStars.length;
  77. // this.countLabel.string = `${isUnlockedCount}/${totalCount} 已收集的明星`;
  78. // let percent = (isUnlockedCount/totalCount * 100).toFixed(2);
  79. // this.percentRichText.string = `<b><outline color=#3d2e1d width=2><color=#ff9600 >${percent}%</color></outline></b>`;
  80. // this.progressBar.progress = isUnlockedCount/totalCount;
  81. this._sortStarData();
  82. this._initTab();
  83. }
  84. let percent = parseInt(this.respondData.starZk * 100);
  85. this.discountRichText.string = `<b><outline color=#3d2e1d width=2>${percent}%</outline></b>`;
  86. //遍历礼包折扣
  87. this.discount50Array = [];
  88. this.discount30Array = [];
  89. this.discount50 = 0;
  90. this.discount30 = 0;
  91. if (this.respondData.userShops.length > 0) {
  92. for (let i = 0; i < this.respondData.userShops.length; ++i) {
  93. let shop = this.respondData.userShops[i];
  94. if (shop.zk == 0.5) {
  95. this.discount50 += 1;
  96. this.discount50Array.push(shop);
  97. } else if (shop.zk == 0.3) {
  98. this.discount30 += 1;
  99. this.discount30Array.push(shop);
  100. }
  101. }
  102. }
  103. this.discount50Label.string = `x ${this.discount50}`;
  104. this.discount30Label.string = `x ${this.discount30}`;
  105. },
  106. //重新筛选数据
  107. _sortStarData() {
  108. this.commonStars = [];
  109. this.drawStars = [];
  110. this.giftStars = [];
  111. this.commonUnlocked = 0;
  112. this.drawUnlocked = 0;
  113. this.giftUnlocked = 0;
  114. for (let i = 0; i < this.respondData.userStars.length; ++i) {
  115. let star = this.respondData.userStars[i];
  116. switch (star.type) {
  117. case StarType.Common:
  118. this.commonStars.push(star);
  119. if (star.starCount > 0) {
  120. this.commonUnlocked++;
  121. }
  122. break;
  123. case StarType.Draw:
  124. this.drawStars.push(star);
  125. if (star.starCount > 0) {
  126. this.drawUnlocked++;
  127. }
  128. break;
  129. case StarType.Gift:
  130. this.giftStars.push(star);
  131. if (star.starCount > 0) {
  132. this.giftUnlocked++;
  133. }
  134. break;
  135. default:
  136. break;
  137. }
  138. }
  139. this.commonTabText = `普通明星\n${this.commonUnlocked}/${this.commonStars.length}`;
  140. this.drawTabText = `抽奖明星\n${this.drawUnlocked}/${this.drawStars.length}`;
  141. this.giftTabText = `礼包明星\n${this.giftUnlocked}/${this.giftStars.length}`;
  142. },
  143. /**
  144. * 初始化Tab
  145. */
  146. _initTab () {
  147. for(let i = 0; i < this.tab.length; i++) {
  148. this.tab[i] = this.tab[i].getComponent('StarHandbookTypeTab');
  149. if (i == 0) {
  150. this.tab[i].init(this.commonTabText);
  151. } else if (i == 1) {
  152. this.tab[i].init(this.drawTabText);
  153. } else {
  154. this.tab[i].init(this.giftTabText);
  155. }
  156. }
  157. this.handleTab(null, this.tabIndex);
  158. },
  159. handleTab(event, tabIndex) {
  160. this.tabIndex = parseInt(tabIndex);
  161. this.tab.forEach((item, index) => {
  162. if(this.tabIndex == index) {
  163. item.show();
  164. if (this.isFirst) {
  165. this.isFirst = false
  166. } else {
  167. GameModule.audioMng.playClickButton();
  168. }
  169. this._refreshListData();
  170. } else {
  171. item.hide();
  172. }
  173. });
  174. },
  175. _refreshListData() {
  176. this.starLayout.node.removeAllChildren();
  177. switch (this.tabIndex) {
  178. case 0:
  179. for (let i = 0; i < this.commonStars.length; ++i) {
  180. let star = this.commonStars[i];
  181. let item = cc.instantiate(this.starItemPrefab);
  182. item = item.getComponent('StarHandbookItem');
  183. item.configData(star);
  184. this.starLayout.node.addChild(item.node);
  185. let totalCount = this.commonStars.length;
  186. this.countLabel.string = `${this.commonUnlocked}/${totalCount} 已收集的明星`;
  187. let percent = (this.commonUnlocked/totalCount * 100).toFixed(2);
  188. this.percentRichText.string = `<b><outline color=#3d2e1d width=2><color=#ff9600 >${percent}%</color></outline></b>`;
  189. this.progressBar.progress = this.commonUnlocked/totalCount;
  190. }
  191. break;
  192. case 1:
  193. for (let i = 0; i < this.drawStars.length; ++i) {
  194. let star = this.drawStars[i];
  195. let item = cc.instantiate(this.starItemPrefab);
  196. item = item.getComponent('StarHandbookItem');
  197. item.configData(star);
  198. this.starLayout.node.addChild(item.node);
  199. let totalCount = this.drawStars.length;
  200. this.countLabel.string = `${this.drawUnlocked}/${totalCount} 已收集的明星`;
  201. let percent = (this.drawUnlocked/totalCount * 100).toFixed(2);
  202. this.percentRichText.string = `<b><outline color=#3d2e1d width=2><color=#ff9600 >${percent}%</color></outline></b>`;
  203. this.progressBar.progress = this.drawUnlocked/totalCount;
  204. }
  205. break;
  206. case 2:
  207. for (let i = 0; i < this.giftStars.length; ++i) {
  208. let star = this.giftStars[i];
  209. let item = cc.instantiate(this.starItemPrefab);
  210. item = item.getComponent('StarHandbookItem');
  211. item.configData(star);
  212. this.starLayout.node.addChild(item.node);
  213. let totalCount = this.giftStars.length;
  214. this.countLabel.string = `${this.giftUnlocked}/${totalCount} 已收集的明星`;
  215. let percent = (this.giftUnlocked/totalCount * 100).toFixed(2);
  216. this.percentRichText.string = `<b><outline color=#3d2e1d width=2><color=#ff9600 >${percent}%</color></outline></b>`;
  217. this.progressBar.progress = this.giftUnlocked/totalCount;
  218. }
  219. break;
  220. default:
  221. break;
  222. }
  223. },
  224. closeView() {
  225. this.node.destroy();
  226. },
  227. //显示描述信息
  228. showDescView(starInfo) {
  229. GameModule.audioMng.playClickButton();
  230. this.descNode.active = true;
  231. this.descNode.getComponent('StarHandbookDesc').configData(starInfo);
  232. },
  233. closeDescView() {
  234. GameModule.audioMng.playClickButton();
  235. this.descNode.active = false;
  236. },
  237. refreshData() {
  238. for (let i = 0; i < this.commonStars.length; ++i) {
  239. let star = this.commonStars[i];
  240. star.starRoomCount = 0;
  241. }
  242. for (let i = 0; i < this.drawStars.length; ++i) {
  243. let star = this.drawStars[i];
  244. star.starRoomCount = 0;
  245. }
  246. for (let i = 0; i < this.giftStars.length; ++i) {
  247. let star = this.giftStars[i];
  248. star.starRoomCount = 0;
  249. }
  250. },
  251. recallAllStar() {
  252. GameModule.audioMng.playClickButton();
  253. if (this.respondData.userStars.length <= 0) {
  254. return;
  255. }
  256. if (this.isRecalling) { return; }
  257. this.recallAllNode.active = true;
  258. },
  259. confirmRecallAll() {
  260. GameModule.audioMng.playClickButton();
  261. this.isRecalling = true;
  262. StarApi.recallAllStar((respondData) => {
  263. this.isRecalling = false;
  264. this.refreshData();
  265. GameEvent.fire(GameNotificationKey.AllStarLeaveRoom);
  266. this.recallAllNode.active = false;
  267. }, (code, msg) => {
  268. this.isRecalling = false;
  269. GameGlobal.commonAlert.showCommonErrorAlert(`收回失败 \n${msg}`);
  270. });
  271. },
  272. closeRecallAllNode() {
  273. GameModule.audioMng.playClickButton();
  274. this.recallAllNode.active = false;
  275. },
  276. //tabIndex:0为5折,1为3折数据
  277. showDiscountGift(event, tabIndex) {
  278. GameModule.audioMng.playClickButton();
  279. if (this.respondData == undefined) {
  280. return;
  281. }
  282. if (tabIndex == 0) {
  283. this.showGiftBag(this.discount50Array);
  284. } else {
  285. this.showGiftBag(this.discount30Array);
  286. }
  287. },
  288. // update (dt) {},
  289. });