From 9399f32de0148030559547e0a35ae2411d8c1e1f Mon Sep 17 00:00:00 2001 From: Richard Davey Date: Tue, 7 Aug 2018 03:16:48 +0100 Subject: [PATCH] FB API calls complete --- .eslintignore | 1 - .eslintrc.json | 3 +- src/fbinstant/AdInstance.js | 17 + src/fbinstant/FacebookInstantGamesPlugin.js | 470 +++++++++++++------- src/fbinstant/Leaderboard.js | 39 +- 5 files changed, 363 insertions(+), 167 deletions(-) create mode 100644 src/fbinstant/AdInstance.js diff --git a/.eslintignore b/.eslintignore index 78cc9ea67..8618d2af7 100644 --- a/.eslintignore +++ b/.eslintignore @@ -9,4 +9,3 @@ src/utils/array/StableSort.js src/utils/object/Extend.js src/structs/RTree.js src/boot/ScaleManager.js -src/fbinstant/ diff --git a/.eslintrc.json b/.eslintrc.json index 1fa89e6e4..f0680fd43 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -16,7 +16,8 @@ "CANVAS_RENDERER": true, "Phaser": true, "process": true, - "ActiveXObject": true + "ActiveXObject": true, + "FBInstant": true }, "rules": { diff --git a/src/fbinstant/AdInstance.js b/src/fbinstant/AdInstance.js new file mode 100644 index 000000000..fc050bc81 --- /dev/null +++ b/src/fbinstant/AdInstance.js @@ -0,0 +1,17 @@ +/** + * @author Richard Davey + * @copyright 2018 Photon Storm Ltd. + * @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License} + */ + +var AdInstance = function (instance, video) +{ + return { + instance: instance, + placementID: instance.getPlacementID(), + shown: false, + video: video + }; +}; + +module.exports = AdInstance; diff --git a/src/fbinstant/FacebookInstantGamesPlugin.js b/src/fbinstant/FacebookInstantGamesPlugin.js index dd681386f..f3aa09715 100644 --- a/src/fbinstant/FacebookInstantGamesPlugin.js +++ b/src/fbinstant/FacebookInstantGamesPlugin.js @@ -1,13 +1,15 @@ +/* eslint no-console: 0 */ + /** * @author Richard Davey * @copyright 2018 Photon Storm Ltd. * @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License} */ +var AdInstance = require('./AdInstance'); var Class = require('../utils/Class'); var DataManager = require('../data/DataManager'); var EventEmitter = require('eventemitter3'); -var GetValue = require('../utils/object/GetValue'); var Leaderboard = require('./Leaderboard'); var Product = require('./Product'); var Purchase = require('./Purchase'); @@ -31,7 +33,7 @@ var FacebookInstantGamesPlugin = new Class({ initialize: - function FacebookInstantGamesPlugin (game, config) + function FacebookInstantGamesPlugin (game) { EventEmitter.call(this); @@ -57,26 +59,26 @@ var FacebookInstantGamesPlugin = new Class({ this.entryPoint = ''; this.entryPointData = null; - this.contextID = 0; + this.contextID = null; // POST - A facebook post. // THREAD - A messenger thread. // GROUP - A facebook group. // SOLO - Default context, where the player is the only participant. - this.contextType = ''; - this.locale = ''; - this.platform = ''; - this.version = ''; + this.contextType = null; + this.locale = null; + this.platform = null; + this.version = null; - this.playerID = ''; - this.playerName = ''; - this.playerPhotoURL = ''; + this.playerID = null; + this.playerName = null; + this.playerPhotoURL = null; this.paymentsReady = false; this.catalog = []; this.purchases = []; - this.leaderboards = {}; + this.ads = []; }, setDataHandler: function (parent, key, value) @@ -93,8 +95,10 @@ var FacebookInstantGamesPlugin = new Class({ var _this = this; - FBInstant.player.setDataAsync(data).then(function() { + FBInstant.player.setDataAsync(data).then(function () + { console.log('sdh saved', data); + _this.emit('savedata', data); }); }, @@ -113,25 +117,30 @@ var FacebookInstantGamesPlugin = new Class({ var _this = this; - FBInstant.player.setDataAsync(data).then(function() { + FBInstant.player.setDataAsync(data).then(function () + { console.log('cdh saved', data); + _this.emit('savedata', data); }); }, showLoadProgress: function (scene) { - scene.load.on('progress', function (value) { + scene.load.on('progress', function (value) + { if (!this.hasLoaded) { console.log(value); + FBInstant.setLoadingProgress(value * 100); } }, this); - scene.load.on('complete', function () { + scene.load.on('complete', function () + { this.hasLoaded = true; @@ -152,17 +161,16 @@ var FacebookInstantGamesPlugin = new Class({ var supported = {}; - var dotToUpper = function (match, offset, string) + var dotToUpper = function (match) { return match[1].toUpperCase(); - } - - APIs.forEach(function (api) { + }; + APIs.forEach(function (api) + { api = api.replace(/\../g, dotToUpper); supported[api] = true; - }); this.supportedAPIs = supported; @@ -181,27 +189,50 @@ var FacebookInstantGamesPlugin = new Class({ var _this = this; - FBInstant.onPause(function() { + FBInstant.onPause(function () + { _this.emit('pause'); }); - FBInstant.getEntryPointAsync().then(function (entrypoint) { - + FBInstant.getEntryPointAsync().then(function (entrypoint) + { _this.entryPoint = entrypoint; _this.entryPointData = FBInstant.getEntryPointData(); + _this.emit('startgame'); + }).catch(function (e) + { + console.warn(e); }); - FBInstant.payments.onReady(function () { + // Facebook.com and Android 6 only + if (this.supportedAPIs.paymentsPurchaseAsync) + { + FBInstant.payments.onReady(function () + { + console.log('payments ready'); + + _this.paymentsReady = true; + }).catch(function (e) + { + console.warn(e); + }); + } + }, - console.log('payments ready'); + checkAPI: function (api) + { + if (!this.supportedAPIs[api]) + { + console.warn(api + ' not supported'); - _this.paymentsReady = true; - - }); - - // this.emit('startgame'); + return false; + } + else + { + return true; + } }, getID: function () @@ -258,7 +289,7 @@ var FacebookInstantGamesPlugin = new Class({ { if (!this.playerID && this.supportedAPIs.playerGetID) { - this.playerID = FBInstant.player.getPlayerID(); + this.playerID = FBInstant.player.getID(); } return this.playerID; @@ -268,7 +299,7 @@ var FacebookInstantGamesPlugin = new Class({ { if (!this.playerName && this.supportedAPIs.playerGetName) { - this.playerName = FBInstant.player.getPlayerName(); + this.playerName = FBInstant.player.getName(); } return this.playerName; @@ -278,7 +309,7 @@ var FacebookInstantGamesPlugin = new Class({ { if (!this.playerPhotoURL && this.supportedAPIs.playerGetPhoto) { - this.playerPhotoURL = FBInstant.player.getPlayerPhoto(); + this.playerPhotoURL = FBInstant.player.getPhoto(); } return this.playerPhotoURL; @@ -286,40 +317,23 @@ var FacebookInstantGamesPlugin = new Class({ loadPlayerPhoto: function (scene, key) { - if (!this.playerPhotoURL) + if (this.playerPhotoURL) { - return false; + console.log('load'); + + scene.load.setCORS('anonymous'); + + scene.load.image(key, this.playerPhotoURL); + + scene.load.on('complete', function () + { + this.emit('photocomplete', key); + }, this); + + scene.load.start(); } - console.log('load'); - - scene.load.setCORS('anonymous'); - - scene.load.image(key, this.playerPhotoURL); - - scene.load.on('complete', function () { - - this.emit('photocomplete', key); - - }, this); - - scene.load.start(); - - return true; - }, - - checkAPI: function (api) - { - if (!this.supportedAPIs[api]) - { - console.warn(api + ' not supported'); - - return false; - } - else - { - return true; - } + return this; }, getData: function (keys) @@ -338,8 +352,8 @@ var FacebookInstantGamesPlugin = new Class({ var _this = this; - FBInstant.player.getDataAsync(keys).then(function(data) { - + FBInstant.player.getDataAsync(keys).then(function (data) + { console.log('getdata req', data); _this.dataLocked = true; @@ -352,7 +366,6 @@ var FacebookInstantGamesPlugin = new Class({ _this.dataLocked = false; _this.emit('getdata', data); - }); return this; @@ -367,8 +380,10 @@ var FacebookInstantGamesPlugin = new Class({ var _this = this; - FBInstant.player.setDataAsync(data).then(function() { + FBInstant.player.setDataAsync(data).then(function () + { console.log('data saved to fb'); + _this.emit('savedata', data); }); @@ -384,8 +399,10 @@ var FacebookInstantGamesPlugin = new Class({ var _this = this; - FBInstant.player.flushDataAsync().then(function() { + FBInstant.player.flushDataAsync().then(function () + { console.log('data flushed'); + _this.emit('flushdata'); }); @@ -401,8 +418,10 @@ var FacebookInstantGamesPlugin = new Class({ var _this = this; - FBInstant.player.getStatsAsync(keys).then(function(data) { + FBInstant.player.getStatsAsync(keys).then(function (data) + { console.log('stats got from fb'); + _this.emit('getstats', data); }); @@ -428,7 +447,8 @@ var FacebookInstantGamesPlugin = new Class({ var _this = this; - FBInstant.player.setStatsAsync(output).then(function() { + FBInstant.player.setStatsAsync(output).then(function () + { console.log('stats saved to fb'); _this.emit('savestats', output); }); @@ -455,8 +475,10 @@ var FacebookInstantGamesPlugin = new Class({ var _this = this; - FBInstant.player.incrementStatsAsync(output).then(function(stats) { + FBInstant.player.incrementStatsAsync(output).then(function (stats) + { console.log('stats modified'); + _this.emit('incstats', stats); }); @@ -534,7 +556,8 @@ var FacebookInstantGamesPlugin = new Class({ var _this = this; - FBInstant.shareAsync(payload).then(function() { + FBInstant.shareAsync(payload).then(function () + { _this.emit('resume'); }); @@ -562,11 +585,10 @@ var FacebookInstantGamesPlugin = new Class({ { var _this = this; - FBInstant.context.switchAsync(contextID).then(function() { - + FBInstant.context.switchAsync(contextID).then(function () + { _this.contextID = FBInstant.context.getID(); _this.emit('switch', _this.contextID); - }); } @@ -582,11 +604,10 @@ var FacebookInstantGamesPlugin = new Class({ var _this = this; - FBInstant.context.chooseAsync(options).then(function() { - + FBInstant.context.chooseAsync(options).then(function () + { _this.contextID = FBInstant.context.getID(); _this.emit('choose', _this.contextID); - }); return this; @@ -601,11 +622,10 @@ var FacebookInstantGamesPlugin = new Class({ var _this = this; - FBInstant.context.createAsync(playerID).then(function() { - + FBInstant.context.createAsync(playerID).then(function () + { _this.contextID = FBInstant.context.getID(); _this.emit('create', _this.contextID); - }); return this; @@ -620,13 +640,12 @@ var FacebookInstantGamesPlugin = new Class({ var _this = this; - FBInstant.player.getConnectedPlayersAsync().then(function(players) { + FBInstant.player.getConnectedPlayersAsync().then(function (players) + { console.log('got player data'); console.log(players); - _this.emit('players', players); - // id: player.getID(), - // name: player.getName(), + _this.emit('players', players); }); return this; @@ -634,28 +653,28 @@ var FacebookInstantGamesPlugin = new Class({ getCatalog: function () { - // if (!this.checkAPI('setSessionData')) - // { - // return this; - // } + if (!this.paymentsReady) + { + return this; + } var _this = this; var catalog = this.catalog; - FBInstant.payments.getCatalogAsync().then(function(data) { - + FBInstant.payments.getCatalogAsync().then(function (data) + { console.log('got catalog'); catalog = []; - data.forEach(function (item) { + data.forEach(function (item) + { catalog.push(Product(item)); }); _this.emit('getcatalog', catalog); - }); return this; @@ -663,9 +682,12 @@ var FacebookInstantGamesPlugin = new Class({ purchase: function (productID, developerPayload) { - var config = { - productID: productID - }; + if (!this.paymentsReady) + { + return this; + } + + var config = {productID: productID}; if (developerPayload) { @@ -674,14 +696,13 @@ var FacebookInstantGamesPlugin = new Class({ var _this = this; - FBInstant.payments.purchaseAsync(config).then(function(data) { - + FBInstant.payments.purchaseAsync(config).then(function (data) + { var purchase = Purchase(data); console.log('product purchase', purchase); _this.emit('purchase', purchase); - }); return this; @@ -689,23 +710,28 @@ var FacebookInstantGamesPlugin = new Class({ getPurchases: function () { + if (!this.paymentsReady) + { + return this; + } + var _this = this; var purchases = this.purchases; - FBInstant.payments.getPurchasesAsync().then(function(data) { - + FBInstant.payments.getPurchasesAsync().then(function (data) + { console.log('got purchases'); purchases = []; - data.forEach(function (item) { + data.forEach(function (item) + { purchases.push(Purchase(item)); }); _this.emit('getpurchases', purchases); - }); return this; @@ -713,20 +739,34 @@ var FacebookInstantGamesPlugin = new Class({ consumePurchases: function (purchaseToken) { + if (!this.paymentsReady) + { + return this; + } + var _this = this; - FBInstant.payments.consumePurchaseAsync(purchaseToken).then(function() { - + FBInstant.payments.consumePurchaseAsync(purchaseToken).then(function () + { console.log('purchase consumed'); _this.emit('consumepurchase', purchaseToken); - }); return this; }, update: function (cta, text, key, frame, template, updateData) + { + return this._update('CUSTOM', cta, text, key, frame, template, updateData); + }, + + updateLeaderboard: function (cta, text, key, frame, template, updateData) + { + return this._update('LEADERBOARD', cta, text, key, frame, template, updateData); + }, + + _update: function (action, cta, text, key, frame, template, updateData) { if (!this.checkAPI('shareAsync')) { @@ -737,9 +777,7 @@ var FacebookInstantGamesPlugin = new Class({ if (typeof text === 'string') { - text = { - default: text - }; + text = {default: text}; } if (updateData === undefined) { updateData = {}; } @@ -750,7 +788,7 @@ var FacebookInstantGamesPlugin = new Class({ } var payload = { - action: 'CUSTOM', + action: action, cta: cta, image: imageData, text: text, @@ -762,7 +800,8 @@ var FacebookInstantGamesPlugin = new Class({ var _this = this; - FBInstant.updateAsync(payload).then(function() { + FBInstant.updateAsync(payload).then(function () + { _this.emit('update'); }); @@ -782,17 +821,16 @@ var FacebookInstantGamesPlugin = new Class({ if (test.length > 1000) { - console.warn('switch Game entry point data too long. Max 1000 chars.'); + console.warn('Switch Game data too long. Max 1000 chars.'); return this; } } var _this = this; - FBInstant.switchGameAsync(appID, data).then(function() { - + FBInstant.switchGameAsync(appID, data).then(function () + { _this.emit('switchgame', appID); - }); return this; @@ -802,13 +840,15 @@ var FacebookInstantGamesPlugin = new Class({ { var _this = this; - FBInstant.canCreateShortcutAsync().then(function(canCreateShortcut) { - + FBInstant.canCreateShortcutAsync().then(function (canCreateShortcut) + { if (canCreateShortcut) { - FBInstant.createShortcutAsync().then(function() { + FBInstant.createShortcutAsync().then(function () + { _this.emit('shortcutcreated'); - }).catch(function() { + }).catch(function () + { _this.emit('shortcutfailed'); }); } @@ -838,54 +878,174 @@ var FacebookInstantGamesPlugin = new Class({ return this; }, - showAd: function (placementID) + preloadAds: function (placementID) { - // if (!this.checkAPI('logEvent')) - // { - // return this; - // } + if (!this.checkAPI('getInterstitialAdAsync')) + { + return this; + } - var adID; + if (!Array.isArray(placementID)) + { + placementID = [ placementID ]; + } - FBInstant.getInterstitialAdAsync(placementID).then(function (ad) { + var i; + var _this = this; - console.log(ad); + var total = 0; - adID = ad.getPlacementID(); + for (i = 0; i < this.ads.length; i++) + { + if (!this.ads[i].shown) + { + total++; + } + } - return ad.loadAsync(); + if (total + placementID.length >= 3) + { + console.warn('Too many AdInstances. Show an ad before loading more'); + return this; + } - }).then(function (ad) { + for (i = 0; i < placementID.length; i++) + { + var id = placementID[i]; - return ad.showAsync(); - - }); + FBInstant.getInterstitialAdAsync(id).then(function (data) + { + console.log('ad preloaded'); + + var ad = AdInstance(data, true); + + _this.ads.push(ad); + + return ad.loadAsync(); + + }).catch(function (e) + { + console.error(e); + }); + } return this; }, - showRewardVideo: function (placementID) + preloadVideoAds: function (placementID) { - // if (!this.checkAPI('logEvent')) - // { - // return this; - // } + if (!this.checkAPI('getRewardedVideoAsync')) + { + return this; + } - var adID; + if (!Array.isArray(placementID)) + { + placementID = [ placementID ]; + } - FBInstant.getRewardedVideoAsync(placementID).then(function (ad) { + var i; + var _this = this; - console.log('video', ad); + var total = 0; - adID = ad.getPlacementID(); + for (i = 0; i < this.ads.length; i++) + { + if (!this.ads[i].shown) + { + total++; + } + } - return ad.loadAsync(); + if (total + placementID.length >= 3) + { + console.warn('Too many AdInstances. Show an ad before loading more'); + return this; + } - }).then(function (ad) { + for (i = 0; i < placementID.length; i++) + { + var id = placementID[i]; - return ad.showAsync(); + FBInstant.getRewardedVideoAsync(id).then(function (data) + { + console.log('video ad preloaded'); - }); + var ad = AdInstance(data, true); + + _this.ads.push(ad); + + return ad.loadAsync(); + + }).catch(function (e) + { + console.error(e); + }); + } + + return this; + }, + + showAd: function (placementID) + { + var _this = this; + + for (var i = 0; i < this.ads.length; i++) + { + var ad = this.ads[i]; + + if (ad.placementID === placementID) + { + ad.instance.showAsync().then(function () + { + ad.shown = true; + + _this.emit('showad', ad); + }).catch(function (e) + { + if (e.code === 'ADS_NO_FILL') + { + _this.emit('adsnofill'); + } + else + { + console.error(e); + } + }); + } + } + + return this; + }, + + showVideo: function (placementID) + { + var _this = this; + + for (var i = 0; i < this.ads.length; i++) + { + var ad = this.ads[i]; + + if (ad.placementID === placementID && ad.video) + { + ad.instance.showAsync().then(function () + { + ad.shown = true; + + _this.emit('showvideo', ad); + }).catch(function (e) + { + if (e.code === 'ADS_NO_FILL') + { + _this.emit('adsnofill'); + } + else + { + console.error(e); + } + }); + } + } return this; }, @@ -902,15 +1062,14 @@ var FacebookInstantGamesPlugin = new Class({ var _this = this; - FBInstant.matchPlayerAsync(matchTag, switchImmediately).then(function () { - + FBInstant.matchPlayerAsync(matchTag, switchImmediately).then(function () + { console.log('match player'); _this.getID(); _this.getType(); _this.emit('matchplayer', _this.contextID, _this.contextType); - }); return this; @@ -927,8 +1086,8 @@ var FacebookInstantGamesPlugin = new Class({ var _this = this; - FBInstant.getLeaderboardAsync(name).then(function (data) { - + FBInstant.getLeaderboardAsync(name).then(function (data) + { console.log('leaderboard'); console.log(data); @@ -936,14 +1095,15 @@ var FacebookInstantGamesPlugin = new Class({ _this.leaderboards[name] = leaderboard; - _this.emit('leaderboard', leaderboard); - + _this.emit('getleaderboard', leaderboard); + }).catch(function (e) + { + console.warn(e); }); return this; }, - /** * Destroys the FacebookInstantGamesPlugin. * diff --git a/src/fbinstant/Leaderboard.js b/src/fbinstant/Leaderboard.js index 0a192bada..2c34e20e9 100644 --- a/src/fbinstant/Leaderboard.js +++ b/src/fbinstant/Leaderboard.js @@ -36,33 +36,43 @@ var Leaderboard = new Class({ this.playerScore = null; this.scores = []; + + this.getEntryCount(); }, getEntryCount: function () { var _this = this; - this.ref.getEntryCountAsync().then(function (count) { - + this.ref.getEntryCountAsync().then(function (count) + { console.log('entry count', count); _this.entryCount = count; _this.emit('getentrycount', count, _this.name); + }).catch(function (e) + { + console.warn(e); }); }, setScore: function (score, data) { + if (data === undefined) { data = ''; } + var _this = this; - this.ref.setScoreAsync(score, data).then(function (entry) { - + this.ref.setScoreAsync(score, data).then(function (entry) + { console.log('set score', entry); _this.emit('setscore', entry.getScore(), entry.getExtraData(), _this.name); + }).catch(function (e) + { + console.warn(e); }); }, @@ -70,16 +80,21 @@ var Leaderboard = new Class({ { var _this = this; - this.ref.getPlayerEntryAsync().then(function (entry) { - - console.log('get player score', entry); + this.ref.getPlayerEntryAsync().then(function (entry) + { + console.log('get player score'); var score = LeaderboardScore(entry); + console.log(score); + _this.playerScore = score; _this.emit('getplayerscore', score, _this.name); + }).catch(function (e) + { + console.warn(e); }); }, @@ -91,13 +106,14 @@ var Leaderboard = new Class({ var _this = this; - this.ref.getEntriesAsync().then(function (entries) { - + this.ref.getEntriesAsync().then(function (entries) + { console.log('get scores', entries); _this.scores = []; - entries.forEach(function (entry) { + entries.forEach(function (entry) + { _this.scores.push(LeaderboardScore(entry)); @@ -105,6 +121,9 @@ var Leaderboard = new Class({ _this.emit('getscores', _this.scores, _this.name); + }).catch(function (e) + { + console.warn(e); }); }