phaser/plugins/fbinstant/src/Leaderboard.js

145 lines
3 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
* [description]
*
2018-09-19 16:09:08 +00:00
* @class Leaderboard
* @memberOf Phaser.Boot.FacebookInstantGamesPlugin
2018-08-07 00:25:32 +00:00
* @constructor
2018-09-19 16:09:08 +00:00
* @since 3.13.0
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.
*
* @name Phaser.Boot.FacebookInstantGamesPlugin#plugin
* @type {Phaser.Boot.FacebookInstantGamesPlugin}
* @since 3.13.0
*/
2018-08-07 00:25:32 +00:00
this.plugin = plugin;
2018-09-19 16:09:08 +00:00
2018-08-07 00:25:32 +00:00
this.ref = data;
this.name = data.getName();
2018-09-19 16:09:08 +00:00
2018-08-07 00:25:32 +00:00
this.contextID = data.getContextID();
2018-09-19 16:09:08 +00:00
2018-08-07 00:25:32 +00:00
this.entryCount = 0;
this.playerScore = null;
2018-09-19 16:09:08 +00:00
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
},
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;