-
Notifications
You must be signed in to change notification settings - Fork 0
/
node_helper.js
112 lines (96 loc) · 4.12 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/* Magic Mirror
* Module: MMM-Fortnite
*
* By Johan Persson, https://github.com/retroflex
* MIT Licensed.
*/
const NodeHelper = require('node_helper');
const rp = require('request-promise');
const baseURL = 'https://fortnite-public-api.theapinetwork.com/prod09/users/public/br_stats_v2?user_id=';
module.exports = NodeHelper.create({
start: function() {
//console.log('Starting node_helper for: ' + this.name);
},
// Extracts JSON into the relevant stats.
// JSON can contain these (but not all are necessarily present):
// json.overallData.defaultModes.score / .matchesPlayed / .kills
// json.overallData.ltmModes.score / .matchesPlayed / .kills
// json.overallData.largeTeamModes.score / .matchesPlayed / .kills
// @param json - The full JSON for the user.
// @return Object with stats we want to show.
extractStats: function(json, includeDefaultGameModes, includeLimitedTimeGameModes, includeLargeTeamGameModes) {
let totalScore = 0;
let totalMatchesPlayed = 0;
let totalKills = 0;
if (includeDefaultGameModes) {
const defaultModes = json.overallData.defaultModes;
if (defaultModes != null) {
if (defaultModes.score != null) totalScore += defaultModes.score;
if (defaultModes.matchesplayed != null) totalMatchesPlayed += defaultModes.matchesplayed;
if (defaultModes.kills != null) totalKills += defaultModes.kills;
}
}
if (includeLimitedTimeGameModes) {
const limitedTimeModes = json.overallData.ltmModes;
if (limitedTimeModes != null) {
if (limitedTimeModes.score != null) totalScore += limitedTimeModes.score;
if (limitedTimeModes.matchesplayed != null) totalMatchesPlayed += limitedTimeModes.matchesplayed;
if (limitedTimeModes.kills != null) totalKills += limitedTimeModes.kills;
}
}
if (includeLargeTeamGameModes) {
const largeTeamModes = json.overallData.largeTeamModes;
if (largeTeamModes != null) {
if (largeTeamModes.score != null) totalScore += largeTeamModes.score;
if (largeTeamModes.matchesplayed != null) totalMatchesPlayed += largeTeamModes.matchesplayed;
if (largeTeamModes.kills != null) totalKills += largeTeamModes.kills;
}
}
const stats = { username: json.epicName,
score: totalScore,
matchesPlayed: totalMatchesPlayed,
kills: totalKills };
return stats;
},
// Gets Fortnite user stats from API and adds them to an array.
// Each item in the array contains username / score / matchesPlayed / kills.
// The stats are the total for all seasons and all game modes.
// The array is then sent to the client (to MMM-Fortnite.js).
// @param userIDs - String array of user ID's.
getStats: function(payload) {
let identifier = payload.identifier;
let userIDs = payload.userIDs;
let promises = [];
for (let i = 0; i < userIDs.length; ++i) {
const userURL = baseURL + userIDs[i];
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, payload.includeDefaultGameModes, payload.includeLimitedTimeGameModes, payload.includeLargeTeamGameModes);
stats.push(stat);
}
if ('matchesPlayed' === payload.sortBy)
stats.sort((a, b) => Number(b.matchesPlayed) - Number(a.matchesPlayed));
else if ('kills' === payload.sortBy)
stats.sort((a, b) => Number(b.kills) - Number(a.kills));
else
stats.sort((a, b) => Number(b.score) - Number(a.score));
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-Fortnite.js).
// Client sends a notification when it wants download new stats.
// @param payload - String array of user ID's.
socketNotificationReceived: function(notification, payload) {
if (notification === 'GET_STATS') {
this.getStats(payload);
}
}
});