GameShop.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. // const DWTool = require("../utils/DWTool");
  2. const AlertManager = require('../utils/AlertManager');
  3. const StoreApi = require('../net/StoreApi');
  4. const GameNotificationKey = require('../utils/GameEnum').GameNotificationKey;
  5. const TapTapTool = require("../utils/TapTapTool");
  6. const GameModule = require("../utils/GameModule");
  7. var Promise = require('../lib/es6-promise').Promise;
  8. cc.Class({
  9. extends: cc.Component,
  10. properties: {
  11. },
  12. // LIFE-CYCLE CALLBACKS:
  13. onLoad () {
  14. this.setUpNotification();
  15. this.handelUserShops();
  16. GameModule.userInfo.shop = this;
  17. },
  18. handelUserShops() {
  19. let shops = GameGlobal.shops;
  20. this._mtArr = [];
  21. this._useTimeArr = [];
  22. this._diamondShopCoinTime = 0;
  23. if (shops == undefined || shops.length == 0) {
  24. return;
  25. }
  26. for (let i = 0; i < shops.length; ++ i) {
  27. let shop = shops[i];
  28. this.handelShop(shop);
  29. }
  30. this.updateTimeSchedule();
  31. },
  32. /// 设置通知
  33. setUpNotification() {
  34. GameEvent.on('store_buy_coin_updateDiamond', this, () => {
  35. /// 说明前面没有这个商品
  36. if (this._diamondShopCoinTime === 0) {
  37. let objc2 = {'n': 2, 'e': 0};
  38. GameModule.userInfo.perpetualMt = TapTapTool.multiple(GameModule.userInfo.perpetualMt, objc2);
  39. GameModule.userInfo.perpetualClickMt = TapTapTool.multiple(GameModule.userInfo.perpetualClickMt, objc2);
  40. let objct = {'cdTime': 15 * 60 * 1000, 'desc': '所有金币产量增加2倍,持续15分钟', 'icon': 900004, 'sId': 12, 'type': 1};
  41. GameGlobal._timeInformations.push(objct);
  42. GameEvent.fire(GameNotificationKey.GameUpdateMessageList, 1, true);
  43. GameModule.skill.updateClickGold();
  44. GameModule.userInfo.refreshSecondText();
  45. } else {
  46. for(let i = 0; i < GameGlobal._timeInformations.length; ++i) {
  47. let information = GameGlobal._timeInformations[i];
  48. if (information.type == 1 && information.sId == 12) {
  49. information.cdTime += 15 * 60 * 1000;
  50. break;
  51. }
  52. }
  53. }
  54. this._diamondShopCoinTime += 15 * 60;
  55. if (!this._isTimer) {
  56. this.schedule(this.timeAction, 1);
  57. }
  58. });
  59. },
  60. /// 看是否需要添加定时器
  61. updateTimeSchedule() {
  62. this._isTimer = false;
  63. if (this._useTimeArr.length > 0 || this._diamondShopCoinTime > 0) {
  64. this.schedule(this.timeAction, 1);
  65. this._isTimer = true;
  66. }
  67. },
  68. ///根据商品信息作出处理
  69. handelShop(shop) {
  70. /// 没有显示过 那么就显示弹窗
  71. if (shop.isAlert == 0) {
  72. let iconPath = './textures/store/' + shop.picId;
  73. let desc = shop.desc + '';
  74. let stringArr = desc.split('n');
  75. if (stringArr.length > 1) {
  76. desc = stringArr[0] + '<br/>' + stringArr[1];
  77. }
  78. /// 如果是每天获取钻石的数量 那么点击确定之后才更新砖石数量
  79. if (shop.shopId === 0 && shop.diamond !== undefined && shop.diamond > 0) {
  80. AlertManager.showGetDiamondEveryDayAlert(iconPath, desc, shop.name, shop.diamond);
  81. } else {
  82. AlertManager.showCommonAlert(iconPath, desc, shop.name);
  83. }
  84. /// 如果商品id小于等于0什么都不做
  85. if (shop.shopId > 0) {
  86. this.reportShop(shop.shopId);
  87. }
  88. }
  89. /// 如果有时间并且有cd剩余时间
  90. if (shop.minuteTime > 0 && shop.cdTime > 0) {
  91. if(shop.shopId == 12) {
  92. this._diamondShopCoinTime = shop.cdTime / 1000;
  93. } else {
  94. this._useTimeArr.push(shop.cdTime / 1000);
  95. this._mtArr.push(shop.mt);
  96. }
  97. GameModule.userInfo.perpetualMt = TapTapTool.multiple(GameModule.userInfo.perpetualMt, {'e': 0, 'n': shop.mt});
  98. GameModule.userInfo.perpetualClickMt = TapTapTool.multiple(GameModule.userInfo.perpetualClickMt, {'e': 0, 'n': shop.mt});
  99. GameModule.skill.updateClickGold();
  100. GameModule.userInfo.refreshSecondText();
  101. }
  102. /// 说明是礼包类型
  103. if (shop.type == 4) {
  104. GameModule.userInfo.perpetualMt = TapTapTool.multiple(GameModule.userInfo.perpetualMt, {'e': 0, 'n': shop.mt});
  105. GameModule.userInfo.perpetualClickMt = TapTapTool.multiple(GameModule.userInfo.perpetualClickMt, {'e': 0, 'n': shop.mt});
  106. GameModule.skill.updateClickGold();
  107. GameModule.userInfo.refreshSecondText();
  108. }
  109. //// 如果是长按点击的商品
  110. if (shop.shopId == 4) {
  111. GameGlobal.isLongPressClick = true;
  112. }
  113. },
  114. /// 处理商品的信息流信息
  115. handleShpDataToMessageList(shopData) {
  116. /// 如果有时间并且有cd剩余时间
  117. if (shopData.minuteTime > 0 && shopData.cdTime > 0) {
  118. let desc = shopData.desc + '';
  119. let shopObjc = {'cdTime': shopData.cdTime * 1000, 'desc': desc, 'icon': shopData.icon, 'sId': shopData.shopId, 'type': 1};
  120. GameGlobal._timeInformations.push(shopObjc);
  121. GameEvent.fire(GameNotificationKey.GameUpdateMessageList, 1, true);
  122. /// 说明不是需要时间cd的
  123. } else {
  124. let shopObjc = {'cdTime': -6 * 1000, 'desc': '获得' + shopData.name, 'icon': shopData.icon, 'sId': shopData.shopId, 'type': 1};
  125. GameGlobal._fixInformations.push(shopObjc);
  126. GameEvent.fire(GameNotificationKey.GameUpdateMessageList, 1);
  127. }
  128. },
  129. timeAction () {
  130. let newArr = this._useTimeArr.filter((n) => {
  131. return n > 0;
  132. })
  133. if (newArr.length <= 0 && this._diamondShopCoinTime <= 0) {
  134. this.unschedule(this.timeAction, this);
  135. this._isTimer = false;
  136. return;
  137. }
  138. if (this._diamondShopCoinTime > 0) {
  139. this._diamondShopCoinTime -= 1;
  140. if (this._diamondShopCoinTime <= 0) {
  141. GameModule.userInfo.perpetualMt = TapTapTool.multiple(GameModule.userInfo.perpetualMt, {'e': 0, 'n': 0.5});
  142. GameModule.userInfo.perpetualClickMt = TapTapTool.multiple(GameModule.userInfo.perpetualClickMt, {'e': 0, 'n': 0.5});
  143. GameModule.skill.updateClickGold();
  144. GameModule.userInfo.refreshSecondText();
  145. }
  146. }
  147. for (let i = 0; i < newArr.length; ++i) {
  148. this._useTimeArr[i] -= 1;
  149. if (this._useTimeArr[0] <= 0) {
  150. GameModule.userInfo.perpetualMt = TapTapTool.division(GameModule.userInfo.perpetualMt, {'e': 0, 'n': this._mtArr[i]});
  151. GameModule.userInfo.perpetualClickMt = TapTapTool.division(GameModule.userInfo.perpetualClickMt, {'e': 0, 'n': this._mtArr[i]});
  152. GameModule.skill.updateClickGold();
  153. GameModule.userInfo.refreshSecondText();
  154. }
  155. }
  156. },
  157. reportShop(shopId) {
  158. return new Promise((resolve, reject) => {
  159. //// 购买明星
  160. StoreApi.reportShop(shopId, (respondData) => {
  161. resolve(respondData);
  162. }, (code, msg) => {
  163. reject({code, msg});
  164. });
  165. });
  166. },
  167. // update (dt) {},
  168. });