forked from fewieden/MMM-NHL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode_helper.js
135 lines (124 loc) · 4.76 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/* Magic Mirror
* Module: MMM-NHL
*
* By fewieden https://github.com/fewieden/MMM-NHL
* MIT Licensed.
*/
/* eslint-env node */
const request = require('request');
const moment = require('moment-timezone');
const NodeHelper = require('node_helper');
module.exports = NodeHelper.create({
url: 'http://live.nhle.com/GameData/RegularSeasonScoreboardv3.jsonp',
scores: [],
details: {},
nextMatch: null,
live: {
state: false,
matches: []
},
start() {
console.log(`Starting module helper: ${this.name}`);
},
socketNotificationReceived(notification, payload) {
if (notification === 'CONFIG') {
this.config = payload.config;
this.teams = payload.teams;
this.getData();
setInterval(() => {
this.getData();
}, this.config.reloadInterval);
setInterval(() => {
this.fetchOnLiveState();
}, 60 * 1000);
}
},
getData() {
request({ url: this.url }, (error, response, body) => {
if (response.statusCode === 200) {
// eslint-disable-next-line no-new-func
const f = new Function('loadScoreboard', body);
f((data) => {
if (Object.prototype.hasOwnProperty.call(data, 'games')) {
this.scores = [];
for (let i = data.games.length - 1; i >= 0; i -= 1) {
if (!this.config.focus_on ||
this.config.focus_on.includes(this.teams[data.games[i].htv]) ||
this.config.focus_on.includes(this.teams[data.games[i].atv])) {
if (data.games[i].tsc !== 'final' || i === data.games.length - 1) {
const id = data.games[i].id.toString();
this.details = {
y: id.slice(0, 4),
t: id.slice(4, 6)
};
}
this.scores.unshift(data.games[i]);
}
}
this.setMode();
this.sendSocketNotification('SCORES', { scores: this.scores, details: this.details });
} else {
console.log('Error no NHL data');
}
});
} else {
console.log(`Error getting NHL scores ${response.statusCode}`);
}
});
},
setMode() {
let allEnded = true;
let next = null;
const now = Date.now();
const inGame = 'progress';
const ended = 'final';
for (let i = 0; i < this.scores.length; i += 1) {
const temp = this.scores[i];
if (this.scores[i].bsc === '') {
const time = temp.bs.split(' ')[0].split(':');
const mom = moment().tz('America/Los_Angeles');
if (temp.ts !== 'TODAY') {
const date = temp.ts.split(' ')[1].split('/');
mom.set('month', date[0]);
mom.set('date', date[1]);
mom.subtract(1, 'month');
}
mom.set('hour', time[0]);
mom.set('minute', time[1]);
mom.set('second', 0);
if (temp.bs.slice(-2) === 'PM') {
mom.add(12, 'hours');
}
this.scores[i].starttime = mom;
allEnded = false;
if (next === null) {
next = this.scores[i];
}
} else if ((inGame === this.scores[i].bsc || Date.parse(this.scores[i].starttime) > now) &&
this.live.matches.indexOf(this.scores[i].id) === -1) {
allEnded = false;
this.live.matches.push(this.scores[i].id);
this.live.state = true;
} else if (ended === this.scores[i].bsc && this.live.matches.includes(this.scores[i].id)) {
this.live.matches.splice(this.live.matches.indexOf(this.scores[i].id), 1);
if (this.live.matches.length === 0) {
this.live.state = false;
}
}
}
if (allEnded === true) {
this.nextMatch = null;
}
if ((next !== null && this.nextMatch === null && allEnded === false) || this.live.state === true) {
this.nextMatch = {
id: next.id,
time: next.starttime
};
}
},
fetchOnLiveState() {
if (this.live.state === true) {
this.getData();
}
}
});