-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
node_helper.js
124 lines (106 loc) · 3.85 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
/* Magic Mirror
* Module: MMM-NFL
*
* By fewieden https://github.com/fewieden/MMM-NFL
* MIT Licensed.
*/
/* eslint-env node */
const NodeHelper = require('node_helper');
const Log = require('logger');
const ESPN = require('./espn');
const ONE_MINUTE = 60 * 1000;
const commandStatisticMapping = {
passingTouchdowns: ['PASSING', 'TOUCHDOWNS'],
rushingTouchdowns: ['RUSHING', 'TOUCHDOWNS'],
receivingTouchdowns: ['RECEIVING', 'TOUCHDOWNS'],
totalTouchdowns: ['TOTAL', 'TOUCHDOWNS'],
passingYards: ['PASSING', 'YARDS'],
rushingYards: ['RUSHING', 'YARDS'],
receivingYards: ['RECEIVING', 'YARDS'],
sacks: ['SACKS'],
interceptions: ['INTERCEPTIONS'],
totalTackles: ['TACKLES'],
quarterbackRating: ['QUARTERBACK', 'RATING'],
receptions: ['RECEPTIONS'],
passesDefended: ['PASSES', 'DEFENDED'],
totalPoints: ['TOTAL', 'POINTS'],
puntYards: ['PUNT', 'YARDS'],
kickoffYards: ['KICKOFF', 'YARDS']
};
module.exports = NodeHelper.create({
requiresVersion: '2.15.0',
scores: [],
reloadInterval: null,
liveInterval: null,
async socketNotificationReceived(notification, payload) {
if (notification === 'CONFIG') {
this.config = payload;
this.reloadInterval = setInterval(() => {
this.getData();
}, this.config.reloadInterval);
this.liveInterval = setInterval(() => {
this.fetchOnLiveState();
}, ONE_MINUTE);
await this.getData();
} else if (notification === 'VOICE_COMMAND') {
await this.handleVoiceCommand(payload);
} else if (notification === 'SUSPEND') {
this.stop();
}
},
async getData() {
try {
const data = await ESPN.getData();
this.scores = data.scores;
this.sendSocketNotification('SCORES', data);
} catch (error) {
Log.error(`Error getting NFL scores ${error}`);
}
},
async getStatisticsFromVoiceCommand(command) {
try {
let type = Object.keys(commandStatisticMapping)[0];
for (const statisticType in commandStatisticMapping) {
const matching = commandStatisticMapping[statisticType].every(word => command.includes(word));
if (matching) {
type = statisticType;
break;
}
}
const statistics = await ESPN.getStatistics(type);
this.sendSocketNotification('STATISTICS', { type, statistics });
} catch (error) {
Log.error(`Error getting NFL statistics ${error}`);
}
},
fetchOnLiveState() {
const currentTime = new Date().toISOString();
const endStates = ['final', 'final-overtime'];
const liveMatch = this.scores.find(match => currentTime > match.timestamp && !endStates.includes(match.status));
if (liveMatch) {
this.getData();
}
},
shouldCloseOpenModal(command) {
return command.includes('HELP') && command.includes('CLOSE') || command.includes('STATISTIC') && command.includes('HIDE');
},
shouldShowHelpModal(command) {
return command.includes('HELP') && command.includes('OPEN');
},
shouldShowStatisticModal(command) {
return command.includes('SHOW') && command.includes('STATISTIC');
},
async handleVoiceCommand(command = '') {
if (this.shouldCloseOpenModal(command)) {
this.sendSocketNotification('CLOSE_MODAL');
} else if (this.shouldShowHelpModal(command)) {
this.sendSocketNotification('OPEN_HELP_MODAL');
} else if (this.shouldShowStatisticModal(command)) {
await this.getStatisticsFromVoiceCommand(command);
}
},
stop() {
clearInterval(this.liveInterval);
clearInterval(this.reloadInterval);
}
});