Skip to content

Commit

Permalink
Merge pull request #16 from azerion/gamedock
Browse files Browse the repository at this point in the history
GamedockSDK provider
  • Loading branch information
florisdh authored Feb 13, 2020
2 parents f7a0e41 + 76c0f79 commit 69efc98
Show file tree
Hide file tree
Showing 6 changed files with 239 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

# [0.6.0] - 2020-01-30
### Added
- Gamedock cordova ad provider

## [0.5.1] - 2019-12-19
### Fixed
- Banner offset when scaling and aligning
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion package.json
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": [
{
Expand All @@ -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",
Expand Down
209 changes: 209 additions & 0 deletions src/Providers/cordova-gamedock.ts
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
}
}
1 change: 1 addition & 0 deletions src/h5-ad-wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import EventEmitter from 'eventemitter3'

export { CocoonAds } from './Providers/cocoon'
export { CordovaGamedistribution } from './Providers/cordova-gamedistribution'
export { CordovaGamedock } from './Providers/cordova-gamedock'
export { CordovaHeyzap } from './Providers/cordova-heyzap'
export { CordovaIronSource } from './Providers/cordova-ironsource'
export {
Expand Down
19 changes: 19 additions & 0 deletions vendor/cordova-gamedock.d.ts
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;

0 comments on commit 69efc98

Please sign in to comment.