Skip to content

Commit

Permalink
Fix baseball scoring summary
Browse files Browse the repository at this point in the history
  • Loading branch information
dumbmatter committed Apr 14, 2024
1 parent 81d975f commit 87a708a
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 49 deletions.
6 changes: 2 additions & 4 deletions TODO
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@ indicator of if trade will be accepted
allow undoing a trade

shootouts
- baseball
- scoring summary filtering in eventsToShow can't handle misses, they get shown early
- need football scoringSummary style - build up from events?
- hockey
- need football scoringSummary style - build up from events?
- need football/baseball scoringSummary style - build up from events?
- isScoringPlay -> formatScoringSummaryEvent
- confirm for all sports
- check what fast-forward options are shown during shootout
- no injuries during shootout
Expand Down
41 changes: 41 additions & 0 deletions src/common/formatScoringSummaryEvent.baseball.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import type {
PlayByPlayEvent,
PlayByPlayEventInput,
PlayByPlayEventScore,
} from "../worker/core/GameSim.baseball/PlayByPlayLogger";

export const formatScoringSummaryEvent = (
event: PlayByPlayEventInput,
period: number,
) => {
let scored = false;
if (event.type === "hitResult" && event.numBases === 4) {
// Home run
scored = true;
} else if (event.type === "shootoutShot") {
scored = true;
} else {
const runners = (event as Extract<PlayByPlayEvent, { type: "hitResult" }>)
.runners;
if (runners?.some(runner => runner.scored)) {
scored = true;
}
}

if (scored) {
const scoringSummaryEvent = {
...event,
inning: period,
} as PlayByPlayEventScore;
if (
scoringSummaryEvent.type === "balk" ||
scoringSummaryEvent.type === "wildPitch" ||
scoringSummaryEvent.type === "passedBall"
) {
// Swap team, so it shows up correctly in scoring summary. Basically, t must be team that scored
scoringSummaryEvent.t = scoringSummaryEvent.t === 0 ? 1 : 0;
}

return scoringSummaryEvent;
}
};
15 changes: 5 additions & 10 deletions src/ui/components/BoxScore.baseball.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -263,14 +263,7 @@ const ScoringSummary = ({
Record<number, BoxScorePlayer>
>({});

const eventsToShow = processedEvents.filter(event => {
const ptsKey = event.type === "shootoutShot" ? "sPts" : "pts";
return (
event.score[0] <= teams[0][ptsKey]! && event.score[1] <= teams[1][ptsKey]!
);
});

const someEvents = eventsToShow.length > 0;
const someEvents = processedEvents.length > 0;

useEffect(() => {
if (!someEvents) {
Expand All @@ -295,7 +288,7 @@ const ScoringSummary = ({
return (
<table className="table table-sm border-bottom">
<tbody>
{eventsToShow.map((event, i) => {
{processedEvents.map((event, i) => {
let quarterHeader: ReactNode = null;
const currentInning =
event.type === "shootoutShot" ? "Shootout" : event.inning;
Expand Down Expand Up @@ -334,7 +327,9 @@ const ScoringSummary = ({
className={
!event.noPoints && event.t === i
? "fw-bold"
: "text-body-secondary"
: event.noPoints && event.t === i
? "text-danger"
: "text-body-secondary"
}
>
{pts}
Expand Down
8 changes: 4 additions & 4 deletions src/ui/components/BoxScore.football.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -384,9 +384,7 @@ const ScoringSummary = memo(
{quarterHeader}
<tr>
<td>{teams[event.t].abbrev}</td>
<td className={event.noPoints ? "text-danger" : undefined}>
{event.scoreType === "SH" ? "FG" : event.scoreType}
</td>
<td>{event.scoreType === "SH" ? "FG" : event.scoreType}</td>
<td>
{event.score.map((pts, i) => {
return (
Expand All @@ -395,7 +393,9 @@ const ScoringSummary = memo(
className={
!event.noPoints && event.t === i
? "fw-bold"
: "text-body-secondary"
: event.noPoints && event.t === i
? "text-danger"
: "text-body-secondary"
}
>
{pts}
Expand Down
9 changes: 9 additions & 0 deletions src/ui/util/processLiveGameEvents.baseball.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
POS_NUMBERS_INVERSE,
} from "../../common/constants.baseball";
import type { PlayerInjury } from "../../common/types";
import { formatScoringSummaryEvent } from "../../common/formatScoringSummaryEvent.baseball";

export type BoxScorePlayer = {
name: string;
Expand Down Expand Up @@ -648,6 +649,14 @@ const processLiveGameEvents = ({
e.type === "gameOver";

stop = true;

const scoringSummaryEvent = formatScoringSummaryEvent(e, quarters.length);
if (scoringSummaryEvent) {
boxScore.scoringSummary = [
...boxScore.scoringSummary,
scoringSummaryEvent,
];
}
}
}

Expand Down
35 changes: 5 additions & 30 deletions src/worker/core/GameSim.baseball/PlayByPlayLogger.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import type { POS_NUMBERS_INVERSE } from "../../../common/constants.baseball";
import { formatScoringSummaryEvent } from "../../../common/formatScoringSummaryEvent.baseball";
import type { Runner, TeamNum } from "./types";

type PlayByPlayEventInput =
export type PlayByPlayEventInput =
| {
type: "sideStart";
inning: number;
Expand Down Expand Up @@ -216,35 +217,9 @@ class PlayByPlayLogger {
this.period = event.inning;
}

let scored = false;
if (event.type === "hitResult" && event.numBases === 4) {
// Home run
scored = true;
} else if (event.type === "shootoutShot") {
scored = true;
} else {
const runners = (event as Extract<PlayByPlayEvent, { type: "hitResult" }>)
.runners;
if (runners?.some(runner => runner.scored)) {
scored = true;
}
}

if (scored) {
const scoringSummaryEvent = {
...event,
inning: this.period,
};
if (
scoringSummaryEvent.type === "balk" ||
scoringSummaryEvent.type === "wildPitch" ||
scoringSummaryEvent.type === "passedBall"
) {
// Swap team, so it shows up correctly in scoring summary. Basically, t must be team that scored
scoringSummaryEvent.t = scoringSummaryEvent.t === 0 ? 1 : 0;
}

this.scoringSummary.push(scoringSummaryEvent as any);
const scoringSummaryEvent = formatScoringSummaryEvent(event, this.period);
if (scoringSummaryEvent) {
this.scoringSummary.push(scoringSummaryEvent);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/worker/views/liveGame.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ export const boxScoreToLiveSim = async ({
}

// For FBGM, build up scoringSummary from events, to handle deleting a score due to penalty
if (isSport("football")) {
if (isSport("football") || isSport("baseball")) {
boxScore.scoringSummary = [];
}

Expand Down

0 comments on commit 87a708a

Please sign in to comment.