Skip to content

Commit

Permalink
Merge pull request #17 from fewieden/develop
Browse files Browse the repository at this point in the history
2.1.0
  • Loading branch information
fewieden authored Jan 27, 2021
2 parents 3bda579 + c2cc8c9 commit 0495cad
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 8 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
# MMM-NHL Changelog

## [2.1.0]

### Fixed

* Date queries are now set based on timezone `America/Toronto`.

### Added

* Config option `rollOver`

## [2.0.0]

### Added
Expand Down
12 changes: 10 additions & 2 deletions MMM-NHL.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ 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} rollOverGames - Flag to show today's games and previous/next day based on game status.
*/
defaults: {
colored: false,
Expand All @@ -94,7 +95,8 @@ Module.register('MMM-NHL', {
daysInPast: 1,
daysAhead: 7,
showNames: true,
showLogos: true
showLogos: true,
rollOver: false
},

/**
Expand Down Expand Up @@ -155,14 +157,20 @@ Module.register('MMM-NHL', {

/**
* @function start
* @description Adds nunjuck filters and sends config to node_helper.
* @description Adds nunjuck filters, overrides day config options if rollOver and sends config to node_helper.
* @override
*
* @returns {void}
*/
start() {
Log.info(`Starting module: ${this.name}`);
this.addFilters();

if (this.config.rollOver) {
this.config.daysInPast = 1;
this.config.daysAhead = 1;
}

this.sendSocketNotification('CONFIG', {config: this.config});
},

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? |
| `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
46 changes: 41 additions & 5 deletions node_helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ const BASE_URL = 'https://statsapi.web.nhl.com/api/v1';
* @typedef {object} Game
* @property {number} id - Game identifier.
* @property {string} timestamp - Start date of the game in UTC timezone.
* @property {string} gameDay - Game day in format YYYY-MM-DD in north american timezone.
* @property {object} status - Contains information about the game status.
* @property {string} status.abstract - Abstract game status e.g. Preview, Live or Final.
* @property {string} status.detailed - More detailed version of the abstract game status.
Expand Down Expand Up @@ -136,9 +137,12 @@ module.exports = NodeHelper.create({
async fetchSchedule() {
const date = new Date();
date.setDate(date.getDate() - this.config.daysInPast);
const startDate = date.toISOString().slice(0, 10);
const startDate = new Intl.DateTimeFormat('fr-ca', {timeZone: 'America/Toronto'})
.format(date);

date.setDate(date.getDate() + this.config.daysInPast + this.config.daysAhead);
const endDate = date.toISOString().slice(0, 10);
const endDate = new Intl.DateTimeFormat('fr-ca', {timeZone: 'America/Toronto'})
.format(date);

const query = qs.stringify({startDate, endDate, expand: 'schedule.linescore'});
const url = `${BASE_URL}/schedule?${query}`;
Expand All @@ -151,7 +155,7 @@ module.exports = NodeHelper.create({

const {dates} = await response.json();

return dates.map(date => date.games).flat();
return dates.map(({date, games}) => games.map(game => ({...game, gameDay: date}))).flat();
},

/**
Expand All @@ -174,6 +178,35 @@ module.exports = NodeHelper.create({
return focus.includes(homeTeam) || focus.includes(awayTeam);
},

/**
* @function filterRollOverGames
* @description Helper function to filter games based on rollOver config option.
*
* @param {Game[]} games - List of all games.
*
* @returns {Game[]} List of filtered games.
*/
filterRollOverGames(games) {
if (!this.config.rollOver) {
return games;
}

const date = new Intl.DateTimeFormat('fr-ca', {timeZone: 'America/Toronto'})
.format(new Date());

const yesterday = games.filter(game => game.gameDay < date);
const today = games.filter(game => game.gameDay === date);
const tomorrow = games.filter(game => game.gameDay > date);

const ongoingStates = ['Final', 'Live'];

if (today.some(game => ongoingStates.includes(game.status.abstract))) {
return [...today, ...tomorrow];
}

return [...yesterday, ...today];
},

/**
* @function computeSeasonDetails
* @description Computes current season details (year and mode) from list of games.
Expand Down Expand Up @@ -232,6 +265,7 @@ module.exports = NodeHelper.create({
return {
id: game.gamePk,
timestamp: game.gameDate,
gameDay: game.gameDay,
status: {
abstract: game.status.abstractGameState,
detailed: game.status.detailedState
Expand Down Expand Up @@ -293,8 +327,10 @@ module.exports = NodeHelper.create({

const games = focusSchedule.map(this.parseGame.bind(this));

this.setNextandLiveGames(games);
this.sendSocketNotification('SCHEDULE', {games, season});
const rollOverGames = this.filterRollOverGames(games);

this.setNextandLiveGames(rollOverGames);
this.sendSocketNotification('SCHEDULE', {games: rollOverGames, season});
},

/**
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.0.0",
"version": "2.1.0",
"description": "National Hockey League Module for MagicMirror2",
"scripts": {
"docs": "jsdoc -c jsdoc.json .",
Expand Down

0 comments on commit 0495cad

Please sign in to comment.