thirdVideoTS.ts 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. import { STORAGE_KEY } from "../../../datas/GlobalStorage";
  2. import EffectNode from "../../../ui/EffectNode";
  3. import NativeADAwardNode from "../../../ui/NativeAdAwardNode";
  4. import LogUtil from "../../../utils/LogUtil";
  5. import { Utils } from "../../../utils/Utils";
  6. import AdM from "../../AdM";
  7. import GameM from "../../GameM";
  8. import NativeAdAwardData from "../../NativeAdAwardData";
  9. import SwitchM from "../../SwitchM";
  10. import UiM from "../../UiM";
  11. import { zgSingleton } from "./zgSingleton";
  12. export class thirdVideoTS extends zgSingleton {
  13. private canGet = false
  14. private interType = 0
  15. private onloadInter = false
  16. private onLoadReward = false
  17. init() {
  18. cc["thirdVideo"].proxy = this;
  19. cc["thirdVideo"].init(GameM.commonData.toponId, GameM.commonData.toponKey, GameM.commonData.rewardPlaceId, GameM.commonData.interPlaceId, GameM.commonData.nativePlaceId,
  20. GameM.commonData.fullInterPlaceId, GameM.commonData.bannerPlaceId, GameM.commonData.umengChannel);
  21. this.loadAd()
  22. this.loadNative()
  23. if (SwitchM.forceMakeMoney) {
  24. this.loadBanner()
  25. }
  26. // this.loadInter(1)
  27. //5秒后加载普通插屏
  28. let id = setTimeout(() => {
  29. this.loadInter(0)
  30. clearTimeout(id)
  31. }, 5000);
  32. GameM.commonData.toponLoaded = true
  33. }
  34. /** 加载广告 激励视频
  35. */
  36. loadAd() {
  37. if (this.onLoadReward) {
  38. return
  39. }
  40. LogUtil.logV('showAd ', 'loadAd end')
  41. setTimeout(() => {
  42. this.onLoadReward = false
  43. }, 3000);
  44. this.onLoadReward = true
  45. this.canGet = false
  46. cc["thirdVideo"].loadAd("userIdKey", "userDataKey", GameM.commonData.rewardPlaceId);
  47. let eventID = Utils.randomString(32);
  48. AdM.sendUROIEvent(eventID, '', GameM.commonData.rewardPlaceId, "ad_request", "rewarded", 'other', '')
  49. }
  50. showAd() {
  51. LogUtil.logV('showAd ', 'before')
  52. if (!GameM.commonData.toponLoaded) {
  53. return
  54. }
  55. LogUtil.logV('showAd ', 'end')
  56. if (this.checkReady()) {
  57. LogUtil.logV('showAd ', 'showAd')
  58. cc["thirdVideo"].showAd();
  59. }
  60. else {
  61. this.loadAd()
  62. AdM.onWatchFailEnd()
  63. }
  64. //ATRewardedVideoJSSDK.showAdInScenario(this.placementID(), "f5e54970dc84e6");
  65. }
  66. checkReady() {
  67. //ATJSSDK.printLog("AnyThinkRewardedVideoDemo::checkReady() " + (ATRewardedVideoJSSDK.hasAdReady(this.placementID()) ? "Ready" : "No"));
  68. return cc["thirdVideo"].checkReady()
  69. }
  70. //Callbacks
  71. onRewardedVideoAdLoaded(placementId: string) {
  72. // cc["thirdVideo"].onRewardedVideoAdLoaded(placementId);
  73. // ATJSSDK.printLog("AnyThinkRewardedVideoDemo::onRewardedVideoAdLoaded(" + placementId + ")");
  74. console.log('AnyThinkRewardedVideoDemo loadAd完毕,准备显示Ad')
  75. this.onLoadReward = false
  76. let eventID = Utils.randomString(32);
  77. AdM.sendUROIEvent(eventID, '', placementId, "ad_fill", "rewarded", 'other', '')
  78. }
  79. onRewardedVideoAdFailed(placementId, errorInfo) {
  80. // cc["thirdVideo"].onRewardedVideoAdFailed(placementId,errorInfo);
  81. //ATJSSDK.printLog("AnyThinkRewardedVideoDemo::onRewardedVideoAdFailed(" + placementId + ", " + errorInfo + ")");
  82. console.log('AnyThinkRewardedVideoDemo onRewardedVideoAdFailed')
  83. console.log('placementId ', placementId, ' onRewardedVideoAdFailed ', errorInfo)
  84. this.onLoadReward = false
  85. GameM.commonData.updateADLog(5, placementId)
  86. }
  87. onRewardedVideoAdPlayStart(placementId, callbackInfo) {
  88. console.log('AnyThinkRewardedVideoDemo onRewardedVideoAdPlayStart')
  89. //ATJSSDK.printLog("AnyThinkRewardedVideoDemo::onRewardedVideoAdPlayStart(" + placementId + ", " + callbackInfo + ")");
  90. let data = JSON.parse(callbackInfo)
  91. GameM.commonData.updateADLog(4, placementId, data.adunit_format, data.network_type, data.network_placement_id,
  92. data.network_firm_id, data.adsource_id, data.adsource_index, data.adsource_price,
  93. data.adsource_isheaderbidding, data.publisher_revenue, data.precision, data.ecpm_level)
  94. let eventID = Utils.randomString(32);
  95. let adAdn = this.getAdAdn(data.network_firm_id)
  96. let price = GameM.commonData.getAdPrice(data.adsource_id, data.adsource_price)
  97. AdM.sendUROIEvent(eventID, data.id, placementId, "ad_show", "rewarded", adAdn, price)
  98. }
  99. onRewardedVideoAdPlayEnd(placementId, callbackInfo) {
  100. console.log('AnyThinkRewardedVideoDemo onRewardedVideoAdPlayEnd')
  101. //ATJSSDK.printLog("AnyThinkRewardedVideoDemo::onRewardedVideoAdPlayEnd(" + placementId + ", " + callbackInfo + ")");
  102. }
  103. onRewardedVideoAdPlayFailed(placementId, errorInfo, callbackInfo) {
  104. console.log('AnyThinkRewardedVideoDemo onRewardedVideoAdPlayFailed')
  105. //ATJSSDK.printLog("AnyThinkRewardedVideoDemo::onRewardedVideoAdPlayFailed(" + placementId + ", " + errorInfo + ", " + callbackInfo + ")");
  106. // console.log('play placementId ', placementId, ' errorInfo ', errorInfo)
  107. AdM.onWatchFailEnd()
  108. }
  109. onRewardedVideoAdClosed(placementId, callbackInfo) {
  110. // ATJSSDK.printLog("AnyThinkRewardedVideoDemo::onRewardedVideoAdClosed(" + placementId + ", " + callbackInfo + ")");
  111. console.log('AnyThinkRewardedVideoDemo onRewardedVideoAdClosed, canGet ', this.canGet)
  112. if (this.canGet) {
  113. let data = JSON.parse(callbackInfo)
  114. AdM.onWatchEnd(placementId, data.adunit_format, data.network_type, data.network_placement_id,
  115. data.network_firm_id, data.adsource_id, data.adsource_index, data.adsource_price,
  116. data.adsource_isheaderbidding, data.publisher_revenue, data.precision, data.ecpm_level)
  117. AdM.onKSEvent(3)
  118. }
  119. else {
  120. AdM.onWatchCancleEnd()
  121. }
  122. this.canGet = false
  123. this.loadAd()
  124. }
  125. onRewardedVideoAdPlayClicked(placementId, callbackInfo) {
  126. // ATJSSDK.printLog("AnyThinkRewardedVideoDemo::onRewardedVideoAdPlayClicked(" + placementId + ", " + callbackInfo + ")");
  127. let data = JSON.parse(callbackInfo)
  128. let eventID = Utils.randomString(32);
  129. console.log('AnyThinkRewardedVideoDemo onRewardedVideoAdPlayClicked')
  130. let adAdn = this.getAdAdn(data.network_firm_id)
  131. let price = GameM.commonData.getAdPrice(data.adsource_id, data.adsource_price)
  132. AdM.sendUROIEvent(eventID, data.id, placementId, "ad_click", "rewarded", adAdn, price)
  133. }
  134. onReward(placementId, callbackInfo) {
  135. // ATJSSDK.printLog("AnyThinkRewardedVideoDemo::onReward(" + placementId + ", " + callbackInfo + ")");
  136. console.log('AnyThinkRewardedVideoDemo onReward')
  137. this.canGet = true
  138. }
  139. /** 插屏广告
  140. * @param type 0 普通插屏 1 全屏视频插屏
  141. */
  142. loadInter(type) {
  143. // console.log('99999999 loadInterPre ', type)
  144. if (!this.onloadInter) {
  145. this.onloadInter = true
  146. // console.log('99999999 loadInter ', type)
  147. cc["thirdVideo"].loadInter(type);
  148. let eventID = Utils.randomString(32);
  149. AdM.sendUROIEvent(eventID, '', GameM.commonData.interPlaceId, "ad_request", "interstitial", 'other', '')
  150. }
  151. }
  152. showInter(type) {
  153. if (!GameM.commonData.toponLoaded) {
  154. return
  155. }
  156. this.interType = type
  157. if (cc["thirdVideo"].checkInterReady(type)) {
  158. // console.log('99999999 showInter success')
  159. cc["thirdVideo"].showInter(type);
  160. }
  161. else {
  162. // console.log('99999999 showInter fail')
  163. this.loadInter(type)
  164. }
  165. }
  166. onInterstitialAdLoaded(placementId) {
  167. this.onloadInter = false
  168. // console.log('99999999 onInterstitialAdLoaded ')
  169. let eventID = Utils.randomString(32);
  170. AdM.sendUROIEvent(eventID, '', placementId, "ad_fill", "interstitial", 'other', '')
  171. }
  172. onInterstitialAdLoadFail(placementId, errorInfo) {
  173. this.onloadInter = false
  174. // console.log('99999999 onInterstitialAdLoadFail')
  175. if (placementId == GameM.commonData.fullInterPlaceId) {
  176. EffectNode.instance.PlayTip('拉取全屏视频失败,请稍后再试')
  177. GameM.commonData.updateADLog(10, placementId)
  178. }
  179. else if (placementId == GameM.commonData.interPlaceId) {
  180. GameM.commonData.updateADLog(11, placementId)
  181. }
  182. }
  183. //广告展示成功
  184. onInterstitialAdShow(placementId, callbackInfo) {
  185. // console.log('99999999 onInterstitialAdShow')
  186. let data = JSON.parse(callbackInfo)
  187. let id = 2
  188. if (placementId == GameM.commonData.interPlaceId) {
  189. id = 2
  190. }
  191. else if (placementId == GameM.commonData.fullInterPlaceId) {
  192. id = 9
  193. }
  194. GameM.commonData.updateADLog(id, placementId, data.adunit_format, data.network_type, data.network_placement_id,
  195. data.network_firm_id, data.adsource_id, data.adsource_index, data.adsource_price,
  196. data.adsource_isheaderbidding, data.publisher_revenue, data.precision, data.ecpm_level)
  197. let eventID = Utils.randomString(32);
  198. let adAdn = this.getAdAdn(data.network_firm_id)
  199. let price = GameM.commonData.getAdPrice(data.adsource_id, data.adsource_price)
  200. AdM.sendUROIEvent(eventID, data.id, placementId, "ad_show", "interstitial", adAdn, price)
  201. }
  202. onInterstitialAdClick(placementId, callbackInfo) {
  203. let data = JSON.parse(callbackInfo)
  204. let eventID = Utils.randomString(32);
  205. let adAdn = this.getAdAdn(data.network_firm_id)
  206. let price = GameM.commonData.getAdPrice(data.adsource_id, data.adsource_price)
  207. AdM.sendUROIEvent(eventID, data.id, placementId, "ad_click", "interstitial", adAdn, price)
  208. }
  209. //插屏广告关闭
  210. onInterstitialAdClose(placementId, callbackInfo) {
  211. //给奖励
  212. // console.log('AnyThinkInterstitialDemo ', this.interType)
  213. if (placementId == GameM.commonData.fullInterPlaceId) {
  214. GameM.commonData.showFull = false
  215. GameM.commonData.todayFullInterTimes++
  216. GameM.globalStorage.setStorage(STORAGE_KEY.todayFullInterTimes, GameM.commonData.todayFullInterTimes)
  217. AdM.videoReward()
  218. let data = JSON.parse(callbackInfo)
  219. GameM.commonData.updateADLog(8, placementId, data.adunit_format, data.network_type, data.network_placement_id,
  220. data.network_firm_id, data.adsource_id, data.adsource_index, data.adsource_price,
  221. data.adsource_isheaderbidding, data.publisher_revenue, data.precision, data.ecpm_level)
  222. }
  223. this.loadInter(this.interType)
  224. }
  225. loadNative(type = 0, show = false) {
  226. // console.log('AnyThinkNativeDemo loadNative ')
  227. cc["thirdVideo"].loadNative(type, show);
  228. let eventID = Utils.randomString(32);
  229. AdM.sendUROIEvent(eventID, '', GameM.commonData.nativePlaceId, "ad_request", "flow", 'other', '')
  230. }
  231. /** 信息流位置
  232. * @param type 0 弹窗里 1 最底部 2 弹窗顶部 3 底部小窗口
  233. */
  234. showNative(type) {
  235. if (!GameM.commonData.toponLoaded) {
  236. return
  237. }
  238. // console.log('AnyThinkNativeDemo showNative ')
  239. cc["thirdVideo"].showNative(type);
  240. }
  241. onNativeAdLoaded(placementId) {
  242. let eventID = Utils.randomString(32);
  243. AdM.sendUROIEvent(eventID, '', placementId, "ad_fill", "flow", 'other', '')
  244. }
  245. //广告展示成功
  246. onNativeAdShow(placementId, callbackInfo) {
  247. let data = JSON.parse(callbackInfo)
  248. GameM.commonData.updateADLog(3, placementId, data.adunit_format, data.network_type, data.network_placement_id,
  249. data.network_firm_id, data.adsource_id, data.adsource_index, data.adsource_price,
  250. data.adsource_isheaderbidding, data.publisher_revenue, data.precision, data.ecpm_level)
  251. let eventID = Utils.randomString(32);
  252. let adAdn = this.getAdAdn(data.network_firm_id)
  253. let price = GameM.commonData.getAdPrice(data.adsource_id, data.adsource_price)
  254. AdM.sendUROIEvent(eventID, data.id, placementId, "ad_show", "flow", adAdn, price)
  255. this.loadNative()
  256. }
  257. onNativeAdClick(placementId, callbackInfo) {
  258. let data = JSON.parse(callbackInfo)
  259. let eventID = Utils.randomString(32);
  260. let adAdn = this.getAdAdn(data.network_firm_id)
  261. let price = GameM.commonData.getAdPrice(data.adsource_id, data.adsource_price)
  262. AdM.sendUROIEvent(eventID, data.id, placementId, "ad_click", "flow", adAdn, price)
  263. if (NativeAdAwardData.Instance.openNativeState) {
  264. NativeAdAwardData.Instance.setHasClickNative()
  265. if (UiM.Instance.nativeAdAwardNode) {
  266. // let type = UiM.Instance.nativeAdAwardNode.getComponent(NativeADAwardNode).type
  267. // AdM.onSendEvent('NativeAdAwardClick' + type, `信息流变现点击${type}`, 'NativeAdAwardClick')
  268. }
  269. }
  270. }
  271. onNativeAdLoadFail(placementId, errorInfo) {
  272. GameM.commonData.updateADLog(12, placementId)
  273. }
  274. destroyNative() {
  275. if (!GameM.commonData.toponLoaded) {
  276. return
  277. }
  278. cc["thirdVideo"].destroyNative();
  279. }
  280. getIsNativeShow() {
  281. return cc["thirdVideo"].nativeIsShow || cc["thirdVideo"].nativeShow
  282. }
  283. /** banner相关------------- */
  284. loadBanner() {
  285. cc["thirdVideo"].loadBanner();
  286. }
  287. checkBannerReady() {
  288. return cc["thirdVideo"].checkBannerReady()
  289. }
  290. /** banner是否显示 */
  291. getIsBannerShow() {
  292. return cc["thirdVideo"].bannerIsShow
  293. }
  294. /** 显示banner
  295. */
  296. showBanner() {
  297. if (!GameM.commonData.toponLoaded) {
  298. return
  299. }
  300. if (this.checkBannerReady()) {
  301. cc["thirdVideo"].showBanner();
  302. }
  303. else {
  304. this.loadBanner()
  305. }
  306. }
  307. destroyBanner() {
  308. if (!GameM.commonData.toponLoaded) {
  309. return
  310. }
  311. cc["thirdVideo"].destroyBanner();
  312. }
  313. onBannerAdLoaded(placementId) {
  314. let eventID = Utils.randomString(32);
  315. AdM.sendUROIEvent(eventID, '', placementId, "ad_fill", "flow", 'other', '')
  316. }
  317. //广告展示成功
  318. onBannerAdShow(placementId, callbackInfo) {
  319. let data = JSON.parse(callbackInfo)
  320. GameM.commonData.updateADLog(7, placementId, data.adunit_format, data.network_type, data.network_placement_id,
  321. data.network_firm_id, data.adsource_id, data.adsource_index, data.adsource_price,
  322. data.adsource_isheaderbidding, data.publisher_revenue, data.precision, data.ecpm_level)
  323. let eventID = Utils.randomString(32);
  324. let adAdn = this.getAdAdn(data.network_firm_id)
  325. let price = GameM.commonData.getAdPrice(data.adsource_id, data.adsource_price)
  326. AdM.sendUROIEvent(eventID, data.id, placementId, "ad_show", "flow", adAdn, price)
  327. }
  328. onBannerAdClick(placementId, callbackInfo) {
  329. let data = JSON.parse(callbackInfo)
  330. let eventID = Utils.randomString(32);
  331. let adAdn = this.getAdAdn(data.network_firm_id)
  332. let price = GameM.commonData.getAdPrice(data.adsource_id, data.adsource_price)
  333. AdM.sendUROIEvent(eventID, data.id, placementId, "ad_click", "flow", adAdn, price)
  334. }
  335. onBannerAdLoadFail(placementId, errorInfo) {
  336. GameM.commonData.updateADLog(14, placementId)
  337. }
  338. /** 获取 广告平台来源
  339. * @param network_firm_id topon平台ID
  340. */
  341. getAdAdn(network_firm_id) {
  342. let adAdn = ''
  343. if (network_firm_id == 8) {
  344. adAdn = "gdt"
  345. }
  346. else if (network_firm_id == 15) {
  347. adAdn = "bytedance_pangle"
  348. }
  349. else if (network_firm_id == 28) {
  350. adAdn = "other"
  351. }
  352. return adAdn;
  353. }
  354. }