Skip to content

Commit

Permalink
Merge pull request #78 from stephenyeargin/nhl-standings
Browse files Browse the repository at this point in the history
Show NHL division standings
  • Loading branch information
stephenyeargin authored Oct 16, 2023
2 parents 73520ce + cd3f6b8 commit 48f349d
Show file tree
Hide file tree
Showing 8 changed files with 3,411 additions and 22 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ See full instructions [here](https://github.com/github/hubot/blob/master/docs/sc
## Commands

- `hubot <team>` - Show your team's current playoff odds and next game
- `hubot nhl [division]` - Show division leaders or division standings
38 changes: 23 additions & 15 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"moment-timezone": "^0.5.43"
},
"peerDependencies": {
"hubot": "^3 || ^4"
"hubot": ">=2 <10 || 0.0.0-development"
},
"devDependencies": {
"chai": "^4.3.7",
Expand Down
77 changes: 71 additions & 6 deletions src/hockey.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#
# Commands:
# hubot <team or city> - Get the lastest playoff odds from MoneyPuck.com
# hubot nhl [division] - Show division leaders or division standings
#
# Author:
# stephenyeargin
Expand Down Expand Up @@ -75,8 +76,8 @@ module.exports = (robot) ->
howToWatch = howToWatch + "; TV: " + networks.join(' | ')

# Say it
switch robot.adapterName
when 'slack'
switch true
when /slack/.test(robot.adapterName)
msg.send {
attachments: [
{
Expand All @@ -93,7 +94,7 @@ module.exports = (robot) ->
}
]
}
when 'discord'
when /discord/.test(robot.adapterName)
output = []
output.push "#{moment(date).format('l')} - #{howToWatch}"
output.push "```" + table.toString() + "```"
Expand Down Expand Up @@ -165,8 +166,8 @@ module.exports = (robot) ->
fallback = "Odds to " + oddsParts.join(" / ")

# Say it
switch robot.adapterName
when 'slack'
switch true
when /slack/.test(robot.adapterName)
msg.send {
attachments: [
{
Expand All @@ -181,7 +182,7 @@ module.exports = (robot) ->
}
]
}
when 'discord'
when /discord/.test(robot.adapterName)
msg.send "__**MoneyPuck.com**__\n" + discordFields.join("\n")
else
msg.send fallback
Expand All @@ -194,3 +195,67 @@ module.exports = (robot) ->
# Loop through teams and create multiple listeners
for team_item in hockey_teams
registerDefaultListener team_item

# NHL Standings
robot.respond /nhl\s?(atlantic|metro|metropolitan|pacific|central)?\s?(?:standings)?/i, (msg) ->
division = msg.match[1] || ''
if !division
tableTitle = 'Division Leaders'
else
tableTitle = division.toLowerCase().charAt(0).toUpperCase() + ' Standings'
table = new AsciiTable(tableTitle)
table.setHeading([
'Team',
'GP',
'W',
'L',
'OT',
'PTS',
'L10'
])
msg.http('https://statsapi.web.nhl.com/api/v1/standings')
.query({
date: moment().tz('America/Los_Angeles').format('YYYY-MM-DD'),
expand: 'standings.record'
})
.get() (err, res, body) ->
# Catch errors
if err || res.statusCode != 200
msg.send "Cannot get standings right now."
return

json = JSON.parse(body)

# No division selected
if tableTitle == 'Division Leaders'
for d in json.records
lastTen = d.teamRecords[0].records.overallRecords.find((r) => r.type == 'lastTen')
table.addRow [
d.teamRecords[0].team.name,
d.teamRecords[0].gamesPlayed,
d.teamRecords[0].leagueRecord.wins,
d.teamRecords[0].leagueRecord.losses,
d.teamRecords[0].leagueRecord.ot,
d.teamRecords[0].points,
"#{lastTen.wins}-#{lastTen.losses}-#{lastTen.ot}",
]
# Division selected
else
teams = json.records.find((d) => d.division.name.toUpperCase() == division.toUpperCase() || d.division.nameShort.toUpperCase() == division.toUpperCase())
for t in teams.teamRecords
lastTen = t.records.overallRecords.find((r) => r.type == 'lastTen')
table.addRow [
t.team.name,
t.gamesPlayed,
t.leagueRecord.wins,
t.leagueRecord.losses,
t.leagueRecord.ot,
t.points,
"#{lastTen.wins}-#{lastTen.losses}-#{lastTen.ot}",
]

# Format based on adapter
if /(slack|discord)/.test(robot.adapterName)
msg.send "```" + table.toString() + "```"
else
msg.send table.toString()
Loading

0 comments on commit 48f349d

Please sign in to comment.