ADHandler.ts 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*
  2. * @Author: mojunshou 1637302775@qq.com
  3. * @Date: 2025-04-11 10:14:44
  4. * @LastEditors: mojunshou 1637302775@qq.com
  5. * @LastEditTime: 2025-04-23 20:53:36
  6. * @Description:
  7. */
  8. // ADHandler.ts
  9. import { CocosHandlerType, CocosHandler } from './CocosHandler';
  10. import { AD_TYPE } from '../config/GameDefine';
  11. import { oops } from 'db://oops-framework/core/Oops';
  12. import { UIID } from '../config/GameUIConfig';
  13. import { smc } from '../SingletonModuleComp';
  14. import { ServerHandler } from './ServerHandler';
  15. import { LoginHandler } from './LoginHandler';
  16. import { GameEvent } from '../config/GameEvent';
  17. export class ADHandler {
  18. private static _inst: ADHandler;
  19. public static get inst(): ADHandler {
  20. if (!this._inst) {
  21. this._inst = new ADHandler();
  22. }
  23. return this._inst;
  24. }
  25. showAd = async (id: string) => {
  26. oops.gui.waitOpen();
  27. smc.game.GameModel.isShowAd = true;
  28. const { method, param } = this.buildAdParam(id);
  29. const data: CocosHandlerType = {
  30. method,
  31. param: JSON.stringify(param)
  32. };
  33. return await CocosHandler.inst.sendMessageToAndroid(data, `广告 ${id}`);
  34. };
  35. private buildAdParam(id: string): { method: string, param: any } {
  36. const param: any = {
  37. funcId: id,
  38. callback: {
  39. onLoaded: '',
  40. onShow: '',
  41. onLoadFailed: '',
  42. onShowFailed: '',
  43. onClose: ''
  44. }
  45. };
  46. let method = '';
  47. switch (id) {
  48. case AD_TYPE.Start:
  49. method = 'ad.splash';
  50. param.callback.onLoadFailed = 'ADHandler.inst.adSplashLoadFailed';
  51. param.callback.onClose = 'ADHandler.inst.adSplashClose';
  52. break;
  53. case AD_TYPE.Jion_Main:
  54. case AD_TYPE.Double_Close:
  55. case AD_TYPE.Rebates:
  56. case AD_TYPE.Double_Speed_Close:
  57. method = 'ad.interstitial';
  58. param.callback.onLoadFailed = 'ADHandler.inst.adInterstitialLoadFailed';
  59. param.callback.onClose = 'ADHandler.inst.adInterstitialClose';
  60. break;
  61. default:
  62. method = 'ad.reward';
  63. param.callback.onShow = 'ADHandler.inst.adRewardShow';
  64. param.callback.onClose = 'ADHandler.inst.adRewardClose';
  65. param.callback.onLoadFailed = 'ADHandler.inst.adRewardLoadFailed';
  66. }
  67. return { method, param };
  68. }
  69. //启屏广告关闭
  70. adSplashClose = async () => {
  71. console.log("启屏广告关闭")
  72. smc.game.GameModel.isShowAd = false;
  73. if (oops.gui.has(UIID.KindTips)) oops.gui.remove(UIID.KindTips);
  74. if (oops.gui.has(UIID.Retention)) oops.gui.remove(UIID.Retention);
  75. const result = await LoginHandler.inst.savePrivacyStatus(true);
  76. if (result?.code === 0) {
  77. ServerHandler.inst.getAccountInfo();
  78. }
  79. oops.gui.waitClose();
  80. };
  81. adSplashLoadFailed = () => {
  82. smc.game.GameModel.isShowAd = false;
  83. console.log('[广告] 启屏广告加载失败');
  84. ServerHandler.inst.getAccountInfo();
  85. oops.gui.waitClose();
  86. };
  87. adInterstitialLoadFailed = () => {
  88. console.log('[广告] 插屏广告加载失败');
  89. oops.message.dispatchEvent(GameEvent.updateGameState, "playing");
  90. oops.gui.waitClose();
  91. };
  92. adInterstitialClose = (type: boolean) => {
  93. smc.game.GameModel.isShowAd = false;
  94. console.log('[广告] 插屏广告关闭', type);
  95. //如果有弹窗,则不发送事件,
  96. oops.message.dispatchEvent(GameEvent.updateGameState, "playing");
  97. oops.gui.waitClose();
  98. };
  99. adRewardShow = (str: string) => {
  100. const data = JSON.parse(str);
  101. ServerHandler.inst.getSign(data.price);
  102. oops.gui.waitClose();
  103. };
  104. adRewardLoadFailed = (str: string) => {
  105. smc.game.GameModel.isShowAd = false;
  106. oops.message.dispatchEvent(GameEvent.updateGameState, "playing");
  107. //展示失败
  108. if (smc.game.GameModel.viewType === "revive_reward") {
  109. const score = smc.game.GameModel.curScore;
  110. smc.game.GameModel.viewType = "";
  111. smc.game.GameModel.curScore = Math.floor(score / 2);
  112. oops.message.dispatchEvent(GameEvent.RestartGame);
  113. }
  114. oops.gui.waitClose();
  115. };
  116. adRewardClose = (state: boolean) => {
  117. smc.game.GameModel.isShowAd = false;
  118. console.log("[广告] 激励视频关闭", state)
  119. smc.game.GameModel.isDone = state;
  120. oops.message.dispatchEvent(GameEvent.updateGameState, "playing");
  121. if (state) {
  122. if (smc.game.GameModel.viewType === "speed_reward") {
  123. //直接成功-增加时长
  124. oops.message.dispatchEvent(GameEvent.DoubleSpeedOpenSuccess);
  125. smc.game.GameModel.viewType = "";
  126. return;
  127. }
  128. //如果是复活,分数减半
  129. if (smc.game.GameModel.viewType === "revive_reward") {
  130. oops.message.dispatchEvent(GameEvent.RestartGame);
  131. }
  132. ServerHandler.inst.getVideorReward();
  133. }
  134. oops.gui.waitClose();
  135. };
  136. }
  137. window["ADHandler"] = ADHandler;