-
Notifications
You must be signed in to change notification settings - Fork 1
/
node_helper.js
126 lines (111 loc) · 4.32 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
/* Magic Mirror
* Module: MMM-NBA
*
* By jupadin
* MIT Licensed.
*/
const NodeHelper = require('node_helper');
const request = require('request');
const Log = require('../../js/logger.js');
module.exports = NodeHelper.create({
start: function() {
this.config = null;
this.updateInterval = null;
this.live = false;
this.seasonTypeMapping = {
1: "P",
2: "R",
};
},
socketNotificationReceived: function(notification, payload) {
if (notification == "SET_CONFIG") {
this.config = payload;
this.updateInterval = this.config.updateInterval;
}
// Retrieve data from NBA-Server
this.getData();
},
getGameStatus: function(eventStatus) {
if (eventStatus.type.state === "pre") {
// Upcoming
return "P";
} else if (eventStatus.type.name === "STATUS_HALFTIME") {
// Halftime
return "H";
} else if (eventStatus.type.state === "post") {
// Game has ended -> Overtime or regular end ?
if (eventStatus.period > 4) {
return "FO";
}
return "F";
} else if (eventStatus.period > 4) {
// Game is still running -> Overtime
return "OT";
}
return eventStatus.period;
},
mapEvent: function(event) {
const ongoing = !['pre', 'post'].includes(event.status.type);
const remainingTime = ongoing && event.status.displayClock;
const formattedEvent = {
// Name team home
h: event.competitions[0].competitors[0].team.abbreviation,
// Scores team home
hs: event.competitions[0].competitors[0].score,
// Game status (live, quarter, over, ...)
q: this.getGameStatus(event.status),
// Start date of match
starttime: event.date,
// Name team guest
v: event.competitions[0].competitors[1].team.abbreviation,
// Score team guest
vs: event.competitions[0].competitors[1].score,
// Remaining time
k: remainingTime,
// Link logo team home
hl: event.competitions[0].competitors[0].team.logo,
// Link logo team guest
vl: event.competitions[0].competitors[1].team.logo,
};
return formattedEvent;
},
getData: function() {
const self = this;
Log.info(this.name + ": Fetching data from NBA-Server...");
const nbaURL = self.config.urls[self.config.mode];
request(nbaURL, function(error, response, body) {
if (error || response.statusCode != 200) {
Log.debug(self.name + ": Error getting NBA scores (" + response.statusCode + ")");
self.sendSocketNotification("ERROR", response.statusCode);
return;
}
const json = JSON.parse(body);
const details = {
w: json.day.date,
y: json.season.year - 1,
t: self.seasonTypeMapping[json.season.type],
};
// Create events array
const events = json.events || [];
// Format each event based on callback function (mapEvent) and sort it afterwards, based on start date (starttime).
const scores = events.map(self.mapEvent.bind(self)).sort((a, b) => {
return (a.starttime < b.starttime) ? -1 : ((a.starttime > b.starttime) ? 1 : 0);
});
// Send data to front-end
self.sendSocketNotification("DATA", {games: scores, details: details});
// Check if there is currently a live match
if (scores.some(e => e.q in ["1", "2", "3", "4", "H", "OT"])) {
self.live = true;
};
if (self.live) {
// If there is a match currently live, set update interval to 1 minute.
self.updateInterval = self.config.updateIntervalLive;
} else {
// Otherwise set it to the specified update interval time.
self.updateInterval = self.config.updateInterval;
}
// Set timeout to continuiusly fetch new data from NBA-Server
setTimeout(self.getData.bind(self), self.updateInterval);
});
}
})