-
Notifications
You must be signed in to change notification settings - Fork 0
/
node_helper.js
89 lines (75 loc) · 2.85 KB
/
node_helper.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
/* Magic Mirror
* Module: MMM-Overwatch
*
* By Johan Persson, https://github.com/retroflex
* MIT Licensed.
*/
const NodeHelper = require('node_helper');
const rp = require('request-promise');
const baseURL = 'https://ow-api.com/v1/stats/';
module.exports = NodeHelper.create({
start: function() {
//console.log('Starting node_helper for: ' + this.name);
},
// Extracts JSON into the relevant stats.
// JSON contains these (amongst others):
// json.name
// json.level
// json.rating
// json.gamesWon
// json.quickPlayStats.games.played
// json.quickPlayStats.games.won
// json.competitiveStats.games.played
// json.competitiveStats.games.won
// @param json - The full JSON for the user.
// @return Object with stats we want to show.
extractStats: function(json) {
const stats = { battleTag: json.name,
level: json.level,
rating: json.rating,
gamesWon: json.gamesWon };
return stats;
},
// Gets Overwatch user stats from API and adds them to an array.
// The array is then sent to the client (to MMM-Overwatch.js).
// @param userInfos - String array of user info's. Each info contains platform and battleTag.
getStats: function(payload) {
let identifier = payload.identifier;
let userInfos = payload.userInfos;
let promises = [];
for (let i = 0; i < userInfos.length; ++i) {
const platform = userInfos[i].platform;
const region = 'asia'; // Should be 'us', 'eu' or 'asia' according to API doc, but it seems it can be any value.
const battleTag = userInfos[i].battleTag;
const userURL = baseURL + platform + '/' + region + '/' + battleTag + '/profile';
const options = {uri: userURL};
promises.push(rp(options));
}
Promise.all(promises).then((contents) => {
let stats = [];
for (let i = 0; i < contents.length; ++i) {
const content = contents[i];
const json = JSON.parse(content);
const stat = this.extractStats(json);
stats.push(stat);
}
// Always sort by rating first. Good if the column to sort on have equal values.
stats.sort((a, b) => Number(a.rating) - Number(b.rating));
if ('level' === payload.sortBy)
stats.sort((a, b) => Number(b.level) - Number(a.level));
if ('gamesWon' === payload.sortBy)
stats.sort((a, b) => Number(b.gamesWon) - Number(a.gamesWon));
this.sendSocketNotification('STATS_RESULT', {identifier: identifier, stats: stats} );
}).catch(err => {
console.log(this.name + ' error when fetching data: ' + err);
});
},
// Listens to notifications from client (from MMM-Overwatch.js).
// Client sends a notification when it wants download new stats.
// @param payload - String array of user info's (where each info contains platform and battle tag).
socketNotificationReceived: function(notification, payload) {
if (notification === 'GET_STATS') {
this.getStats(payload);
}
}
});