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

Add Playoff Series and fixe for all-star logos #30

Merged
merged 25 commits into from
Feb 8, 2022
Merged
Show file tree
Hide file tree
Changes from 23 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
c7deeb3
Fixed Vegas icon. Added PPD support. Added config for in game refres…
dannoh Jan 15, 2021
b2b58da
Reverted changes to translations
dannoh Jan 15, 2021
6d01a82
Added condensed
dannoh Jan 15, 2021
5f689a0
Fixed condensed key fail over
dannoh Jan 16, 2021
f6b3647
Updated changelog and readme
dannoh Jan 16, 2021
595ad21
Merge pull request #1 from dannoh/UI_Tweaks
dannoh Jan 16, 2021
66799db
Adding screenshots to readme
dannoh Jan 16, 2021
caf5892
Cropped images
dannoh Jan 16, 2021
10650d5
Cropped images a bit more
dannoh Jan 16, 2021
6301d84
Cropped images a bit more
dannoh Jan 16, 2021
62e8417
Merge pull request #2 from dannoh/UI_Tweaks
dannoh Jan 16, 2021
3bda579
Merge pull request #12 from fewieden/develop
fewieden Jan 25, 2021
0495cad
Merge pull request #17 from fewieden/develop
fewieden Jan 27, 2021
59bdc2c
Merge remote-tracking branch 'upstream/master'
dannoh Jan 30, 2021
79d79f7
Added playoff series
dannoh Jun 16, 2021
b509a90
Cleanup and updated README
dannoh Jun 16, 2021
faf4981
Fixed up a couple of lint errors
dannoh Jun 16, 2021
ad9eeef
Merge pull request #4 from dannoh/master
dannoh Feb 5, 2022
5a199e8
Merge pull request #5 from dannoh/PlayoffsV2
dannoh Feb 5, 2022
b672ead
Changed the logo urls so they work with all-star teams and fall back …
dannoh Feb 5, 2022
e3f7409
Merge remote-tracking branch 'origin/master' into LogoUrlChange
dannoh Feb 5, 2022
3eb6603
Updated changelog for 2.2.1
dannoh Feb 5, 2022
0d1cbd2
Merge pull request #6 from dannoh/LogoUrlChange
dannoh Feb 5, 2022
c98d4c3
Code cleanup
dannoh Feb 8, 2022
8c61278
Merge remote-tracking branch 'origin/LogoUrlChange'
dannoh Feb 8, 2022
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
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
# MMM-NHL Changelog

## [2.2.1]

### Fixed

* Changed Logo Urls to support all teams (specifically all-star teams)
* Added support for teams with no short name when showNames is true

## [2.2.0]

### Added

* Playoff series display
dannoh marked this conversation as resolved.
Show resolved Hide resolved

## [2.1.0]

### Fixed
Expand Down
17 changes: 16 additions & 1 deletion MMM-NHL.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,11 @@ Module.register('MMM-NHL', {
'3rd': '3RD_PERIOD',
OT: 'OVER_TIME',
SO: 'SHOOTOUT',
SHOOTOUT: 'SHOOTOUT',
FINAL: 'FINAL',
'FINAL OT': 'FINAL_OVERTIME',
'FINAL SO': 'FINAL_SHOOTOUT'
'FINAL SO': 'FINAL_SHOOTOUT',
PPD: 'PPD'
},

/**
Expand All @@ -58,6 +60,12 @@ Module.register('MMM-NHL', {
* @member {Game[]} games - List of all games matching focus and timespan config options.
*/
games: [],

/**
* @member {Series[]} playoffSeries - List of all current playoff series.
*/
playoffSeries: [],

/**
* @member {SeasonDetails} season - Current season details e.g. year and mode.
*/
Expand All @@ -83,11 +91,13 @@ Module.register('MMM-NHL', {
* @property {number} daysAhead - Amount of days a match should be displayed before it starts.
* @property {boolean} showNames - Flag to show team names.
* @property {boolean} showLogos - Flag to show club logos.
* @property {boolean} showPlayoffSeries - Flag to show playoff series status during playoffs.
* @property {boolean} rollOverGames - Flag to show today's games and previous/next day based on game status.
*/
defaults: {
colored: false,
focus_on: false,
condensed: false,
dannoh marked this conversation as resolved.
Show resolved Hide resolved
matches: 6,
rotateInterval: 20 * 1000,
reloadInterval: 30 * 60 * 1000,
Expand All @@ -96,6 +106,7 @@ Module.register('MMM-NHL', {
daysAhead: 7,
showNames: true,
showLogos: true,
showPlayoffSeries: true,
rollOver: false
},

Expand Down Expand Up @@ -149,6 +160,7 @@ Module.register('MMM-NHL', {
modes: this.modes,
season: this.season,
games: this.games,
playoffSeries: this.playoffSeries,
rotateIndex: this.rotateIndex,
maxGames: Math.min(this.games.length, this.rotateIndex + this.config.matches),
config: this.config
Expand Down Expand Up @@ -188,6 +200,9 @@ Module.register('MMM-NHL', {
this.games = payload.games;
this.season = payload.season;
this.setRotateInterval();
} else if (notification === 'PLAYOFFS') {
this.playoffSeries = payload;
this.updateDom(300);
}
},

Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ National Hockey League Module for MagicMirror<sup>2</sup>
| `liveReloadInterval` | `60000 (1 min)` | How often should the data be fetched during a live game. |
| `showNames` | `true` | Should team names be displayed? |
| `showLogos` | `true` | Should team logos be displayed? |
| `showPlayoffSeries` | `true` | Should playoff series information (if in playoffs)? |
dannoh marked this conversation as resolved.
Show resolved Hide resolved
| `rollOver` | `false` | Displays today's games and based on game status also yesterdays games or tomorrows games. Automatically overrides `daysInPast` and `daysAhead` to 1. |

## Global config
Expand Down
Binary file added images/screenshot1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/screenshot2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
113 changes: 109 additions & 4 deletions node_helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const qs = require('querystring');
const NodeHelper = require('node_helper');

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

/**
* Derived team details of a game from API endpoint for easier usage.
Expand Down Expand Up @@ -57,6 +58,16 @@ const BASE_URL = 'https://statsapi.web.nhl.com/api/v1';
* @property {string} live.timeRemaining - Remaining time of the current period in format mm:ss.
*/

/**
* Derived game details from API endpoint for easier usage.
*
* @typedef {object} Series
* @property {number} number - Game identifier.
* @property {number} round - Start date of the game in UTC timezone.
* @property {Team} teams.away - Contains information about the away team.
* @property {Team} teams.home - Contains information about the home team.
*/

/**
* Derived season details from API endpoint for easier usage.
*
Expand Down Expand Up @@ -158,6 +169,26 @@ module.exports = NodeHelper.create({
return dates.map(({date, games}) => games.map(game => ({...game, gameDay: date}))).flat();
},

/**
* @function fetchPlayoffs
* @description Retrieves playoff data from the API.
* @async
*
* @returns {object} Raw playoff data from API endpoint.
*/
async fetchPlayoffs() {
const response = await fetch(BASE_PLAYOFF_URL);
fewieden marked this conversation as resolved.
Show resolved Hide resolved

if (!response.ok) {
console.error(`Fetching NHL playoffs failed: ${response.status} ${response.statusText}. Url: ${BASE_PLAYOFF_URL}`);
dannoh marked this conversation as resolved.
Show resolved Hide resolved
return;
}

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

/**
* @function filterGameByFocus
* @description Helper function to filter games based on config option.
Expand Down Expand Up @@ -235,6 +266,30 @@ module.exports = NodeHelper.create({
};
},

/**
* @function computePlayoffDetails
* @description Computes current playoff details from list of series.
*
* @param {object} playoffData - List of raw series from API endpoint.
*
* @returns {Series[]} Current season details.
*/
computePlayoffDetails(playoffData) {
if (!playoffData || !playoffData.rounds) {
return [];
}
const series = [];
playoffData.rounds.forEach(r => {
r.series.forEach(s => {
const parsed = this.parseSeries(s);
if (parsed) {
series.push(parsed);
}
});
});
return series;
},

/**
* @function parseTeam
* @description Transforms raw team information for easier usage.
Expand All @@ -245,14 +300,35 @@ module.exports = NodeHelper.create({
* @returns {Team} Parsed team information.
*/
parseTeam(teams = {}, type) {
const team = teams[type];
if (!team) {
console.error({NoTeamFond: teams});
dannoh marked this conversation as resolved.
Show resolved Hide resolved
return {};
}
return {
id: teams[type].team.id,
name: teams[type].team.name,
short: this.teamMapping[teams[type].team.id],
score: teams[type].score
id: team.team.id,
name: team.team.name,
short: this.teamMapping[team.team.id],
score: team.score
};
},

/**
* @function parsePlayoffTeam
* @description Transforms raw game information for easier usage.
*
* @param {object} teamData - Raw game information.
*
* @param {number} index - Which index of teamData to operate on.
*
* @returns {Game} Parsed game information.
*/
parsePlayoffTeam(teamData = {}, index) {
const team = this.parseTeam(teamData, index);
team.score = teamData[index].seriesRecord.wins;
return team;
},

/**
* @function parseGame
* @description Transforms raw game information for easier usage.
Expand Down Expand Up @@ -281,6 +357,28 @@ module.exports = NodeHelper.create({
};
},

/**
* @function parseSeries
* @description Transforms raw series information for easier usage.
*
* @param {object} series - Raw series information.
*
* @returns {Series} Parsed series information.
*/
parseSeries(series = {}) {
if (!series.matchupTeams || series.matchupTeams.length === 0) {
return null;
}
return {
number: series.number,
round: series.round.number,
teams: {
home: this.parsePlayoffTeam(series.matchupTeams, 0),
away: this.parsePlayoffTeam(series.matchupTeams, 1),
}
}
},

/**
* @function setNextandLiveGames
* @description Sets the next scheduled and live games from a list of games.
Expand Down Expand Up @@ -331,6 +429,13 @@ module.exports = NodeHelper.create({

this.setNextandLiveGames(rollOverGames);
this.sendSocketNotification('SCHEDULE', {games: rollOverGames, season});
if (season.mode === 'P' || games.length === 0) {

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

this.sendSocketNotification('PLAYOFFS', playoffSeries);
}
},

/**
Expand Down
2 changes: 1 addition & 1 deletion 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
@@ -1,6 +1,6 @@
{
"name": "mmm-nhl",
"version": "2.1.0",
"version": "2.2.1",
"description": "National Hockey League Module for MagicMirror2",
"scripts": {
"docs": "jsdoc -c jsdoc.json .",
Expand Down
55 changes: 51 additions & 4 deletions templates/MMM-NHL.njk
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@
</td>
{% if config.showNames %}
<td class="align-right">
{{ games[index].teams.home.short }}
{{ games[index].teams.home.short if games[index].teams.home.short else games[index].teams.home.name }}
</td>
{% endif %}
{% if config.showLogos %}
<td>
<img class="icon {{ "no-color" if not config.colored }}"
src="https://assets.nhle.com/logos/nhl/svg/{{ games[index].teams.home.short }}_dark.svg"/>
src="https://www-league.nhlstatic.com/images/logos/teams-20212022-dark/{{games[index].teams.home.id}}.svg"/>
</td>
{% endif %}
<td>{{ games[index].teams.home.score }}</td>
Expand All @@ -59,16 +59,63 @@
{% if config.showLogos %}
<td>
<img class="icon {{ "no-color" if not config.colored }}"
src="https://assets.nhle.com/logos/nhl/svg/{{ games[index].teams.away.short }}_dark.svg"/>
src="https://www-league.nhlstatic.com/images/logos/teams-20212022-dark/{{games[index].teams.away.id}}.svg"/>
</td>
{% endif %}
{% if config.showNames %}
<td class="align-left">
{{ games[index].teams.away.short }}
{{ games[index].teams.away.short if games[index].teams.away.short else games[index].teams.away.name }}
</td>
{% endif %}
</tr>
{% endfor %}
</tbody>
</table>
{% if config.showPlayoffSeries and playoffSeries and playoffSeries.length > 0 %}
<header style="margin-top: 10px">Series</header>
dannoh marked this conversation as resolved.
Show resolved Hide resolved
<table class="small table">
<thead>
dannoh marked this conversation as resolved.
Show resolved Hide resolved
<tr>
<th colspan="{{ 1 + (1 if config.showNames else 0) + (1 if config.showLogos else 0) }}">

</th>
<th></th>
<th colspan="{{ 1 + (1 if config.showNames else 0) + (1 if config.showLogos else 0) }}">

</th>
</tr>
</thead>
<tbody>
{% for series in playoffSeries %}
<tr>
{% if config.showNames %}
<td class="align-right">
{{ series.teams.home.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"/>
dannoh marked this conversation as resolved.
Show resolved Hide resolved
</td>
{% endif %}
<td>{{ series.teams.home.score }}</td>
<td>:</td>
<td>{{ series.teams.away.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"/>
</td>
{% endif %}
{% if config.showNames %}
<td class="align-left">
{{ series.teams.away.short }}
</td>
{% endif %}
</tr>
{% endfor %}
</tbody>
</table>
{% endif %}
{% endif %}