UIShopView.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. import auto_shopView from "../../../ui/uidata/Interface/auto_shopView";
  2. import UIBase from "../../../framework/ui/UIBase";
  3. import UIHelp from "../../../framework/ui/UIHelp";
  4. import Utils from "../../../framework/utils/utils";
  5. import EventManager from "../../../framework/event/EventManager";
  6. import {EVENT_TYPE} from "../../../gameLogic/utrl/gameEnum";
  7. import jumpBy = cc.jumpBy;
  8. import gameData from "../../../gameLogic/utrl/gameData";
  9. import UIObtainView from "./UIObtainView";
  10. import rewardDtMgr from "../../../dataManager/rewardDtMgr";
  11. import itemDtManager from "../../../dataManager/itemDtManager";
  12. import AccountModel from "../../../data/Account/AccountModel";
  13. import gameEventManager from "../../../gameLogic/utrl/gameEventManager";
  14. const {ccclass, menu, property} = cc._decorator;
  15. const needRefreshTime = 5 * 60;
  16. const reward_items = [1, 2, 3, 4];
  17. @ccclass
  18. @menu("UI/Interface/UIShopView")
  19. export default class UIShopView extends UIBase {
  20. ui: auto_shopView = null;
  21. protected static prefabUrl = "Interface/shopView";
  22. protected static className = "UIShopView";
  23. private lastTime = null;
  24. private cb: Function = null;
  25. onUILoad() {
  26. this.ui = this.node.addComponent(auto_shopView);
  27. }
  28. onShow() {
  29. let shopTime = AccountModel.getInstance().shop_time;
  30. let curTime = Utils.getTimeStamp();
  31. if (!shopTime) {
  32. AccountModel.getInstance().shop_time = curTime - needRefreshTime;
  33. shopTime = AccountModel.getInstance().shop_time;
  34. }
  35. let time = needRefreshTime - (curTime - shopTime);
  36. if (time > 0) {
  37. this.lastTime = shopTime;
  38. this.ui.time.getComponent(cc.Label).string = `${Utils.getTime(time)}`;
  39. this.refreshTime();
  40. }
  41. }
  42. onHide() {
  43. }
  44. onStart() {
  45. }
  46. refreshTime() {
  47. this.ui.btn.getComponent(cc.Button).interactable = false;
  48. this.cb = () => {
  49. let curTime = Utils.getTimeStamp();
  50. let time = needRefreshTime - (curTime - this.lastTime);
  51. if (time <= 0) {
  52. this.ui.btn.getComponent(cc.Button).interactable = true;
  53. this.unschedule(this.cb);
  54. }
  55. this.ui.time.getComponent(cc.Label).string = `${Utils.getTime(time)}`;
  56. }
  57. this.schedule(this.cb, 1);
  58. }
  59. clickSearch() {
  60. let self = this
  61. zjSdk?.sendEvent('超市-立即搜刮')
  62. const params = {
  63. payType: zjSdk?.TYPE.VIDEO,
  64. success() {
  65. zjSdk?.sendEvent('超市-成功搜刮')
  66. self.doSearch();
  67. },
  68. fail() {
  69. // self.doSearch();
  70. }
  71. };
  72. zjSdk?.doPay(params);
  73. }
  74. private doSearch() {
  75. this.lastTime = Utils.getTimeStamp();
  76. AccountModel.getInstance().shop_time = this.lastTime;
  77. const count = Utils.random(100, 250);
  78. const nums = this.getNums(count);
  79. let all = nums.reduce(function (prev, curr, idx, arr) {
  80. return prev + curr;
  81. });
  82. gameData.curPowerNum += all;
  83. UIHelp.ShowUI(UIObtainView, null, {ids: reward_items, nums: nums});
  84. gameEventManager.emit(EVENT_TYPE.OFF_SHOP_TIP);
  85. this.refreshTime();
  86. }
  87. clickClose() {
  88. gameEventManager.emit(EVENT_TYPE.CHANGE_STATUS, false);
  89. this.onClose();
  90. }
  91. onClose() {
  92. UIHelp.CloseUI(UIShopView);
  93. }
  94. /**
  95. * 求出数量
  96. * @param count
  97. * @private
  98. */
  99. private getNums(count: number) {
  100. let nums = [0, 0, 0, 0];
  101. while (true) {
  102. for (let i = reward_items.length - 1; i >= 0; i--) {
  103. const dataByID = itemDtManager.getDataByID(reward_items[i]);
  104. if (count > dataByID.value) {
  105. count -= dataByID.value;
  106. nums[i] += dataByID.value;
  107. } else if (i == 0) {
  108. // 比最小值还小
  109. return nums;
  110. }
  111. }
  112. }
  113. }
  114. }