phaser/plugins/fbinstant/src/Leaderboard.js

314 lines
9.8 KiB
JavaScript
Raw Normal View History

2018-08-07 00:25:32 +00:00
/**
* @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 Class = require('../../../src/utils/Class');
2018-08-07 00:25:32 +00:00
var EventEmitter = require('eventemitter3');
var LeaderboardScore = require('./LeaderboardScore');
/**
* @classdesc
2018-09-20 10:49:39 +00:00
* This class represents one single Leaderboard that belongs to a Facebook Instant Game.
*
* You do not need to instantiate this class directly, it will be created when you use the
* `getLeaderboard()` method of the main plugin.
2018-08-07 00:25:32 +00:00
*
2019-05-09 10:21:52 +00:00
* @class FacebookInstantGamesLeaderboard
* @memberOf Phaser
2018-08-07 00:25:32 +00:00
* @constructor
2018-09-19 16:09:08 +00:00
* @since 3.13.0
2018-09-20 10:49:39 +00:00
*
* @param {Phaser.FacebookInstantGamesPlugin} plugin - A reference to the Facebook Instant Games Plugin.
* @param {any} data - An Instant Game leaderboard instance.
2018-08-07 00:25:32 +00:00
*/
var Leaderboard = new Class({
Extends: EventEmitter,
initialize:
function Leaderboard (plugin, data)
{
EventEmitter.call(this);
2018-09-19 16:09:08 +00:00
/**
* A reference to the Facebook Instant Games Plugin.
*
2019-05-09 10:21:52 +00:00
* @name Phaser.FacebookInstantGamesLeaderboard#plugin
2018-09-20 10:49:39 +00:00
* @type {Phaser.FacebookInstantGamesPlugin}
2018-09-19 16:09:08 +00:00
* @since 3.13.0
*/
2018-08-07 00:25:32 +00:00
this.plugin = plugin;
2018-09-19 16:09:08 +00:00
2018-09-20 10:49:39 +00:00
/**
* An Instant Game leaderboard instance.
*
2019-05-09 10:21:52 +00:00
* @name Phaser.FacebookInstantGamesLeaderboard#ref
2018-09-20 10:49:39 +00:00
* @type {any}
* @since 3.13.0
*/
2018-08-07 00:25:32 +00:00
this.ref = data;
2018-09-20 10:49:39 +00:00
/**
* The name of the leaderboard.
*
2019-05-09 10:21:52 +00:00
* @name Phaser.FacebookInstantGamesLeaderboard#name
2018-09-20 10:49:39 +00:00
* @type {string}
* @since 3.13.0
*/
2018-08-07 00:25:32 +00:00
this.name = data.getName();
2018-09-19 16:09:08 +00:00
2018-09-20 10:49:39 +00:00
/**
* The ID of the context that the leaderboard is associated with, or null if the leaderboard is not tied to a particular context.
*
2019-05-09 10:21:52 +00:00
* @name Phaser.FacebookInstantGamesLeaderboard#contextID
2018-09-20 10:49:39 +00:00
* @type {string}
* @since 3.13.0
*/
2018-08-07 00:25:32 +00:00
this.contextID = data.getContextID();
2018-09-19 16:09:08 +00:00
2018-09-20 10:49:39 +00:00
/**
* The total number of player entries in the leaderboard.
* This value defaults to zero. Populate it via the `getEntryCount()` method.
*
2019-05-09 10:21:52 +00:00
* @name Phaser.FacebookInstantGamesLeaderboard#entryCount
2018-09-20 10:49:39 +00:00
* @type {integer}
* @since 3.13.0
*/
2018-08-07 00:25:32 +00:00
this.entryCount = 0;
2018-09-20 10:49:39 +00:00
/**
* The players score object.
* This value defaults to `null`. Populate it via the `getPlayerScore()` method.
*
2019-05-09 10:21:52 +00:00
* @name Phaser.FacebookInstantGamesLeaderboard#playerScore
2018-09-20 10:49:39 +00:00
* @type {LeaderboardScore}
* @since 3.13.0
*/
2018-08-07 00:25:32 +00:00
this.playerScore = null;
2018-09-19 16:09:08 +00:00
2018-09-20 10:49:39 +00:00
/**
* The scores in the Leaderboard from the currently requested range.
* This value defaults to an empty array. Populate it via the `getScores()` method.
* The contents of this array are reset each time `getScores()` is called.
*
2019-05-09 10:21:52 +00:00
* @name Phaser.FacebookInstantGamesLeaderboard#scores
2018-09-20 10:49:39 +00:00
* @type {LeaderboardScore[]}
* @since 3.13.0
*/
2018-08-07 00:25:32 +00:00
this.scores = [];
2018-08-07 02:16:48 +00:00
this.getEntryCount();
2018-08-07 00:25:32 +00:00
},
2018-09-20 10:49:39 +00:00
/**
* Fetches the total number of player entries in the leaderboard.
*
* The data is requested in an async call, so the result isn't available immediately.
*
* When the call completes this Leaderboard will emit the `getentrycount` event along with the count and name of the Leaderboard.
*
2019-05-09 10:21:52 +00:00
* @method Phaser.FacebookInstantGamesLeaderboard#getEntryCount
2018-09-20 10:49:39 +00:00
* @since 3.13.0
*
* @return {this} This Leaderboard instance.
*/
2018-08-07 00:25:32 +00:00
getEntryCount: function ()
{
var _this = this;
2018-08-07 02:16:48 +00:00
this.ref.getEntryCountAsync().then(function (count)
{
2018-08-07 00:25:32 +00:00
_this.entryCount = count;
_this.emit('getentrycount', count, _this.name);
2018-08-07 02:16:48 +00:00
}).catch(function (e)
{
console.warn(e);
2018-08-07 00:25:32 +00:00
});
2018-09-20 10:49:39 +00:00
return this;
2018-08-07 00:25:32 +00:00
},
2018-09-20 10:49:39 +00:00
/**
* Updates the player's score. If the player has an existing score, the old score will only be replaced if the new score is better than it.
* NOTE: If the leaderboard is associated with a specific context, the game must be in that context to set a score for the player.
*
* The data is requested in an async call, so the result isn't available immediately.
*
2018-11-12 12:38:18 +00:00
* When the call completes this Leaderboard will emit the `setscore` event along with the LeaderboardScore object and the name of the Leaderboard.
*
* If the save fails the event will send `null` as the score value.
2018-09-20 10:49:39 +00:00
*
2019-05-09 10:21:52 +00:00
* @method Phaser.FacebookInstantGamesLeaderboard#setScore
2018-09-20 10:49:39 +00:00
* @since 3.13.0
*
* @param {integer} score - The new score for the player. Must be a 64-bit integer number.
2018-11-12 17:15:00 +00:00
* @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`.
2018-09-20 10:49:39 +00:00
*
* @return {this} This Leaderboard instance.
*/
2018-08-07 00:25:32 +00:00
setScore: function (score, data)
{
2018-08-07 02:16:48 +00:00
if (data === undefined) { data = ''; }
2018-08-07 00:25:32 +00:00
2018-11-12 17:15:00 +00:00
if (typeof data === 'object')
{
data = JSON.stringify(data);
}
2018-08-07 02:16:48 +00:00
var _this = this;
2018-08-07 00:25:32 +00:00
2018-08-07 02:16:48 +00:00
this.ref.setScoreAsync(score, data).then(function (entry)
{
2018-11-12 12:38:18 +00:00
if (entry)
{
var score = LeaderboardScore(entry);
_this.playerScore = score;
_this.emit('setscore', score, _this.name);
}
else
{
_this.emit('setscore', null, _this.name);
}
2018-08-07 00:25:32 +00:00
2018-08-07 02:16:48 +00:00
}).catch(function (e)
{
console.warn(e);
2018-08-07 00:25:32 +00:00
});
2018-09-20 10:49:39 +00:00
return this;
2018-08-07 00:25:32 +00:00
},
2018-09-20 10:49:39 +00:00
/**
* Gets the players leaderboard entry and stores it in the `playerScore` property.
*
* The data is requested in an async call, so the result isn't available immediately.
*
* When the call completes this Leaderboard will emit the `getplayerscore` event along with the score and the name of the Leaderboard.
2018-11-12 12:38:18 +00:00
*
* If the player has not yet saved a score, the event will send `null` as the score value, and `playerScore` will be set to `null` as well.
2018-09-20 10:49:39 +00:00
*
2019-05-09 10:21:52 +00:00
* @method Phaser.FacebookInstantGamesLeaderboard#getPlayerScore
2018-09-20 10:49:39 +00:00
* @since 3.13.0
*
* @return {this} This Leaderboard instance.
*/
2018-08-07 00:25:32 +00:00
getPlayerScore: function ()
{
var _this = this;
2018-08-07 02:16:48 +00:00
this.ref.getPlayerEntryAsync().then(function (entry)
{
2018-11-12 12:38:18 +00:00
if (entry)
{
var score = LeaderboardScore(entry);
_this.playerScore = score;
_this.emit('getplayerscore', score, _this.name);
}
else
{
_this.emit('getplayerscore', null, _this.name);
}
2018-08-07 00:25:32 +00:00
2018-08-07 02:16:48 +00:00
}).catch(function (e)
{
console.warn(e);
2018-08-07 00:25:32 +00:00
});
2018-09-20 10:49:39 +00:00
return this;
2018-08-07 00:25:32 +00:00
},
2018-09-20 10:49:39 +00:00
/**
* Retrieves a set of leaderboard entries, ordered by score ranking in the leaderboard.
*
* The data is requested in an async call, so the result isn't available immediately.
*
2018-11-12 17:15:00 +00:00
* When the call completes this Leaderboard will emit the `getscores` event along with an array of LeaderboardScore entries and the name of the Leaderboard.
2018-09-20 10:49:39 +00:00
*
2019-05-09 10:21:52 +00:00
* @method Phaser.FacebookInstantGamesLeaderboard#getScores
2018-09-20 10:49:39 +00:00
* @since 3.13.0
*
* @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.
* @param {integer} [offset=0] - The offset from the top of the leaderboard that entries will be fetched from.
*
* @return {this} This Leaderboard instance.
*/
2018-08-07 00:25:32 +00:00
getScores: function (count, offset)
{
if (count === undefined) { count = 10; }
if (offset === undefined) { offset = 0; }
var _this = this;
2019-01-02 03:13:52 +00:00
this.ref.getEntriesAsync(count, offset).then(function (entries)
2018-08-07 02:16:48 +00:00
{
2018-08-07 00:25:32 +00:00
_this.scores = [];
2018-08-07 02:16:48 +00:00
entries.forEach(function (entry)
{
2018-08-07 00:25:32 +00:00
_this.scores.push(LeaderboardScore(entry));
});
_this.emit('getscores', _this.scores, _this.name);
2018-08-07 02:16:48 +00:00
}).catch(function (e)
{
console.warn(e);
2018-08-07 00:25:32 +00:00
});
2018-11-12 17:15:00 +00:00
return this;
},
/**
* 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.
*
* The data is requested in an async call, so the result isn't available immediately.
*
* When the call completes this Leaderboard will emit the `getconnectedscores` event along with an array of LeaderboardScore entries and the name of the Leaderboard.
*
2019-05-09 10:21:52 +00:00
* @method Phaser.FacebookInstantGamesLeaderboard#getConnectedScores
2018-11-12 17:15:00 +00:00
* @since 3.16.0
*
* @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.
* @param {integer} [offset=0] - The offset from the top of the leaderboard that entries will be fetched from.
*
* @return {this} This Leaderboard instance.
*/
getConnectedScores: function (count, offset)
{
if (count === undefined) { count = 10; }
if (offset === undefined) { offset = 0; }
var _this = this;
this.ref.getConnectedPlayerEntriesAsync().then(function (entries)
{
_this.scores = [];
entries.forEach(function (entry)
{
_this.scores.push(LeaderboardScore(entry));
});
_this.emit('getconnectedscores', _this.scores, _this.name);
}).catch(function (e)
{
console.warn(e);
});
2018-09-20 10:49:39 +00:00
return this;
2018-08-07 00:25:32 +00:00
}
});
module.exports = Leaderboard;