Skip to content

Commit

Permalink
Merge pull request #38 from AlecM33/end-of-game-bug
Browse files Browse the repository at this point in the history
don't try to re-subscribe for same game, fix score property
  • Loading branch information
AlecM33 authored Aug 11, 2024
2 parents 575b27c + 25253e5 commit f786b44
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 3 deletions.
7 changes: 6 additions & 1 deletion modules/gameday.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,15 @@ async function statusPoll (bot) {
globalCache.values.nearestGames = nearestGames.filter(g => g.status.codedGameState !== 'D');
globalCache.values.game.isDoubleHeader = nearestGames.length > 1;
const inProgressGame = nearestGames.find(nearestGame => nearestGame.status.statusCode === 'I' || nearestGame.status.statusCode === 'PW');
if (inProgressGame) {
/*
the "game_finished" socket event is received before a game's status changes to "Final", typically. So we shouldn't try to
re-subscribe just because the status is still "In Progress". We should check if it's a different game.
*/
if (inProgressGame && inProgressGame.gamePk !== globalCache.values.game.currentGamePk) {
LOGGER.info('Gameday: polling stopped: a game is live.');
globalCache.resetGameCache();
globalCache.values.game.currentLiveFeed = await mlbAPIUtil.liveFeed(inProgressGame.gamePk);
globalCache.values.game.currentGamePk = inProgressGame.gamePk;
gamedayUtil.getConstrastingEmbedColors();
module.exports.subscribe(bot, inProgressGame, nearestGames);
} else {
Expand Down
2 changes: 2 additions & 0 deletions modules/global-cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const values = {
subscribedChannels: [],
game: {
currentLiveFeed: null,
currentGamePk: null,
isDoubleHeader: null,
lastReportedCompleteAtBatIndex: null,
lastReportedPlayDescription: null,
Expand All @@ -19,6 +20,7 @@ const values = {

function resetGameCache () {
values.game.currentLiveFeed = null;
values.game.currentGamePk = null;
values.game.isDoubleHeader = null;
values.game.lastReportedCompleteAtBatIndex = null;
values.game.lastReportedPlayDescription = null;
Expand Down
4 changes: 2 additions & 2 deletions modules/livefeed.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ module.exports = {
return liveFeed.liveData.linescore;
},
homeTeamScore: () => {
return liveFeed.liveData.linescore.teams.home.runs;
return liveFeed.liveData.plays.currentPlay.result.homeScore;
},
awayTeamScore: () => {
return liveFeed.liveData.linescore.teams.away.runs;
return liveFeed.liveData.plays.currentPlay.result.awayScore;
},
currentBatterBatSide: () => {
return liveFeed.liveData.plays.currentPlay.matchup.batSide.code;
Expand Down

0 comments on commit f786b44

Please sign in to comment.