PogBoosterItem.ts 4.7 KB

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