Skip to content

Commit 8513dba

Browse files
committed
feat(Leaderboard): add updateStrategy
1 parent 1903f06 commit 8513dba

File tree

3 files changed

+73
-13
lines changed

3 files changed

+73
-13
lines changed

src/leaderboard.js

Lines changed: 42 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ AV.LeaderboardOrder = {
2525
DESCENDING: 'descending',
2626
};
2727

28+
AV.LeaderboardUpdateStrategy = {
29+
BETTER: 'better',
30+
LAST: 'last',
31+
};
32+
2833
/**
2934
* @class
3035
*/
@@ -60,6 +65,14 @@ AV.Leaderboard = function(statisticName) {
6065
* @type {string}
6166
*/
6267
this.statisticName = statisticName;
68+
/**
69+
* @type {AV.LeaderboardOrder}
70+
*/
71+
this.order = undefined;
72+
/**
73+
* @type {AV.LeaderboardUpdateStrategy}
74+
*/
75+
this.updateStrategy = undefined;
6376
/**
6477
* @type {AV.LeaderboardVersionChangeInterval}
6578
*/
@@ -72,6 +85,10 @@ AV.Leaderboard = function(statisticName) {
7285
* @type {Date?}
7386
*/
7487
this.nextResetAt = undefined;
88+
/**
89+
* @type {Date?}
90+
*/
91+
this.createdAt = undefined;
7592
};
7693
const Leaderboard = AV.Leaderboard;
7794

@@ -87,12 +104,13 @@ AV.Leaderboard.createWithoutData = statisticName =>
87104
* @param {Object} options
88105
* @param {string} options.statisticName
89106
* @param {AV.LeaderboardOrder} options.order
90-
* @param {AV.LeaderboardVersionChangeInterval} options.versionChangeInterval
107+
* @param {AV.LeaderboardVersionChangeInterval} [options.versionChangeInterval] default to WEEK
108+
* @param {AV.LeaderboardUpdateStrategy} [options.updateStrategy] default to BETTER
91109
* @param {AuthOptions} [authOptions]
92110
* @return {Promise<AV.Leaderboard>}
93111
*/
94112
AV.Leaderboard.createLeaderboard = (
95-
{ statisticName, order, versionChangeInterval },
113+
{ statisticName, order, versionChangeInterval, updateStrategy },
96114
authOptions
97115
) =>
98116
request({
@@ -102,6 +120,7 @@ AV.Leaderboard.createLeaderboard = (
102120
statisticName,
103121
order,
104122
versionChangeInterval,
123+
updateStrategy,
105124
},
106125
authOptions,
107126
}).then(data => {
@@ -185,6 +204,9 @@ _.extend(
185204
if (key === 'expiredAt') {
186205
key = 'nextResetAt';
187206
}
207+
if (key === 'createdAt') {
208+
value = parseDate(value);
209+
}
188210
if (value.__type === 'Date') {
189211
value = parseDate(value.iso);
190212
}
@@ -253,21 +275,31 @@ _.extend(
253275
getResultsAroundUser({ limit, includeUserKeys } = {}, authOptions) {
254276
return this._getResults({ limit, includeUserKeys }, authOptions, true);
255277
},
278+
_update(data, authOptions) {
279+
return request({
280+
method: 'PUT',
281+
path: `/play/leaderboards/${this.statisticName}`,
282+
data,
283+
authOptions,
284+
}).then(result => this._finishFetch(result));
285+
},
256286
/**
257287
* (masterKey required) Update the version change interval of the Leaderboard.
258288
* @param {AV.LeaderboardVersionChangeInterval} versionChangeInterval
259289
* @param {AuthOptions} [authOptions]
260290
* @return {Promise<AV.Leaderboard>}
261291
*/
262292
updateVersionChangeInterval(versionChangeInterval, authOptions) {
263-
return request({
264-
method: 'PUT',
265-
path: `/play/leaderboards/${this.statisticName}`,
266-
data: {
267-
versionChangeInterval,
268-
},
269-
authOptions,
270-
}).then(data => this._finishFetch(data));
293+
return this._update({ versionChangeInterval }, authOptions);
294+
},
295+
/**
296+
* (masterKey required) Update the version change interval of the Leaderboard.
297+
* @param {AV.LeaderboardUpdateStrategy} updateStrategy
298+
* @param {AuthOptions} [authOptions]
299+
* @return {Promise<AV.Leaderboard>}
300+
*/
301+
updateUpdateStrategy(updateStrategy, authOptions) {
302+
return this._update({ updateStrategy }, authOptions);
271303
},
272304
/**
273305
* (masterKey required) Reset the Leaderboard. The version of the Leaderboard will be incremented by 1.

storage.d.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -798,16 +798,20 @@ declare class Statistic {
798798

799799
export class Leaderboard {
800800
statisticName: string;
801-
versionChangeInterval: LeaderboardVersionChangeInterval;
802-
version: number;
801+
order?: LeaderboardOrder;
802+
updateStrategy?: LeaderboardUpdateStrategy;
803+
versionChangeInterval?: LeaderboardVersionChangeInterval;
804+
version?: number;
803805
nextResetAt?: Date;
806+
createdAt?: Date;
804807

805808
static createWithoutData(statisticName: string): Leaderboard;
806809
static createLeaderboard(
807810
options: {
808811
statisticName: string;
809812
order: LeaderboardOrder;
810-
versionChangeInterval: LeaderboardVersionChangeInterval;
813+
versionChangeInterval?: LeaderboardVersionChangeInterval;
814+
updateStrategy?: LeaderboardUpdateStrategy;
811815
},
812816
authOptions?: AuthOptions
813817
): Promise<Leaderboard>;
@@ -838,6 +842,10 @@ export class Leaderboard {
838842
versionChangeInterval: LeaderboardVersionChangeInterval,
839843
authOptions?: AuthOptions
840844
): Promise<Leaderboard>;
845+
updateUpdateStrategy(
846+
updateStrategy: LeaderboardUpdateStrategy,
847+
authOptions?: AuthOptions
848+
): Promise<Leaderboard>;
841849
reset(authOptions?: AuthOptions): Promise<Leaderboard>;
842850
}
843851

@@ -846,6 +854,11 @@ export enum LeaderboardOrder {
846854
DESCENDING,
847855
}
848856

857+
export enum LeaderboardUpdateStrategy {
858+
BETTER,
859+
LAST,
860+
}
861+
849862
export enum LeaderboardVersionChangeInterval {
850863
NEVER,
851864
HOUR,

test/leaderboard.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ describe('Leaderboard', () => {
55
return AV.Leaderboard.createLeaderboard(
66
{
77
statisticName,
8+
updateStrategy: AV.LeaderboardUpdateStrategy.BETTER,
89
order: AV.LeaderboardOrder.ASCENDING,
910
versionChangeInterval: AV.LeaderboardVersionChangeInterval.WEEK,
1011
},
@@ -19,6 +20,9 @@ describe('Leaderboard', () => {
1920
function validateLeaderboard(leaderboard) {
2021
leaderboard.should.be.instanceof(AV.Leaderboard);
2122
leaderboard.statisticName.should.be.eql(statisticName);
23+
leaderboard.updateStrategy.should.be.eql(
24+
AV.LeaderboardUpdateStrategy.BETTER
25+
);
2226
leaderboard.versionChangeInterval.should.be.eql(
2327
AV.LeaderboardVersionChangeInterval.WEEK
2428
);
@@ -50,6 +54,17 @@ describe('Leaderboard', () => {
5054
);
5155
});
5256
});
57+
it('mutation with masterKey', function() {
58+
return this.leaderboard
59+
.updateUpdateStrategy(AV.LeaderboardUpdateStrategy.LAST, {
60+
useMasterKey: true,
61+
})
62+
.then(leaderboard => {
63+
leaderboard.updateStrategy.should.be.eql(
64+
AV.LeaderboardUpdateStrategy.LAST
65+
);
66+
});
67+
});
5368

5469
describe('Statistics', function() {
5570
let users;

0 commit comments

Comments
 (0)