UNPKG

playball

Version:

Watch MLB games from the comfort of your terminal

119 lines 4.15 kB
import React from 'react'; import { useSelector } from "react-redux/lib/alternate-renderers.js"; import PropTypes from 'prop-types'; import { selectAllPlays, selectTeams } from "../features/games.js"; import { get } from "../config.js"; import style from "../style/index.js"; function getPlayResultColor(play) { var _play$playEvents; const lastPlay = (_play$playEvents = play.playEvents[play.playEvents.length - 1]) === null || _play$playEvents === void 0 ? void 0 : _play$playEvents.details; if (!lastPlay) { return get('color.other-event'); } else if (lastPlay.isBall) { return get('color.walk'); } else if (lastPlay.isStrike) { return get('color.strike-out'); } else if (lastPlay.isInPlay && !play.about.hasOut) { return get('color.in-play-no-out'); } else { return get('color.in-play-out'); } } function formatOut(out) { return ` {bold}${out} out${out > 1 ? 's' : ''}{/}`; } function AllPlays({ reverse, scoringOnly }) { const plays = useSelector(selectAllPlays); const teams = useSelector(selectTeams); const formatScoreDetail = scoreObj => ` {bold}{${get('color.in-play-runs-bg')}-bg}{${get('color.in-play-runs-fg')}-fg} ` + `${teams.away.abbreviation} ${scoreObj.awayScore} - ` + `${teams.home.abbreviation} ${scoreObj.homeScore}` + ' {/}'; const playsByInning = {}; plays === null || plays === void 0 ? void 0 : plays.forEach(play => { var _play$playEvents2; const playInning = play.about.halfInning + ' ' + play.about.inning; (_play$playEvents2 = play.playEvents) === null || _play$playEvents2 === void 0 ? void 0 : _play$playEvents2.forEach(event => { if (event.type === 'action') { if (scoringOnly && !event.details.isScoringPlay) { return; } if (event.details.eventType === 'batter_timeout' || event.details.eventType === 'mound_visit') { return; } let line = ''; if (event.details.event) { line += `{${get('color.other-event')}-fg}[${event.details.event}]{/} `; } line += event.details.description; if (event.details.isOut) { var _event$count; line += formatOut((_event$count = event.count) === null || _event$count === void 0 ? void 0 : _event$count.outs); } if (event.isScoringPlay || event.details.isScoringPlay) { line += formatScoreDetail(event.details); } if (!(playInning in playsByInning)) { playsByInning[playInning] = []; } playsByInning[playInning].push(line); } }); if (play.about.isComplete && (!scoringOnly || play.about.isScoringPlay)) { const color = getPlayResultColor(play); let line = `{${color}-fg}[${play.result.event}]{/} ${play.result.description}`; if (play.about.hasOut) { const lastOut = play.playEvents[play.playEvents.length - 1].count.outs; if (lastOut !== play.count.outs) { line += formatOut(play.count.outs); } } if (play.about.isScoringPlay) { line += formatScoreDetail(play.result); } if (!(playInning in playsByInning)) { playsByInning[playInning] = []; } playsByInning[playInning].push(line); } }); const lines = []; const inningKeys = Object.keys(playsByInning); if (reverse) { inningKeys.reverse(); } inningKeys.forEach(inning => { if (lines.length > 0) { lines.push(''); } lines.push(`{bold}[${inning.toUpperCase()}]{/}`); if (reverse) { lines.push(...playsByInning[inning].slice().reverse()); } else { lines.push(...playsByInning[inning]); } }); if (lines.length === 0) { if (scoringOnly) { lines.push('No scoring plays yet'); } else { lines.push('No plays yet'); } } return /*#__PURE__*/React.createElement("box", { content: lines.join('\n'), focused: true, mouse: true, keys: true, vi: true, scrollable: true, scrollbar: style.scrollbar, alwaysScroll: true, tags: true }); } AllPlays.propTypes = { reverse: PropTypes.bool, scoringOnly: PropTypes.bool }; export default AllPlays;