@@ -17,6 +17,22 @@ const csvParser = require('csv-parse');
17
17
const AsciiTable = require ( 'ascii-table' ) ;
18
18
const leagueTeams = require ( './teams.json' ) ;
19
19
20
+ const BEFORE_GAME_STATES = [
21
+ 'FUT' , // Future game
22
+ 'PRE' , // Pre-game
23
+ ] ;
24
+
25
+ const LIVE_GAME_STATES = [
26
+ 'LIVE' , // Live
27
+ 'CRIT' , // Critical, last five minutes
28
+ ] ;
29
+
30
+ const AFTER_GAME_STATES = [
31
+ 'OVER' , // Recently completed
32
+ 'FINAL' , // Game ended, focused
33
+ 'OFF' , // Game ended, not focused
34
+ ] ;
35
+
20
36
module . exports = ( robot ) => {
21
37
const periodFormat = ( periodDescriptor ) => {
22
38
if ( periodDescriptor . type === 'SO' ) {
@@ -54,8 +70,24 @@ module.exports = (robot) => {
54
70
return cb ;
55
71
}
56
72
57
- // Find the game closest to right now
58
- const games = json . gamesByDate . find ( ( d ) => moment ( d . date ) >= moment ( json . focusedDate ) ) ;
73
+ let games ;
74
+
75
+ // Determine if there is a game.gameState of FUT before focusedDate (e.g. preseason)
76
+ const focusedDate = moment ( json . focusedDate ) ;
77
+ if (
78
+ json . gamesByDate . find (
79
+ ( d ) => moment ( d . date ) <= focusedDate && d . games . find (
80
+ ( g ) => BEFORE_GAME_STATES . includes ( g . gameState ) ,
81
+ ) ,
82
+ ) ) {
83
+ games = json . gamesByDate . find (
84
+ ( d ) => moment ( d . date ) <= focusedDate && d . games . find (
85
+ ( g ) => BEFORE_GAME_STATES . includes ( g . gameState ) ,
86
+ ) ,
87
+ ) ;
88
+ } else {
89
+ games = json . gamesByDate . find ( ( d ) => moment ( d . date ) >= focusedDate ) ;
90
+ }
59
91
60
92
// Catch if final game of season played
61
93
if ( ! games || games . length === 0 ) {
@@ -66,17 +98,17 @@ module.exports = (robot) => {
66
98
// TODO: Handle doubleheaders, etc.
67
99
const game = games . games [ 0 ] ;
68
100
69
- if ( game . gameState === 'OFF' || game . gameState === 'FINAL' || game . gameState === 'OVER' ) {
101
+ if ( AFTER_GAME_STATES . includes ( game . gameState ) ) {
70
102
gameStatus = 'Final' ;
71
103
if ( game . period > 3 ) {
72
104
gameStatus = `${ gameStatus } /${ periodFormat ( game . periodDescriptor ) } ` ;
73
105
}
74
- } else if ( game . gameState === 'LIVE' || game . gameState === 'CRIT' ) {
106
+ } else if ( LIVE_GAME_STATES . includes ( game . gameState ) ) {
75
107
gameStatus = `${ game . clock . timeRemaining } ${ periodFormat ( game . periodDescriptor ) } ` ;
76
108
if ( game . clock ?. inIntermission ) {
77
109
gameStatus += ' Intermission' ;
78
110
}
79
- } else if ( ( game . gameState === 'FUT' || game . gameState === 'PRE' ) && ( game . gameScheduleState === 'OK' ) ) {
111
+ } else if ( BEFORE_GAME_STATES . includes ( game . gameState ) && ( game . gameScheduleState === 'OK' ) ) {
80
112
gameStatus = `${ moment ( game . startTimeUTC ) . tz ( team . time_zone ) . format ( 'h:mm a z' ) } ` ;
81
113
} else {
82
114
gameStatus = 'TBD' ;
@@ -111,7 +143,7 @@ module.exports = (robot) => {
111
143
}
112
144
113
145
const table = new AsciiTable ( ) ;
114
- if ( game . gameState === 'FUT' || game . gameState === 'PRE' ) {
146
+ if ( BEFORE_GAME_STATES . includes ( game . gameState ) ) {
115
147
if ( game . gameType !== 3 ) {
116
148
table . addRow ( `${ game . awayTeam . name . default } (${ game . awayTeam . record } )` ) ;
117
149
table . addRow ( `${ game . homeTeam . name . default } (${ game . homeTeam . record } )` ) ;
@@ -126,7 +158,11 @@ module.exports = (robot) => {
126
158
table . removeBorder ( ) ;
127
159
128
160
let howToWatch = game . venue . default ;
129
- if ( ( game . gameState !== 'OFF' && game . gameState !== 'FINAL' ) && game . tvBroadcasts && ( game . tvBroadcasts . length > 0 ) ) {
161
+ if (
162
+ ! AFTER_GAME_STATES . includes ( game . gameState )
163
+ && game . tvBroadcasts
164
+ && ( game . tvBroadcasts . length > 0 )
165
+ ) {
130
166
const networks = [ ] ;
131
167
game . tvBroadcasts . forEach ( ( broadcast ) => networks . push ( `${ broadcast . network } (${ broadcast . market } )` ) ) ;
132
168
howToWatch = `${ howToWatch } ; TV: ${ networks . join ( ' | ' ) } ` ;
0 commit comments