Skip to content

Commit

Permalink
due up, indicate barrels
Browse files Browse the repository at this point in the history
  • Loading branch information
AlecM33 committed Jul 23, 2024
1 parent 5442eda commit 7fa727e
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 16 deletions.
20 changes: 11 additions & 9 deletions modules/current-play-processor.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,24 @@ module.exports = {
}
}
}
/* two kinds of objects get processed - "at bats", which will have a "result" object, and events within at bats, which put
the same information in a "details" object. So we often have to check for both.
*/
return {
reply,
isStartEvent: currentPlayJSON.playEvents?.find(event => event?.details?.description === 'Status Change - In Progress'),
homeScore: currentPlayJSON.result?.homeScore,
awayScore: currentPlayJSON.result?.awayScore,
isOut: currentPlayJSON.result?.isOut || currentPlayJSON.details?.isOut,
outs: currentPlayJSON.count?.outs,
homeScore: (currentPlayJSON.result ? currentPlayJSON.result.homeScore : currentPlayJSON.details?.homeScore),
awayScore: (currentPlayJSON.result ? currentPlayJSON.result.awayScore : currentPlayJSON.details?.awayScore),
isComplete: currentPlayJSON.about?.isComplete,
description: (currentPlayJSON.result?.description || currentPlayJSON.details?.description),
event: (currentPlayJSON.result?.event || currentPlayJSON.details?.event),
eventType: (currentPlayJSON.result?.eventType || currentPlayJSON.details?.eventType),
isScoringPlay: (currentPlayJSON.about?.isScoringPlay || currentPlayJSON.details?.isScoringPlay),
isInPlay: (lastEvent?.details?.isInPlay || currentPlayJSON.details?.isInPlay),
playId: (lastEvent?.playId || currentPlayJSON.playId),
metricsAvailable: (lastEvent?.hitData?.launchSpeed !== undefined || currentPlayJSON.hitData?.launchSpeed !== undefined),
hitDistance: (lastEvent?.hitData?.totalDistance || currentPlayJSON.hitData?.totalDistance)
};
}
Expand Down Expand Up @@ -82,15 +88,11 @@ function addMetrics (lastEvent, reply) {
getFireEmojis(lastEvent.hitData.launchSpeed) + '\n';
reply += 'Launch Angle: ' + lastEvent.hitData.launchAngle + '° \n';
reply += 'Distance: ' + lastEvent.hitData.totalDistance + ' ft.\n';
reply += 'xBA: Pending...\n';
reply += lastEvent.hitData.totalDistance && lastEvent.hitData.totalDistance >= 300 ? 'HR/Park: Pending...' : '';
reply += 'xBA: Pending...';
reply += lastEvent.hitData.totalDistance && lastEvent.hitData.totalDistance >= 300 ? '\nHR/Park: Pending...' : '';
} else {
reply += '\n\n**Statcast Metrics:**\n';
reply += 'Exit Velocity: Unavailable\n';
reply += 'Launch Angle: Unavailable\n';
reply += 'Distance: Unavailable\n';
reply += 'xBA: Unavailable\n';
reply += 'HR/Park: Unavailable';
reply += 'Data was not available.';
}

return reply;
Expand Down
28 changes: 23 additions & 5 deletions modules/gameday.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,9 +164,11 @@ async function processAndPushPlay (bot, play, gamePk, atBatIndex) {
const embed = new EmbedBuilder()
.setTitle(deriveHalfInning(feed.halfInning()) + ' ' +
feed.inning() + ', ' +
feed.awayAbbreviation() + (play.isScoringPlay ? ' vs. ' : ' ' + play.awayScore + ' - ' + play.homeScore + ' ') +
feed.awayAbbreviation() + (play.isScoringPlay
? ' vs. '
: ' ' + play.awayScore + ' - ' + play.homeScore + ' ') +
feed.homeAbbreviation() + (play.isScoringPlay ? ' - Scoring Play \u2757' : ''))
.setDescription(play.reply)
.setDescription(play.reply + (play.isOut && play.outs === 3 && !didGameEnd(play.homeScore, play.awayScore) ? getDueUp() : ''))
.setColor((feed.halfInning() === 'top'
? globalCache.values.game.awayTeamColor
: globalCache.values.game.homeTeamColor
Expand All @@ -191,6 +193,22 @@ async function processAndPushPlay (bot, play, gamePk, atBatIndex) {
}
}

function didGameEnd (homeScore, awayScore) {
const feed = liveFeed(globalCache.values.game.currentLiveFeed);
return feed.linescore().currentInning >= 9
&& (
(homeScore > awayScore && feed.linescore().inningState === 'Top')
|| (awayScore > homeScore && feed.linescore().inningState === 'Bottom')
);
}

function getDueUp () {
const feed = liveFeed(globalCache.values.game.currentLiveFeed);
const linescore = feed.linescore();

return '\n\n**Due up**: ' + linescore.offense.batter.fullName + ', ' + linescore.offense.onDeck.fullName + ', ' + linescore.offense.inHole.fullName;
}

async function sendMessage (returnedChannel, embed, messages) {
LOGGER.debug('Sending!');
const message = await returnedChannel.send({
Expand All @@ -213,7 +231,7 @@ async function sendDelayedMessage (play, gamePk, channelSubscription, returnedCh
}

async function maybePopulateAdvancedStatcastMetrics (play, messages, gamePk) {
if (play.isInPlay) {
if (play.isInPlay && play.metricsAvailable) {
if (play.playId) {
try {
// xBA and HR/Park for balls in play is available on a delay via baseballsavant.
Expand All @@ -228,7 +246,7 @@ async function maybePopulateAdvancedStatcastMetrics (play, messages, gamePk) {
notifySavantDataUnavailable(messages);
}
} else {
LOGGER.debug('Skipping savant poll - not in play.');
LOGGER.debug('Skipping savant poll - not in play or metrics unavailable.');
}
}

Expand Down Expand Up @@ -277,7 +295,7 @@ function processMatchingPlay (matchingPlay, messages, messageTrackers, playId, h
if (matchingPlay.xba && description.includes('xBA: Pending...')) {
LOGGER.debug('Editing with xba: ' + playId);
description = description.replaceAll('xBA: Pending...', 'xBA: ' + matchingPlay.xba +
(parseFloat(matchingPlay.xba) > 0.5 ? ' \uD83D\uDFE2' : ''));
(matchingPlay.is_barrel === 1 ? ' \uD83D\uDFE2 (Barreled)' : ''));
receivedEmbed.setDescription(description);
messages[i].edit({
embeds: [receivedEmbed]
Expand Down
4 changes: 2 additions & 2 deletions modules/livefeed.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module.exports = (liveFeed) => {
homeTeamId: () => { return liveFeed.gameData.teams.home.id; },
awayTeamId: () => { return liveFeed.gameData.teams.away.id; },
currentPlay: () => { return liveFeed.liveData.plays.currentPlay; },
allPlays: () => { return liveFeed.liveData.plays.allPlays; }

allPlays: () => { return liveFeed.liveData.plays.allPlays; },
linescore: () => { return liveFeed.liveData.linescore; }
};
};

0 comments on commit 7fa727e

Please sign in to comment.