UIShopView.ts 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. import AESUtil from "../../../AESUtil"
  15. import ATSDK from "../../../AnyThinkAds/ATJSSDK";
  16. import ATRewardedVideoSDK from "../../../AnyThinkAds/ATRewardedVideoJSSDK";
  17. import AAJS2 from "../../../ATAndroidJS2";
  18. import GlobalManager from '../../../GlobalManager';
  19. const {ccclass, menu, property} = cc._decorator;
  20. const needRefreshTime = 5 * 60;
  21. const reward_items = [1, 2, 3, 4];
  22. @ccclass
  23. @menu("UI/Interface/UIShopView")
  24. export default class UIShopView extends UIBase {
  25. ui: auto_shopView = null;
  26. protected static prefabUrl = "Interface/shopView";
  27. protected static className = "UIShopView";
  28. private lastTime = null;
  29. private cb: Function = null;
  30. onUILoad() {
  31. this.ui = this.node.addComponent(auto_shopView);
  32. this.initAdForPage();
  33. }
  34. initAdForPage() {
  35. if (cc.sys.os === cc.sys.OS_ANDROID) {
  36. let deviceId = AAJS2.getDeviceUserId();
  37. console.log("zh:checkstatus:", ATRewardedVideoSDK.checkAdStatus(AAJS2.getPlacementId()));
  38. var setting = {};
  39. setting[ATRewardedVideoSDK.userIdKey] = deviceId;
  40. ATRewardedVideoSDK.loadRewardedVideo(AAJS2.getPlacementId(), setting);
  41. }
  42. }
  43. onShow() {
  44. let shopTime = AccountModel.getInstance().shop_time;
  45. let curTime = Utils.getTimeStamp();
  46. if (!shopTime) {
  47. AccountModel.getInstance().shop_time = curTime - needRefreshTime;
  48. shopTime = AccountModel.getInstance().shop_time;
  49. }
  50. let time = needRefreshTime - (curTime - shopTime);
  51. if (time > 0) {
  52. this.lastTime = shopTime;
  53. this.ui.time.getComponent(cc.Label).string = `${Utils.getTime(time)}`;
  54. this.refreshTime();
  55. }
  56. }
  57. onHide() {
  58. }
  59. onStart() {
  60. }
  61. refreshTime() {
  62. this.ui.btn.getComponent(cc.Button).interactable = false;
  63. this.cb = () => {
  64. let curTime = Utils.getTimeStamp();
  65. let time = needRefreshTime - (curTime - this.lastTime);
  66. if (time <= 0) {
  67. this.ui.btn.getComponent(cc.Button).interactable = true;
  68. this.unschedule(this.cb);
  69. }
  70. this.ui.time.getComponent(cc.Label).string = `${Utils.getTime(time)}`;
  71. }
  72. this.schedule(this.cb, 1);
  73. }
  74. clickSearch() {
  75. let self = this
  76. zjSdk?.sendEvent('超市-立即搜刮')
  77. if(2>1){//zh:diy
  78. if (cc.sys.os === cc.sys.OS_ANDROID) {
  79. GlobalManager.getInstance().registerMethod('doSearchForAdEvent', this.doSearchForAdEvent.bind(this));
  80. let adKey = "zh_ad_type"
  81. if (ATRewardedVideoSDK.hasAdReady(AAJS2.getPlacementId())) {
  82. cc.sys.localStorage.setItem(adKey, 'doSearchForAdEvent');
  83. console.log('zh:AD ready for idx2')
  84. ATRewardedVideoSDK.showAd(AAJS2.getPlacementId());
  85. } else {
  86. console.log('zh:AD not ready for idx2')
  87. self.doSearchForAdEvent();
  88. }
  89. }
  90. return;
  91. }
  92. //下面是原始的代码
  93. const params = {
  94. payType: zjSdk?.TYPE.VIDEO,
  95. success() {
  96. zjSdk?.sendEvent('超市-成功搜刮')
  97. self.doSearch();
  98. },
  99. fail() {
  100. // self.doSearch();
  101. }
  102. };
  103. zjSdk?.doPay(params);
  104. }
  105. private doSearch() {
  106. this.lastTime = Utils.getTimeStamp();
  107. AccountModel.getInstance().shop_time = this.lastTime;
  108. const count = Utils.random(100, 250);
  109. const nums = this.getNums(count);
  110. let all = nums.reduce(function (prev, curr, idx, arr) {
  111. return prev + curr;
  112. });
  113. gameData.curPowerNum += all;
  114. UIHelp.ShowUI(UIObtainView, null, {ids: reward_items, nums: nums});
  115. gameEventManager.emit(EVENT_TYPE.OFF_SHOP_TIP);
  116. this.refreshTime();
  117. }
  118. //AD zh
  119. public doSearchForAdEvent() {
  120. console.log('zh: doSearchForAdEvent 被触发' )
  121. this.lastTime = Utils.getTimeStamp();
  122. AccountModel.getInstance().shop_time = this.lastTime;
  123. const count = Utils.random(100, 250);
  124. const nums = this.getNums(count);
  125. let all = nums.reduce(function (prev, curr, idx, arr) {
  126. return prev + curr;
  127. });
  128. gameData.curPowerNum += all;
  129. UIHelp.ShowUI(UIObtainView, null, {ids: reward_items, nums: nums});
  130. gameEventManager.emit(EVENT_TYPE.OFF_SHOP_TIP);
  131. this.refreshTime();
  132. }
  133. clickClose() {
  134. gameEventManager.emit(EVENT_TYPE.CHANGE_STATUS, false);
  135. this.onClose();
  136. }
  137. onClose() {
  138. UIHelp.CloseUI(UIShopView);
  139. }
  140. /**
  141. * 求出数量
  142. * @param count
  143. * @private
  144. */
  145. private getNums(count: number) {
  146. let nums = [0, 0, 0, 0];
  147. while (true) {
  148. for (let i = reward_items.length - 1; i >= 0; i--) {
  149. const dataByID = itemDtManager.getDataByID(reward_items[i]);
  150. if (count > dataByID.value) {
  151. count -= dataByID.value;
  152. nums[i] += dataByID.value;
  153. } else if (i == 0) {
  154. // 比最小值还小
  155. return nums;
  156. }
  157. }
  158. }
  159. }
  160. }