Skip to content

Commit

Permalink
Show team records in all cases
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenyeargin committed Oct 31, 2024
1 parent 591fae2 commit 66cb606
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 43 deletions.
63 changes: 53 additions & 10 deletions src/hockey.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,44 @@ module.exports = (robot) => {
}
};

const postGameResults = (team, msg, cb) => msg.http(`https://api-web.nhle.com/v1/scoreboard/${team.abbreviation.toLowerCase()}/now`)
.get()((err, res, body) => {
const getScoreboard = (team) => new Promise((resolve, reject) => {
robot.http(`https://api-web.nhle.com/v1/scoreboard/${team.abbreviation.toLowerCase()}/now`)
.get()((err, res, body) => {
if (err) {
robot.logger.error(err);
reject(err);
}
const json = JSON.parse(body);
resolve(json);
});
});

const getStandings = () => new Promise((resolve, reject) => {
robot.http(`https://api-web.nhle.com/v1/standings/${moment().tz('America/Los_Angeles').format('YYYY-MM-DD')}`)
.get()((err, res, body) => {
if (err) {
robot.logger.error(err);
reject(err);
}
const json = JSON.parse(body);
resolve(json);
});
});

const getTeamRecord = (team, standings) => {
const teamRecord = standings.standings?.find((t) => t.teamAbbrev.default === team.abbrev);
return `${teamRecord.wins}-${teamRecord.losses}-${teamRecord.otLosses}`;
};

const postGameResults = (team, msg, cb) => Promise.all([
getStandings(),
getScoreboard(team),
])
.then((results) => {
const [standings, scoreboard] = results;

let gameStatus;
if (err) {
robot.logger.error(err);
return cb;
}
const json = JSON.parse(body);
const json = scoreboard;
if (!json || (
!json.gamesByDate
|| json.gamesByDate.length === 0)
Expand Down Expand Up @@ -152,8 +182,8 @@ module.exports = (robot) => {
table.addRow(`${game.homeTeam.name.default}`);
}
} else {
table.addRow(`${game.awayTeam.name.default}`, `${game.awayTeam.score}`);
table.addRow(`${game.homeTeam.name.default}`, `${game.homeTeam.score}`);
table.addRow(`${game.awayTeam.name.default} (${getTeamRecord(game.awayTeam, standings)})`, `${game.awayTeam.score}`);
table.addRow(`${game.homeTeam.name.default} (${getTeamRecord(game.homeTeam, standings)})`, `${game.homeTeam.score}`);
}
table.removeBorder();

Expand All @@ -170,13 +200,19 @@ module.exports = (robot) => {

const output = [];

const formatFallback = () => {
const date = moment(game.startTimeUTC).tz(team.time_zone).format('l');
const tableRows = table.getRows();
return `${date} - ${tableRows[0].join(' ')}, ${tableRows[1].join(' ')} (${gameStatus})`;
};

// Say it
switch (true) {
case /slack/.test(robot.adapterName):
msg.send({
attachments: [
{
fallback: `${moment(game.startTimeUTC).tz(team.time_zone).format('l')} - ${table.getRows()[0].join(' ')}, ${table.getRows()[1].join(' ')} (${gameStatus})`,
fallback: formatFallback(),
title_link: `https://www.nhl.com/gamecenter/${game.id}`,
author_name: 'NHL.com',
author_link: 'https://nhl.com',
Expand All @@ -201,6 +237,13 @@ module.exports = (robot) => {
msg.send(table.toString());
msg.send(`${gameStatus} - https://www.nhl.com/gamecenter/${game.id}`);
}
return cb;
})
.catch((err) => {
robot.logger.error(err);
return cb;
})
.finally(() => {
if (typeof cb === 'function') {
return cb();
}
Expand Down
13 changes: 11 additions & 2 deletions test/hockey-discord_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@ describe('hubot-hockey for discord', () => {
process.env.HUBOT_LOG_LEVEL = 'error';
nock.disableNetConnect();
room = helper.createRoom();

// Re-used in every call
nock('https://api-web.nhle.com')
.get(/\/v1\/standings\/\d{4}-\d{2}-\d{2}/)
.delay({
head: 100,
body: 200,
})
.replyWithFile(200, `${__dirname}/fixtures/api-web-nhle-standings.json`);
});

afterEach(() => {
Expand Down Expand Up @@ -71,8 +80,8 @@ describe('hubot-hockey for discord', () => {
'hubot',
'11/7/2023 - Scotiabank Saddledome; TV: BSSO (A) | SNW (H)\n'
+ '```\n'
+ ' Nashville Predators 2 \n'
+ ' Calgary Flames 3 \n'
+ ' Nashville Predators (5-6-0) 2 \n'
+ ' Calgary Flames (3-7-1) 3 \n'
+ '```\n'
+ '09:04 3rd - https://www.nhl.com/gamecenter/2023020186',
],
Expand Down
27 changes: 18 additions & 9 deletions test/hockey-slack_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@ describe('hubot-hockey for slack', () => {
process.env.HUBOT_LOG_LEVEL = 'error';
nock.disableNetConnect();
room = helper.createRoom();

// Re-used in every call
nock('https://api-web.nhle.com')
.get(/\/v1\/standings\/\d{4}-\d{2}-\d{2}/)
.delay({
head: 100,
body: 200,
})
.replyWithFile(200, `${__dirname}/fixtures/api-web-nhle-standings.json`);
});

afterEach(() => {
Expand Down Expand Up @@ -76,16 +85,16 @@ describe('hubot-hockey for slack', () => {
author_link: 'https://nhl.com',
author_name: 'NHL.com',
color: '#FFB81C',
fallback: '11/7/2023 - Nashville Predators 2, Calgary Flames 3 (09:04 3rd)',
fallback: '11/7/2023 - Nashville Predators (5-6-0) 2, Calgary Flames (3-7-1) 3 (09:04 3rd)',
footer: 'Scotiabank Saddledome; TV: BSSO (A) | SNW (H)',
mrkdwn_in: [
'text',
'pretext',
],
text:
'```\n'
+ ' Nashville Predators 2 \n'
+ ' Calgary Flames 3 \n'
+ ' Nashville Predators (5-6-0) 2 \n'
+ ' Calgary Flames (3-7-1) 3 \n'
+ '```',
title: '11/7/2023 - 09:04 3rd',
title_link: 'https://www.nhl.com/gamecenter/2023020186',
Expand Down Expand Up @@ -195,16 +204,16 @@ describe('hubot-hockey for slack', () => {
author_link: 'https://nhl.com',
author_name: 'NHL.com',
color: '#FFB81C',
fallback: '12/16/2023 - Washington Capitals 0, Nashville Predators 1 (07:21 1st Intermission)',
fallback: '12/16/2023 - Washington Capitals (5-4-1) 0, Nashville Predators (5-6-0) 1 (07:21 1st Intermission)',
footer: 'Bridgestone Arena; TV: NHLN (N) | BSSO (H) | MNMT (A)',
mrkdwn_in: [
'text',
'pretext',
],
text:
'```\n'
+ ' Washington Capitals 0 \n'
+ ' Nashville Predators 1 \n'
+ ' Washington Capitals (5-4-1) 0 \n'
+ ' Nashville Predators (5-6-0) 1 \n'
+ '```',
title: '12/16/2023 - 07:21 1st Intermission',
title_link: 'https://www.nhl.com/gamecenter/2023020468',
Expand Down Expand Up @@ -436,16 +445,16 @@ describe('hubot-hockey for slack', () => {
author_link: 'https://nhl.com',
author_name: 'NHL.com',
color: '#FFB81C',
fallback: '11/7/2023 - Nashville Predators 2, Calgary Flames 4 (Final)',
fallback: '11/7/2023 - Nashville Predators (5-6-0) 2, Calgary Flames (3-7-1) 4 (Final)',
footer: 'Scotiabank Saddledome',
mrkdwn_in: [
'text',
'pretext',
],
text:
'```\n'
+ ' Nashville Predators 2 \n'
+ ' Calgary Flames 4 \n'
+ ' Nashville Predators (5-6-0) 2 \n'
+ ' Calgary Flames (3-7-1) 4 \n'
+ '```',
title: '11/7/2023 - Final',
title_link: 'https://www.nhl.com/gamecenter/2023020186',
Expand Down
80 changes: 58 additions & 22 deletions test/hockey_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@ describe('hubot-hockey', () => {
nock.disableNetConnect();
room = helper.createRoom();
nock.disableNetConnect();

// Re-used in every call
nock('https://api-web.nhle.com')
.get(/\/v1\/standings\/\d{4}-\d{2}-\d{2}/)
.delay({
head: 100,
body: 200,
})
.replyWithFile(200, `${__dirname}/fixtures/api-web-nhle-standings.json`);
});

afterEach(() => {
Expand Down Expand Up @@ -68,8 +77,8 @@ describe('hubot-hockey', () => {
['hubot', '11/7/2023 - Scotiabank Saddledome; TV: BSSO (A) | SNW (H)'],
[
'hubot',
' Nashville Predators 2 \n'
+ ' Calgary Flames 3 ',
' Nashville Predators (5-6-0) 2 \n'
+ ' Calgary Flames (3-7-1) 3 ',
],
['hubot', '09:04 3rd - https://www.nhl.com/gamecenter/2023020186'],
['hubot', 'Sports Club Stats: 77.7% to Make Playoffs'],
Expand Down Expand Up @@ -122,8 +131,8 @@ describe('hubot-hockey', () => {
['hubot', '12/16/2023 - Bridgestone Arena; TV: NHLN (N) | BSSO (H) | MNMT (A)'],
[
'hubot',
' Washington Capitals 0 \n'
+ ' Nashville Predators 1 ',
' Washington Capitals (5-4-1) 0 \n'
+ ' Nashville Predators (5-6-0) 1 ',
],
['hubot', '07:21 1st Intermission - https://www.nhl.com/gamecenter/2023020468'],
['hubot', 'Sports Club Stats: 77.7% to Make Playoffs'],
Expand Down Expand Up @@ -230,8 +239,8 @@ describe('hubot-hockey', () => {
['hubot', '11/7/2023 - Scotiabank Saddledome'],
[
'hubot',
' Nashville Predators 2 \n'
+ ' Calgary Flames 4 ',
' Nashville Predators (5-6-0) 2 \n'
+ ' Calgary Flames (3-7-1) 4 ',
],
['hubot', 'Final - https://www.nhl.com/gamecenter/2023020186'],
['hubot', 'Sports Club Stats: 77.7% to Make Playoffs'],
Expand Down Expand Up @@ -338,8 +347,8 @@ describe('hubot-hockey', () => {
['hubot', '12/15/2023 - PNC Arena; TV: ESPN+ (N) | HULU (N)'],
[
'hubot',
' Nashville Predators 6 \n'
+ ' Carolina Hurricanes 5 ',
' Nashville Predators (5-6-0) 6 \n'
+ ' Carolina Hurricanes (8-5-0) 5 ',
],
['hubot', '04:25 OT - https://www.nhl.com/gamecenter/2023020455'],
['hubot', 'Sports Club Stats: 77.7% to Make Playoffs'],
Expand Down Expand Up @@ -447,8 +456,8 @@ describe('hubot-hockey', () => {
['hubot', '6/15/2024 - Rogers Place; TV: ABC (N) | ESPN+ (N) | SN (N) | CBC (N) | TVAS (N)'],
[
'hubot',
' Florida Panthers 1 \n'
+ ' Edmonton Oilers 6 ',
' Florida Panthers (6-4-1) 1 \n'
+ ' Edmonton Oilers (2-8-1) 6 ',
],
[
'hubot',
Expand Down Expand Up @@ -502,8 +511,8 @@ describe('hubot-hockey', () => {
['hubot', '4/23/2024 - Rogers Arena'],
[
'hubot',
' Nashville Predators 4 \n'
+ ' Vancouver Canucks 1 ',
' Nashville Predators (5-6-0) 4 \n'
+ ' Vancouver Canucks (9-2-1) 1 ',
],
[
'hubot',
Expand Down Expand Up @@ -557,8 +566,8 @@ describe('hubot-hockey', () => {
['hubot', '5/3/2024 - Bridgestone Arena'],
[
'hubot',
' Vancouver Canucks 1 \n'
+ ' Nashville Predators 0 ',
' Vancouver Canucks (9-2-1) 1 \n'
+ ' Nashville Predators (5-6-0) 0 ',
],
[
'hubot',
Expand Down Expand Up @@ -612,8 +621,8 @@ describe('hubot-hockey', () => {
['hubot', '11/22/2023 - Bridgestone Arena'],
[
'hubot',
' Calgary Flames 2 \n'
+ ' Nashville Predators 4 ',
' Calgary Flames (3-7-1) 2 \n'
+ ' Nashville Predators (5-6-0) 4 ',
],
['hubot', 'Final - https://www.nhl.com/gamecenter/2023020288'],
['hubot', 'Sports Club Stats: 77.7% to Make Playoffs'],
Expand Down Expand Up @@ -666,8 +675,8 @@ describe('hubot-hockey', () => {
['hubot', '12/15/2023 - UBS Arena'],
[
'hubot',
' Boston Bruins 5 \n'
+ ' New York Islanders 4 ',
' Boston Bruins (10-1-1) 5 \n'
+ ' New York Islanders (5-3-3) 4 ',
],
['hubot', 'Final/SO - https://www.nhl.com/gamecenter/2023020457'],
['hubot', 'Sports Club Stats: 100.0% to Make Playoffs'],
Expand Down Expand Up @@ -772,8 +781,8 @@ describe('hubot-hockey', () => {
['hubot', '12/15/2023 - UBS Arena'],
[
'hubot',
' Boston Bruins 5 \n'
+ ' New York Islanders 4 ',
' Boston Bruins (10-1-1) 5 \n'
+ ' New York Islanders (5-3-3) 4 ',
],
['hubot', 'Final/SO - https://www.nhl.com/gamecenter/2023020457'],
]);
Expand All @@ -785,6 +794,24 @@ describe('hubot-hockey', () => {
500,
);
});
});

describe('hubot-hockey league standings', () => {
let room = null;

beforeEach(() => {
process.env.HUBOT_LOG_LEVEL = 'error';
nock.disableNetConnect();
room = helper.createRoom();
nock.disableNetConnect();
});

afterEach(() => {
delete process.env.HUBOT_LOG_LEVEL;
Date.now = originalDateNow;
nock.cleanAll();
room.destroy();
});

it('responds with division leader standings', (done) => {
Date.now = () => Date.parse('Tues Nov 7 22:36:00 CST 2023');
Expand Down Expand Up @@ -1127,6 +1154,15 @@ describe('hubot-hockey HUBOT_HOCKEY_HIDE_ODDS=true', () => {
nock.disableNetConnect();
room = helper.createRoom();
nock.disableNetConnect();

// Re-used in every call
nock('https://api-web.nhle.com')
.get(/\/v1\/standings\/\d{4}-\d{2}-\d{2}/)
.delay({
head: 100,
body: 200,
})
.replyWithFile(200, `${__dirname}/fixtures/api-web-nhle-standings.json`);
});

afterEach(() => {
Expand Down Expand Up @@ -1157,8 +1193,8 @@ describe('hubot-hockey HUBOT_HOCKEY_HIDE_ODDS=true', () => {
['hubot', '11/7/2023 - Scotiabank Saddledome; TV: BSSO (A) | SNW (H)'],
[
'hubot',
' Nashville Predators 2 \n'
+ ' Calgary Flames 3 ',
' Nashville Predators (5-6-0) 2 \n'
+ ' Calgary Flames (3-7-1) 3 ',
],
['hubot', '09:04 3rd - https://www.nhl.com/gamecenter/2023020186'],
]);
Expand Down

0 comments on commit 66cb606

Please sign in to comment.