-
Notifications
You must be signed in to change notification settings - Fork 0
/
node_helper.js
executable file
·88 lines (70 loc) · 2.28 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
/* Magic Mirror
* Module: MMM-RocketLeagueZ
*
* By Oliver Petersen, https://github.com/Zebrakadebra
* MIT Licensed.
*/
const NodeHelper = require('node_helper');
const axios = require('axios');
module.exports = NodeHelper.create({
start: function() {
console.log('Starting node_helper for: ' + this.name);
},
getStat: function(userURL) {
return axios.get(userURL)
.catch((error) => {
console.log('node_helper of ' + this.name + ' ' + error + ' during axios get from ' + userURL);
});
},
sendStats: function(payload) {
let identifier = payload.identifier;
let gamers = payload.gamers;
let baseURL = payload.baseURL;
let promises = [];
gamers.forEach((gamer) => {
const userURL = baseURL + gamer + '?';
promises.push(this.getStat(userURL));
});
let stats = [];
Promise.all(promises)
.then((gamers) => {
gamers.forEach((gamer) => {
const gamertag = gamer.data.data.platformInfo.platformUserIdentifier;
const segments = gamer.data.data.segments;
const avatarURL = gamer.data.data.platformInfo.avatarUrl;
console.log(avatarURL);
let playlists = [];
segments.forEach((segment) => {
if (segment.type == 'playlist') {
const playlist = {
name: segment.metadata.name,
rankName: segment.stats.tier.metadata.name,
divisionNumber: segment.stats.division.metadata.name,
iconURL: segment.stats.tier.metadata.iconUrl,
rankValue: segment.stats.rating.value
};
playlists.push(playlist);
}
});
const stat = {
gamertag: gamertag,
avatarURL: avatarURL,
playlists: playlists
};
stats.push(stat);
});
console.log('node_helper of ' + this.name + ' sending STATS_RESULT including ' + stats.length + ' stats.');
this.sendSocketNotification('STATS_RESULT', {identifier: identifier, stats: stats} );
})
.catch((error) => {
console.log('node_helper of ' + this.name + ':' + error + ' during axios promise receiption');
});
},
// Listens to notifications from client (from MMM-RocketLeagueZ.js).
socketNotificationReceived: function(notification, payload) {
if (notification === 'GET_STATS') {
console.log('node_helper of ' + this.name + ' received GET_STATS socket notification');
this.sendStats(payload);
}
}
});