Skip to content

Commit

Permalink
Fix playoff series display in new API
Browse files Browse the repository at this point in the history
  • Loading branch information
parnic committed Apr 22, 2024
1 parent e59bb7b commit 626219a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 26 deletions.
35 changes: 15 additions & 20 deletions node_helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const Log = require('logger');
*/
const NodeHelper = require('node_helper');

const BASE_PLAYOFF_URL = 'https://statsapi.web.nhl.com/api/v1/tournaments/playoffs?expand=round.series';
const BASE_PLAYOFF_URL = 'https://api-web.nhle.com/v1/playoff-series/carousel';

/**
* Derived team details of a game from API endpoint for easier usage.
Expand Down Expand Up @@ -253,16 +253,18 @@ module.exports = NodeHelper.create({
* @returns {object} Raw playoff data from API endpoint.
*/
async fetchPlayoffs() {
// TODO: Find playoff endpoints in new API
const response = await fetch(BASE_PLAYOFF_URL);
const year = new Date().getFullYear();
const lastYear = year - 1;
const url = `${BASE_PLAYOFF_URL}/${lastYear}${year}`;
const response = await fetch(url);

if (!response.ok) {
Log.error(`Fetching NHL playoffs failed: ${response.status} ${response.statusText}.`);
Log.error(`Fetching NHL playoffs from ${url} failed: ${response.status} ${response.statusText}.`);
return;
}

const playoffs = await response.json();
playoffs.rounds.sort((a, b) => a.number <= b.number ? 1 : -1);
playoffs.rounds.sort((a, b) => a.roundNumber <= b.roundNumber ? 1 : -1);

return playoffs;
},
Expand Down Expand Up @@ -398,18 +400,11 @@ module.exports = NodeHelper.create({
*
* @param {object} rawTeam - Raw team information.
*
* @param {object} game - Raw game information.
*
* @returns {Game} Parsed game information.
*/
parsePlayoffTeam(rawTeam, game) {
parsePlayoffTeam(rawTeam) {
const team = this.parseTeam(rawTeam);

if (game?.seriesStatus?.topSeedTeamId === team.id) {
team.score = game?.seriesStatus?.topSeedWins;
} else {
team.score = game?.seriesStatus?.bottomSeedWins;
}
team.score = rawTeam.wins;

return team;
},
Expand Down Expand Up @@ -465,16 +460,16 @@ module.exports = NodeHelper.create({
* @returns {Series} Parsed series information.
*/
parseSeries(series = {}) {
if (!series.matchupTeams || series.matchupTeams.length === 0) {
if (!series.bottomSeed || !series.topSeed) {
return null;
}

return {
number: series.number,
round: series.round.number,
letter: series.seriesLetter,
round: series.roundNumber,
teams: {
home: this.parsePlayoffTeam(series.matchupTeams, undefined), // TODO: Don't pass undefined to retrieve the correct score
away: this.parsePlayoffTeam(series.matchupTeams, undefined), // TODO: Don't pass undefined to retrieve the correct score
bottomSeed: this.parsePlayoffTeam(series.bottomSeed),
topSeed: this.parsePlayoffTeam(series.topSeed),
}
}
},
Expand Down Expand Up @@ -533,7 +528,7 @@ module.exports = NodeHelper.create({
if (season.mode === 3 || games.length === 0) {

const playoffData = await this.fetchPlayoffs();
const playoffSeries = this.computePlayoffDetails(playoffData).filter(s => s.round >= playoffData.defaultRound);
const playoffSeries = this.computePlayoffDetails(playoffData).filter(s => s.roundNumber != playoffData.currentRound);

Check failure on line 531 in node_helper.js

View workflow job for this annotation

GitHub Actions / lint

Expected '!==' and instead saw '!='

this.sendSocketNotification('PLAYOFFS', playoffSeries);
}
Expand Down
12 changes: 6 additions & 6 deletions templates/MMM-NHL.njk
Original file line number Diff line number Diff line change
Expand Up @@ -80,27 +80,27 @@
<tr>
{% if config.showNames %}
<td class="align-right">
{{ series.teams.home.short }}
{{ series.teams.topSeed.short }}
</td>
{% endif %}
{% if config.showLogos %}
<td>
<img class="icon {{ "no-color" if not config.colored }}"
src="https://assets.nhle.com/logos/nhl/svg/{{ series.teams.home.short }}_dark.svg"/>
src="https://assets.nhle.com/logos/nhl/svg/{{ series.teams.topSeed.short }}_dark.svg"/>
</td>
{% endif %}
<td>{{ series.teams.home.score }}</td>
<td>{{ series.teams.topSeed.score }}</td>
<td>:</td>
<td>{{ series.teams.away.score }}</td>
<td>{{ series.teams.bottomSeed.score }}</td>
{% if config.showLogos %}
<td>
<img class="icon {{ "no-color" if not config.colored }}"
src="https://assets.nhle.com/logos/nhl/svg/{{ series.teams.away.short }}_dark.svg"/>
src="https://assets.nhle.com/logos/nhl/svg/{{ series.teams.bottomSeed.short }}_dark.svg"/>
</td>
{% endif %}
{% if config.showNames %}
<td class="align-left">
{{ series.teams.away.short }}
{{ series.teams.bottomSeed.short }}
</td>
{% endif %}
</tr>
Expand Down

0 comments on commit 626219a

Please sign in to comment.