This repository has been archived by the owner on Jun 3, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #54 from jaebradley/command-line-output-refactor
Command line output refactor
- Loading branch information
Showing
4 changed files
with
67 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters