mirror of
https://github.com/photonstorm/phaser
synced 2025-02-17 06:28:30 +00:00
Updated FB Plugin
This commit is contained in:
parent
fcf5d20d7b
commit
d415e2ff7f
3 changed files with 570 additions and 37 deletions
|
@ -9,6 +9,8 @@ var DataManager = require('../data/DataManager');
|
|||
var EventEmitter = require('eventemitter3');
|
||||
var GetValue = require('../utils/object/GetValue');
|
||||
var LeaderboardScore = require('./LeaderboardScore');
|
||||
var Product = require('./Product');
|
||||
var Purchase = require('./Purchase');
|
||||
|
||||
/**
|
||||
* @classdesc
|
||||
|
@ -51,10 +53,17 @@ var FacebookInstantGamesPlugin = new Class({
|
|||
this.hasLoaded = false;
|
||||
this.dataLocked = false;
|
||||
|
||||
this.apis = [];
|
||||
this.supportedAPIs = [];
|
||||
|
||||
console.log(this.supportedAPIs);
|
||||
this.entryPoint = '';
|
||||
this.entryPointData = null;
|
||||
this.contextID = 0;
|
||||
|
||||
// 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 = '';
|
||||
|
@ -63,6 +72,10 @@ var FacebookInstantGamesPlugin = new Class({
|
|||
this.playerID = '';
|
||||
this.playerName = '';
|
||||
this.playerPhotoURL = '';
|
||||
|
||||
this.paymentsReady = false;
|
||||
this.catalog = [];
|
||||
this.purchases = [];
|
||||
},
|
||||
|
||||
setDataHandler: function (parent, key, value)
|
||||
|
@ -134,17 +147,36 @@ var FacebookInstantGamesPlugin = new Class({
|
|||
{
|
||||
console.log('FBP gameStarted');
|
||||
|
||||
this.apis = FBInstant.getSupportedAPIs();
|
||||
var APIs = FBInstant.getSupportedAPIs();
|
||||
|
||||
this.contextID = FBInstant.context.getID();
|
||||
this.contextType = FBInstant.context.getType();
|
||||
this.locale = FBInstant.getLocale();
|
||||
this.platform = FBInstant.getPlatform();
|
||||
this.version = FBInstant.getSDKVersion();
|
||||
var supported = {};
|
||||
|
||||
this.playerID = FBInstant.player.getID();
|
||||
this.playerName = FBInstant.player.getName();
|
||||
this.playerPhotoURL = FBInstant.player.getPhoto();
|
||||
var dotToUpper = function (match, offset, string)
|
||||
{
|
||||
return match[1].toUpperCase();
|
||||
}
|
||||
|
||||
APIs.forEach(function (api) {
|
||||
|
||||
api = api.replace(/\../g, dotToUpper);
|
||||
|
||||
supported[api] = true;
|
||||
|
||||
});
|
||||
|
||||
this.supportedAPIs = supported;
|
||||
|
||||
console.log(this.supportedAPIs);
|
||||
|
||||
this.getID();
|
||||
this.getType();
|
||||
this.getLocale();
|
||||
this.getPlatform();
|
||||
this.getSDKVersion();
|
||||
|
||||
this.getPlayerID();
|
||||
this.getPlayerName();
|
||||
this.getPlayerPhotoURL();
|
||||
|
||||
var _this = this;
|
||||
|
||||
|
@ -160,11 +192,104 @@ var FacebookInstantGamesPlugin = new Class({
|
|||
|
||||
});
|
||||
|
||||
FBInstant.payments.onReady(function () {
|
||||
|
||||
console.log('payments ready');
|
||||
|
||||
_this.paymentsReady = true;
|
||||
|
||||
});
|
||||
|
||||
// this.emit('startgame');
|
||||
},
|
||||
|
||||
getID: function ()
|
||||
{
|
||||
if (!this.contextID && this.supportedAPIs.contextGetID)
|
||||
{
|
||||
this.contextID = FBInstant.context.getID();
|
||||
}
|
||||
|
||||
return this.contextID;
|
||||
},
|
||||
|
||||
getType: function ()
|
||||
{
|
||||
if (!this.contextType && this.supportedAPIs.contextGetType)
|
||||
{
|
||||
this.contextType = FBInstant.context.getType();
|
||||
}
|
||||
|
||||
return this.contextType;
|
||||
},
|
||||
|
||||
getLocale: function ()
|
||||
{
|
||||
if (!this.locale && this.supportedAPIs.getLocale)
|
||||
{
|
||||
this.locale = FBInstant.getLocale();
|
||||
}
|
||||
|
||||
return this.locale;
|
||||
},
|
||||
|
||||
getPlatform: function ()
|
||||
{
|
||||
if (!this.platform && this.supportedAPIs.getPlatform)
|
||||
{
|
||||
this.platform = FBInstant.getPlatform();
|
||||
}
|
||||
|
||||
return this.platform;
|
||||
},
|
||||
|
||||
getSDKVersion: function ()
|
||||
{
|
||||
if (!this.version && this.supportedAPIs.getSDKVersion)
|
||||
{
|
||||
this.version = FBInstant.getSDKVersion();
|
||||
}
|
||||
|
||||
return this.version;
|
||||
},
|
||||
|
||||
getPlayerID: function ()
|
||||
{
|
||||
if (!this.playerID && this.supportedAPIs.playerGetID)
|
||||
{
|
||||
this.playerID = FBInstant.player.getPlayerID();
|
||||
}
|
||||
|
||||
return this.playerID;
|
||||
},
|
||||
|
||||
getPlayerName: function ()
|
||||
{
|
||||
if (!this.playerName && this.supportedAPIs.playerGetName)
|
||||
{
|
||||
this.playerName = FBInstant.player.getPlayerName();
|
||||
}
|
||||
|
||||
return this.playerName;
|
||||
},
|
||||
|
||||
getPlayerPhotoURL: function ()
|
||||
{
|
||||
if (!this.playerPhotoURL && this.supportedAPIs.playerGetPhoto)
|
||||
{
|
||||
this.playerPhotoURL = FBInstant.player.getPlayerPhoto();
|
||||
}
|
||||
|
||||
return this.playerPhotoURL;
|
||||
},
|
||||
|
||||
loadPlayerPhoto: function (scene, key)
|
||||
{
|
||||
if (!this.playerPhotoURL)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
console.log('load');
|
||||
|
||||
scene.load.setCORS('anonymous');
|
||||
|
@ -179,11 +304,30 @@ var FacebookInstantGamesPlugin = new Class({
|
|||
|
||||
scene.load.start();
|
||||
|
||||
return this;
|
||||
return true;
|
||||
},
|
||||
|
||||
checkAPI: function (api)
|
||||
{
|
||||
if (!this.supportedAPIs[api])
|
||||
{
|
||||
console.warn(api + ' not supported');
|
||||
|
||||
return false;
|
||||
}
|
||||
else
|
||||
{
|
||||
return true;
|
||||
}
|
||||
},
|
||||
|
||||
getData: function (keys)
|
||||
{
|
||||
if (!this.checkAPI('playerGetDataAsync'))
|
||||
{
|
||||
return this;
|
||||
}
|
||||
|
||||
if (!Array.isArray(keys))
|
||||
{
|
||||
keys = [ keys ];
|
||||
|
@ -215,6 +359,11 @@ var FacebookInstantGamesPlugin = new Class({
|
|||
|
||||
saveData: function (data)
|
||||
{
|
||||
if (!this.checkAPI('playerSetDataAsync'))
|
||||
{
|
||||
return this;
|
||||
}
|
||||
|
||||
var _this = this;
|
||||
|
||||
FBInstant.player.setDataAsync(data).then(function() {
|
||||
|
@ -227,6 +376,11 @@ var FacebookInstantGamesPlugin = new Class({
|
|||
|
||||
flushData: function ()
|
||||
{
|
||||
if (!this.checkAPI('playerFlushDataAsync'))
|
||||
{
|
||||
return this;
|
||||
}
|
||||
|
||||
var _this = this;
|
||||
|
||||
FBInstant.player.flushDataAsync().then(function() {
|
||||
|
@ -239,6 +393,11 @@ var FacebookInstantGamesPlugin = new Class({
|
|||
|
||||
getStats: function (keys)
|
||||
{
|
||||
if (!this.checkAPI('playerGetStatsAsync'))
|
||||
{
|
||||
return this;
|
||||
}
|
||||
|
||||
var _this = this;
|
||||
|
||||
FBInstant.player.getStatsAsync(keys).then(function(data) {
|
||||
|
@ -251,6 +410,11 @@ var FacebookInstantGamesPlugin = new Class({
|
|||
|
||||
saveStats: function (data)
|
||||
{
|
||||
if (!this.checkAPI('playerSetStatsAsync'))
|
||||
{
|
||||
return this;
|
||||
}
|
||||
|
||||
var output = {};
|
||||
|
||||
for (var key in data)
|
||||
|
@ -273,6 +437,11 @@ var FacebookInstantGamesPlugin = new Class({
|
|||
|
||||
incStats: function (data)
|
||||
{
|
||||
if (!this.checkAPI('playerIncrementStatsAsync'))
|
||||
{
|
||||
return this;
|
||||
}
|
||||
|
||||
var output = {};
|
||||
|
||||
for (var key in data)
|
||||
|
@ -295,6 +464,11 @@ var FacebookInstantGamesPlugin = new Class({
|
|||
|
||||
saveSession: function (data)
|
||||
{
|
||||
if (!this.checkAPI('setSessionData'))
|
||||
{
|
||||
return this;
|
||||
}
|
||||
|
||||
var test = JSON.stringify(data);
|
||||
|
||||
if (test.length <= 1000)
|
||||
|
@ -329,26 +503,13 @@ var FacebookInstantGamesPlugin = new Class({
|
|||
return this._share('CHALLENGE', text, key, frame, sessionData);
|
||||
},
|
||||
|
||||
createShortcut: function ()
|
||||
{
|
||||
var _this = this;
|
||||
|
||||
FBInstant.canCreateShortcutAsync().then(function(canCreateShortcut) {
|
||||
|
||||
if (canCreateShortcut)
|
||||
{
|
||||
FBInstant.createShortcutAsync().then(function() {
|
||||
_this.emit('shortcutcreated');
|
||||
}).catch(function() {
|
||||
_this.emit('shortcutfailed');
|
||||
});
|
||||
}
|
||||
|
||||
});
|
||||
},
|
||||
|
||||
_share: function (intent, text, key, frame, sessionData)
|
||||
{
|
||||
if (!this.checkAPI('shareAsync'))
|
||||
{
|
||||
return this;
|
||||
}
|
||||
|
||||
if (sessionData === undefined) { sessionData = {}; }
|
||||
|
||||
if (key)
|
||||
|
@ -379,8 +540,293 @@ var FacebookInstantGamesPlugin = new Class({
|
|||
return this;
|
||||
},
|
||||
|
||||
isSizeBetween: function (min, max)
|
||||
{
|
||||
if (!this.checkAPI('contextIsSizeBetween'))
|
||||
{
|
||||
return this;
|
||||
}
|
||||
|
||||
return FBInstant.context.isSizeBetween(min, max);
|
||||
},
|
||||
|
||||
switchContext: function (contextID)
|
||||
{
|
||||
if (!this.checkAPI('contextSwitchAsync'))
|
||||
{
|
||||
return this;
|
||||
}
|
||||
|
||||
if (contextID !== this.contextID)
|
||||
{
|
||||
var _this = this;
|
||||
|
||||
FBInstant.context.switchAsync(contextID).then(function() {
|
||||
|
||||
_this.contextID = FBInstant.context.getID();
|
||||
_this.emit('switch', _this.contextID);
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
return this;
|
||||
},
|
||||
|
||||
chooseContext: function (options)
|
||||
{
|
||||
if (!this.checkAPI('contextChoseAsync'))
|
||||
{
|
||||
return this;
|
||||
}
|
||||
|
||||
var _this = this;
|
||||
|
||||
FBInstant.context.chooseAsync(options).then(function() {
|
||||
|
||||
_this.contextID = FBInstant.context.getID();
|
||||
_this.emit('choose', _this.contextID);
|
||||
|
||||
});
|
||||
|
||||
return this;
|
||||
},
|
||||
|
||||
createContext: function (playerID)
|
||||
{
|
||||
if (!this.checkAPI('contextCreateAsync'))
|
||||
{
|
||||
return this;
|
||||
}
|
||||
|
||||
var _this = this;
|
||||
|
||||
FBInstant.context.createAsync(playerID).then(function() {
|
||||
|
||||
_this.contextID = FBInstant.context.getID();
|
||||
_this.emit('create', _this.contextID);
|
||||
|
||||
});
|
||||
|
||||
return this;
|
||||
},
|
||||
|
||||
getPlayers: function ()
|
||||
{
|
||||
if (!this.checkAPI('playerGetConnectedPlayersAsync'))
|
||||
{
|
||||
return this;
|
||||
}
|
||||
|
||||
var _this = this;
|
||||
|
||||
FBInstant.player.getConnectedPlayersAsync().then(function(players) {
|
||||
console.log('got player data');
|
||||
console.log(players);
|
||||
_this.emit('players', players);
|
||||
|
||||
// id: player.getID(),
|
||||
// name: player.getName(),
|
||||
});
|
||||
|
||||
return this;
|
||||
},
|
||||
|
||||
getCatalog: function ()
|
||||
{
|
||||
// if (!this.checkAPI('setSessionData'))
|
||||
// {
|
||||
// return this;
|
||||
// }
|
||||
|
||||
var _this = this;
|
||||
var catalog = this.catalog;
|
||||
|
||||
FBInstant.payments.getCatalogAsync().then(function(data) {
|
||||
|
||||
console.log('got catalog');
|
||||
|
||||
catalog = [];
|
||||
|
||||
data.forEach(function (item) {
|
||||
|
||||
catalog.push(Product(item));
|
||||
|
||||
});
|
||||
|
||||
_this.emit('getcatalog', catalog);
|
||||
|
||||
});
|
||||
|
||||
return this;
|
||||
},
|
||||
|
||||
purchase: function (productID, developerPayload)
|
||||
{
|
||||
var config = {
|
||||
productID: productID
|
||||
};
|
||||
|
||||
if (developerPayload)
|
||||
{
|
||||
config.developerPayload = developerPayload;
|
||||
}
|
||||
|
||||
var _this = this;
|
||||
|
||||
FBInstant.payments.purchaseAsync(config).then(function(data) {
|
||||
|
||||
var purchase = Purchase(data);
|
||||
|
||||
console.log('product purchase', purchase);
|
||||
|
||||
_this.emit('purchase', purchase);
|
||||
|
||||
});
|
||||
|
||||
return this;
|
||||
},
|
||||
|
||||
getPurchases: function ()
|
||||
{
|
||||
var _this = this;
|
||||
var purchases = this.purchases;
|
||||
|
||||
FBInstant.payments.getPurchasesAsync().then(function(data) {
|
||||
|
||||
console.log('got purchases');
|
||||
|
||||
purchases = [];
|
||||
|
||||
data.forEach(function (item) {
|
||||
|
||||
purchases.push(Purchase(item));
|
||||
|
||||
});
|
||||
|
||||
_this.emit('getpurchases', purchases);
|
||||
|
||||
});
|
||||
|
||||
return this;
|
||||
},
|
||||
|
||||
consumePurchases: function (purchaseToken)
|
||||
{
|
||||
var _this = this;
|
||||
|
||||
FBInstant.payments.consumePurchaseAsync(purchaseToken).then(function() {
|
||||
|
||||
console.log('purchase consumed');
|
||||
|
||||
_this.emit('consumepurchase', purchaseToken);
|
||||
|
||||
});
|
||||
|
||||
return this;
|
||||
},
|
||||
|
||||
update: function (cta, text, key, frame, template, updateData)
|
||||
{
|
||||
if (!this.checkAPI('shareAsync'))
|
||||
{
|
||||
return this;
|
||||
}
|
||||
|
||||
if (cta === undefined) { cta = ''; }
|
||||
|
||||
if (typeof text === 'string')
|
||||
{
|
||||
text = {
|
||||
default: text
|
||||
};
|
||||
}
|
||||
|
||||
if (updateData === undefined) { updateData = {}; }
|
||||
|
||||
if (key)
|
||||
{
|
||||
var imageData = this.game.textures.getBase64(key, frame);
|
||||
}
|
||||
|
||||
var payload = {
|
||||
action: 'CUSTOM',
|
||||
cta: cta,
|
||||
image: imageData,
|
||||
text: text,
|
||||
template: template,
|
||||
data: updateData,
|
||||
strategy: 'IMMEDIATE',
|
||||
notification: 'NO_PUSH'
|
||||
};
|
||||
|
||||
var _this = this;
|
||||
|
||||
FBInstant.updateAsync(payload).then(function() {
|
||||
_this.emit('update');
|
||||
});
|
||||
|
||||
return this;
|
||||
},
|
||||
|
||||
switchGame: function (appID, data)
|
||||
{
|
||||
if (!this.checkAPI('switchGameAsync'))
|
||||
{
|
||||
return this;
|
||||
}
|
||||
|
||||
if (data)
|
||||
{
|
||||
var test = JSON.stringify(data);
|
||||
|
||||
if (test.length > 1000)
|
||||
{
|
||||
console.warn('switch Game entry point data too long. Max 1000 chars.');
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
var _this = this;
|
||||
|
||||
FBInstant.switchGameAsync(appID, data).then(function() {
|
||||
|
||||
_this.emit('switchgame', appID);
|
||||
|
||||
});
|
||||
|
||||
return this;
|
||||
},
|
||||
|
||||
createShortcut: function ()
|
||||
{
|
||||
var _this = this;
|
||||
|
||||
FBInstant.canCreateShortcutAsync().then(function(canCreateShortcut) {
|
||||
|
||||
if (canCreateShortcut)
|
||||
{
|
||||
FBInstant.createShortcutAsync().then(function() {
|
||||
_this.emit('shortcutcreated');
|
||||
}).catch(function() {
|
||||
_this.emit('shortcutfailed');
|
||||
});
|
||||
}
|
||||
|
||||
});
|
||||
},
|
||||
|
||||
quit: function ()
|
||||
{
|
||||
FBInstant.quit();
|
||||
},
|
||||
|
||||
log: function (name, value, params)
|
||||
{
|
||||
if (!this.checkAPI('logEvent'))
|
||||
{
|
||||
return this;
|
||||
}
|
||||
|
||||
if (params === undefined) { params = {}; }
|
||||
|
||||
if (name.length >= 2 && name.length <= 40)
|
||||
|
@ -391,17 +837,53 @@ var FacebookInstantGamesPlugin = new Class({
|
|||
return this;
|
||||
},
|
||||
|
||||
getPlayers: function ()
|
||||
showAd: function (placementID)
|
||||
{
|
||||
var _this = this;
|
||||
// if (!this.checkAPI('logEvent'))
|
||||
// {
|
||||
// return this;
|
||||
// }
|
||||
|
||||
FBInstant.player.getConnectedPlayersAsync().then(function(players) {
|
||||
console.log('got player data');
|
||||
console.log(players);
|
||||
_this.emit('players', players);
|
||||
var adID;
|
||||
|
||||
FBInstant.getInterstitialAdAsync(placementID).then(function (ad) {
|
||||
|
||||
console.log(ad);
|
||||
|
||||
adID = ad.getPlacementID();
|
||||
|
||||
return ad.loadAsync();
|
||||
|
||||
}).then(function (ad) {
|
||||
|
||||
return ad.showAsync();
|
||||
|
||||
});
|
||||
|
||||
return this;
|
||||
},
|
||||
|
||||
showRewardVideo: function (placementID)
|
||||
{
|
||||
// if (!this.checkAPI('logEvent'))
|
||||
// {
|
||||
// return this;
|
||||
// }
|
||||
|
||||
var adID;
|
||||
|
||||
FBInstant.getRewardedVideoAsync(placementID).then(function (ad) {
|
||||
|
||||
console.log('video', ad);
|
||||
|
||||
adID = ad.getPlacementID();
|
||||
|
||||
return ad.loadAsync();
|
||||
|
||||
}).then(function (ad) {
|
||||
|
||||
return ad.showAsync();
|
||||
|
||||
// id: player.getID(),
|
||||
// name: player.getName(),
|
||||
});
|
||||
|
||||
return this;
|
||||
|
|
30
src/fbinstant/Product.js
Normal file
30
src/fbinstant/Product.js
Normal file
|
@ -0,0 +1,30 @@
|
|||
/**
|
||||
* @author Richard Davey <rich@photonstorm.com>
|
||||
* @copyright 2018 Photon Storm Ltd.
|
||||
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
|
||||
*/
|
||||
|
||||
var GetFastValue = require('../utils/object/GetFastValue');
|
||||
|
||||
/**
|
||||
* @classdesc
|
||||
* [description]
|
||||
*
|
||||
* @class FacebookInstantGamesPlugin
|
||||
* @memberOf Phaser
|
||||
* @constructor
|
||||
* @since 3.12.0
|
||||
*/
|
||||
var Product = function (data)
|
||||
{
|
||||
return {
|
||||
title: GetFastValue(data, 'title', ''),
|
||||
productID: GetFastValue(data, 'productID', ''),
|
||||
description: GetFastValue(data, 'description', ''),
|
||||
imageURI: GetFastValue(data, 'imageURI', ''),
|
||||
price: GetFastValue(data, 'price', ''),
|
||||
priceCurrencyCode: GetFastValue(data, 'priceCurrencyCode', '')
|
||||
};
|
||||
};
|
||||
|
||||
module.exports = Product;
|
21
src/fbinstant/Purchase.js
Normal file
21
src/fbinstant/Purchase.js
Normal file
|
@ -0,0 +1,21 @@
|
|||
/**
|
||||
* @author Richard Davey <rich@photonstorm.com>
|
||||
* @copyright 2018 Photon Storm Ltd.
|
||||
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
|
||||
*/
|
||||
|
||||
var GetFastValue = require('../utils/object/GetFastValue');
|
||||
|
||||
var Purchase = function (data)
|
||||
{
|
||||
return {
|
||||
developerPayload: GetFastValue(data, 'developerPayload', ''),
|
||||
paymentID: GetFastValue(data, 'paymentID', ''),
|
||||
productID: GetFastValue(data, 'productID', ''),
|
||||
purchaseTime: GetFastValue(data, 'purchaseTime', ''),
|
||||
purchaseToken: GetFastValue(data, 'purchaseToken', ''),
|
||||
signedRequest: GetFastValue(data, 'signedRequest', '')
|
||||
};
|
||||
};
|
||||
|
||||
module.exports = Purchase;
|
Loading…
Add table
Reference in a new issue