UIAirdropView.ts 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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. const {ccclass, menu, property} = cc._decorator;
  16. const enum ItemType {
  17. food = 1,
  18. bullet = 2,
  19. coldWeapon = 3,
  20. hotWeapon = 4,
  21. role = 5,
  22. }
  23. // ui 打开暂停游戏
  24. const needRefreshTime = 10 * 60;
  25. @ccclass
  26. @menu("UI/Interface/UIAirdropView")
  27. export default class UIAirdropView extends UIBase {
  28. ui: auto_airdropView = null;
  29. //
  30. protected static prefabUrl = "Interface/airdropView";
  31. protected static className = "UIAirdropView";
  32. @property(cc.Prefab)
  33. itemPrefab: cc.Prefab = null;
  34. @property(cc.Prefab)
  35. obtainPrefab: cc.Prefab = null;
  36. /**
  37. * 对应上面的枚举
  38. * @private
  39. */
  40. private typeData = {
  41. 1: [],
  42. 2: [],
  43. 3: [],
  44. 4: [],
  45. 5: [],
  46. }
  47. private result: any[] = [];
  48. private lastTime = null;
  49. private cb: Function = null;
  50. onUILoad() {
  51. this.ui = this.node.addComponent(auto_airdropView);
  52. }
  53. onShow() {
  54. const infos = rewardDtMgr.getInstance().getAirDropRewardList();
  55. for (let key in infos) {
  56. const info = infos[key];
  57. this.typeData[info.type].push(info);
  58. }
  59. const airdropRewards = AccountModel.getInstance().airdrop_rewards;
  60. if (airdropRewards.length == 0) {
  61. this.refreshInfo();
  62. }else {
  63. this.result = airdropRewards;
  64. this.refreshItemView(this.result);
  65. }
  66. let airdrop_time = AccountModel.getInstance().airdrop_time;
  67. if (!airdrop_time) {
  68. airdrop_time = Utils.getTimeStamp();
  69. }
  70. let curTime = Utils.getTimeStamp();
  71. let time = needRefreshTime - (curTime - airdrop_time);
  72. if (time > 0) {
  73. //上次的
  74. this.lastTime = airdrop_time;
  75. this.ui.time.getComponent(cc.Label).string = `${Utils.getTime(time)}`;
  76. if (AccountModel.getInstance().is_airdrop) {
  77. this.changeBtnState(false);
  78. }
  79. } else {
  80. AccountModel.getInstance().is_airdrop = false;
  81. this.lastTime = curTime;
  82. }
  83. this.refreshTime();
  84. }
  85. refreshInfo() {
  86. let result = [];
  87. for (let key in this.typeData) {
  88. const arr = this.typeData[key];
  89. const info = arr[Math.floor(Math.random() * arr.length)];
  90. result[key] = info;
  91. }
  92. this.refreshItemView(result);
  93. AccountModel.singleInstance.airdrop_rewards = result;
  94. this.result = result;
  95. }
  96. /**
  97. *刷新view层
  98. * @param result
  99. * @private
  100. */
  101. private refreshItemView(result: any[]) {
  102. this.ui.content.destroyAllChildren();
  103. for (let viewInfo of result) {
  104. if (!viewInfo) {
  105. continue;
  106. }
  107. const node = cc.instantiate(this.itemPrefab);
  108. const itemComponent = node.getComponent(UIAirdropItem);
  109. const data = itemDtManager.getDataByID(viewInfo.itemID);
  110. if (viewInfo.type == ItemType.role) {
  111. const modelInfo = modelDtlManager.getDataByID(viewInfo.modelID);
  112. itemComponent.refreshRole({
  113. itemname: modelInfo.desc,
  114. nums: viewInfo.nums,
  115. roleRes: modelInfo.modelname
  116. });
  117. } else {
  118. itemComponent.refresh({itemname: data.itemname, nums: viewInfo.nums, resPath1: data.resPath1});
  119. }
  120. this.ui.content.addChild(node)
  121. }
  122. }
  123. refreshTime() {
  124. AccountModel.getInstance().airdrop_time = this.lastTime;
  125. this.cb = () => {
  126. let curTime = Utils.getTimeStamp();
  127. let time = needRefreshTime - (curTime - this.lastTime);
  128. if (time <= 0) {
  129. this.changeBtnState(true);
  130. this.refreshInfo();
  131. this.lastTime = curTime;
  132. AccountModel.getInstance().airdrop_time = this.lastTime;
  133. }
  134. this.ui.time.getComponent(cc.Label).string = `${Utils.getTime(time)}`;
  135. }
  136. this.schedule(this.cb, 1);
  137. }
  138. onHide() {
  139. }
  140. onStart() {
  141. }
  142. /**
  143. * 添加数值
  144. *
  145. * */
  146. clickGet() {
  147. if (!this.result) {
  148. return;
  149. }
  150. zjSdk?.sendEvent('空投-全部获取')
  151. let self = this
  152. const params = {
  153. payType: zjSdk?.TYPE.VIDEO,
  154. success() {
  155. zjSdk?.sendEvent('空投-成功获取')
  156. self.doGet();
  157. },
  158. fail() {
  159. // self.doGet();
  160. }
  161. };
  162. zjSdk?.doPay(params);
  163. }
  164. private doGet() {
  165. const food = this.result[ItemType.food];
  166. const bullet = this.result[ItemType.bullet];
  167. const coldWeapon = this.result[ItemType.coldWeapon];
  168. const hotWeapon = this.result[ItemType.hotWeapon];
  169. const role = this.result[ItemType.role];
  170. let incNum = this.getIncNums(food);
  171. gameData.curPowerNum += incNum;
  172. incNum = this.getIncNums(bullet);
  173. gameData.curBulletNum += incNum;
  174. let damage = itemDtManager.getDataByID(coldWeapon.itemID).value;
  175. gameEventManager.emit(EVENT_TYPE.ADD_WEAPONS, coldWeapon.itemID, damage, true);
  176. damage = itemDtManager.getDataByID(hotWeapon.itemID).value;
  177. gameEventManager.emit(EVENT_TYPE.ADD_WEAPONS, hotWeapon.itemID, damage, false);
  178. gameEventManager.emit(EVENT_TYPE.ADD_HERO_BY_ID, role.modelID);
  179. //奖励
  180. const node = cc.instantiate(this.obtainPrefab);
  181. const obtainView = node.getComponent(UIObtainView);
  182. obtainView.showByWelfare(this.result);
  183. this.node.addChild(node);
  184. AccountModel.getInstance().is_airdrop = true;
  185. this.changeBtnState(false);
  186. }
  187. private changeBtnState(flag: boolean) {
  188. this.ui.btn_get.getComponent(cc.Button).interactable = flag;
  189. this.ui.btn_refrsh.getComponent(cc.Button).interactable = flag;
  190. }
  191. private getIncNums(item) {
  192. const itemValue = itemDtManager.getItemValue(item.itemID);
  193. let incNum = itemValue ;
  194. return incNum;
  195. }
  196. clickRefresh() {
  197. let self = this
  198. zjSdk?.sendEvent('空投-刷新按钮')
  199. const params = {
  200. payType: zjSdk?.TYPE.VIDEO,
  201. success() {
  202. zjSdk?.sendEvent('空投-成功刷新')
  203. self.refreshInfo()
  204. },
  205. fail() {
  206. Log.log(LOG_TAG.DEBUG,"失败")
  207. // self.refreshInfo()
  208. }
  209. };
  210. zjSdk?.doPay(params);
  211. }
  212. clickClose() {
  213. gameEventManager.emit(EVENT_TYPE.CHANGE_STATUS, false);
  214. this.onClose();
  215. }
  216. onClose() {
  217. UIHelp.CloseUI(UIAirdropView);
  218. }
  219. }