Skip to content

Commit

Permalink
Merge pull request #11 from AlecM33/challenges
Browse files Browse the repository at this point in the history
small corrections, html site
  • Loading branch information
AlecM33 authored Jul 6, 2024
2 parents ca4875c + e94b735 commit af0efe8
Show file tree
Hide file tree
Showing 7 changed files with 129 additions and 83 deletions.
1 change: 1 addition & 0 deletions config/globals.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ module.exports = {
AMERICAN_LEAGUE: 103,
SAVANT_POLLING_INTERVAL: 15000,
SLOW_POLL_INTERVAL: 300000,
GAMEDAY_PING_INTERVAL: 10000,
HIGHLIGHTS_PER_MESSAGE: 8,
SCORING_PLAYS_PER_MESSAGE: 8,
DATE: null,
Expand Down
101 changes: 101 additions & 0 deletions docs/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>MLB Gameday Bot</title>
<style>
html {
display: flex;
justify-content: center;
}
body {
justify-content: center;
padding: 10px;
width: 100%;
max-width: 50em;
display: flex;
flex-direction: column;
align-items: center;
text-align: left;
font-size: 18px;
background-color: #03142d;
color: #e7e7e7;
font-family: "Segoe UI", Arial, sans-serif
}
img {
border-radius: 5px;
border: 1px solid #4d4d4d;
width: 100%;
max-width: 27em;
}
</style>
</head>
<body>
<h1>MLB Gameday Bot ⚾</h1>
<p id="intro">A bot that integrates with the MLB Stats API to track your team of choice.
When running, the bot periodically polls for games in a 48-hour window centered on the current date. Whichever game is closest in time is considered the "current" game, and will be the game for which a lot of the commands returns data. If a game is live, the bot subscribes to its MLB.com Gameday live feed, and in turn reports events to any number of subscribed Discord channels.
</p>
<img src="./images/screenshots/gameday_feed.png"/>
<p>
The embeds are styled with contrasting colors from each team's color set. For balls in play, the bot will give Statcast metrics.
This includes not only exit velo, launch angle, and distance, but also xBA and HR/Park, which are sourced separately from the
MLB Savant site. By default, the bot will report the result of each at bat, plus any key events that happen during at-bats,
such as baserunning events. If this is too much, the bot can be configured to report scoring plays only. Furthermore, if you wish
to avoid spoilers for people watching or listening on a delay, you can provide a delay from 0-180 seconds when subscribing.

For example, in a server for the Guardians, the bot has provided nice context right in the chat where people are following the game.
You can of course personalize it to have a bit more flair for your team:
</p>
<img src="./images/screenshots/personalized_gameday.png"/>

<h1 class="command-header">/lineup</h1>

<img src="./images/screenshots/lineup.png"/>

<h1 class="command-header">/starters</h1>

<img src="./images/screenshots/starters.png"/>

<h1 class="command-header">/box_score</h1>

<img src="./images/screenshots/box_score.png"/>

<h1 class="command-header">/line_score</h1>

<img src="./images/screenshots/line_score.png"/>

<h1 class="command-header">/pitcher</h1>

<img src="./images/screenshots/pitcher.png"/>

<h1 class="command-header">/batter</h1>

<img src="./images/screenshots/batter.png"/>

<h1 class="command-header">/scoring_plays</h1>

<img src="./images/screenshots/scoring_plays.png"/>

<h1 class="command-header">/highlights</h1>

<img src="./images/screenshots/highlights.png"/>

<h1 class="command-header">/standings</h1>

<img src="./images/screenshots/standings.png"/>

<h1 class="command-header">/schedule</h1>

<img src="./images/screenshots/schedule.png"/>

<h1 class="command-header">/weather</h1>

<img src="./images/screenshots/weather.png"/>

<h1 class="command-header">/attendance</h1>

<img src="images/screenshots/attendance.png"/>
</body>
</html>
74 changes: 0 additions & 74 deletions docs/index.md

This file was deleted.

18 changes: 16 additions & 2 deletions modules/MLB-API-util.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,10 +138,24 @@ module.exports = {
},
websocketSubscribe: (gamePk) => {
const { WebSocket } = require('ws');
return new ReconnectingWebSocket(endpoints.websocketSubscribe(gamePk),
const socket = new ReconnectingWebSocket(endpoints.websocketSubscribe(gamePk),
[],
{ WebSocket, maxRetries: 5 }
{ WebSocket, maxRetries: 3 }
);
let heartbeatInterval;
const heartbeat = () => {
LOGGER.debug('ping: Gameday5');
socket.send('Gameday5');
};
socket.addEventListener('open', () => {
LOGGER.info('Gameday socket opened.');
heartbeatInterval = setInterval(heartbeat, globals.GAMEDAY_PING_INTERVAL);
});
socket.addEventListener('close', () => {
clearInterval(heartbeatInterval);
});

return socket;
},
websocketQueryUpdateId: async (gamePk, updateId, timestamp) => {
return (await fetch(endpoints.websocketQueryUpdateId(gamePk, updateId, timestamp))).json();
Expand Down
10 changes: 7 additions & 3 deletions modules/current-play-processor.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@ module.exports = {
if (!globalCache.values.game.startReported
&& currentPlayJSON.playEvents?.find(event => event?.details?.description === 'Status Change - In Progress')) {
globalCache.values.game.startReported = true;
reply += (globalCache.values.game.currentLiveFeed.gameData.teams.home.id === globals.GUARDIANS
? 'And we\'re underway at the corner of Carnegie and Ontario.'
: 'A game is starting! Go Guards!');
if (parseInt(process.env.TEAM_ID) === globals.GUARDIANS) {
reply += (globalCache.values.game.currentLiveFeed.gameData.teams.home.id === globals.GUARDIANS
? 'And we\'re underway at the corner of Carnegie and Ontario.'
: 'A game is starting! Go Guards!');
} else {
reply += 'A game is starting!';
}
}
let lastEvent;
if (currentPlayJSON.about?.isComplete
Expand Down
6 changes: 3 additions & 3 deletions modules/gameday.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ async function statusPoll (bot) {
const now = globals.DATE || new Date();
try {
const currentGames = await mlbAPIUtil.currentGames();
LOGGER.info('Current game PKs: ' + JSON.stringify(currentGames
LOGGER.trace('Current game PKs: ' + JSON.stringify(currentGames
.map(game => { return { key: game.gamePk, date: game.officialDate, status: game.status.statusCode }; }), null, 2));
currentGames.sort((a, b) => Math.abs(now - new Date(a.gameDate)) - Math.abs(now - new Date(b.gameDate)));
globalCache.values.currentGames = currentGames;
Expand Down Expand Up @@ -55,7 +55,7 @@ function subscribe (bot, liveGame, games) {
*/
if (globalCache.values.game.lastSocketMessageTimestamp === eventJSON.timeStamp
&& globalCache.values.game.lastSocketMessageLength === e.data.length) {
LOGGER.trace('DUPLICATE MESSAGE: ' + eventJSON.updateId + ' - DISREGARDING');
LOGGER.debug('DUPLICATE MESSAGE: ' + eventJSON.updateId + ' - DISREGARDING');
return;
}
globalCache.values.game.lastSocketMessageTimestamp = eventJSON.timeStamp;
Expand Down Expand Up @@ -128,7 +128,7 @@ async function reportPlays (bot, gamePk) {
await processAndPushPlay(bot, currentPlayProcessor.process(lastAtBat), gamePk, atBatIndex - 1);
} else if (lastReportedCompleteAtBatIndex !== null
&& (atBatIndex - lastReportedCompleteAtBatIndex > 1)) { // indicates we missed the result of an at-bat. happens rarely when the data moves quickly to the next at-bat.
LOGGER.trace('Missed at-bat index: ' + atBatIndex - 1);
LOGGER.debug('Missed at-bat index: ' + atBatIndex - 1);
await reportAnyMissedEvents(lastAtBat, bot, gamePk, atBatIndex - 1);
await processAndPushPlay(bot, currentPlayProcessor.process(lastAtBat), gamePk, atBatIndex - 1);
}
Expand Down
2 changes: 1 addition & 1 deletion modules/interaction-handlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -552,7 +552,7 @@ module.exports = {
embeds: [],
components: [],
content: weather && Object.keys(weather).length > 0
? 'Weather for game time at ' + currentLiveFeed.gameData.venue.name + ':\n' +
? 'Weather at ' + currentLiveFeed.gameData.venue.name + ':\n' +
getWeatherEmoji(weather.condition) + ' ' + weather.condition + '\n' +
'\uD83C\uDF21 ' + weather.temp + '°\n' +
'\uD83C\uDF43 ' + weather.wind
Expand Down

0 comments on commit af0efe8

Please sign in to comment.