PogBoosterItem.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. import { _decorator, Component, Node } from "cc";
  2. import BaseUI from "../../scripts/base/BaseUI";
  3. import { IPayListener, PayItems, PayType } from "../common/PayItems";
  4. import { Tips } from "../../scripts/mgr/Tips";
  5. import ShopM, { ShopDto } from "../../scripts/api/ShopM";
  6. import ConfigM, { ConfigShopItem } from "../../scripts/api/ConfigM";
  7. import { Event } from "cc";
  8. import UserM from "../../scripts/api/UserM";
  9. import PushMsgM from "../../scripts/mgr/PushMsgM";
  10. import { RewardLayer } from "../layer/RewardLayer";
  11. import ItemsM from "../../scripts/mgr/ItemsM";
  12. import { Sprite } from "cc";
  13. import PogAd from "../../scripts/mgr/PogAd";
  14. const { ccclass, property } = _decorator;
  15. @ccclass("PogBoosterItem")
  16. export class PogBoosterItem extends BaseUI implements IPayListener {
  17. public static readonly TYPE_GAME_PASS = 1;
  18. public static readonly TYPE_POG_BOOSTER = 2;
  19. public static readonly TYPE_ITEM_BOX = 3;
  20. public static readonly TYPE_POG_BOX = 4;
  21. public type: number;
  22. onPay(type: PayType, price: number) {
  23. switch (type) {
  24. case PayType.Gem:
  25. this.buyUseGem();
  26. break;
  27. }
  28. }
  29. async buyUseGem() {
  30. let shopItemId = this._configItem.id;
  31. let num = this._configItem.num;
  32. let gem = this._configItem.diamondPrice;
  33. // UserM.ins.subtractGoods(GoodsId.GEM, gem);
  34. let result = await ShopM.ins.buyUseGem(shopItemId, num, gem);
  35. if (result) {
  36. result.changeList.forEach(async (item) => {
  37. if (item.count > 0 && ConfigM.ins.GoodIsBox(item.id)) {
  38. let newResult = await ShopM.ins.openBox(item.id, item.count);
  39. if (newResult) {
  40. ItemsM.ins.itemChange(newResult.changeList, newResult.goodList);
  41. }
  42. } else {
  43. ItemsM.ins.itemChange(result.changeList, result.goodList);
  44. }
  45. });
  46. }
  47. }
  48. private _data: ShopDto;
  49. private _configItem: ConfigShopItem;
  50. private _payItems: PayItems;
  51. init(type: number, item: ShopDto) {
  52. this.type = type;
  53. this._data = item;
  54. this._configItem = ConfigM.ins.getConfigShopIteByShopId(type, item.id);
  55. this.setText("lbl_times", "x" + this._configItem.num);
  56. this.setText("lbl_title", ""+ConfigM.ins.getGoodName(this._configItem.goodId));
  57. this.setIcon();
  58. this._payItems = this.getComponentInChildren(PayItems);
  59. let btnAD = this.FindNode("btn_ad");
  60. this._payItems.node.active = true;
  61. let showAd = this._configItem.adPrice > 0;
  62. this._payItems.node.active = !showAd;
  63. btnAD.active = showAd;
  64. if (showAd) {
  65. //
  66. if (this._data.dailyUserAdSell >= this._configItem.dailyUserAdLimit) {
  67. this.FindAs("btn_ad", Sprite).grayscale = true;
  68. }
  69. } else {
  70. this._payItems.init(
  71. [
  72. {
  73. type: PayType.Gem,
  74. price: this._configItem.diamondPrice,
  75. },
  76. ],
  77. this
  78. );
  79. }
  80. }
  81. setIcon() {
  82. let icons = this.FindNode("icons");
  83. icons.children.forEach((child) => {
  84. child.active = false;
  85. });
  86. switch (this.type) {
  87. case PogBoosterItem.TYPE_GAME_PASS:
  88. break;
  89. case PogBoosterItem.TYPE_POG_BOOSTER:
  90. icons.getChildByName("icon_crit").active = true;
  91. break;
  92. case PogBoosterItem.TYPE_ITEM_BOX:
  93. icons.getChildByName("icon_item_box").active = true;
  94. break;
  95. case PogBoosterItem.TYPE_POG_BOX:
  96. icons.getChildByName("icon_pog_box").active = true;
  97. }
  98. }
  99. getName(): string {
  100. switch (this.type) {
  101. case PogBoosterItem.TYPE_GAME_PASS:
  102. return "Game Pass";
  103. case PogBoosterItem.TYPE_POG_BOOSTER:
  104. return "POG Crit Tickets";
  105. case PogBoosterItem.TYPE_ITEM_BOX:
  106. return "Item Box";
  107. case PogBoosterItem.TYPE_POG_BOX:
  108. return "Pog Box";
  109. }
  110. return "Unknown";
  111. }
  112. private _requestingAD: boolean = false;
  113. private async buyUseAD() {
  114. let self = this;
  115. // Tips.show("AD Coming soon");
  116. if (this._data.dailyUserAdSell >= this._configItem.dailyUserAdLimit) {
  117. this.FindAs("btn_ad", Sprite).grayscale = true;
  118. Tips.show("AD Limit");
  119. return;
  120. }
  121. if (this._requestingAD) {
  122. return;
  123. }
  124. self._requestingAD = true;
  125. setTimeout(() => {
  126. self._requestingAD = false;
  127. }, 3000);
  128. let showAdResult = await PogAd.ins.showAd();
  129. if (!showAdResult) {
  130. return;
  131. }
  132. Tips.show("AD Success");
  133. let result = await ShopM.ins.buyUseAD(
  134. this._configItem.id,
  135. this._configItem.num,
  136. this._configItem.adPrice
  137. );
  138. if (result) {
  139. ItemsM.ins.itemChange(result.changeList, result.goodList);
  140. this._data.dailyUserAdSell += 1;
  141. this.init(this.type, this._data);
  142. }
  143. }
  144. protected onBtnClick(name: string, event: Event, customEventData: any): void {
  145. if (name == "btn_ad") {
  146. this.buyUseAD();
  147. }
  148. }
  149. }