From 162e910d18c34e7c8f66c2915f1ab33a4ebe01d7 Mon Sep 17 00:00:00 2001 From: Richard Davey Date: Fri, 27 Jul 2018 11:19:41 +0100 Subject: [PATCH] Added intent methods and leaderboard score class --- src/fbinstant/FacebookInstantGamesPlugin.js | 70 +++++++++++++++++++-- src/fbinstant/LeaderboardScore.js | 33 ++++++++++ 2 files changed, 97 insertions(+), 6 deletions(-) create mode 100644 src/fbinstant/LeaderboardScore.js diff --git a/src/fbinstant/FacebookInstantGamesPlugin.js b/src/fbinstant/FacebookInstantGamesPlugin.js index 52ee88ccf..755553f1f 100644 --- a/src/fbinstant/FacebookInstantGamesPlugin.js +++ b/src/fbinstant/FacebookInstantGamesPlugin.js @@ -8,6 +8,7 @@ var Class = require('../utils/Class'); var DataManager = require('../data/DataManager'); var EventEmitter = require('eventemitter3'); var GetValue = require('../utils/object/GetValue'); +var LeaderboardScore = require('./LeaderboardScore'); /** * @classdesc @@ -126,14 +127,12 @@ var FacebookInstantGamesPlugin = new Class({ }, this); - scene.load.start(); - return this; }, gameStarted: function () { - console.log('gameStarted'); + console.log('FBP gameStarted'); this.apis = FBInstant.getSupportedAPIs(); @@ -149,14 +148,19 @@ var FacebookInstantGamesPlugin = new Class({ var _this = this; + FBInstant.onPause(function() { + _this.emit('pause'); + }); + FBInstant.getEntryPointAsync().then(function (entrypoint) { _this.entryPoint = entrypoint; _this.entryPointData = FBInstant.getEntryPointData(); + _this.emit('startgame'); }); - this.emit('startgame'); + // this.emit('startgame'); }, loadPlayerPhoto: function (scene, key) @@ -294,6 +298,44 @@ var FacebookInstantGamesPlugin = new Class({ }, openShare: function (text, key, frame, sessionData) + { + return this._share('SHARE', text, key, frame, sessionData); + }, + + openInvite: function (text, key, frame, sessionData) + { + return this._share('INVITE', text, key, frame, sessionData); + }, + + openRequest: function (text, key, frame, sessionData) + { + return this._share('REQUEST', text, key, frame, sessionData); + }, + + openChallenge: function (text, key, frame, sessionData) + { + 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 (sessionData === undefined) { sessionData = {}; } @@ -303,13 +345,13 @@ var FacebookInstantGamesPlugin = new Class({ } var payload = { - intent: 'SHARE', + intent: intent, image: imageData, text: text, data: sessionData }; - console.log(payload); + // console.log(payload); // intent ("INVITE" | "REQUEST" | "CHALLENGE" | "SHARE") Indicates the intent of the share. // image string A base64 encoded image to be shared. @@ -321,6 +363,20 @@ var FacebookInstantGamesPlugin = new Class({ FBInstant.shareAsync(payload).then(function() { _this.emit('resume'); }); + + return this; + }, + + log: function (name, value, params) + { + if (params === undefined) { params = {}; } + + if (name.length >= 2 && name.length <= 40) + { + FBInstant.logEvent(name, parseFloat(value), params); + } + + return this; }, /** @@ -331,6 +387,8 @@ var FacebookInstantGamesPlugin = new Class({ */ destroy: function () { + FBInstant.quit(); + this.game = null; } diff --git a/src/fbinstant/LeaderboardScore.js b/src/fbinstant/LeaderboardScore.js new file mode 100644 index 000000000..7bddd8726 --- /dev/null +++ b/src/fbinstant/LeaderboardScore.js @@ -0,0 +1,33 @@ +/** + * @author Richard Davey + * @copyright 2018 Photon Storm Ltd. + * @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License} + */ + +var Class = require('../utils/Class'); + +/** + * @classdesc + * [description] + * + * @class FacebookInstantGamesPlugin + * @memberOf Phaser + * @constructor + * @since 3.12.0 + */ +var LeaderboardScore = new Class({ + + initialize: + + function LeaderboardScore () + { + this.value; + this.valueFormatted; + this.timestamp; + this.rank; + this.data; + } + +}); + +module.exports = LeaderboardScore;