ADHandler.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. };
  80. adInterstitialClose = (type: boolean) => {
  81. smc.game.GameModel.isShowAd = false;
  82. console.log('[广告] 插屏广告关闭', type);
  83. };
  84. adRewardShow = (str: string) => {
  85. const data = JSON.parse(str);
  86. ServerHandler.inst.getSign(data.price);
  87. };
  88. adRewardLoadFailed = (str: string) => {
  89. smc.game.GameModel.isShowAd = false;
  90. console.log('[广告] 激励视频展示失败', str);
  91. //展示失败
  92. if (smc.game.GameModel.viewType === "revive_reward") {
  93. const score = smc.game.GameModel.curScore;
  94. smc.game.GameModel.viewType = "";
  95. smc.game.GameModel.curScore = Math.floor(score / 2);
  96. oops.message.dispatchEvent(GameEvent.RestartGame);
  97. }
  98. };
  99. adRewardClose = (state: boolean) => {
  100. smc.game.GameModel.isShowAd = false;
  101. console.log("[广告] 激励视频关闭", state)
  102. smc.game.GameModel.isDone = state;
  103. if (state) {
  104. //如果是二倍速,就加速就好,不用发放奖励
  105. console.log("激励视频关闭", smc.game.GameModel.viewType)
  106. if (smc.game.GameModel.viewType === "speed_reward") {
  107. //直接成功-增加时长
  108. oops.message.dispatchEvent(GameEvent.DoubleSpeedOpenSuccess);
  109. smc.game.GameModel.viewType = "";
  110. return;
  111. }
  112. //如果是复活,分数减半
  113. if (smc.game.GameModel.viewType === "revive_reward") {
  114. const score = smc.game.GameModel.curScore;
  115. smc.game.GameModel.curScore = Math.floor(score / 2);
  116. oops.message.dispatchEvent(GameEvent.RestartGame);
  117. }
  118. ServerHandler.inst.getVideorReward();
  119. }
  120. };
  121. }
  122. window["ADHandler"] = ADHandler;