Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix playoff series display in new API #55

Open
wants to merge 6 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

### Fixed

* Updated module to display playoff series, if configured to, with the new NHL API.

### Added

### Changed
Expand Down
46 changes: 26 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 @@ -178,6 +178,9 @@ module.exports = NodeHelper.create({
* @description Helper function to retrieve remaining game time.
* @async
*
* @param {string} game - Game info
* @param {string} scores - Scores
*
* @returns {string?} Remaining game time.
*/
getRemainingGameTime(game, scores) {
Expand All @@ -198,6 +201,8 @@ module.exports = NodeHelper.create({
* @description Hydrates remaining time on the games in the schedule from the scores API endpoint.
* @async
*
* @param {string} schedule - Schedule data
*
* @returns {object[]} Raw games from API endpoint including remaining time.
*/
async hydrateRemainingTime(schedule) {
Expand Down Expand Up @@ -253,16 +258,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 +405,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 +465,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 +533,13 @@ 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);
let currentRound = playoffData ? playoffData.currentRound : 0;
if (playoffData && playoffData.rounds) {
const activeRounds = playoffData.rounds.filter(round => round.series.some(series => series.bottomSeed.wins < series.neededToWin && series.topSeed.wins < series.neededToWin));
const activeRoundNumbers = activeRounds.map(round => round.roundNumber);
currentRound = Math.min(...activeRoundNumbers);
}
const playoffSeries = this.computePlayoffDetails(playoffData).filter(s => s.round >= currentRound);

this.sendSocketNotification('PLAYOFFS', playoffSeries);
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mmm-nhl",
"version": "2.3.1",
"version": "2.3.2",
"description": "National Hockey League Module for MagicMirror²",
"scripts": {
"docs": "jsdoc -c jsdoc.json .",
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
Loading