Skip to content
This repository has been archived by the owner on Jun 3, 2020. It is now read-only.

Commit

Permalink
Merge pull request #54 from jaebradley/command-line-output-refactor
Browse files Browse the repository at this point in the history
Command line output refactor
  • Loading branch information
jaebradley committed May 17, 2016
2 parents 5a05670 + a21034c commit e147385
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 63 deletions.
3 changes: 2 additions & 1 deletion src/cli/commands/GamesCommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import Constants from '../../constants/Constants.js';
export default class GamesCommand {
constructor() {
this.userTimezone = jstz.determine().name();
this.commandLineOutputClient = new CommandLineOutputClient();
}

generateDateRange(timeRangeOption) {
Expand Down Expand Up @@ -50,6 +51,6 @@ export default class GamesCommand {

run(timeRangeOption) {
let dateRange = this.generateDateRange(timeRangeOption);
CommandLineOutputClient.outputGamesForDateRange(dateRange.startDate, dateRange.endDate);
this.commandLineOutputClient.outputGamesForDateRange(dateRange.startDate, dateRange.endDate);
}
};
102 changes: 51 additions & 51 deletions src/cli/output/CommandLineOutputClient.js
Original file line number Diff line number Diff line change
@@ -1,71 +1,71 @@
const Table = require('cli-table2');
const moment = require('moment-timezone');
import Table from 'cli-table2';
import moment from 'moment-timezone';

import NbaDataClient from '../../data/NbaDataClient';
import StartedGameTableCreator from '../../tables/StartedGameTableCreator';
import UpcomingGameTableCreator from '../../tables/UpcomingGameTableCreator';
import PlayByPlayTableCreator from '../../tables/PlayByPlayTableCreator';
import BoxScoreTableCreator from '../../tables/BoxScoreTableCreator';

const nbaDataClient = new NbaDataClient();
const playByPlayTableCreator = new PlayByPlayTableCreator();
const upcomingGameTableCreator = new UpcomingGameTableCreator();
const boxScoreTableCreator = new BoxScoreTableCreator();
const startedGameTableCreator = new StartedGameTableCreator();
export default class CommandLineOutputClient {
constructor() {
this.nbaDataClient = new NbaDataClient();
this.startedGameTableCreator = new StartedGameTableCreator();
this.playByPlayTableCreator = new PlayByPlayTableCreator();
this.upcomingGameTableCreator = new UpcomingGameTableCreator();
this.boxScoreTableCreator = new BoxScoreTableCreator();
}

function isGameUpcoming(data) {
return data.unixMillisecondsStartTime > moment().valueOf();
}
static hasPlayByPlay(data) {
return typeof data.playByPlay !== 'undefined' && data.playByPlay.length > 0;
}

function hasPlayByPlay(gameData) {
return typeof gameData.playByPlay !== 'undefined' && gameData.playByPlay.length > 0;
}
static hasBoxScore(data) {
return typeof data.boxScore !== 'undefined';
}

function hasBoxScore(gameData) {
return typeof gameData.boxScore !== 'undefined';
}
generateFirstRow(data) {
const row = [this.startedGameTableCreator.create(data)];
if (CommandLineOutputClient.hasPlayByPlay(data)) {
row.push(this.playByPlayTableCreator.create(data.playByPlay));
}
return row;
}

function outputStartedGameTable(gameData) {
var table = new Table();
var firstRow = [];
var secondRow = [];

firstRow.push(startedGameTableCreator.create(gameData));

if (hasPlayByPlay(gameData)) {
firstRow.push(playByPlayTableCreator.create(gameData.playByPlay))
generateSecondRow(data) {
const row = [];
if (CommandLineOutputClient.hasBoxScore(data)) {
row.push(this.boxScoreTableCreator.create(data.boxScore.home));
row.push(this.boxScoreTableCreator.create(data.boxScore.visitor));
}
return row;
}

if (hasBoxScore(gameData)) {
let homeBoxScoreTable = boxScoreTableCreator.create(gameData.boxScore.home);
let visitorBoxScoreTable = boxScoreTableCreator.create(gameData.boxScore.visitor);
secondRow.push(homeBoxScoreTable);
secondRow.push(visitorBoxScoreTable);
outputStartedGameTable(data) {
let table = new Table();
table.push(this.generateFirstRow(data));
table.push(this.generateSecondRow(data));
console.log(table.toString());
}

table.push(firstRow);
table.push(secondRow);
console.log(table.toString());
}
outputUpcomingGames(upcomingGames) {
upcomingGames.map(game => console.log(this.upcomingGameTableCreator.create(game)));
}

function outputGames(data) {
const upcomingGameData = [];
Object.keys(data).forEach(function(key) {
const gameData = data[key];
if (isGameUpcoming(gameData)) {
upcomingGameData.push(gameData);
} else {
outputStartedGameTable(gameData);
outputGames(data) {
const upcomingGames = [];
for (let gameId in data) {
let gameData = data[gameId];
if (gameData.isUpcoming) {
upcomingGames.push(gameData);
} else {
this.outputStartedGameTable(gameData);
}
}
});

if (upcomingGameData.length > 0) {
console.log(upcomingGameTableCreator.create(upcomingGameData));
this.outputUpcomingGames(upcomingGames);
}
}

module.exports = {
outputGamesForDateRange: function(startDate, endDate) {
nbaDataClient.fetchDataForDateRange(startDate, endDate, outputGames);
outputGamesForDateRange(startDate, endDate) {
this.nbaDataClient.fetchDataForDateRange(startDate, endDate, this.outputGames.bind(this));
}
};
}
18 changes: 8 additions & 10 deletions src/tables/UpcomingGameTableCreator.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,15 @@ export default class UpcomingGameTableCreator {
this.defaultFormat = { head: this.header };
}

create(upcomingGamesData) {
create(upcomingGame) {
const table = new Table(this.defaultFormat);
upcomingGamesData.map(upcomingGame => (
table.push([
upcomingGame.formattedLocalizedStartDate,
upcomingGame.homeName,
upcomingGame.visitorName,
upcomingGame.broadcasts.toString(),
UpcomingGameTableCreator.formatLocation(upcomingGame.arena, upcomingGame.city, upcomingGame.state)
])
));
table.push([
upcomingGame.formattedLocalizedStartDate,
upcomingGame.homeName,
upcomingGame.visitorName,
upcomingGame.broadcasts.toString(),
UpcomingGameTableCreator.formatLocation(upcomingGame.arena, upcomingGame.city, upcomingGame.state)
]);
return table.toString();
}

Expand Down
7 changes: 6 additions & 1 deletion src/translators/data/ScoreboardDataTranslator.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export default class ScoreboardDataTranslator {
const visitorCity = HtmlEscaper.escapeHtml(gameData.visitor.city);
const visitorNickname = HtmlEscaper.escapeHtml(gameData.visitor.nickname);
const visitorScore = parseInt(gameData.visitor.score);

const homeAbbreviation = HtmlEscaper.escapeHtml(gameData.home.abbreviation);
const homeCity = HtmlEscaper.escapeHtml(gameData.home.city);
const homeNickname = HtmlEscaper.escapeHtml(gameData.home.nickname);
Expand All @@ -48,6 +48,7 @@ export default class ScoreboardDataTranslator {
url: gameUrl,
nbaFormatStartDate: startDate,
unixMillisecondsStartTime: ScoreboardDataTranslator.getUnixMillisecondsStartTime(dateStartTime),
isUpcoming: ScoreboardDataTranslator.isUpcoming(dateStartTime),
arena: arena,
city: city,
state: state,
Expand Down Expand Up @@ -118,6 +119,10 @@ export default class ScoreboardDataTranslator {
return ScoreboardDataTranslator.getDefaultDateStartTime(dateStartTime).clone().tz("UTC").valueOf();
}

static isUpcoming(dateStartTime) {
return ScoreboardDataTranslator.getUnixMillisecondsStartTime(dateStartTime) > moment().valueOf();
}

static getGameStatus(periodStatus, gameStatus) {
if (periodStatus != "Halftime") {
return Constants.TRANSLATED_GAME_STATUS_MAP[gameStatus];
Expand Down

0 comments on commit e147385

Please sign in to comment.