-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16 from azerion/gamedock
GamedockSDK provider
- Loading branch information
Showing
6 changed files
with
239 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
{ | ||
"name": "@azerion/h5-ad-wrapper", | ||
"author": "Azerion", | ||
"version": "0.5.1", | ||
"version": "0.6.0", | ||
"description": "Advertisement provider wrapper, similar to @azerion/phaser-ads but not tied into Phaser :)", | ||
"contributors": [ | ||
{ | ||
|
@@ -15,6 +15,10 @@ | |
{ | ||
"name": "Floris de Haan", | ||
"email": "[email protected]" | ||
}, | ||
{ | ||
"name": "Bram Willem van der Kuip", | ||
"email": "[email protected]" | ||
} | ||
], | ||
"main": "dist/h5-ad-wrapper.umd.js", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,209 @@ | ||
/// <reference path='../../vendor/cordova-gamedock.d.ts'/> | ||
|
||
import { IProvider } from './ad-provider' | ||
import { AdType, AdEvents, H5AdWrapper } from '../h5-ad-wrapper' | ||
|
||
/** | ||
* The cordova gamedock ad provider requires the gamedock-sdk-cordova plugin to be setup within your cordova app. | ||
*/ | ||
export class CordovaGamedock implements IProvider { | ||
public adManager!: H5AdWrapper | ||
public adsEnabled: boolean = false | ||
|
||
/** | ||
* Flag for if an rewarded ad is loaded or not. | ||
*/ | ||
private rewardedLoaded: boolean = false | ||
|
||
/** | ||
* Creates an instance of CordovaGamedock. | ||
* Initializes the gamedockSDK and adds listeners for events. | ||
*/ | ||
constructor() { | ||
if (typeof gamedockSDK === 'undefined') { | ||
return | ||
} | ||
|
||
// Variables the gamedockSDK requires to be initialized. | ||
let withAgeGate: boolean = false | ||
let ageGateOptions: IAgeGateOptions = { shouldBlock: true, requiredAge: 12 } | ||
let withPrivacyPolicy: boolean = true | ||
let environment: string = 'PRODUCTION' | ||
let externalIds: any[] = [] | ||
|
||
// Calls AD_PROVIDER_LOADED on PrivacyPolicyStatus with flag accepted. | ||
gamedockSDK.on('PrivacyPolicyStatus', (data: any) => { | ||
this.adManager.emit(AdEvents.AD_PROVIDER_LOADED, data.accepted) | ||
}) | ||
|
||
// Initialize the gamedockSDK. | ||
gamedockSDK.initialise( | ||
withAgeGate, | ||
ageGateOptions, | ||
withPrivacyPolicy, | ||
environment, | ||
externalIds | ||
) | ||
|
||
// Called whenever there is an ad available via the requestInterstitial and reqeuestRewardVideo methods. | ||
gamedockSDK.on('AdAvailable', (data: any) => { | ||
switch (data.type) { | ||
case 'interstitial': | ||
this.interstitalAvailable() | ||
break | ||
case 'rewardVideo': | ||
this.rewardedChanged(true) | ||
break | ||
case 'banner': | ||
throw new Error('Not yet implemented.') | ||
} | ||
}) | ||
|
||
// Called whenever there is no ad available (for now just resumes gameplay). | ||
gamedockSDK.on('AdNotAvailable', (data: any) => { | ||
switch (data.type) { | ||
case 'interstitial': | ||
this.resumeGameplay() | ||
break | ||
case 'rewardVideo': | ||
this.resumeGameplay() | ||
break | ||
case 'banner': | ||
throw new Error('Not yet implemented.') | ||
} | ||
}) | ||
|
||
// Called whenever the shown ad is finished or closed for given reason. | ||
gamedockSDK.on('AdFinished', (data: any) => { | ||
switch (data.type) { | ||
case 'interstitial': | ||
this.resumeGameplay() | ||
break | ||
case 'rewardVideo': | ||
this.rewardVideoFinished(data.reason) | ||
break | ||
case 'banner': | ||
throw new Error('Not yet implemented.') | ||
} | ||
}) | ||
} | ||
|
||
public setManager(manager: H5AdWrapper): void { | ||
this.adManager = manager | ||
} | ||
|
||
/** | ||
* Show ad of type AdType | ||
* @param {AdType} [adType=AdType.interstitial] | ||
*/ | ||
public showAd(adType: AdType = AdType.interstitial): void { | ||
if (typeof gamedockSDK === 'undefined') { | ||
return | ||
} | ||
|
||
switch (adType) { | ||
case AdType.interstitial: | ||
this.adManager.emit(AdEvents.CONTENT_PAUSED) | ||
// requestInterstitial is called because playInstestitial will not check ad timeout. | ||
// playInstestitial will be called on AdAvailable. | ||
gamedockSDK.requestInterstitial() | ||
break | ||
case AdType.rewarded: | ||
if (!this.adAvailable(AdType.rewarded)) { | ||
this.resumeGameplay() | ||
break | ||
} | ||
this.adManager.emit(AdEvents.CONTENT_PAUSED) | ||
this.rewardedChanged(false) | ||
gamedockSDK.playRewardVideo() | ||
break | ||
default: | ||
this.resumeGameplay() | ||
break | ||
} | ||
} | ||
|
||
/** | ||
* Called when the gamedockSDK event AdAvailable is called with typeof interstitial. | ||
*/ | ||
private interstitalAvailable(): void { | ||
if (typeof gamedockSDK === 'undefined') { | ||
return | ||
} | ||
|
||
gamedockSDK.playInterstitial() | ||
} | ||
|
||
/** | ||
* Called when the gamedockSDK event AdFinished is called with typeof rewardVideo. | ||
* Will handle dismiss and close reason. | ||
* @param {string} reason | ||
*/ | ||
private rewardVideoFinished(reason: string): void { | ||
switch (reason) { | ||
case 'dismiss': | ||
// If rewardVideo is dismissed (closed before finished), resume the gameplay. | ||
this.resumeGameplay() | ||
break | ||
case 'close': | ||
// If rewardVideo is closed (player saw video until end), emit AD_REWARDED event. | ||
this.adManager.emit(AdEvents.AD_REWARDED) | ||
break | ||
} | ||
} | ||
|
||
/** | ||
* Emits event CONTENT_RESUMED | ||
*/ | ||
private resumeGameplay(): void { | ||
this.adManager.emit(AdEvents.CONTENT_RESUMED) | ||
} | ||
|
||
/** | ||
* Preloads ad of type AdType | ||
* @param {AdType} [adType=AdType.interstitial] | ||
*/ | ||
public preloadAd(adType: AdType = AdType.interstitial): void { | ||
if (typeof gamedockSDK === 'undefined') { | ||
return | ||
} | ||
|
||
switch (adType) { | ||
case AdType.rewarded: | ||
if (this.rewardedLoaded) { | ||
return | ||
} | ||
gamedockSDK.requestRewardVideo() | ||
break | ||
} | ||
} | ||
|
||
public destroyAd(): void { | ||
return | ||
} | ||
|
||
public hideAd(): void { | ||
return | ||
} | ||
|
||
/** | ||
* Returns if ad is available of type AdType | ||
* @param {AdType} adType | ||
* @returns {boolean} | ||
*/ | ||
public adAvailable(adType: AdType): boolean { | ||
switch (adType) { | ||
case AdType.rewarded: | ||
return this.rewardedLoaded | ||
} | ||
return false | ||
} | ||
|
||
/** | ||
* Sets the rewardedLoaded flag to given boolean "available". | ||
* @param {boolean} available | ||
*/ | ||
private rewardedChanged(available: boolean): void { | ||
this.rewardedLoaded = available | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
declare interface IAgeGateOptions { | ||
shouldBlock: boolean; | ||
requiredAge: number; | ||
} | ||
|
||
declare interface IGamedockSDK { | ||
on(event: string, callback: Function): void; | ||
initialise(withAgeGate: boolean, ageGateOptions: IAgeGateOptions, withPrivacyPolicy: boolean, environment: string, externalIds: any[]): void; | ||
|
||
requestInterstitial(): void | ||
requestRewardVideo(): void | ||
|
||
playInterstitial(): void; | ||
playRewardVideo(): void; | ||
|
||
isAdAvailable(adType: string): boolean; | ||
} | ||
|
||
declare var gamedockSDK: IGamedockSDK | undefined; |