UNPKG

playball

Version:

Watch MLB games from the comfort of your terminal

141 lines 6.31 kB
import React, { useEffect, useMemo } from 'react'; import { useDispatch, useSelector } from "react-redux/lib/alternate-renderers.js"; import figlet from 'figlet'; import LineScore from "./LineScore.js"; import { get } from "../config.js"; import { selectBoxscore, selectDecisions, selectGameStatus, selectLineScore, selectSelectedId, selectTeams, setReplayGame } from "../features/games.js"; import { resetTitle, setTitle } from "../screen.js"; import useKey from "../hooks/useKey.js"; import BoxScore from "./BoxScore.js"; import AllPlays from "./AllPlays.js"; const BOX_SCORE = 'BOX_SCORE'; const ALL_PLAYS = 'ALL_PLAYS'; const SCORING_PLAYS = 'SCORING_PLAYS'; const getPlayer = (id, boxscore) => { var _boxscore$home, _boxscore$away; const homePlayer = (_boxscore$home = boxscore.home) === null || _boxscore$home === void 0 || (_boxscore$home = _boxscore$home.players) === null || _boxscore$home === void 0 ? void 0 : _boxscore$home['ID' + id]; if (homePlayer !== undefined) { return homePlayer; } return (_boxscore$away = boxscore.away) === null || _boxscore$away === void 0 ? void 0 : _boxscore$away.players['ID' + id]; }; const formatDecisions = (decisions, boxscore) => { if (!decisions) { return ''; } const content = []; const winner = decisions.winner; if (winner) { var _pitcher$seasonStats, _pitcher$seasonStats2; const pitcher = getPlayer(winner.id, boxscore); content.push(`Win: ${pitcher.person.fullName} (${(_pitcher$seasonStats = pitcher.seasonStats) === null || _pitcher$seasonStats === void 0 || (_pitcher$seasonStats = _pitcher$seasonStats.pitching) === null || _pitcher$seasonStats === void 0 ? void 0 : _pitcher$seasonStats.wins}-${(_pitcher$seasonStats2 = pitcher.seasonStats) === null || _pitcher$seasonStats2 === void 0 || (_pitcher$seasonStats2 = _pitcher$seasonStats2.pitching) === null || _pitcher$seasonStats2 === void 0 ? void 0 : _pitcher$seasonStats2.losses})`); } const loser = decisions.loser; if (loser) { var _pitcher$seasonStats3, _pitcher$seasonStats4; const pitcher = getPlayer(loser.id, boxscore); content.push(`Loss: ${pitcher.person.fullName} (${(_pitcher$seasonStats3 = pitcher.seasonStats) === null || _pitcher$seasonStats3 === void 0 || (_pitcher$seasonStats3 = _pitcher$seasonStats3.pitching) === null || _pitcher$seasonStats3 === void 0 ? void 0 : _pitcher$seasonStats3.wins}-${(_pitcher$seasonStats4 = pitcher.seasonStats) === null || _pitcher$seasonStats4 === void 0 || (_pitcher$seasonStats4 = _pitcher$seasonStats4.pitching) === null || _pitcher$seasonStats4 === void 0 ? void 0 : _pitcher$seasonStats4.losses})`); } const save = decisions.save; if (save) { var _pitcher$seasonStats5; const pitcher = getPlayer(save.id, boxscore); content.push(`Save: ${pitcher.person.fullName} (${(_pitcher$seasonStats5 = pitcher.seasonStats) === null || _pitcher$seasonStats5 === void 0 ? void 0 : _pitcher$seasonStats5.pitching.saves})`); } return content.join('\n'); }; function FinishedGame() { const dispatch = useDispatch(); const id = useSelector(selectSelectedId); const boxscore = useSelector(selectBoxscore); const decisions = useSelector(selectDecisions); const linescore = useSelector(selectLineScore); const status = useSelector(selectGameStatus); const teams = useSelector(selectTeams); const [view, setView] = React.useState(BOX_SCORE); useKey('r', () => dispatch(setReplayGame(id)), { key: 'R', label: 'Replay' }); useKey('b', () => setView(BOX_SCORE), { key: 'B', label: 'Box Score' }); useKey('a', () => setView(ALL_PLAYS), { key: 'A', label: 'All Plays' }); useKey('p', () => setView(SCORING_PLAYS), { key: 'P', label: 'Scoring Plays' }); useEffect(() => { if (get('title')) { const homeRuns = linescore.teams['home'].runs; const awayRuns = linescore.teams['away'].runs; const home = teams.home.abbreviation; const away = teams.away.abbreviation; setTitle(`${away} ${awayRuns} - ${home} ${homeRuns} F`); return () => { resetTitle(); }; } }, [get, linescore, resetTitle, setTitle, teams]); const bigTextOptions = { font: 'Small Block' }; const awayTeam = `${teams.away.teamName}\n(${teams.away.record.wins}-${teams.away.record.losses})`; const awayRuns = useMemo(() => figlet.textSync(linescore.teams.away.runs, bigTextOptions), [linescore.teams.away.runs]); const homeTeam = `${teams.home.teamName}\n(${teams.home.record.wins}-${teams.home.record.losses})`; const homeRuns = useMemo(() => figlet.textSync(linescore.teams.home.runs, bigTextOptions), [linescore.teams.home.runs]); return /*#__PURE__*/React.createElement("element", null, status.detailedState === 'Postponed' ? /*#__PURE__*/React.createElement("element", null, /*#__PURE__*/React.createElement("box", { top: 1 }, "status.detailedState"), /*#__PURE__*/React.createElement("box", { top: 2 }, status.reason)) : /*#__PURE__*/React.createElement("element", { top: 1 }, /*#__PURE__*/React.createElement("box", { top: 1, left: 0, width: "25%", content: awayTeam, align: "right" }), /*#__PURE__*/React.createElement("box", { top: 0, left: "25%", width: "25%", content: awayRuns, align: "center" }), /*#__PURE__*/React.createElement("box", { top: 0, left: "50%", width: "25%", content: homeRuns, align: "center" }), /*#__PURE__*/React.createElement("box", { top: 1, left: "75%", width: "25%", content: homeTeam, align: "left" }), /*#__PURE__*/React.createElement("element", { top: 5, left: 0, width: "50%-2" }, /*#__PURE__*/React.createElement(LineScore, { align: "right", final: true })), /*#__PURE__*/React.createElement("element", { top: 5, left: "50%+2", width: "50%-2", align: "right" }, /*#__PURE__*/React.createElement("box", { content: formatDecisions(decisions, boxscore) })), /*#__PURE__*/React.createElement("element", { top: 9 }, view === BOX_SCORE && /*#__PURE__*/React.createElement(BoxScore, null), view === ALL_PLAYS && /*#__PURE__*/React.createElement(AllPlays, null), view === SCORING_PLAYS && /*#__PURE__*/React.createElement(AllPlays, { scoringOnly: true })))); } export default FinishedGame;