Skip to content

Commit 86fe5ef

Browse files
Merge pull request #91 from stephenyeargin/fix-preseason
Fix pre-season schedule weirdness
2 parents d8d8874 + 76598cd commit 86fe5ef

File tree

5 files changed

+692
-10
lines changed

5 files changed

+692
-10
lines changed

.github/workflows/nodejs.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ jobs:
1010
strategy:
1111
fail-fast: false
1212
matrix:
13-
node-version: [16.x, 18.x, 20.x]
13+
node-version: [18.x, 20.x, 22.x]
1414

1515
steps:
1616
- uses: actions/checkout@v4
1717

1818
- name: Use Node.js ${{ matrix.node-version }}
19-
uses: actions/setup-node@v3
19+
uses: actions/setup-node@v4
2020
with:
2121
node-version: ${{ matrix.node-version }}
2222

script/test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@
33
# bootstrap environment
44
source script/bootstrap
55

6-
mocha "test/**/*.js" --reporter spec --forbid-only --forbid-pending --exit
6+
mocha "test/**/*.js" --reporter spec --forbid-pending --exit

src/hockey.js

Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,22 @@ const csvParser = require('csv-parse');
1717
const AsciiTable = require('ascii-table');
1818
const leagueTeams = require('./teams.json');
1919

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+
2036
module.exports = (robot) => {
2137
const periodFormat = (periodDescriptor) => {
2238
if (periodDescriptor.type === 'SO') {
@@ -54,8 +70,24 @@ module.exports = (robot) => {
5470
return cb;
5571
}
5672

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+
}
5991

6092
// Catch if final game of season played
6193
if (!games || games.length === 0) {
@@ -66,17 +98,17 @@ module.exports = (robot) => {
6698
// TODO: Handle doubleheaders, etc.
6799
const game = games.games[0];
68100

69-
if (game.gameState === 'OFF' || game.gameState === 'FINAL' || game.gameState === 'OVER') {
101+
if (AFTER_GAME_STATES.includes(game.gameState)) {
70102
gameStatus = 'Final';
71103
if (game.period > 3) {
72104
gameStatus = `${gameStatus}/${periodFormat(game.periodDescriptor)}`;
73105
}
74-
} else if (game.gameState === 'LIVE' || game.gameState === 'CRIT') {
106+
} else if (LIVE_GAME_STATES.includes(game.gameState)) {
75107
gameStatus = `${game.clock.timeRemaining} ${periodFormat(game.periodDescriptor)}`;
76108
if (game.clock?.inIntermission) {
77109
gameStatus += ' Intermission';
78110
}
79-
} else if ((game.gameState === 'FUT' || game.gameState === 'PRE') && (game.gameScheduleState === 'OK')) {
111+
} else if (BEFORE_GAME_STATES.includes(game.gameState) && (game.gameScheduleState === 'OK')) {
80112
gameStatus = `${moment(game.startTimeUTC).tz(team.time_zone).format('h:mm a z')}`;
81113
} else {
82114
gameStatus = 'TBD';
@@ -111,7 +143,7 @@ module.exports = (robot) => {
111143
}
112144

113145
const table = new AsciiTable();
114-
if (game.gameState === 'FUT' || game.gameState === 'PRE') {
146+
if (BEFORE_GAME_STATES.includes(game.gameState)) {
115147
if (game.gameType !== 3) {
116148
table.addRow(`${game.awayTeam.name.default} (${game.awayTeam.record})`);
117149
table.addRow(`${game.homeTeam.name.default} (${game.homeTeam.record})`);
@@ -126,7 +158,11 @@ module.exports = (robot) => {
126158
table.removeBorder();
127159

128160
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+
) {
130166
const networks = [];
131167
game.tvBroadcasts.forEach((broadcast) => networks.push(`${broadcast.network} (${broadcast.market})`));
132168
howToWatch = `${howToWatch}; TV: ${networks.join(' | ')}`;

0 commit comments

Comments
 (0)