UIAirdropView.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. import auto_airdropView from "../../../ui/uidata/Interface/auto_airdropView";
  2. import UIBase from "../../../framework/ui/UIBase";
  3. import UIHelp from "../../../framework/ui/UIHelp";
  4. import rewardDtMgr from "../../../dataManager/rewardDtMgr";
  5. import UIAirdropItem from "./UIAirdropItem";
  6. import gameData from "../../../gameLogic/utrl/gameData";
  7. import itemDtManager from "../../../dataManager/itemDtManager";
  8. import gameEventManager from "../../../gameLogic/utrl/gameEventManager";
  9. import {EVENT_TYPE} from "../../../gameLogic/utrl/gameEnum";
  10. import Utils from "../../../framework/utils/utils";
  11. import UIObtainView from "./UIObtainView";
  12. import modelDtlManager from "../../../dataManager/modelDtlManager";
  13. import AccountModel from "../../../data/Account/AccountModel";
  14. import {Log, LOG_TAG} from "../../../framework/log/Log";
  15. import AESUtil from "../../../AESUtil"
  16. import ATSDK from "../../../AnyThinkAds/ATJSSDK";
  17. import ATRewardedVideoSDK from "../../../AnyThinkAds/ATRewardedVideoJSSDK";
  18. import AAJS2 from "../../../ATAndroidJS2";
  19. import GlobalManager from '../../../GlobalManager';
  20. const {ccclass, menu, property} = cc._decorator;
  21. const enum ItemType {
  22. food = 1,
  23. bullet = 2,
  24. coldWeapon = 3,
  25. hotWeapon = 4,
  26. role = 5,
  27. }
  28. // ui 打开暂停游戏
  29. const needRefreshTime = 10 * 60;
  30. @ccclass
  31. @menu("UI/Interface/UIAirdropView")
  32. export default class UIAirdropView extends UIBase {
  33. ui: auto_airdropView = null;
  34. //
  35. protected static prefabUrl = "Interface/airdropView";
  36. protected static className = "UIAirdropView";
  37. @property(cc.Prefab)
  38. itemPrefab: cc.Prefab = null;
  39. @property(cc.Prefab)
  40. obtainPrefab: cc.Prefab = null;
  41. /**
  42. * 对应上面的枚举
  43. * @private
  44. */
  45. private typeData = {
  46. 1: [],
  47. 2: [],
  48. 3: [],
  49. 4: [],
  50. 5: [],
  51. }
  52. private result: any[] = [];
  53. private lastTime = null;
  54. private cb: Function = null;
  55. onUILoad() {
  56. this.ui = this.node.addComponent(auto_airdropView);
  57. this.initAdForPage();
  58. }
  59. initAdForPage() {
  60. if (cc.sys.os === cc.sys.OS_ANDROID) {
  61. let deviceId = AAJS2.getDeviceUserId();
  62. console.log("zh:checkstatus:", ATRewardedVideoSDK.checkAdStatus(AAJS2.getPlacementId()));
  63. var setting = {};
  64. setting[ATRewardedVideoSDK.userIdKey] = deviceId;
  65. ATRewardedVideoSDK.loadRewardedVideo(AAJS2.getPlacementId(), setting);
  66. }
  67. }
  68. onShow() {
  69. const infos = rewardDtMgr.getInstance().getAirDropRewardList();
  70. for (let key in infos) {
  71. const info = infos[key];
  72. this.typeData[info.type].push(info);
  73. }
  74. const airdropRewards = AccountModel.getInstance().airdrop_rewards;
  75. if (airdropRewards.length == 0) {
  76. this.refreshInfo();
  77. }else {
  78. this.result = airdropRewards;
  79. this.refreshItemView(this.result);
  80. }
  81. let airdrop_time = AccountModel.getInstance().airdrop_time;
  82. if (!airdrop_time) {
  83. airdrop_time = Utils.getTimeStamp();
  84. }
  85. let curTime = Utils.getTimeStamp();
  86. let time = needRefreshTime - (curTime - airdrop_time);
  87. if (time > 0) {
  88. //上次的
  89. this.lastTime = airdrop_time;
  90. this.ui.time.getComponent(cc.Label).string = `${Utils.getTime(time)}`;
  91. if (AccountModel.getInstance().is_airdrop) {
  92. this.changeBtnState(false);
  93. }
  94. } else {
  95. AccountModel.getInstance().is_airdrop = false;
  96. this.lastTime = curTime;
  97. }
  98. this.refreshTime();
  99. }
  100. refreshInfo() {
  101. let result = [];
  102. for (let key in this.typeData) {
  103. const arr = this.typeData[key];
  104. const info = arr[Math.floor(Math.random() * arr.length)];
  105. result[key] = info;
  106. }
  107. this.refreshItemView(result);
  108. AccountModel.singleInstance.airdrop_rewards = result;
  109. this.result = result;
  110. }
  111. //zh ad diy
  112. public refreshInfoForAdEventKtsx() {
  113. let result = [];
  114. for (let key in this.typeData) {
  115. const arr = this.typeData[key];
  116. const info = arr[Math.floor(Math.random() * arr.length)];
  117. result[key] = info;
  118. }
  119. this.refreshItemView(result);
  120. AccountModel.singleInstance.airdrop_rewards = result;
  121. this.result = result;
  122. }
  123. /**
  124. *刷新view层
  125. * @param result
  126. * @private
  127. */
  128. private refreshItemView(result: any[]) {
  129. this.ui.content.destroyAllChildren();
  130. for (let viewInfo of result) {
  131. if (!viewInfo) {
  132. continue;
  133. }
  134. const node = cc.instantiate(this.itemPrefab);
  135. const itemComponent = node.getComponent(UIAirdropItem);
  136. const data = itemDtManager.getDataByID(viewInfo.itemID);
  137. if (viewInfo.type == ItemType.role) {
  138. const modelInfo = modelDtlManager.getDataByID(viewInfo.modelID);
  139. itemComponent.refreshRole({
  140. itemname: modelInfo.desc,
  141. nums: viewInfo.nums,
  142. roleRes: modelInfo.modelname
  143. });
  144. } else {
  145. itemComponent.refresh({itemname: data.itemname, nums: viewInfo.nums, resPath1: data.resPath1});
  146. }
  147. this.ui.content.addChild(node)
  148. }
  149. }
  150. refreshTime() {
  151. AccountModel.getInstance().airdrop_time = this.lastTime;
  152. this.cb = () => {
  153. let curTime = Utils.getTimeStamp();
  154. let time = needRefreshTime - (curTime - this.lastTime);
  155. if (time <= 0) {
  156. this.changeBtnState(true);
  157. this.refreshInfo();
  158. this.lastTime = curTime;
  159. AccountModel.getInstance().airdrop_time = this.lastTime;
  160. }
  161. this.ui.time.getComponent(cc.Label).string = `${Utils.getTime(time)}`;
  162. }
  163. this.schedule(this.cb, 1);
  164. }
  165. onHide() {
  166. }
  167. onStart() {
  168. }
  169. /**
  170. * 添加数值
  171. *
  172. * */
  173. clickGet() {
  174. if (!this.result) {
  175. return;
  176. }
  177. zjSdk?.sendEvent('空投-全部获取')
  178. let self = this
  179. if(2>1){//zh:diy
  180. if (cc.sys.os === cc.sys.OS_ANDROID) {
  181. GlobalManager.getInstance().registerMethod('doGetForAdEventKt', self.doGetForAdEventKt.bind(this));
  182. let adKey = "zh_ad_type"
  183. if (ATRewardedVideoSDK.hasAdReady(AAJS2.getPlacementId())) {
  184. cc.sys.localStorage.setItem(adKey, 'doGetForAdEventKt');
  185. console.log('zh:AD ready for idx2')
  186. ATRewardedVideoSDK.showAd(AAJS2.getPlacementId());
  187. } else {
  188. console.log('zh:AD not ready for idx2')
  189. self.doGetForAdEventKt();
  190. }
  191. }else{
  192. self.doGetForAdEventKt();
  193. }
  194. return;
  195. }
  196. const params = {
  197. payType: zjSdk?.TYPE.VIDEO,
  198. success() {
  199. zjSdk?.sendEvent('空投-成功获取')
  200. self.doGet();
  201. },
  202. fail() {
  203. // self.doGet();
  204. }
  205. };
  206. zjSdk?.doPay(params);
  207. }
  208. private doGet() {
  209. console.log('zh:doGet 被触发');
  210. const food = this.result[ItemType.food];
  211. const bullet = this.result[ItemType.bullet];
  212. const coldWeapon = this.result[ItemType.coldWeapon];
  213. const hotWeapon = this.result[ItemType.hotWeapon];
  214. const role = this.result[ItemType.role];
  215. let incNum = this.getIncNums(food);
  216. gameData.curPowerNum += incNum;
  217. incNum = this.getIncNums(bullet);
  218. gameData.curBulletNum += incNum;
  219. let damage = itemDtManager.getDataByID(coldWeapon.itemID).value;
  220. gameEventManager.emit(EVENT_TYPE.ADD_WEAPONS, coldWeapon.itemID, damage, true);
  221. damage = itemDtManager.getDataByID(hotWeapon.itemID).value;
  222. gameEventManager.emit(EVENT_TYPE.ADD_WEAPONS, hotWeapon.itemID, damage, false);
  223. gameEventManager.emit(EVENT_TYPE.ADD_HERO_BY_ID, role.modelID);
  224. //奖励
  225. const node = cc.instantiate(this.obtainPrefab);
  226. const obtainView = node.getComponent(UIObtainView);
  227. obtainView.showByWelfare(this.result);
  228. this.node.addChild(node);
  229. AccountModel.getInstance().is_airdrop = true;
  230. this.changeBtnState(false);
  231. }
  232. //ZH DIY AD 空投获得
  233. private doGetForAdEventKt() {
  234. console.log('zh:doGetForAdEventKt 被触发');
  235. const food = this.result[ItemType.food];
  236. const bullet = this.result[ItemType.bullet];
  237. const coldWeapon = this.result[ItemType.coldWeapon];
  238. const hotWeapon = this.result[ItemType.hotWeapon];
  239. const role = this.result[ItemType.role];
  240. let incNum = this.getIncNums(food);
  241. gameData.curPowerNum += incNum;
  242. incNum = this.getIncNums(bullet);
  243. gameData.curBulletNum += incNum;
  244. let damage = itemDtManager.getDataByID(coldWeapon.itemID).value;
  245. gameEventManager.emit(EVENT_TYPE.ADD_WEAPONS, coldWeapon.itemID, damage, true);
  246. damage = itemDtManager.getDataByID(hotWeapon.itemID).value;
  247. gameEventManager.emit(EVENT_TYPE.ADD_WEAPONS, hotWeapon.itemID, damage, false);
  248. gameEventManager.emit(EVENT_TYPE.ADD_HERO_BY_ID, role.modelID);
  249. //奖励
  250. const node = cc.instantiate(this.obtainPrefab);
  251. const obtainView = node.getComponent(UIObtainView);
  252. obtainView.showByWelfare(this.result);
  253. this.node.addChild(node);
  254. AccountModel.getInstance().is_airdrop = true;
  255. this.changeBtnState(false);
  256. }
  257. private changeBtnState(flag: boolean) {
  258. this.ui.btn_get.getComponent(cc.Button).interactable = flag;
  259. this.ui.btn_refrsh.getComponent(cc.Button).interactable = flag;
  260. }
  261. private getIncNums(item) {
  262. const itemValue = itemDtManager.getItemValue(item.itemID);
  263. let incNum = itemValue ;
  264. return incNum;
  265. }
  266. clickRefresh() {
  267. let self = this
  268. zjSdk?.sendEvent('空投-刷新按钮')
  269. if(2>1){//zh:diy
  270. if (cc.sys.os === cc.sys.OS_ANDROID) {
  271. GlobalManager.getInstance().registerMethod('refreshInfoForAdEventKtsx', this.refreshInfoForAdEventKtsx.bind(this));
  272. let adKey = "zh_ad_type"
  273. if (ATRewardedVideoSDK.hasAdReady(AAJS2.getPlacementId())) {
  274. cc.sys.localStorage.setItem(adKey, 'refreshInfoForAdEventKtsx');
  275. console.log('zh:AD ready for idx2')
  276. ATRewardedVideoSDK.showAd(AAJS2.getPlacementId());
  277. } else {
  278. console.log('zh:AD not ready for idx2')
  279. self.refreshInfoForAdEventKtsx();
  280. }
  281. }else{
  282. self.refreshInfoForAdEventKtsx();
  283. }
  284. return;
  285. }
  286. //下面是原始的代码
  287. const params = {
  288. payType: zjSdk?.TYPE.VIDEO,
  289. success() {
  290. zjSdk?.sendEvent('空投-成功刷新')
  291. self.refreshInfo()
  292. },
  293. fail() {
  294. Log.log(LOG_TAG.DEBUG,"失败")
  295. // self.refreshInfo()
  296. }
  297. };
  298. zjSdk?.doPay(params);
  299. }
  300. clickClose() {
  301. gameEventManager.emit(EVENT_TYPE.CHANGE_STATUS, false);
  302. this.onClose();
  303. }
  304. onClose() {
  305. UIHelp.CloseUI(UIAirdropView);
  306. }
  307. }