ADHandler.ts 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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 16:41:31
  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. smc.game.GameModel.isShowAd = true;
  27. const { method, param } = this.buildAdParam(id);
  28. const data: CocosHandlerType = {
  29. method,
  30. param: JSON.stringify(param)
  31. };
  32. return await CocosHandler.inst.sendMessageToAndroid(data, `广告 ${id}`);
  33. };
  34. private buildAdParam(id: string): { method: string, param: any } {
  35. const param: any = {
  36. funcId: id,
  37. callback: {
  38. onLoaded: '',
  39. onShow: '',
  40. onLoadFailed: '',
  41. onShowFailed: '',
  42. onClose: ''
  43. }
  44. };
  45. let method = '';
  46. switch (id) {
  47. case AD_TYPE.Start:
  48. method = 'ad.splash';
  49. param.callback.onLoadFailed = 'ADHandler.inst.adSplashLoadFailed';
  50. param.callback.onClose = 'ADHandler.inst.adSplashClose';
  51. break;
  52. case AD_TYPE.Jion_Main:
  53. case AD_TYPE.Double_Close:
  54. case AD_TYPE.Rebates:
  55. case AD_TYPE.Double_Speed_Close:
  56. method = 'ad.interstitial';
  57. param.callback.onLoadFailed = 'ADHandler.inst.adInterstitialLoadFailed';
  58. param.callback.onClose = 'ADHandler.inst.adInterstitialClose';
  59. break;
  60. default:
  61. method = 'ad.reward';
  62. param.callback.onShow = 'ADHandler.inst.adRewardShow';
  63. param.callback.onClose = 'ADHandler.inst.adRewardClose';
  64. param.callback.onLoadFailed = 'ADHandler.inst.adRewardLoadFailed';
  65. }
  66. return { method, param };
  67. }
  68. //启屏广告关闭
  69. adSplashClose = async () => {
  70. console.log("启屏广告关闭")
  71. smc.game.GameModel.isShowAd = false;
  72. if (oops.gui.has(UIID.KindTips)) oops.gui.remove(UIID.KindTips);
  73. if (oops.gui.has(UIID.Retention)) oops.gui.remove(UIID.Retention);
  74. const result = await LoginHandler.inst.savePrivacyStatus(true);
  75. if (result?.code === 0) {
  76. ServerHandler.inst.getAccountInfo();
  77. }
  78. };
  79. adSplashLoadFailed = () => {
  80. smc.game.GameModel.isShowAd = false;
  81. console.log('[广告] 启屏广告加载失败');
  82. ServerHandler.inst.getAccountInfo();
  83. };
  84. adInterstitialLoadFailed = () => {
  85. console.log('[广告] 插屏广告加载失败');
  86. oops.message.dispatchEvent(GameEvent.updateGameState, "playing");
  87. };
  88. adInterstitialClose = (type: boolean) => {
  89. smc.game.GameModel.isShowAd = false;
  90. console.log('[广告] 插屏广告关闭', type);
  91. //如果有弹窗,则不发送事件,
  92. oops.message.dispatchEvent(GameEvent.updateGameState, "playing");
  93. };
  94. adRewardShow = (str: string) => {
  95. const data = JSON.parse(str);
  96. ServerHandler.inst.getSign(data.price);
  97. };
  98. adRewardLoadFailed = (str: string) => {
  99. smc.game.GameModel.isShowAd = false;
  100. oops.message.dispatchEvent(GameEvent.updateGameState, "playing");
  101. //展示失败
  102. if (smc.game.GameModel.viewType === "revive_reward") {
  103. const score = smc.game.GameModel.curScore;
  104. smc.game.GameModel.viewType = "";
  105. smc.game.GameModel.curScore = Math.floor(score / 2);
  106. oops.message.dispatchEvent(GameEvent.RestartGame);
  107. }
  108. };
  109. adRewardClose = (state: boolean) => {
  110. smc.game.GameModel.isShowAd = false;
  111. console.log("[广告] 激励视频关闭", state)
  112. smc.game.GameModel.isDone = state;
  113. oops.message.dispatchEvent(GameEvent.updateGameState, "playing");
  114. if (state) {
  115. if (smc.game.GameModel.viewType === "speed_reward") {
  116. //直接成功-增加时长
  117. oops.message.dispatchEvent(GameEvent.DoubleSpeedOpenSuccess);
  118. smc.game.GameModel.viewType = "";
  119. return;
  120. }
  121. //如果是复活,分数减半
  122. if (smc.game.GameModel.viewType === "revive_reward") {
  123. const score = smc.game.GameModel.curScore;
  124. smc.game.GameModel.curScore = Math.floor(score / 2);
  125. oops.message.dispatchEvent(GameEvent.RestartGame);
  126. }
  127. ServerHandler.inst.getVideorReward();
  128. }
  129. };
  130. }
  131. window["ADHandler"] = ADHandler;