|
| 1 | +const _ = require('underscore'); |
| 2 | +const Promise = require('./promise'); |
| 3 | +const { request } = require('./request'); |
| 4 | +const { ensureArray, parseDate } = require('./utils'); |
| 5 | +const AV = require('./av'); |
| 6 | + |
| 7 | +const LeaderboardVersionChangeInterval = { |
| 8 | + NEVER: 'never', |
| 9 | + HOUR: 'hour', |
| 10 | + DAY: 'day', |
| 11 | + WEEK: 'week', |
| 12 | + MONTH: 'month', |
| 13 | +}; |
| 14 | + |
| 15 | +const LeaderboardOrder = { |
| 16 | + ASCENDING: 'ascending', |
| 17 | + DESCENDING: 'descending', |
| 18 | +}; |
| 19 | + |
| 20 | +function Statistic({ user, name, value, position, version }) { |
| 21 | + this.name = name; |
| 22 | + this.value = value; |
| 23 | + this.user = user; |
| 24 | + this.position = position; |
| 25 | + this.version = version; |
| 26 | +} |
| 27 | + |
| 28 | +function Leaderboard(statisticName) { |
| 29 | + this.statisticName = statisticName; |
| 30 | + this.versionChangeInterval = undefined; |
| 31 | + this.version = undefined; |
| 32 | + this.nextResetAt = undefined; |
| 33 | +} |
| 34 | + |
| 35 | +Leaderboard.createWithoutData = statisticName => new Leaderboard(statisticName); |
| 36 | +Leaderboard.createLeaderboard = ( |
| 37 | + { statisticName, order, versionChangeInterval }, |
| 38 | + authOptions |
| 39 | +) => |
| 40 | + request({ |
| 41 | + method: 'POST', |
| 42 | + path: '/play/leaderboards', |
| 43 | + data: { |
| 44 | + statisticName, |
| 45 | + order, |
| 46 | + versionChangeInterval, |
| 47 | + }, |
| 48 | + authOptions, |
| 49 | + }).then(data => { |
| 50 | + const leaderboard = new Leaderboard(statisticName); |
| 51 | + return leaderboard._finishFetch(data); |
| 52 | + }); |
| 53 | +Leaderboard.getLeaderboard = (statisticName, authOptions) => |
| 54 | + Leaderboard.createWithoutData(statisticName).fetch(authOptions); |
| 55 | +Leaderboard.getStatistics = (user, { statisticNames } = {}, authOptions) => |
| 56 | + Promise.resolve().then(() => { |
| 57 | + if (!(user && user.id)) throw new Error('user must be an AV.User'); |
| 58 | + return request({ |
| 59 | + method: 'GET', |
| 60 | + path: `/play/users/${user.id}/statistics`, |
| 61 | + query: { |
| 62 | + statistics: statisticNames ? ensureArray(statisticNames) : undefined, |
| 63 | + }, |
| 64 | + authOptions, |
| 65 | + }).then(({ results }) => |
| 66 | + results.map(statisticData => { |
| 67 | + const { |
| 68 | + statisticName: name, |
| 69 | + statisticValue: value, |
| 70 | + version, |
| 71 | + } = AV._decode(statisticData); |
| 72 | + return new Statistic({ user, name, value, version }); |
| 73 | + }) |
| 74 | + ); |
| 75 | + }); |
| 76 | +Leaderboard.updateStatistics = (user, statistics, authOptions) => |
| 77 | + Promise.resolve().then(() => { |
| 78 | + if (!(user && user.id)) throw new Error('user must be an AV.User'); |
| 79 | + const data = _.map(statistics, (value, key) => ({ |
| 80 | + statisticName: key, |
| 81 | + statisticValue: value, |
| 82 | + })); |
| 83 | + return request({ |
| 84 | + method: 'POST', |
| 85 | + path: `/play/users/${user.id}/statistics`, |
| 86 | + data, |
| 87 | + authOptions, |
| 88 | + }).then(({ results }) => |
| 89 | + results.map(statisticData => { |
| 90 | + const { |
| 91 | + statisticName: name, |
| 92 | + statisticValue: value, |
| 93 | + version, |
| 94 | + } = AV._decode(statisticData); |
| 95 | + return new Statistic({ user, name, value, version }); |
| 96 | + }) |
| 97 | + ); |
| 98 | + }); |
| 99 | + |
| 100 | +_.extend(Leaderboard.prototype, { |
| 101 | + _finishFetch(data) { |
| 102 | + _.forEach(data, (value, key) => { |
| 103 | + if (key === 'updatedAt' || key === 'objectId') return; |
| 104 | + if (key === 'expiredAt') { |
| 105 | + key = 'nextResetAt'; |
| 106 | + } |
| 107 | + if (value.__type === 'Date') { |
| 108 | + value = parseDate(value.iso); |
| 109 | + } |
| 110 | + this[key] = value; |
| 111 | + }); |
| 112 | + return this; |
| 113 | + }, |
| 114 | + fetch(authOptions) { |
| 115 | + return request({ |
| 116 | + method: 'GET', |
| 117 | + path: `/play/leaderboards/${this.statisticName}`, |
| 118 | + authOptions, |
| 119 | + }).then(data => this._finishFetch(data)); |
| 120 | + }, |
| 121 | + _getResults({ skip, limit, includeUserKeys }, authOptions, self) { |
| 122 | + return request({ |
| 123 | + method: 'GET', |
| 124 | + path: `/play/leaderboards/${this.statisticName}/positions${ |
| 125 | + self ? '/self' : '' |
| 126 | + }`, |
| 127 | + query: { |
| 128 | + skip, |
| 129 | + limit, |
| 130 | + includeUser: includeUserKeys |
| 131 | + ? ensureArray(includeUserKeys).join(',') |
| 132 | + : undefined, |
| 133 | + }, |
| 134 | + authOptions, |
| 135 | + }).then(({ results }) => |
| 136 | + results.map(statisticData => { |
| 137 | + const { |
| 138 | + user, |
| 139 | + statisticName: name, |
| 140 | + statisticValue: value, |
| 141 | + position, |
| 142 | + } = AV._decode(statisticData); |
| 143 | + return new Statistic({ user, name, value, position }); |
| 144 | + }) |
| 145 | + ); |
| 146 | + }, |
| 147 | + getResults({ skip, limit, includeUserKeys } = {}, authOptions) { |
| 148 | + return this._getResults({ skip, limit, includeUserKeys }, authOptions); |
| 149 | + }, |
| 150 | + getResultsAroundUser({ limit, includeUserKeys } = {}, authOptions) { |
| 151 | + return this._getResults({ limit, includeUserKeys }, authOptions, true); |
| 152 | + }, |
| 153 | + updateVersionChangeInterval(versionChangeInterval, authOptions) { |
| 154 | + return request({ |
| 155 | + method: 'PUT', |
| 156 | + path: `/play/leaderboards/${this.statisticName}`, |
| 157 | + data: { |
| 158 | + versionChangeInterval, |
| 159 | + }, |
| 160 | + authOptions, |
| 161 | + }).then(data => this._finishFetch(data)); |
| 162 | + }, |
| 163 | + reset(authOptions) { |
| 164 | + return request({ |
| 165 | + method: 'PUT', |
| 166 | + path: `/play/leaderboards/${this.statisticName}/incrementVersion`, |
| 167 | + authOptions, |
| 168 | + }).then(data => this._finishFetch(data)); |
| 169 | + }, |
| 170 | +}); |
| 171 | + |
| 172 | +module.exports = { |
| 173 | + Leaderboard, |
| 174 | + LeaderboardOrder, |
| 175 | + LeaderboardVersionChangeInterval, |
| 176 | +}; |
0 commit comments