Skip to content

Commit

Permalink
Merge pull request #28 from AlecM33/x-parks
Browse files Browse the repository at this point in the history
List exceptional HR/Park scenarios
  • Loading branch information
AlecM33 authored Jul 29, 2024
2 parents cd10400 + cfe6b73 commit c7d45d8
Show file tree
Hide file tree
Showing 7 changed files with 886 additions and 31 deletions.
27 changes: 2 additions & 25 deletions config/globals.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,4 @@
module.exports = {
EVENT_BLACKLIST: [
"batter_timeout",
"mound_visit",
"offensive_substitution",
"defensive_switch",
"foul",
"ball",
"called_strike",
"swinging_strike",
"swinging_strike_blocked",
"hit_into_play",
"pickoff_1b",
"pickoff_2b",
"pickoff_3b",
"hit_into_play_no_out",
"foul_tip",
"at_bat_start",
"blocked_ball",
"defensive_indiff",
"pitcher_step_off",
"no_pitch",
"ejection",
"injury",
"umpire_substitution"
],
EVENT_WHITELIST:[
'pickoff_1b',
'pickoff_2b',
Expand Down Expand Up @@ -95,6 +70,8 @@ module.exports = {
DATE: null,
ADMIN_ROLES: [ "Mod" ],
TEAM_COLOR_CONTRAST_RATIO: 1.5,
HOME_RUN_PARKS_MIN: 4,
HOME_RUN_PARKS_MAX: 26,
EVENTS: [
"Double",
"Double Play",
Expand Down
37 changes: 37 additions & 0 deletions modules/gameday-util.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const liveFeed = require('./livefeed');
const globalCache = require('./global-cache');
const globals = require('../config/globals');
const ColorContrastChecker = require('color-contrast-checker');
const mlbAPIUtil = require('./MLB-API-util');

module.exports = {
deriveHalfInning: (halfInningFull) => {
Expand Down Expand Up @@ -38,5 +39,41 @@ module.exports = {
const linescore = feed.linescore();

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

getXParks: async (gamePk, playId, numberOfParks) => {
const feed = liveFeed.init(globalCache.values.game.currentLiveFeed);
const isHome = feed.homeTeamId() === process.env.TEAM_ID;
let reply = '';
if (numberOfParks === 0 || numberOfParks === 30) {
return reply;
}
let xParks;
try {
xParks = await mlbAPIUtil.xParks(gamePk, playId);
} catch (e) {
console.error(e);
return reply;
}
if (numberOfParks <= globals.HOME_RUN_PARKS_MIN || numberOfParks >= globals.HOME_RUN_PARKS_MAX) {
reply += ' - ';
const parks = numberOfParks >= globals.HOME_RUN_PARKS_MAX ? xParks.not : xParks.hr;
for (let i = 0; i < parks.length; i ++) {
reply += parks[i].name + ' (' + parks[i].team_abbrev + ')';
if (i < parks.length - 1) {
reply += ', ';
}
}
return reply;
} else if (!isHome) {
if (xParks.hr.find(park => park.id === feed.awayTeamVenue().id)) {
reply += ', including ' + feed.awayTeamVenue().name;
} else if (xParks.not.find(park => park.id === feed.awayTeamVenue().id)) {
reply += ', but not ' + feed.awayTeamVenue().name;
}
return reply;
}

return reply;
}
};
10 changes: 6 additions & 4 deletions modules/gameday.js
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,8 @@ async function pollForSavantData (gamePk, playId, messages, hitDistance) {
await pollingFunction();
}

function processMatchingPlay (matchingPlay, messages, messageTrackers, playId, hitDistance) {
async function processMatchingPlay (matchingPlay, messages, messageTrackers, playId, hitDistance) {
const feed = liveFeed.init(globalCache.values.game.currentLiveFeed);
for (let i = 0; i < messages.length; i ++) {
const receivedEmbed = EmbedBuilder.from(messages[i].embeds[0]);
let description = messages[i].embeds[0].description;
Expand All @@ -277,9 +278,10 @@ function processMatchingPlay (matchingPlay, messages, messageTrackers, playId, h
&& matchingPlay.contextMetrics.homeRunBallparks !== undefined
&& description.includes('HR/Park: Pending...')) {
LOGGER.debug('Editing with HR/Park: ' + playId);
description = description.replaceAll('HR/Park: Pending...', 'HR/Park: ' +
matchingPlay.contextMetrics.homeRunBallparks + '/30' +
(matchingPlay.contextMetrics.homeRunBallparks === 30 ? '\u203C\uFE0F' : ''));
const homeRunBallParksDescription = 'HR/Park: ' + matchingPlay.contextMetrics.homeRunBallparks + '/30' +
(matchingPlay.contextMetrics.homeRunBallparks === 30 ? '\u203C\uFE0F' : '') +
(await gamedayUtil.getXParks(feed.gamePk(), playId, matchingPlay.contextMetrics.homeRunBallparks));
description = description.replaceAll('HR/Park: Pending...', homeRunBallParksDescription);
receivedEmbed.setDescription(description);
messages[i].edit({
embeds: [receivedEmbed]
Expand Down
9 changes: 9 additions & 0 deletions modules/livefeed.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
module.exports = {
init: (liveFeed) => {
return {
gamePk: () => {
return liveFeed.gameData.game.pk;
},
timestamp: () => {
return liveFeed.metaData.timeStamp;
},
Expand All @@ -22,6 +25,12 @@ module.exports = {
awayTeamId: () => {
return liveFeed.gameData.teams.away.id;
},
homeTeamVenue: () => {
return liveFeed.gameData.teams.home.venue;
},
awayTeamVenue: () => {
return liveFeed.gameData.teams.away.venue;
},
currentPlay: () => {
return liveFeed.liveData.plays.currentPlay;
},
Expand Down
Loading

0 comments on commit c7d45d8

Please sign in to comment.