forked from fewieden/MMM-NHL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MMM-Liiga.js
256 lines (234 loc) · 7.56 KB
/
MMM-Liiga.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
/**
* @file MMM-Liiga.js
*
* @author fewieden
* @license MIT
*
* @see https://github.com/fewieden/MMM-NHL
*/
/* global Module Log config */
/**
* @external Module
* @see https://github.com/MichMich/MagicMirror/blob/master/js/module.js
*/
/**
* @external Log
* @see https://github.com/MichMich/MagicMirror/blob/master/js/logger.js
*/
/**
* @module MMM-Liiga
* @description Frontend of the MagicMirror² module.
*
* @requires external:Module
* @requires external:Log
*/
Module.register('MMM-Liiga', {
/**
* @member {object.<string, string>} modes - Maps mode short codes to names.
*/
modes: {
1: 'Pre-season',
2: 'Regular season',
3: 'Playoffs',
},
/**
* @member {object.<string, string>} states - Maps game state short codes to translation keys.
*/
states: {
'1st': '1ST_PERIOD',
'2nd': '2ND_PERIOD',
'3rd': '3RD_PERIOD',
OT: 'OVER_TIME',
SO: 'SHOOTOUT',
SHOOTOUT: 'SHOOTOUT',
FINAL: 'FINAL',
'FINAL OT': 'FINAL_OVERTIME',
'FINAL SO': 'FINAL_SHOOTOUT',
PPD: 'PPD'
},
/**
* @member {boolean} loading - Indicates loading state of module and data.
*/
loading: true,
/**
* @member {Game[]} games - List of all games matching focus and timespan config options.
*/
games: [],
/**
* @member {Series[]} playoffSeries - List of all current playoff series.
*/
playoffSeries: [],
/**
* @member {SeasonDetails} season - Current season details e.g. year and mode.
*/
season: {},
/**
* @member {number} rotateIndex - Current index of rotation carousel.
*/
rotateIndex: 0,
/**
* @member {?Interval} rotateInterval - Interval to update rotation index.
*/
rotateInterval: null,
/**
* @member {object} defaults - Defines the default config values.
* @property {boolean} colored - Flag to show logos in color or black/white.
* @property {boolean|string[]} focus_on - List of team name short codes to display games from.
* @property {number} matches - Max amount of matches to display at once.
* @property {number} rotateInterval - Amount of milliseconds a page of the carousel is displayed.
* @property {number} reloadInterval - Amount of milliseconds between data fetching.
* @property {number} liveReloadInterval - Amount of milliseconds between data fetching during a live game.
* @property {number} daysInPast - Amount of days a match should be displayed after it is finished.
* @property {number} daysAhead - Amount of days a match should be displayed before it starts.
* @property {boolean} showNames - Flag to show team names.
* @property {boolean} showLogos - Flag to show club logos.
* @property {boolean} showPlayoffSeries - Flag to show playoff series status during playoffs.
* @property {boolean} rollOverGames - Flag to show today's games and previous/next day based on game status.
*/
defaults: {
colored: false,
focus_on: false,
matches: 6,
rotateInterval: 20 * 1000,
reloadInterval: 30 * 60 * 1000,
liveReloadInterval: 60 * 1000,
daysInPast: 1,
daysAhead: 7,
showNames: true,
showLogos: true,
showPlayoffSeries: true,
rollOver: false
},
/**
* @function getTranslations
* @description Translations for this module.
* @override
*
* @returns {object.<string, string>} Available translations for this module (key: language code, value: filepath).
*/
getTranslations() {
return {
en: 'translations/en.json',
de: 'translations/de.json',
fr: 'translations/fr.json',
fi: 'translations/fi.json'
};
},
/**
* @function getStyles
* @description Style dependencies for this module.
* @override
*
* @returns {string[]} List of the style dependency filepaths.
*/
getStyles() {
return ['font-awesome.css', 'MMM-Liiga.css'];
},
/**
* @function getTemplate
* @description Nunjuck template.
* @override
*
* @returns {string} Path to nunjuck template.
*/
getTemplate() {
return 'templates/MMM-Liiga.njk';
},
/**
* @function getTemplateData
* @description Data that gets rendered in the nunjuck template.
* @override
*
* @returns {object} Data for the nunjuck template.
*/
getTemplateData() {
return {
loading: this.loading,
modes: this.modes,
season: this.season,
games: this.games,
playoffSeries: this.playoffSeries,
rotateIndex: this.rotateIndex,
maxGames: Math.min(this.games.length, this.rotateIndex + this.config.matches),
config: this.config
};
},
/**
* @function start
* @description Adds nunjuck filters, overrides day config options if rollOver and sends config to node_helper.
* @override
*
* @returns {void}
*/
start() {
Log.info(`Starting module: ${this.name}`);
this.addFilters();
if (this.config.rollOver) {
this.config.daysInPast = 1;
this.config.daysAhead = 1;
}
this.sendSocketNotification('CONFIG', { config: this.config });
},
/**
* @function socketNotificationReceived
* @description Handles incoming messages from node_helper.
* @override
*
* @param {string} notification - Notification name
* @param {*} payload - Detailed payload of the notification.
*/
socketNotificationReceived(notification, payload) {
if (notification === 'SCHEDULE') {
this.loading = false;
this.games = payload.games;
this.season = payload.season;
this.setRotateInterval();
} else if (notification === 'PLAYOFFS') {
this.playoffSeries = payload;
this.updateDom(300);
}
},
/**
* @function setRotateInterval
* @description Sets interval if necessary which updates the rotateIndex.
*
* @returns {void}
*/
setRotateInterval() {
if (!this.rotateInterval && this.games.length > this.config.matches) {
this.rotateInterval = setInterval(() => {
if (this.rotateIndex + this.config.matches >= this.games.length) {
this.rotateIndex = 0;
} else {
this.rotateIndex += this.config.matches;
}
this.updateDom(300);
}, this.config.rotateInterval);
} else if (this.games.length <= this.config.matches) {
clearInterval(this.rotateInterval);
this.rotateIndex = 0;
}
this.updateDom(300);
},
/**
* @function addFilters
* @description Adds the filter used by the nunjuck template.
*
* @returns {void}
*/
addFilters() {
this.nunjucksEnvironment().addFilter('formatStartDate', game => {
const now = new Date();
const inAWeek = now.setDate(now.getDate() + 7);
const start = new Date(game.timestamp);
if (start > inAWeek) {
return new Intl.DateTimeFormat(config.locale, {
month: '2-digit', day: '2-digit', hour: '2-digit', minute: '2-digit'
}).format(start);
}
return new Intl.DateTimeFormat(config.locale, {
weekday: 'short', hour: '2-digit', minute: '2-digit'
}).format(start);
});
}
});