@@ -36,10 +36,18 @@ AV.LeaderboardUpdateStrategy = {
3636 LAST : 'last' ,
3737} ;
3838
39+ /**
40+ * @typedef {Object } Ranking
41+ * @property {number } rank Starts at 0
42+ * @property {number } value the statistic value of this ranking
43+ * @property {AV.User } user The user of this ranking
44+ * @property {Statistic[] } [includedStatistics] Other statistics of the user, specified by the `includeStatistic` option of `AV.Leaderboard.getResults()`
45+ */
46+
3947/**
4048 * @class
4149 */
42- function Statistic ( { user , name, value, position , version } ) {
50+ function Statistic ( { name, value, version } ) {
4351 /**
4452 * @type {string }
4553 */
@@ -48,21 +56,19 @@ function Statistic({ user, name, value, position, version }) {
4856 * @type {number }
4957 */
5058 this . value = value ;
51- /**
52- * @type {AV.User }
53- */
54- this . user = user ;
55- /**
56- * The position of the leandboard. Only occurs in leaderboard results.
57- * @type {number? }
58- */
59- this . position = position ;
6059 /**
6160 * @type {number? }
6261 */
6362 this . version = version ;
6463}
6564
65+ const parseStatisticData = statisticData => {
66+ const { statisticName : name , statisticValue : value , version } = AV . _decode (
67+ statisticData
68+ ) ;
69+ return new Statistic ( { name, value, version } ) ;
70+ } ;
71+
6672/**
6773 * @class
6874 */
@@ -156,19 +162,12 @@ AV.Leaderboard.getStatistics = (user, { statisticNames } = {}, authOptions) =>
156162 method : 'GET' ,
157163 path : `/leaderboard/users/${ user . id } /statistics` ,
158164 query : {
159- statistics : statisticNames ? ensureArray ( statisticNames ) : undefined ,
165+ statistics : statisticNames
166+ ? ensureArray ( statisticNames ) . join ( ',' )
167+ : undefined ,
160168 } ,
161169 authOptions,
162- } ) . then ( ( { results } ) =>
163- results . map ( statisticData => {
164- const {
165- statisticName : name ,
166- statisticValue : value ,
167- version,
168- } = AV . _decode ( statisticData ) ;
169- return new Statistic ( { user, name, value, version } ) ;
170- } )
171- ) ;
170+ } ) . then ( ( { results } ) => results . map ( parseStatisticData ) ) ;
172171 } ) ;
173172/**
174173 * Update Statistics for the specified user.
@@ -189,16 +188,7 @@ AV.Leaderboard.updateStatistics = (user, statistics, authOptions) =>
189188 path : `/leaderboard/users/${ user . id } /statistics` ,
190189 data,
191190 authOptions,
192- } ) . then ( ( { results } ) =>
193- results . map ( statisticData => {
194- const {
195- statisticName : name ,
196- statisticValue : value ,
197- version,
198- } = AV . _decode ( statisticData ) ;
199- return new Statistic ( { user, name, value, version } ) ;
200- } )
201- ) ;
191+ } ) . then ( ( { results } ) => results . map ( parseStatisticData ) ) ;
202192 } ) ;
203193
204194_ . extend (
@@ -213,7 +203,7 @@ _.extend(
213203 if ( key === 'createdAt' ) {
214204 value = parseDate ( value ) ;
215205 }
216- if ( value . __type === 'Date' ) {
206+ if ( value && value . __type === 'Date' ) {
217207 value = parseDate ( value . iso ) ;
218208 }
219209 this [ key ] = value ;
@@ -232,29 +222,41 @@ _.extend(
232222 authOptions,
233223 } ) . then ( data => this . _finishFetch ( data ) ) ;
234224 } ,
235- _getResults ( { skip, limit, includeUserKeys } , authOptions , self ) {
225+ _getResults (
226+ { skip, limit, selectUserKeys, includeStatistics } ,
227+ authOptions ,
228+ self
229+ ) {
236230 return request ( {
237231 method : 'GET' ,
238- path : `/leaderboard/leaderboards/${ this . statisticName } /positions ${
232+ path : `/leaderboard/leaderboards/${ this . statisticName } /ranks ${
239233 self ? '/self' : ''
240234 } `,
241235 query : {
242236 skip,
243237 limit,
244- includeUser : includeUserKeys
245- ? ensureArray ( includeUserKeys ) . join ( ',' )
238+ includeUser : selectUserKeys
239+ ? ensureArray ( selectUserKeys ) . join ( ',' )
240+ : undefined ,
241+ includeStatistics : includeStatistics
242+ ? ensureArray ( includeStatistics ) . join ( ',' )
246243 : undefined ,
247244 } ,
248245 authOptions,
249- } ) . then ( ( { results } ) =>
250- results . map ( statisticData => {
246+ } ) . then ( ( { results : rankings } ) =>
247+ rankings . map ( rankingData => {
251248 const {
252249 user,
253- statisticName : name ,
254250 statisticValue : value ,
255- position,
256- } = AV . _decode ( statisticData ) ;
257- return new Statistic ( { user, name, value, position } ) ;
251+ rank,
252+ statistics = [ ] ,
253+ } = AV . _decode ( rankingData ) ;
254+ return {
255+ user,
256+ value,
257+ rank,
258+ includedStatistics : statistics . map ( parseStatisticData ) ,
259+ } ;
258260 } )
259261 ) ;
260262 } ,
@@ -263,23 +265,38 @@ _.extend(
263265 * @param {Object } [options]
264266 * @param {number } [options.skip] The number of results to skip. This is useful for pagination.
265267 * @param {number } [options.limit] The limit of the number of results.
266- * @param {string[] } [options.includeUserKeys] Specify keys of the users to include
268+ * @param {string[] } [options.selectUserKeys] Specify keys of the users to include
269+ * @param {string[] } [options.includeStatistics] Specify other statistics to include in the Rankings
267270 * @param {AuthOptions } [authOptions]
268- * @return {Promise<Statistic []> }
271+ * @return {Promise<Ranking []> }
269272 */
270- getResults ( { skip, limit, includeUserKeys } = { } , authOptions ) {
271- return this . _getResults ( { skip, limit, includeUserKeys } , authOptions ) ;
273+ getResults (
274+ { skip, limit, selectUserKeys, includeStatistics } = { } ,
275+ authOptions
276+ ) {
277+ return this . _getResults (
278+ { skip, limit, selectUserKeys, includeStatistics } ,
279+ authOptions
280+ ) ;
272281 } ,
273282 /**
274283 * Retrieve a list of ranked users for this Leaderboard, centered on the specified user.
275284 * @param {Object } [options]
276285 * @param {number } [options.limit] The limit of the number of results.
277- * @param {string[] } [options.includeUserKeys] Specify keys of the users to include
286+ * @param {string[] } [options.selectUserKeys] Specify keys of the users to include
287+ * @param {string[] } [options.includeStatistics] Specify other statistics to include in the Rankings
278288 * @param {AuthOptions } [authOptions]
279- * @return {Promise<Statistic []> }
289+ * @return {Promise<Ranking []> }
280290 */
281- getResultsAroundUser ( { limit, includeUserKeys } = { } , authOptions ) {
282- return this . _getResults ( { limit, includeUserKeys } , authOptions , true ) ;
291+ getResultsAroundUser (
292+ { limit, selectUserKeys, includeStatistics } = { } ,
293+ authOptions
294+ ) {
295+ return this . _getResults (
296+ { limit, selectUserKeys, includeStatistics } ,
297+ authOptions ,
298+ true
299+ ) ;
283300 } ,
284301 _update ( data , authOptions ) {
285302 return request ( {
0 commit comments