module PhaserAds { export module AdProvider { export enum GameDistributionAdType { interstitial = 'interstitial' , rewarded = 'rewarded' , display = 'display' } export enum GameDistributionBannerSize { LargeRectangle , MediumRectangle , Billboard , Leaderboard , Skyscraper , WideSkyscraper } export enum GameDistributionAlignment { TopLeft , TopCenter , TopRight , CenterLeft , Center , CenterRight , BottomLeft , BottomCenter , BottomRight } export class GameDistributionBanner { public element : HTMLElement ; private resizeListener : ( ) => void ; private parent : HTMLElement ; private alignment : GameDistributionAlignment ; private width : number ; private height : number ; private offsetX : number = 0 ; private offsetY : number = 0 ; constructor ( ) { this . element = document . createElement ( 'div' ) ; this . element . style . position = 'absolute' ; this . element . style . top = `0px` ; this . element . style . left = `0px` ; this . element . id = `banner-${ Date . now ( ) }${ Math . random ( ) * 10000000 | 0 }` ; document . body . appendChild ( this . element ) ; } ; public loadBanner ( ) { return gdsdk . showAd ( GameDistributionAdType . display , { containerId : this . element . id } ) ; } public destroy ( ) { document . body . removeChild ( this . element ) ; this . element = null ; this . parent = null ; this . alignment = null ; if ( this . resizeListener ) { window . removeEventListener ( 'resize' , this . resizeListener ) ; } } public alignIn ( element , position ) { this . parent = element ; this . alignment = position ; this . resizeListener = ( ) => this . resize ( ) ; window . addEventListener ( 'resize' , this . resizeListener ) ; this . resize ( ) ; } public setOffset ( x = 0 , y = 0 ) { this . offsetX = x ; this . offsetY = y ; this . resize ( ) ; } private resize ( ) { const parentBoundingRect = this . parent . getBoundingClientRect ( ) ; switch ( this . alignment ) { case GameDistributionAlignment . TopLeft : this . position ( parentBoundingRect . left , parentBoundingRect . top ) ; break ; case GameDistributionAlignment . TopCenter : this . position ( parentBoundingRect . left + parentBoundingRect . width / 2 - this . width / 2 , parentBoundingRect . top ) ; break ; case GameDistributionAlignment . TopRight : this . position ( parentBoundingRect . left + parentBoundingRect . width - this . width , parentBoundingRect . top ) ; break ; case GameDistributionAlignment . CenterLeft : this . position ( parentBoundingRect . left , parentBoundingRect . top + parentBoundingRect . height / 2 - this . height / 2 ) ; break ; case GameDistributionAlignment . Center : this . position ( parentBoundingRect . left + parentBoundingRect . width / 2 - this . width / 2 , parentBoundingRect . top + parentBoundingRect . height / 2 - this . height / 2 ) ; break ; case GameDistributionAlignment . CenterRight : this . position ( parentBoundingRect . left + parentBoundingRect . width - this . width , parentBoundingRect . top + parentBoundingRect . height / 2 - this . height / 2 ) ; break ; case GameDistributionAlignment . BottomLeft : this . position ( parentBoundingRect . left , parentBoundingRect . top + parentBoundingRect . height - this . height ) ; break ; case GameDistributionAlignment . BottomCenter : this . position ( parentBoundingRect . left + parentBoundingRect . width / 2 - this . width / 2 , parentBoundingRect . top + parentBoundingRect . height - this . height ) ; break ; case GameDistributionAlignment . BottomRight : this . position ( parentBoundingRect . left + parentBoundingRect . width - this . width , parentBoundingRect . top + parentBoundingRect . height - this . height ) ; break ; } } public setSize ( size ) { let width , height ; switch ( size ) { default : case GameDistributionBannerSize . LargeRectangle : width = 336 ; height = 280 ; break ; case GameDistributionBannerSize . MediumRectangle : width = 300 ; height = 250 ; break ; case GameDistributionBannerSize . Billboard : width = 970 ; height = 250 ; break ; case GameDistributionBannerSize . Leaderboard : width = 728 ; height = 90 ; break ; case GameDistributionBannerSize . Skyscraper : width = 120 ; height = 600 ; break ; case GameDistributionBannerSize . WideSkyscraper : width = 160 ; height = 600 ; break ; } this . width = width ; this . height = height ; this . element . style . width = `${ width }px` ; this . element . style . height = `${ height }px` ; } public position ( x , y ) { this . element . style . left = `${ x + this . offsetX }px` ; this . element . style . top = `${ y + this . offsetY }px` ; } } export class GameDistributionAds implements PhaserAds . AdProvider . IProvider { public adManager : AdManager ; public adsEnabled : boolean = true ; public hasRewarded : boolean = false ; constructor ( game , gameId , userId = '' ) { this . areAdsEnabled ( ) ; GD_OPTIONS = < IGameDistributionSettings > { gameId : gameId , advertisementSettings : { autoplay : false } , onEvent : ( event ) : void => { switch ( event . name as string ) { case 'SDK_GAME_PAUSE' : this . adManager . onContentPaused . dispatch ( ) ; break ; default : break ; } } } ; ( function ( d , s , id ) : void { let js ; let fjs = < HTMLScriptElement > d . getElementsByTagName ( s ) [ 0 ] ; if ( d . getElementById ( id ) ) { return ; } js = < HTMLScriptElement > d . createElement ( s ) ; js . id = id ; js . src = '//html5.api.gamedistribution.com/main.min.js' ; fjs . parentNode . insertBefore ( js , fjs ) ; } ( document , 'script' , 'gamedistribution-jssdk' ) ) ; } public setManager ( manager ) { this . adManager = manager ; } public showAd ( adType , containerId ? ) { if ( ! this . adsEnabled ) { this . adManager . unMuteAfterAd ( ) ; this . adManager . onContentResumed . dispatch ( ) ; return ; } if ( typeof gdsdk === 'undefined' || ( gdsdk && typeof gdsdk . showAd === 'undefined' ) ) { this . adsEnabled = false ; this . adManager . unMuteAfterAd ( ) ; this . adManager . onContentResumed . dispatch ( ) ; return ; } if ( adType === PhaserAds . AdType . rewarded && this . hasRewarded === false ) { this . adManager . unMuteAfterAd ( ) ; this . adManager . onContentResumed . dispatch ( ) ; return ; } gdsdk . showAd ( ( adType === PhaserAds . AdType . rewarded ) ? GameDistributionAdType . rewarded : GameDistributionAdType . interstitial ) . then ( ( ) => { if ( adType === PhaserAds . AdType . rewarded && this . hasRewarded === true ) { this . adManager . onAdRewardGranted . dispatch ( ) ; this . hasRewarded = false ; } this . adManager . unMuteAfterAd ( ) ; this . adManager . onContentResumed . dispatch ( ) ; } ) . catch ( ( ) => { if ( adType === PhaserAds . AdType . rewarded && this . hasRewarded === true ) { this . hasRewarded = false ; } this . adManager . unMuteAfterAd ( ) ; this . adManager . onContentResumed . dispatch ( ) ; } ) ; } public loadBanner ( size ) { const banner = new GameDistributionBanner ( ) ; banner . setSize ( size ) ; banner . loadBanner ( ) ; return banner ; } public preloadAd ( adType ) { if ( this . hasRewarded ) { return ; } gdsdk . preloadAd ( GameDistributionAdType . rewarded ) . then ( ( ) => { this . hasRewarded = true ; this . adManager . onAdLoaded . dispatch ( adType ) ; } ) ; } public destroyAd ( ) { return ; } public hideAd ( ) { 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 ) ; } } } }