From f4ef94056a8679c09f42c70eee7396e517c68a89 Mon Sep 17 00:00:00 2001 From: Alec Date: Sat, 10 Aug 2024 23:00:23 -0400 Subject: [PATCH 1/2] don't try to re-subscribe for same game, fix end of game score logic --- modules/MLB-API-util.js | 2 +- modules/gameday.js | 7 ++++++- modules/global-cache.js | 2 ++ modules/livefeed.js | 4 ++-- 4 files changed, 11 insertions(+), 4 deletions(-) diff --git a/modules/MLB-API-util.js b/modules/MLB-API-util.js index 60c11f7..1f11f44 100644 --- a/modules/MLB-API-util.js +++ b/modules/MLB-API-util.js @@ -157,7 +157,7 @@ module.exports = { const { WebSocket } = require('ws'); const socket = new ReconnectingWebSocket(endpoints.websocketSubscribe(gamePk), [], - { WebSocket, maxRetries: 3 } + { WebSocket, maxRetries: 1 } ); let heartbeatInterval; /* diff --git a/modules/gameday.js b/modules/gameday.js index 5a22c64..b206790 100644 --- a/modules/gameday.js +++ b/modules/gameday.js @@ -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 { diff --git a/modules/global-cache.js b/modules/global-cache.js index 0961821..0ece792 100644 --- a/modules/global-cache.js +++ b/modules/global-cache.js @@ -4,6 +4,7 @@ const values = { subscribedChannels: [], game: { currentLiveFeed: null, + currentGamePk: null, isDoubleHeader: null, lastReportedCompleteAtBatIndex: null, lastReportedPlayDescription: null, @@ -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; diff --git a/modules/livefeed.js b/modules/livefeed.js index e91fed2..0cb6504 100644 --- a/modules/livefeed.js +++ b/modules/livefeed.js @@ -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; From 25253e5d270bc31e247210a52f6d72c5f26e636f Mon Sep 17 00:00:00 2001 From: Alec Date: Sat, 10 Aug 2024 23:02:27 -0400 Subject: [PATCH 2/2] revert retries:' --- modules/MLB-API-util.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/MLB-API-util.js b/modules/MLB-API-util.js index 1f11f44..60c11f7 100644 --- a/modules/MLB-API-util.js +++ b/modules/MLB-API-util.js @@ -157,7 +157,7 @@ module.exports = { const { WebSocket } = require('ws'); const socket = new ReconnectingWebSocket(endpoints.websocketSubscribe(gamePk), [], - { WebSocket, maxRetries: 1 } + { WebSocket, maxRetries: 3 } ); let heartbeatInterval; /*