-
Notifications
You must be signed in to change notification settings - Fork 0
/
MMM-RocketLeagueZ.js
172 lines (144 loc) · 4.79 KB
/
MMM-RocketLeagueZ.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
/* Magic Mirror
* Module: MMM-RocketLeagueZ
*
* By Oliver Petersen, https://github.com/Zebrakadebra
* MIT Licensed.
*/
Module.register('MMM-RocketLeagueZ', {
// default configuration
defaults: {
baseURL: 'https://api.tracker.gg/api/v2/rocket-league/standard/profile/',
gamers: [
'epic/KhaliDx.',
'steam/FirstKiller',
'xbl/Rezears',
'psn/M0nkey_M00n_',
'switch/Nuqqet'
],
playlists: [
'Un-Ranked',
'Ranked Duel 1v1',
'Ranked Doubles 2v2',
'Ranked Standard 3v3',
'Tournament Matches'
],
fetchInterval: 10*60*1000,
fetchIntervalMin: 60*1000,
rotateInterval: 10*1000,
rotateIntervalMin: 2*1000,
animationSpeed: 400
},
getStyles: function() {
return [ 'modules/MMM-RocketLeagueZ/MMM-RocketLeagueZ.css' ];
},
getTranslations: function () {
return {
en: 'translations/en.json',
de: 'translations/de.json'
}
},
getHeader: function() {
return this.data.header + ' ' + this.translate(this.config.playlists[this.currentPlaylist]);
},
// Notification from node_helper.js.
socketNotificationReceived: function(notification, payload) {
if (notification === 'STATS_RESULT') {
if (null == payload)
return;
if (null == payload.identifier)
return;
if (payload.identifier !== this.identifier) // To make sure the correct instance is updated, since they share node_helper.
return;
if (null == payload.stats)
return;
if (0 === payload.stats.length)
return;
this.stats = payload.stats;
}
},
// Override dom generator.
getDom: function () {
let wrapper = document.createElement('table');
if (null == this.stats) {
wrapper.innerHTML = this.translate('LOADING');
wrapper.className = 'loading dimmed xsmall';
} else {
this.currentPlaylist = this.currentPlaylist == this.config.playlists.length - 1 ? 0 : this.currentPlaylist + 1;
let gamers = [];
console.log(this.name + ': gamer stats rendering for playlist ' + this.translate(this.config.playlists[this.currentPlaylist]));
this.stats.forEach((stat) => {
stat.playlists.forEach((playlist) => {
if (playlist.name == this.config.playlists[this.currentPlaylist]) {
let gamer = {
tag: stat.gamertag,
avatarURL: stat.avatarURL,
rankName: playlist.rankName,
divisionNumber: this.translate(playlist.divisionNumber),
iconURL: playlist.iconURL,
rankValue: playlist.rankValue,
}
gamers.push(gamer);
}
});
});
gamers.sort((a, b) => {
return b.rankValue - a.rankValue;
});
gamers.forEach((gamer) => {
let row = document.createElement('tr');
let cell = document.createElement('td');
cell.rowSpan = 2;
cell.innerHTML = "<img src='" + gamer.avatarURL + "' width=80px height=80px alt='" + gamer.tag + "'/>"
row.appendChild(cell);
cell = document.createElement('td');
cell.rowSpan = 2;
cell.className = 'gamer';
cell.innerHTML = gamer.tag;
row.appendChild(cell);
cell = document.createElement('td');
cell.className = 'mmr';
cell.innerHTML = gamer.rankValue;
row.appendChild(cell);
cell = document.createElement('td');
cell.rowSpan = 2;
cell.innerHTML = this.config.playlists[this.currentPlaylist] == 'Un-Ranked' ? '' : "<img src='" + gamer.iconURL + "' width=80px height=80px alt='" + gamer.rankName + "'/>"
row.appendChild(cell);
wrapper.appendChild(row);
row = document.createElement('tr');
cell = document.createElement('td');
cell.className = 'division';
cell.innerHTML = this.config.playlists[this.currentPlaylist] == 'Un-Ranked' ? '' : gamer.divisionNumber;
row.appendChild(cell);
wrapper.appendChild(row);
});
}
return wrapper;
},
// Override start to init stuff.
start: function() {
this.stats = null;
this.currentPlaylist = 0;
console.log(this.name + ': Modul started');
// Tell node_helper to load stats at startup.
this.sendSocketNotification('GET_STATS', {
identifier: this.identifier,
baseURL: this.config.baseURL,
gamers: this.config.gamers
});
console.log(this.name + ': GET_STATS SocketNotification sent');
// Make sure stats are reloaded at user specified interval.
let self = this;
setInterval(function() {
self.updateDom(self.config.animationSpeed);
}, Math.max(self.config.rotateInterval, self.config.rotateIntervalMin));
setInterval(function() {
self.sendSocketNotification('GET_STATS', {
identifier: self.identifier,
baseURL: self.config.baseURL,
gamers: self.config.gamers
});
}, Math.max(self.config.fetchInterval, self.config.fetchIntervalMin));
console.log(this.name + ': SocketNotification interval set');
console.log(this.name + ': waiting for reply from node_helper');
}
});