UIShopView.ts 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. }else{
  90. self.doSearchForAdEvent();
  91. }
  92. return;
  93. }
  94. //下面是原始的代码
  95. const params = {
  96. payType: zjSdk?.TYPE.VIDEO,
  97. success() {
  98. zjSdk?.sendEvent('超市-成功搜刮')
  99. self.doSearch();
  100. },
  101. fail() {
  102. // self.doSearch();
  103. }
  104. };
  105. zjSdk?.doPay(params);
  106. }
  107. private doSearch() {
  108. this.lastTime = Utils.getTimeStamp();
  109. AccountModel.getInstance().shop_time = this.lastTime;
  110. const count = Utils.random(100, 250);
  111. const nums = this.getNums(count);
  112. let all = nums.reduce(function (prev, curr, idx, arr) {
  113. return prev + curr;
  114. });
  115. gameData.curPowerNum += all;
  116. UIHelp.ShowUI(UIObtainView, null, {ids: reward_items, nums: nums});
  117. gameEventManager.emit(EVENT_TYPE.OFF_SHOP_TIP);
  118. this.refreshTime();
  119. }
  120. //AD zh
  121. public doSearchForAdEvent() {
  122. console.log('zh: doSearchForAdEvent 被触发' )
  123. this.lastTime = Utils.getTimeStamp();
  124. AccountModel.getInstance().shop_time = this.lastTime;
  125. const count = Utils.random(100, 250);
  126. const nums = this.getNums(count);
  127. let all = nums.reduce(function (prev, curr, idx, arr) {
  128. return prev + curr;
  129. });
  130. gameData.curPowerNum += all;
  131. UIHelp.ShowUI(UIObtainView, null, {ids: reward_items, nums: nums});
  132. gameEventManager.emit(EVENT_TYPE.OFF_SHOP_TIP);
  133. this.refreshTime();
  134. }
  135. clickClose() {
  136. gameEventManager.emit(EVENT_TYPE.CHANGE_STATUS, false);
  137. this.onClose();
  138. }
  139. onClose() {
  140. UIHelp.CloseUI(UIShopView);
  141. }
  142. /**
  143. * 求出数量
  144. * @param count
  145. * @private
  146. */
  147. private getNums(count: number) {
  148. let nums = [0, 0, 0, 0];
  149. while (true) {
  150. for (let i = reward_items.length - 1; i >= 0; i--) {
  151. const dataByID = itemDtManager.getDataByID(reward_items[i]);
  152. if (count > dataByID.value) {
  153. count -= dataByID.value;
  154. nums[i] += dataByID.value;
  155. } else if (i == 0) {
  156. // 比最小值还小
  157. return nums;
  158. }
  159. }
  160. }
  161. }
  162. }