-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
270 lines (228 loc) · 6.31 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
import EventEmitter from 'eventemitter3';
import diagnostics from 'diagnostics';
import parallel from 'async/parallel';
import reduce from 'async/reduce';
import TickTock from 'tick-tock';
import { get } from '../storage';
import request from 'request';
import map from 'async/map';
/**
* Output debug messages.
*
* @type {Function}
* @private
*/
const debug = diagnostics('anubis:trials-report');
/**
* Generate a Trials Report of the given users.
*
* @constructor
* @private
*/
export default class TrialsReport extends EventEmitter {
constructor(boot) {
super();
this.guardian = boot.get('guardian');
this.destiny = boot.get('destiny');
this.timers = new TickTock();
this.membership = null;
this.username = null;
this.fireteam = []; // Current fire team of the user.
this.loadout = []; // Load out of the fire team.
this.on('error', debug);
}
/**
* Search for a player.
*
* @param {String} username Username.
* @param {Function} fn Completion callback.
* @private
*/
search(username, fn) {
this.username = username;
get('playstation', (err, playstation) => {
if (err) {
this.emit('error', err);
return fn(err);
}
const platform = playstation ? 2 : 1;
this.destiny.user.search(platform, this.username, (err, matches) => {
if (err) {
this.emit('error', err);
return fn(err);
}
if (!matches || !matches.length) {
this.emit('error', new Error('Invalid username'));
return fn(new Error('Invalid username'));
}
//
// Call back early so can let the UI know that we've found a matching
// user.
//
this.membership = matches[0].membershipId;
fn();
});
});
}
/**
* Search for a players fire team.
*
* @param {String} username Username.
* @param {Function} fn Completion callback.
* @private
*/
lookup(username, fn) {
username = username.trim();
this.search(username, (err) => {
if (err) return fn(err);
this.members((err) => {
if (err) return fn(err);
parallel({
user: this.user.bind(this),
trialsreport: this.trialsreport.bind(this)
}, this.gather(username, fn));
});
});
}
/**
* Lookup members of a users fireteam.
*
* @param {Function} fn Completion callback.
* @private
*/
members(fn) {
this.guardian.fireteam(this.membership, 14, (err, fireteam) => {
if (err) return fn(err);
this.fireteam = fireteam;
this.emit('fireteam', fireteam);
fn(undefined, fireteam);
});
}
/**
* Lookup trials report information if available. When we cannot get this data
* we'll just fail silently.
*
* @param {Function} fn Completion callback.
* @private
*/
trialsreport(fn) {
reduce(this.fireteam, {}, (memo, member, next) => {
const id = member.membershipId;
request({
url: 'https://api.destinytrialsreport.com/anubis/'+ id,
timeout: 10000,
json: true
}, function trialsreport(err, res, body) {
if (err) {
debug('API call to destinytrials report failed', err);
return next(undefined, memo);
}
if (res.statusCode !== 200) {
debug('API call to destinytrials returned invalid statusCode', res.statusCode);
return next(undefined, memo);
}
memo[id] = body[0];
next(undefined, memo);
});
}, (err, report) => {
if (err) return fn(err);
this.report = report;
this.emit('report', report);
fn(undefined, report);
});
}
/**
* Generate loadout gather stuff.
*
* @param {String} username Username of the original lookup.
* @param {Function} fn Completion callback.
* @returns {Function} Gathering function.
* @private
*/
gather(username, fn) {
return (err, gathered) => {
if (err) return fn(err);
if (this.username !== username) return;
let { user, trialsreport } = gathered;
user.forEach((data) => {
data.report = trialsreport[data.guardian.membershipId];
});
//
// Sort the users on Elo, highest first so we get consistent results when
// we're looking up people in a fireteam.
//
user = user.sort((a, b) => {
return b.guardian.elo - a.guardian.elo;
});
this.loadout = user;
this.emit('loadout', this.loadout);
fn(undefined, user);
this.refresh();
};
}
/**
* Gather all the user and equipment information.
*
* @param {Function} fn Completion callback.
* @public
*/
user(fn) {
map(this.fireteam, (member, next) => {
const platform = member.membershipType;
const id = member.membershipId;
this.destiny.user.account(platform, id, (err, data) => {
if (err) return next(err);
//
// Make sure we retrieve their last active character from the
// character's array.
//
const char = data.characters.sort(function sort(a, b) {
const lastA = new Date(a.dateLastPlayed);
const lastB = new Date(b.dateLastPlayed);
return +lastA < +lastB;
})[0];
const charBase = char.characterBase;
this.destiny.character.inventory(platform, id, charBase.characterId, (err, inv) => {
if (err) return next(err);
next(undefined, {
guardian: member,
character: char,
inventory: inv
});
});
});
}, fn);
}
/**
* Schedule a new refresh cycle.
*
* @private
*/
refresh() {
const gather = this.gather(this.username, () => {});
this.timers.clear();
this.timers.setTimeout('refresh', () => {
this.user((err, data) => {
if (err) return this.refresh();
//
// Simulate the same data structure as the lookup function so we can
// re-use our internally stored data to eliminate some of the HTTP
// requests we need to make.
//
gather(undefined, {
user: data,
trialsreport: this.report
});
});
}, 10000);
}
/**
* Kill all the information as the connection has died.
*
* @private
*/
destroy() {
this.timers.clear();
this.removeAllListeners();
}
}