Skip to content

Commit a499345

Browse files
committed
Leaderboard objects added
1 parent d415e2f commit a499345

3 files changed

Lines changed: 184 additions & 27 deletions

File tree

src/fbinstant/FacebookInstantGamesPlugin.js

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ var Class = require('../utils/Class');
88
var DataManager = require('../data/DataManager');
99
var EventEmitter = require('eventemitter3');
1010
var GetValue = require('../utils/object/GetValue');
11-
var LeaderboardScore = require('./LeaderboardScore');
11+
var Leaderboard = require('./Leaderboard');
1212
var Product = require('./Product');
1313
var Purchase = require('./Purchase');
1414

@@ -55,7 +55,6 @@ var FacebookInstantGamesPlugin = new Class({
5555

5656
this.supportedAPIs = [];
5757

58-
console.log(this.supportedAPIs);
5958
this.entryPoint = '';
6059
this.entryPointData = null;
6160
this.contextID = 0;
@@ -76,6 +75,8 @@ var FacebookInstantGamesPlugin = new Class({
7675
this.paymentsReady = false;
7776
this.catalog = [];
7877
this.purchases = [];
78+
79+
this.leaderboards = {};
7980
},
8081

8182
setDataHandler: function (parent, key, value)
@@ -889,6 +890,60 @@ var FacebookInstantGamesPlugin = new Class({
889890
return this;
890891
},
891892

893+
matchPlayer: function (matchTag, switchImmediately)
894+
{
895+
if (matchTag === undefined) { matchTag = null; }
896+
if (switchImmediately === undefined) { switchImmediately = false; }
897+
898+
if (!this.checkAPI('matchPlayerAsync'))
899+
{
900+
return this;
901+
}
902+
903+
var _this = this;
904+
905+
FBInstant.matchPlayerAsync(matchTag, switchImmediately).then(function () {
906+
907+
console.log('match player');
908+
909+
_this.getID();
910+
_this.getType();
911+
912+
_this.emit('matchplayer', _this.contextID, _this.contextType);
913+
914+
});
915+
916+
return this;
917+
},
918+
919+
// TODO: checkCanPlayerMatchAsync ?
920+
921+
getLeaderboard: function (name)
922+
{
923+
if (!this.checkAPI('getLeaderboardAsync'))
924+
{
925+
return this;
926+
}
927+
928+
var _this = this;
929+
930+
FBInstant.getLeaderboardAsync(name).then(function (data) {
931+
932+
console.log('leaderboard');
933+
console.log(data);
934+
935+
var leaderboard = new Leaderboard(_this, data);
936+
937+
_this.leaderboards[name] = leaderboard;
938+
939+
_this.emit('leaderboard', leaderboard);
940+
941+
});
942+
943+
return this;
944+
},
945+
946+
892947
/**
893948
* Destroys the FacebookInstantGamesPlugin.
894949
*

src/fbinstant/Leaderboard.js

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
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 EventEmitter = require('eventemitter3');
9+
var LeaderboardScore = require('./LeaderboardScore');
10+
11+
/**
12+
* @classdesc
13+
* [description]
14+
*
15+
* @class FacebookInstantGamesPlugin
16+
* @memberOf Phaser
17+
* @constructor
18+
* @since 3.12.0
19+
*/
20+
var Leaderboard = new Class({
21+
22+
Extends: EventEmitter,
23+
24+
initialize:
25+
26+
function Leaderboard (plugin, data)
27+
{
28+
EventEmitter.call(this);
29+
30+
this.plugin = plugin;
31+
this.ref = data;
32+
33+
this.name = data.getName();
34+
this.contextID = data.getContextID();
35+
this.entryCount = 0;
36+
37+
this.playerScore = null;
38+
this.scores = [];
39+
},
40+
41+
getEntryCount: function ()
42+
{
43+
var _this = this;
44+
45+
this.ref.getEntryCountAsync().then(function (count) {
46+
47+
console.log('entry count', count);
48+
49+
_this.entryCount = count;
50+
51+
_this.emit('getentrycount', count, _this.name);
52+
53+
});
54+
},
55+
56+
setScore: function (score, data)
57+
{
58+
var _this = this;
59+
60+
this.ref.setScoreAsync(score, data).then(function (entry) {
61+
62+
console.log('set score', entry);
63+
64+
_this.emit('setscore', entry.getScore(), entry.getExtraData(), _this.name);
65+
66+
});
67+
},
68+
69+
getPlayerScore: function ()
70+
{
71+
var _this = this;
72+
73+
this.ref.getPlayerEntryAsync().then(function (entry) {
74+
75+
console.log('get player score', entry);
76+
77+
var score = LeaderboardScore(entry);
78+
79+
_this.playerScore = score;
80+
81+
_this.emit('getplayerscore', score, _this.name);
82+
83+
});
84+
85+
},
86+
87+
getScores: function (count, offset)
88+
{
89+
if (count === undefined) { count = 10; }
90+
if (offset === undefined) { offset = 0; }
91+
92+
var _this = this;
93+
94+
this.ref.getEntriesAsync().then(function (entries) {
95+
96+
console.log('get scores', entries);
97+
98+
_this.scores = [];
99+
100+
entries.forEach(function (entry) {
101+
102+
_this.scores.push(LeaderboardScore(entry));
103+
104+
});
105+
106+
_this.emit('getscores', _this.scores, _this.name);
107+
108+
});
109+
110+
}
111+
112+
});
113+
114+
module.exports = Leaderboard;

src/fbinstant/LeaderboardScore.js

Lines changed: 13 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,18 @@
44
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
55
*/
66

7-
var Class = require('../utils/Class');
8-
9-
/**
10-
* @classdesc
11-
* [description]
12-
*
13-
* @class FacebookInstantGamesPlugin
14-
* @memberOf Phaser
15-
* @constructor
16-
* @since 3.12.0
17-
*/
18-
var LeaderboardScore = new Class({
19-
20-
initialize:
21-
22-
function LeaderboardScore ()
23-
{
24-
this.value;
25-
this.valueFormatted;
26-
this.timestamp;
27-
this.rank;
28-
this.data;
29-
}
30-
31-
});
7+
var LeaderboardScore = function (entry)
8+
{
9+
return {
10+
score: entry.getScore(),
11+
scoreFormatted: entry.getFormattedScore(),
12+
timestamp: entry.getTimestamp(),
13+
rank: entry.getRank(),
14+
data: entry.getExtraData(),
15+
playerName: entry.getPlayer().getName(),
16+
playerPhotoURL: entry.getPlayer().getPhoto(),
17+
playerID: entry.getPlayer().getID()
18+
};
19+
};
3220

3321
module.exports = LeaderboardScore;

0 commit comments

Comments
 (0)