GameShop.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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 = Global.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. GameModule.userInfo.shop = this;
  60. let shops = GameGlobal.shops;
  61. this._mtArr = [];
  62. this._useTimeArr = [];
  63. this._diamondShopCoinTime = 0;
  64. if (shops == undefined || shops.length == 0) {
  65. return;
  66. }
  67. for (let i = 0; i < shops.length; ++ i) {
  68. let shop = shops[i];
  69. this.handelShop(shop);
  70. }
  71. this.updateTimeSchedule();
  72. },
  73. /// 看是否需要添加定时器
  74. updateTimeSchedule() {
  75. this._isTimer = false;
  76. if (this._useTimeArr.length > 0 || this._diamondShopCoinTime > 0) {
  77. this.schedule(this.timeAction, 1);
  78. this._isTimer = true;
  79. }
  80. },
  81. ///根据商品信息作出处理
  82. handelShop(shop) {
  83. /// 没有显示过 那么就显示弹窗
  84. if (shop.isAlert == 0) {
  85. let iconPath = './textures/store/' + shop.picId;
  86. let desc = shop.desc + '';
  87. let stringArr = desc.split('n');
  88. if (stringArr.length > 1) {
  89. desc = stringArr[0] + '<br/>' + stringArr[1];
  90. }
  91. /// 如果是每天获取钻石的数量 那么点击确定之后才更新砖石数量
  92. if (shop.shopId === 0 && shop.diamond !== undefined && shop.diamond > 0) {
  93. AlertManager.showGetDiamondEveryDayAlert(iconPath, desc, shop.name, shop.diamond);
  94. } else {
  95. AlertManager.showCommonAlert(iconPath, desc, shop.name);
  96. }
  97. /// 如果商品id小于等于0什么都不做
  98. if (shop.shopId > 0) {
  99. this.reportShop(shop.shopId);
  100. }
  101. }
  102. /// 如果有时间并且有cd剩余时间
  103. if (shop.minuteTime > 0 && shop.cdTime > 0) {
  104. if(shop.shopId == 12) {
  105. this._diamondShopCoinTime = shop.cdTime / 1000;
  106. } else {
  107. this._useTimeArr.push(shop.cdTime / 1000);
  108. this._mtArr.push(shop.mt);
  109. }
  110. GameModule.userInfo.perpetualMt = TapTapTool.multiple(GameModule.userInfo.perpetualMt, {'e': 0, 'n': shop.mt});
  111. GameModule.userInfo.perpetualClickMt = TapTapTool.multiple(GameModule.userInfo.perpetualClickMt, {'e': 0, 'n': shop.mt});
  112. GameModule.skill.updateClickGold();
  113. GameModule.userInfo.refreshSecondText();
  114. }
  115. /// 说明是礼包类型
  116. if (shop.type == 4) {
  117. GameModule.userInfo.perpetualMt = TapTapTool.multiple(GameModule.userInfo.perpetualMt, {'e': 0, 'n': shop.mt});
  118. GameModule.userInfo.perpetualClickMt = TapTapTool.multiple(GameModule.userInfo.perpetualClickMt, {'e': 0, 'n': shop.mt});
  119. GameModule.skill.updateClickGold();
  120. GameModule.userInfo.refreshSecondText();
  121. }
  122. //// 如果是长按点击的商品
  123. if (shop.shopId == 4) {
  124. GameGlobal.isLongPressClick = true;
  125. }
  126. },
  127. /// 处理商品的信息流信息
  128. handleShpDataToMessageList(shopData) {
  129. /// 如果有时间并且有cd剩余时间
  130. if (shopData.minuteTime > 0 && shopData.cdTime > 0) {
  131. let desc = shopData.desc + '';
  132. let shopObjc = {'cdTime': shopData.cdTime * 1000, 'desc': desc, 'icon': shopData.icon, 'sId': shopData.shopId, 'type': 1};
  133. GameGlobal._timeInformations.push(shopObjc);
  134. GameEvent.fire(GameNotificationKey.GameUpdateMessageList, 1, true);
  135. /// 说明不是需要时间cd的
  136. } else {
  137. let shopObjc = {'cdTime': -6 * 1000, 'desc': '获得' + shopData.name, 'icon': shopData.icon, 'sId': shopData.shopId, 'type': 1};
  138. GameGlobal._fixInformations.push(shopObjc);
  139. GameEvent.fire(GameNotificationKey.GameUpdateMessageList, 1);
  140. }
  141. },
  142. timeAction () {
  143. let newArr = this._useTimeArr.filter((n) => {
  144. return n > 0;
  145. })
  146. if (newArr.length <= 0 && this._diamondShopCoinTime <= 0) {
  147. this.unschedule(this.timeAction, this);
  148. this._isTimer = false;
  149. return;
  150. }
  151. if (this._diamondShopCoinTime > 0) {
  152. this._diamondShopCoinTime -= 1;
  153. if (this._diamondShopCoinTime <= 0) {
  154. GameModule.userInfo.perpetualMt = TapTapTool.multiple(GameModule.userInfo.perpetualMt, {'e': 0, 'n': 0.5});
  155. GameModule.userInfo.perpetualClickMt = TapTapTool.multiple(GameModule.userInfo.perpetualClickMt, {'e': 0, 'n': 0.5});
  156. GameModule.skill.updateClickGold();
  157. GameModule.userInfo.refreshSecondText();
  158. }
  159. }
  160. for (let i = 0; i < newArr.length; ++i) {
  161. this._useTimeArr[i] -= 1;
  162. if (this._useTimeArr[0] <= 0) {
  163. GameModule.userInfo.perpetualMt = TapTapTool.division(GameModule.userInfo.perpetualMt, {'e': 0, 'n': this._mtArr[i]});
  164. GameModule.userInfo.perpetualClickMt = TapTapTool.division(GameModule.userInfo.perpetualClickMt, {'e': 0, 'n': this._mtArr[i]});
  165. GameModule.skill.updateClickGold();
  166. GameModule.userInfo.refreshSecondText();
  167. }
  168. }
  169. },
  170. reportShop(shopId) {
  171. return new Promise((resolve, reject) => {
  172. //// 购买明星
  173. StoreApi.reportShop(shopId, (respondData) => {
  174. resolve(respondData);
  175. }, (code, msg) => {
  176. reject({code, msg});
  177. });
  178. });
  179. },
  180. // update (dt) {},
  181. });