StarHandbook.js 11 KB

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