|
| 1 | +/** |
| 2 | + * @author Richard Davey <rich@photonstorm.com> |
| 3 | + * @copyright 2018 Photon Storm Ltd. |
| 4 | + * @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License} |
| 5 | + */ |
| 6 | + |
| 7 | +var Class = require('../utils/Class'); |
| 8 | +var DataManager = require('../data/DataManager'); |
| 9 | +var EventEmitter = require('eventemitter3'); |
| 10 | +var GetValue = require('../utils/object/GetValue'); |
| 11 | + |
| 12 | +/** |
| 13 | + * @classdesc |
| 14 | + * [description] |
| 15 | + * |
| 16 | + * @class FacebookInstantGamesPlugin |
| 17 | + * @memberOf Phaser |
| 18 | + * @constructor |
| 19 | + * @extends Phaser.Events.EventEmitter |
| 20 | + * @since 3.12.0 |
| 21 | + * |
| 22 | + * @param {Phaser.Game} game - A reference to the Phaser.Game instance. |
| 23 | + * @param {FBConfig} config |
| 24 | + */ |
| 25 | +var FacebookInstantGamesPlugin = new Class({ |
| 26 | + |
| 27 | + Extends: EventEmitter, |
| 28 | + |
| 29 | + initialize: |
| 30 | + |
| 31 | + function FacebookInstantGamesPlugin (game, config) |
| 32 | + { |
| 33 | + EventEmitter.call(this); |
| 34 | + |
| 35 | + /** |
| 36 | + * A reference to the Phaser.Game instance. |
| 37 | + * |
| 38 | + * @name Phaser.Boot.FacebookInstantGamesPlugin#game |
| 39 | + * @type {Phaser.Game} |
| 40 | + * @readOnly |
| 41 | + * @since 3.12.0 |
| 42 | + */ |
| 43 | + this.game = game; |
| 44 | + |
| 45 | + this.data = new DataManager(this); |
| 46 | + |
| 47 | + this.on('setdata', this.setDataHandler, this); |
| 48 | + this.on('changedata', this.changeDataHandler, this); |
| 49 | + |
| 50 | + this.hasLoaded = false; |
| 51 | + this.dataLocked = false; |
| 52 | + |
| 53 | + this.playerID = ''; |
| 54 | + this.playerName = ''; |
| 55 | + this.playerPhotoURL = ''; |
| 56 | + }, |
| 57 | + |
| 58 | + setDataHandler: function (parent, key, value) |
| 59 | + { |
| 60 | + if (this.dataLocked) |
| 61 | + { |
| 62 | + return; |
| 63 | + } |
| 64 | + |
| 65 | + console.log('set data:', key, value); |
| 66 | + |
| 67 | + var data = {}; |
| 68 | + data[key] = value; |
| 69 | + |
| 70 | + var _this = this; |
| 71 | + |
| 72 | + FBInstant.player.setDataAsync(data).then(function() { |
| 73 | + console.log('sdh saved', data); |
| 74 | + _this.emit('savedata', data); |
| 75 | + }); |
| 76 | + }, |
| 77 | + |
| 78 | + changeDataHandler: function (parent, key, value) |
| 79 | + { |
| 80 | + if (this.dataLocked) |
| 81 | + { |
| 82 | + return; |
| 83 | + } |
| 84 | + |
| 85 | + console.log('change data:', key, value); |
| 86 | + |
| 87 | + var data = {}; |
| 88 | + data[key] = value; |
| 89 | + |
| 90 | + var _this = this; |
| 91 | + |
| 92 | + FBInstant.player.setDataAsync(data).then(function() { |
| 93 | + console.log('cdh saved', data); |
| 94 | + _this.emit('savedata', data); |
| 95 | + }); |
| 96 | + }, |
| 97 | + |
| 98 | + showLoadProgress: function (scene) |
| 99 | + { |
| 100 | + scene.load.on('progress', function (value) { |
| 101 | + |
| 102 | + if (!this.hasLoaded) |
| 103 | + { |
| 104 | + console.log(value); |
| 105 | + FBInstant.setLoadingProgress(value * 100); |
| 106 | + } |
| 107 | + |
| 108 | + }, this); |
| 109 | + |
| 110 | + scene.load.on('complete', function () { |
| 111 | + |
| 112 | + this.hasLoaded = true; |
| 113 | + |
| 114 | + console.log('loaded'); |
| 115 | + |
| 116 | + FBInstant.startGameAsync().then(this.gameStarted.bind(this)); |
| 117 | + |
| 118 | + }, this); |
| 119 | + |
| 120 | + scene.load.start(); |
| 121 | + |
| 122 | + return this; |
| 123 | + }, |
| 124 | + |
| 125 | + gameStarted: function () |
| 126 | + { |
| 127 | + console.log('gameStarted'); |
| 128 | + |
| 129 | + this.playerID = FBInstant.player.getID(); |
| 130 | + this.playerName = FBInstant.player.getName(); |
| 131 | + this.playerPhotoURL = FBInstant.player.getPhoto(); |
| 132 | + |
| 133 | + this.emit('startgame'); |
| 134 | + }, |
| 135 | + |
| 136 | + loadPlayerPhoto: function (scene, key) |
| 137 | + { |
| 138 | + console.log('load'); |
| 139 | + |
| 140 | + scene.load.setCORS('anonymous'); |
| 141 | + |
| 142 | + scene.load.image(key, this.playerPhotoURL); |
| 143 | + |
| 144 | + scene.load.on('complete', function () { |
| 145 | + |
| 146 | + this.emit('photocomplete', key); |
| 147 | + |
| 148 | + }, this); |
| 149 | + |
| 150 | + scene.load.start(); |
| 151 | + |
| 152 | + return this; |
| 153 | + }, |
| 154 | + |
| 155 | + getData: function (keys) |
| 156 | + { |
| 157 | + if (!Array.isArray(keys)) |
| 158 | + { |
| 159 | + keys = [ keys ]; |
| 160 | + } |
| 161 | + |
| 162 | + console.log('getdata', keys); |
| 163 | + |
| 164 | + var _this = this; |
| 165 | + |
| 166 | + FBInstant.player.getDataAsync(keys).then(function(data) { |
| 167 | + |
| 168 | + console.log('getdata req', data); |
| 169 | + |
| 170 | + _this.dataLocked = true; |
| 171 | + |
| 172 | + for (var key in data) |
| 173 | + { |
| 174 | + _this.data.set(key, data[key]); |
| 175 | + } |
| 176 | + |
| 177 | + _this.dataLocked = false; |
| 178 | + |
| 179 | + _this.emit('getdata', data); |
| 180 | + |
| 181 | + }); |
| 182 | + |
| 183 | + return this; |
| 184 | + }, |
| 185 | + |
| 186 | + saveData: function (data) |
| 187 | + { |
| 188 | + FBInstant.player.setDataAsync(data).then(function() { |
| 189 | + console.log('data saved to fb'); |
| 190 | + this.emit('savedata', data); |
| 191 | + }); |
| 192 | + |
| 193 | + return this; |
| 194 | + }, |
| 195 | + |
| 196 | + /** |
| 197 | + * Destroys the FacebookInstantGamesPlugin. |
| 198 | + * |
| 199 | + * @method Phaser.Boot.FacebookInstantGamesPlugin#destroy |
| 200 | + * @since 3.12.0 |
| 201 | + */ |
| 202 | + destroy: function () |
| 203 | + { |
| 204 | + this.game = null; |
| 205 | + } |
| 206 | + |
| 207 | +}); |
| 208 | + |
| 209 | +module.exports = FacebookInstantGamesPlugin; |
0 commit comments