Skip to content

Commit 23cc8b8

Browse files
committed
Added getConnectedScores method
1 parent 1a02f51 commit 23cc8b8

2 files changed

Lines changed: 50 additions & 2 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44

55
### Facebook Instant Games Updates and Fixes
66

7+
* Added the `Leaderboard.getConnectedScores` method, to get a list of scores from player connected entries.
78
* The `loadPlayerPhoto` function in the Instant Games plugin now listens for the updated Loader event correctly, causing the `photocomplete` event to fire properly.
89
* `Leaderboard.setScore` now emits the LeaderboardScore object with the `setscore` event, as the documentation said it did.
910
* `Leaderboard.getPlayerScore` now only populates the `playerScore` property if the entry isn't `null`.
1011
* If the `setScore` or `getPlayerScore` calls fail, it will return `null` as the score instance, instead of causing a run-time error.
12+
* You can now pass an object or a string to `setScore` and objects will be automatically stringified.
1113

1214
### New Features
1315

plugins/fbinstant/src/Leaderboard.js

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,14 +147,19 @@ var Leaderboard = new Class({
147147
* @since 3.13.0
148148
*
149149
* @param {integer} score - The new score for the player. Must be a 64-bit integer number.
150-
* @param {string} [data] - Metadata to associate with the stored score. Must be less than 2KB in size.
150+
* @param {(string|any)} [data] - Metadata to associate with the stored score. Must be less than 2KB in size. If an object is given it will be passed to `JSON.stringify`.
151151
*
152152
* @return {this} This Leaderboard instance.
153153
*/
154154
setScore: function (score, data)
155155
{
156156
if (data === undefined) { data = ''; }
157157

158+
if (typeof data === 'object')
159+
{
160+
data = JSON.stringify(data);
161+
}
162+
158163
var _this = this;
159164

160165
this.ref.setScoreAsync(score, data).then(function (entry)
@@ -226,7 +231,7 @@ var Leaderboard = new Class({
226231
*
227232
* The data is requested in an async call, so the result isn't available immediately.
228233
*
229-
* When the call completes this Leaderboard will emit the `getplayerscore` event along with the score and the name of the Leaderboard.
234+
* When the call completes this Leaderboard will emit the `getscores` event along with an array of LeaderboardScore entries and the name of the Leaderboard.
230235
*
231236
* @method Phaser.FacebookInstantGamesPlugin.Leaderboard#getScores
232237
* @since 3.13.0
@@ -259,6 +264,47 @@ var Leaderboard = new Class({
259264
console.warn(e);
260265
});
261266

267+
return this;
268+
},
269+
270+
/**
271+
* Retrieves a set of leaderboard entries, based on the current player's connected players (including the current player), ordered by local rank within the set of connected players.
272+
*
273+
* The data is requested in an async call, so the result isn't available immediately.
274+
*
275+
* When the call completes this Leaderboard will emit the `getconnectedscores` event along with an array of LeaderboardScore entries and the name of the Leaderboard.
276+
*
277+
* @method Phaser.FacebookInstantGamesPlugin.Leaderboard#getConnectedScores
278+
* @since 3.16.0
279+
*
280+
* @param {integer} [count=10] - The number of entries to attempt to fetch from the leaderboard. Currently, up to a maximum of 100 entries may be fetched per query.
281+
* @param {integer} [offset=0] - The offset from the top of the leaderboard that entries will be fetched from.
282+
*
283+
* @return {this} This Leaderboard instance.
284+
*/
285+
getConnectedScores: function (count, offset)
286+
{
287+
if (count === undefined) { count = 10; }
288+
if (offset === undefined) { offset = 0; }
289+
290+
var _this = this;
291+
292+
this.ref.getConnectedPlayerEntriesAsync().then(function (entries)
293+
{
294+
_this.scores = [];
295+
296+
entries.forEach(function (entry)
297+
{
298+
_this.scores.push(LeaderboardScore(entry));
299+
});
300+
301+
_this.emit('getconnectedscores', _this.scores, _this.name);
302+
303+
}).catch(function (e)
304+
{
305+
console.warn(e);
306+
});
307+
262308
return this;
263309
}
264310

0 commit comments

Comments
 (0)