module PhaserAds { export module AdProvider { export interface ICustomParams { [ name ] : string | number | any [ ] ; } export class Ima3 implements IProvider { private gameContent : any ; private adContent : HTMLElement ; private adDisplay : GoogleAds . ima . AdDisplayContainer ; private adLoader : GoogleAds . ima . AdsLoader ; private adsManager : GoogleAds . ima . AdsManager = null ; private googleEnabled : boolean = false ; public adsEnabled : boolean = true ; private adTagUrl : string = '' ; private game : Phaser . Game ; private adRequested : boolean = false ; public hasRewarded : boolean = false ; public adManager : AdManager = null ; private resizeListener : ( ) => void = null ; constructor ( game , adTagUrl ) { this . areAdsEnabled ( ) ; if ( typeof google === 'undefined' ) { return ; } this . googleEnabled = true ; this . gameContent = ( typeof game . parent === 'string' ) ? document . getElementById ( < string > ( < any > game ) . parent ) : game . parent ; this . gameContent . style . position = 'absolute' ; this . gameContent . style . width = '100%' ; this . adContent = this . gameContent . parentNode . appendChild ( document . createElement ( 'div' ) ) ; this . adContent . id = 'phaser-ad-container' ; this . adContent . style . position = 'absolute' ; this . adContent . style . zIndex = '9999' ; this . adContent . style . display = 'none' ; this . adContent . style . top = '0' ; this . adContent . style . left = '0' ; this . adContent . style . width = '100%' ; this . adContent . style . height = '100%' ; this . adContent . style . overflow = 'hidden' ; this . adTagUrl = adTagUrl ; this . game = game ; this . adDisplay = new google . ima . AdDisplayContainer ( this . adContent ) ; ( < any > google . ima . settings ) . setVpaidMode ( ( < any > google . ima ) . ImaSdkSettings . VpaidMode . ENABLED ) ; ( < any > google . ima . settings ) . setLocale ( 'nl' ) ; this . adLoader = new google . ima . AdsLoader ( this . adDisplay ) ; this . adLoader . addEventListener ( google . ima . AdsManagerLoadedEvent . Type . ADS_MANAGER_LOADED , this . onAdManagerLoader , false , this ) ; this . adLoader . addEventListener ( google . ima . AdErrorEvent . Type . AD_ERROR , this . onAdError , false , this ) ; } public setManager ( manager ) { this . adManager = manager ; } /**
             * Doing an ad request, if anything is wrong with the lib (missing ima3, failed request) we just dispatch the contentResumed event
             * Otherwise we display an ad
             */ public showAd ( customParams ? ) { console . log ( 'Ad Requested' ) ; if ( this . adRequested ) { return ; } if ( ! this . adsEnabled ) { this . adManager . onAdsDisabled . dispatch ( true ) ; } if ( ! this . googleEnabled ) { this . onContentResumeRequested ( ) ; return ; } this . adDisplay . initialize ( ) ; let adsRequest = new google . ima . AdsRequest ( ) ; adsRequest . adTagUrl = this . adTagUrl + this . parseCustomParams ( customParams ) ; let width = window . innerWidth ; let height = window . innerHeight ; if ( this . game . scale . isFullScreen && document . body . clientHeight < window . innerHeight ) { height = document . body . clientHeight ; width = document . body . clientWidth ; } adsRequest . linearAdSlotWidth = width ; adsRequest . linearAdSlotHeight = height ; adsRequest . nonLinearAdSlotWidth = width ; adsRequest . nonLinearAdSlotHeight = height ; adsRequest . forceNonLinearFullSlot = true ; try { this . adRequested = true ; this . adLoader . requestAds ( adsRequest ) ; } catch ( e ) { console . log ( e ) ; this . onContentResumeRequested ( ) ; } } public preloadAd ( ) { return ; } public destroyAd ( ) { return ; } public hideAd ( ) { return ; } param adsManagerLoadedEvent private onAdManagerLoader ( adsManagerLoadedEvent ) { console . log ( 'AdsManager loaded' ) ; let adsRenderingSettings = new google . ima . AdsRenderingSettings ( ) ; adsRenderingSettings . restoreCustomPlaybackStateOnAdBreakComplete = true ; let adsManager = adsManagerLoadedEvent . getAdsManager ( this . gameContent , adsRenderingSettings ) ; this . adsManager = adsManager ; console . log ( adsManager . isCustomClickTrackingUsed ( ) ) ; adsManager . addEventListener ( google . ima . AdEvent . Type . CONTENT_PAUSE_REQUESTED , this . onContentPauseRequested , false , this ) ; adsManager . addEventListener ( google . ima . AdEvent . Type . CONTENT_RESUME_REQUESTED , this . onContentResumeRequested , false , this ) ; adsManager . addEventListener ( google . ima . AdErrorEvent . Type . AD_ERROR , this . onAdError , false , this ) ; [ google . ima . AdEvent . Type . ALL_ADS_COMPLETED , google . ima . AdEvent . Type . CLICK , google . ima . AdEvent . Type . COMPLETE , google . ima . AdEvent . Type . FIRST_QUARTILE , google . ima . AdEvent . Type . LOADED , google . ima . AdEvent . Type . MIDPOINT , google . ima . AdEvent . Type . PAUSED , google . ima . AdEvent . Type . STARTED , google . ima . AdEvent . Type . THIRD_QUARTILE ] . forEach ( ( event ) => { adsManager . addEventListener ( event , this . onAdEvent , false , this ) ; } ) ; try { this . adContent . style . display = 'block' ; let width = window . innerWidth ; let height = window . innerHeight ; this . adsManager . init ( width , height , google . ima . ViewMode . NORMAL ) ; this . adsManager . start ( ) ; this . resizeListener = ( ) => { if ( this . adsManager === null ) { return ; } console . log ( 'Resizing ad size' ) ; this . adsManager . resize ( window . innerWidth , window . innerHeight , google . ima . ViewMode . NORMAL ) ; } ; window . addEventListener ( 'resize' , this . resizeListener ) ; } catch ( adError ) { console . log ( 'Adsmanager error:' , adError ) ; this . onAdError ( adError ) ; } } param adEvent private onAdEvent ( adEvent ) { console . log ( 'onAdEvent' , adEvent ) ; switch ( adEvent . type ) { case google . ima . AdEvent . Type . CLICK : this . adManager . onAdClicked . dispatch ( ) ; break ; case google . ima . AdEvent . Type . LOADED : this . adRequested = false ; let ad = adEvent . getAd ( ) ; console . log ( 'is ad linear?' , ad . isLinear ( ) ) ; if ( ! ad . isLinear ( ) ) { this . onContentResumeRequested ( ) ; } break ; case google . ima . AdEvent . Type . STARTED : this . adManager . onAdProgression . dispatch ( AdEvent . start ) ; break ; case google . ima . AdEvent . Type . FIRST_QUARTILE : this . adManager . onAdProgression . dispatch ( AdEvent . firstQuartile ) ; break ; case google . ima . AdEvent . Type . MIDPOINT : this . adManager . onAdProgression . dispatch ( AdEvent . midPoint ) ; break ; case google . ima . AdEvent . Type . THIRD_QUARTILE : this . adManager . onAdProgression . dispatch ( AdEvent . thirdQuartile ) ; break ; case google . ima . AdEvent . Type . COMPLETE : this . adManager . onAdProgression . dispatch ( AdEvent . complete ) ; break ; case google . ima . AdEvent . Type . ALL_ADS_COMPLETED : this . onContentResumeRequested ( ) ; break ; } } private onAdError ( error ) { console . log ( 'gneric ad error' , error ) ; if ( null !== this . adsManager ) { this . adsManager . destroy ( ) ; this . adsManager = null ; if ( null !== this . resizeListener ) { window . removeEventListener ( 'resize' , this . resizeListener ) ; this . resizeListener = null ; } } if ( this . adRequested ) { this . adRequested = false ; } this . onContentResumeRequested ( ) ; } /**
             * When the ad starts playing, and the game should be paused
             */ private onContentPauseRequested ( ) { console . log ( 'onContentPauseRequested' , arguments ) ; this . adManager . onContentPaused . dispatch ( ) ; } /**
             * When the ad is finished and the game should be resumed
             */ private onContentResumeRequested ( ) { console . log ( 'onContentResumeRequested' , arguments ) ; if ( typeof google === 'undefined' ) { this . adManager . unMuteAfterAd ( ) ; this . adManager . onContentResumed . dispatch ( ) ; return ; } this . adContent . style . display = 'none' ; this . adManager . unMuteAfterAd ( ) ; this . adManager . onContentResumed . dispatch ( ) ; } private parseCustomParams ( customParams ) { if ( undefined !== customParams ) { let customDataString = '' ; for ( let key in customParams ) { if ( customParams . hasOwnProperty ( key ) ) { if ( customDataString . length > 0 ) { customDataString += '' + '&' ; } let param = ( Array . isArray ( customParams [ key ] ) ) ? ( < any [ ] > customParams [ key ] ) . join ( ',' ) : customParams [ key ] ; customDataString += key + '=' + param ; } } return '&cust_params=' + encodeURIComponent ( customDataString ) ; } return '' ; } returns { boolean } private areAdsEnabled ( ) { let test = document . createElement ( 'div' ) ; test . innerHTML = '&nbsp;' ; test . className = 'adsbox' ; test . style . position = 'absolute' ; test . style . fontSize = '10px' ; document . body . appendChild ( test ) ; let isEnabled = ( ) => { let enabled = true ; if ( test . offsetHeight === 0 ) { enabled = false ; } test . parentNode . removeChild ( test ) ; return enabled ; } ; window . setTimeout ( ( ) => { this . adsEnabled = isEnabled ( ) ; } , 100 ) ; } } } }