OpenRedBagPanel.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. import { utils } from "./Utils";
  2. import { BannerLocation, SubLocation } from "./YZ_Constant";
  3. import PlatUtils from "./PlatUtils";
  4. import { YzRedBagInfo } from "./CommonConfig";
  5. const { ccclass, property } = cc._decorator;
  6. @ccclass
  7. export default class OpenRedBagPanel extends cc.Component {
  8. private _panel: cc.Node = null;
  9. private _moneyLbl: cc.Label = null;
  10. private _totalMoneyLbl: cc.Label = null;
  11. private _openPanel: cc.Node = null;
  12. private _rewardPanel: cc.Node = null;
  13. private _btnOpen: cc.Node = null;
  14. private _redBagInfo: YzRedBagInfo = null;
  15. private _btnClose: cc.Node = null;
  16. private _btnVideoOpen: cc.Node = null;
  17. private _title: cc.Node = null;
  18. private _btnDouble: cc.Node = null;
  19. private _proInfo: any = null;
  20. // _location: SubLocation = SubLocation.isMoreGame;
  21. onLoad() {
  22. if (utils.otherConfig && utils.otherConfig.group) {
  23. this.node.group = utils.otherConfig.group;
  24. }
  25. if (!PlatUtils.IsNativeAndroid) {
  26. utils.adManager.HideBanner(BannerLocation.Home);
  27. }
  28. this._panel = this.node.getChildByName("Panel");
  29. this._openPanel = this._panel.getChildByName("onOpenPanel");
  30. this._openPanel.setScale(0.5);
  31. this._openPanel.runAction(cc.scaleTo(0.3, 1));
  32. this._rewardPanel = this._panel.getChildByName("rewardPanel");
  33. this._btnOpen = this._openPanel.getChildByName("btnOpen");
  34. this._btnClose = this._panel.getChildByName("btnClose");
  35. this._btnVideoOpen = this._btnOpen.getChildByName("btn_openVideo");
  36. this._title = this._openPanel.getChildByName("title");
  37. this._totalMoneyLbl = this._rewardPanel.getChildByName("totalMoneyLabel").getComponent(cc.Label);
  38. this._moneyLbl = this._rewardPanel.getChildByName("moneyLbl").getComponent(cc.Label);
  39. this._btnDouble = this._rewardPanel.getChildByName("btnDouble");
  40. setTimeout(() => {
  41. this._btnOpen.runAction(cc.repeatForever(cc.sequence(cc.scaleTo(0.5, 1.2), cc.scaleTo(0.5, 1))));
  42. }, 500);
  43. this._redBagInfo = utils.yzRedBagInfo;
  44. this._proInfo = utils.yzRedBagInfo.curProgressInfo;
  45. let ratio: number = 1;
  46. if (cc.winSize.height < cc.winSize.width) {
  47. // 横屏游戏
  48. ratio = cc.winSize.width / 1920 * 0.5;
  49. } else {
  50. ratio = cc.winSize.width / 1080;
  51. }
  52. this._panel.scale = ratio;
  53. this.init();
  54. }
  55. _money: number = 0;
  56. openRedBag() {
  57. if (this._redBagInfo.progress < this._redBagInfo.totalProgress && !this._redBagInfo.isFreeRedBag && this._redBagInfo.freeRedBagCount <= 0) return;
  58. this._btnClose.opacity = 0;
  59. this._btnClose.active = true;
  60. let timeout: number = utils.ServerConfig.red_bag_close_btn_show_delay ? utils.ServerConfig.red_bag_close_btn_show_delay : 0;
  61. setTimeout(() => {
  62. utils.showLog("拆红包关闭按钮延迟显示 " + timeout + "秒显示!");
  63. this._btnClose.runAction(cc.fadeIn(0.3));
  64. }, timeout * 1000)
  65. setTimeout(() => {
  66. this._btnDouble.runAction(cc.repeatForever(cc.sequence(cc.scaleTo(0.5, 1.2), cc.scaleTo(0.5, 1))));
  67. }, 500);
  68. if (this._canShowVideoOpen) {
  69. utils.adManager.ShowVideo((res, msg) => {
  70. if (res) {
  71. utils.SendEvent('拆红包弹窗-视频拆红包成功!');
  72. this.scheduleOnce(() => {
  73. this.showRedBag();
  74. })
  75. } else {
  76. this._btnClose.runAction(cc.fadeIn(0.3));
  77. utils.SendEvent('拆红包弹窗-视频拆红包失败!');
  78. utils.showMsg(msg ? msg : "视频加载失败!");
  79. }
  80. })
  81. } else {
  82. this.showRedBag();
  83. }
  84. }
  85. showRedBag() {
  86. let money = 0;
  87. let proInfo = this._proInfo;
  88. cc.log("proInfo ", JSON.stringify(proInfo));
  89. money = parseFloat((Math.random() * (proInfo.max_money - proInfo.min_money) + proInfo.min_money).toFixed(3));
  90. cc.log("红包金额:" + money);
  91. // let max = this._redBagInfo.withdrawaMoneys[0];
  92. // if (this._redBagInfo.balance <= 0) {
  93. // money = parseFloat((Math.random() * (max / 4)).toFixed(3));
  94. // } else if (this._redBagInfo.balance <= (max / 2.8)) {
  95. // money = parseFloat((Math.random() * (max / 5)).toFixed(3));
  96. // } else if (this._redBagInfo.balance >= (max - 1)) {
  97. // money = Math.random();
  98. // money = parseFloat((money * 0.01 + 0.001).toFixed(3))
  99. // } else if (this._redBagInfo.balance >= max - 2) {
  100. // money = Math.random();
  101. // money = parseFloat((money * 0.01 + 0.001).toFixed(3))
  102. // } else {
  103. // money = Math.random();
  104. // if (money > 0.1) {
  105. // money = parseFloat((money * 0.1).toFixed(3))
  106. // } else {
  107. // money = parseFloat(money.toFixed(3));
  108. // }
  109. // }
  110. // utils.SendEvent("拆红包弹窗-获得红包:" + money + "元");
  111. this._money = money;
  112. this._redBagInfo.balance = parseFloat((this._redBagInfo.balance + money).toFixed(3));
  113. if (this._redBagInfo.isFreeRedBag && this._show_type == 1) {
  114. this._redBagInfo.lastOpenFreeRedBagTime = new Date().toDateString();
  115. }
  116. if (this._show_type == 2) {
  117. this._redBagInfo.progress -= this._redBagInfo.totalProgress;
  118. }
  119. this._redBagInfo.totalMoney = parseFloat((this._redBagInfo.totalMoney + money).toFixed(3));
  120. this._moneyLbl.string = `¥${money}元`
  121. this._totalMoneyLbl.string = `累计获得现金${this._redBagInfo.totalMoney}元`
  122. this._rewardPanel.scaleX = 0;
  123. this._rewardPanel.active = true;
  124. this._openPanel.runAction(cc.sequence(cc.scaleTo(0.3, 0, 1), cc.callFunc(() => {
  125. this._openPanel.active = false;
  126. this._rewardPanel.runAction(cc.sequence(cc.scaleTo(0.3, 1), cc.callFunc(() => {
  127. })))
  128. })))
  129. if (utils.currentLevel > parseInt(this._redBagInfo.lastOpenLevel) && this._show_type == 2) {
  130. this._redBagInfo.lastOpenLevel = utils.currentLevel.toString();
  131. }
  132. }
  133. onBtnDoubleMoney(event, data) {
  134. utils.SendEvent("拆红包弹窗-点击视频双倍领取");
  135. utils.adManager.ShowVideo((ret, msg) => {
  136. if (ret) {
  137. this._redBagInfo.balance = parseFloat((this._redBagInfo.balance + this._money).toFixed(3));
  138. this._redBagInfo.totalMoney = parseFloat((this._redBagInfo.totalMoney + this._money).toFixed(3));
  139. this._totalMoneyLbl.string = `累计获得现金${this._redBagInfo.totalMoney}元`
  140. utils.showMsg("领取双倍红包成功!");
  141. utils.SendEvent("拆红包弹窗-视频双倍领取成功");
  142. event.target.active = false;
  143. } else {
  144. utils.showMsg(msg);
  145. utils.SendEvent("拆红包弹窗-视频双倍领取失败");
  146. }
  147. })
  148. }
  149. showWithDrawalPanel() {
  150. utils.SendEvent("拆红包弹窗-点击提现上报");
  151. utils.showWithdrawalPanel();
  152. }
  153. // update() {
  154. // if (this._dataDirty) {
  155. // this._dataDirty = false;
  156. // this._updatePanel();
  157. // }
  158. // }
  159. _canShowVideoOpen: boolean = false;
  160. public init() {
  161. utils.SendEvent("拆红包弹窗-展示成功!" + this._show_type + " :" + this._redBagInfo.isFreeRedBag);
  162. this._title.children[0].active = this._show_type == 1 && this._redBagInfo.isFreeRedBag;
  163. this._title.children[1].active = this._show_type == 2;
  164. this._title.children[2].active = this._show_type == 3;
  165. if (this._proInfo.type == 2 && this._show_type == 2) {
  166. this._canShowVideoOpen = true;
  167. this._btnVideoOpen.active = true;
  168. } else {
  169. this._canShowVideoOpen = false;
  170. this._btnVideoOpen.active = false;
  171. }
  172. // utils.ServerConfig.red_bag_open_view_close_btn_show_delay = 1;
  173. if (utils.ServerConfig.red_bag_open_view_close_btn_show_delay && utils.ServerConfig.red_bag_open_view_close_btn_show_delay > 0) {
  174. this._btnClose.opacity = 0;
  175. let timeout: number = utils.ServerConfig.red_bag_open_view_close_btn_show_delay ? utils.ServerConfig.red_bag_open_view_close_btn_show_delay : 0;
  176. setTimeout(() => {
  177. utils.showLog("拆红包关闭按钮延迟显示 " + timeout + "秒显示!");
  178. this._btnClose.runAction(cc.fadeIn(0.3));
  179. }, timeout * 1000)
  180. } else {
  181. if (this._redBagInfo.progress < this._redBagInfo.totalProgress && !this._redBagInfo.isFreeRedBag && this._redBagInfo.freeRedBagCount <= 0) {
  182. this._btnOpen.children[0].active = true;
  183. this._btnOpen.children[1].getComponent(cc.Button).enableAutoGrayEffect = true;
  184. this._btnClose.opacity = 0;
  185. let timeout: number = utils.ServerConfig.red_bag_close_btn_show_delay ? utils.ServerConfig.red_bag_close_btn_show_delay : 0;
  186. setTimeout(() => {
  187. utils.showLog("拆红包关闭按钮延迟显示 " + timeout + "秒显示!");
  188. this._btnClose.runAction(cc.fadeIn(0.3));
  189. }, timeout * 1000)
  190. } else {
  191. this._btnClose.active = false;
  192. }
  193. }
  194. this._totalMoneyLbl.string = `累计获得现金${this._redBagInfo.totalMoney}元`;
  195. }
  196. _show_type: number = 1; // 1、每日红包 2、闯关红包 3、现金红包
  197. initData(showType: number) {
  198. this._show_type = showType;
  199. }
  200. public show() {
  201. this.node.active = true;
  202. }
  203. public hide() {
  204. // console.log(utils.rewardCloseFunc, "<<callFUnc");
  205. // this._panel.runAction(cc.sequence(cc.moveTo(0.3, CompatibleTool.position(-this._panel.getContentSize().width, 0)).easing(cc.easeQuadraticActionOut()), cc.callFunc(() => {
  206. this.node.active = false;
  207. utils.rewardCloseFunc && utils.rewardCloseFunc();
  208. if (utils.rewardCloseFunc) {
  209. utils.rewardCloseFunc = null;
  210. }
  211. // })));
  212. // if (!PlatUtils.IsNativeAndroid) {
  213. // utils.adManager.ShowBanner(BannerLocation.Home);
  214. // }
  215. }
  216. public onCloseBtnHandler(event: any, data: any) {
  217. this.hide();
  218. }
  219. }