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}
|
|
|
|
*/
|
|
|
|
|
2018-09-04 11:35:23 +00:00
|
|
|
var Class = require('../../../src/utils/Class');
|
2018-08-07 00:25:32 +00:00
|
|
|
var EventEmitter = require('eventemitter3');
|
|
|
|
var LeaderboardScore = require('./LeaderboardScore');
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @classdesc
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @class FacebookInstantGamesPlugin
|
|
|
|
* @memberOf Phaser
|
|
|
|
* @constructor
|
|
|
|
* @since 3.12.0
|
|
|
|
*/
|
|
|
|
var Leaderboard = new Class({
|
|
|
|
|
|
|
|
Extends: EventEmitter,
|
|
|
|
|
|
|
|
initialize:
|
|
|
|
|
|
|
|
function Leaderboard (plugin, data)
|
|
|
|
{
|
|
|
|
EventEmitter.call(this);
|
|
|
|
|
|
|
|
this.plugin = plugin;
|
|
|
|
this.ref = data;
|
|
|
|
|
|
|
|
this.name = data.getName();
|
|
|
|
this.contextID = data.getContextID();
|
|
|
|
this.entryCount = 0;
|
|
|
|
|
|
|
|
this.playerScore = null;
|
|
|
|
this.scores = [];
|
2018-08-07 02:16:48 +00:00
|
|
|
|
|
|
|
this.getEntryCount();
|
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
|
|
|
console.log('entry count', count);
|
|
|
|
|
|
|
|
_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
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
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-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-08-07 00:25:32 +00:00
|
|
|
console.log('set score', entry);
|
|
|
|
|
|
|
|
_this.emit('setscore', entry.getScore(), entry.getExtraData(), _this.name);
|
|
|
|
|
2018-08-07 02:16:48 +00:00
|
|
|
}).catch(function (e)
|
|
|
|
{
|
|
|
|
console.warn(e);
|
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)
|
|
|
|
{
|
|
|
|
console.log('get player score');
|
2018-08-07 00:25:32 +00:00
|
|
|
|
|
|
|
var score = LeaderboardScore(entry);
|
|
|
|
|
2018-08-07 02:16:48 +00:00
|
|
|
console.log(score);
|
|
|
|
|
2018-08-07 00:25:32 +00:00
|
|
|
_this.playerScore = score;
|
|
|
|
|
|
|
|
_this.emit('getplayerscore', score, _this.name);
|
|
|
|
|
2018-08-07 02:16:48 +00:00
|
|
|
}).catch(function (e)
|
|
|
|
{
|
|
|
|
console.warn(e);
|
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;
|
|
|
|
|
2018-08-07 02:16:48 +00:00
|
|
|
this.ref.getEntriesAsync().then(function (entries)
|
|
|
|
{
|
2018-08-07 00:25:32 +00:00
|
|
|
console.log('get scores', entries);
|
|
|
|
|
|
|
|
_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
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
module.exports = Leaderboard;
|