ADHandler.ts 5.1 KB

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