From f2ad8b424c4f58afc70f4ede7b6ad58d85634ff4 Mon Sep 17 00:00:00 2001 From: Joel Wetzell Date: Fri, 24 Nov 2023 11:32:45 -0600 Subject: [PATCH] switch to a nested state object --- packages/lib/index.js | 374 ++++++++++++++++++++++-------------------- 1 file changed, 192 insertions(+), 182 deletions(-) diff --git a/packages/lib/index.js b/packages/lib/index.js index d5e3fba..8546d5e 100644 --- a/packages/lib/index.js +++ b/packages/lib/index.js @@ -46,100 +46,104 @@ class Console { */ parseBaseball(buffer) { if (!this.sportInitialized) { - this.time = {}; - - this.home = {}; - - this.guest = {}; - this.bases = {}; - this.innings = { - 1: { - home: { - score: 0, - }, - guest: { - score: 0, - }, + this.state = { + time: {}, + home: {}, + guest: {}, + bases: { + first: false, + second: false, + third: false, }, + innings: { + 1: { + home: { + score: 0, + }, + guest: { + score: 0, + }, + }, - 2: { - home: { - score: 0, + 2: { + home: { + score: 0, + }, + guest: { + score: 0, + }, }, - guest: { - score: 0, - }, - }, - 3: { - home: { - score: 0, + 3: { + home: { + score: 0, + }, + guest: { + score: 0, + }, }, - guest: { - score: 0, - }, - }, - 4: { - home: { - score: 0, + 4: { + home: { + score: 0, + }, + guest: { + score: 0, + }, }, - guest: { - score: 0, - }, - }, - 5: { - home: { - score: 0, + 5: { + home: { + score: 0, + }, + guest: { + score: 0, + }, }, - guest: { - score: 0, - }, - }, - 6: { - home: { - score: 0, + 6: { + home: { + score: 0, + }, + guest: { + score: 0, + }, }, - guest: { - score: 0, - }, - }, - 7: { - home: { - score: 0, + 7: { + home: { + score: 0, + }, + guest: { + score: 0, + }, }, - guest: { - score: 0, - }, - }, - 8: { - home: { - score: 0, + 8: { + home: { + score: 0, + }, + guest: { + score: 0, + }, }, - guest: { - score: 0, - }, - }, - 9: { - home: { - score: 0, + 9: { + home: { + score: 0, + }, + guest: { + score: 0, + }, }, - guest: { - score: 0, - }, - }, - 10: { - home: { - score: 0, - }, - guest: { - score: 0, + 10: { + home: { + score: 0, + }, + guest: { + score: 0, + }, }, }, }; @@ -148,47 +152,47 @@ class Console { const bufferHex = buffer.toString('hex'); - this.time.minutes = buffer.subarray(5, 6).toString('hex'); - this.time.seconds = buffer.subarray(6, 7).toString('hex'); - this.time.tenths = bufferHex.substring(15, 16); + this.state.time.minutes = buffer.subarray(5, 6).toString('hex'); + this.state.time.seconds = buffer.subarray(6, 7).toString('hex'); + this.state.time.tenths = bufferHex.substring(15, 16); - this.time.showTenths = bufferHex.at(8) === '1'; - this.time.display = `${this.time.minutes}:${this.time.seconds}`; + this.state.time.showTenths = bufferHex.at(8) === '1'; + this.state.time.display = `${this.state.time.minutes}:${this.state.time.seconds}`; const seqNum = bufferHex.substring(20, 22); if (seqNum === '11') { const homeBinaryInfo = buffer.at(12).toString(2).padStart(8, '0'); const guestBinaryInfo = buffer.at(14).toString(2).padStart(8, '0'); - this.home.score = parseInt(bufferHex.substring(22, 24), 10); + this.state.home.score = parseInt(bufferHex.substring(22, 24), 10); if (homeBinaryInfo[0] === '1') { - this.home.score += 100; + this.state.home.score += 100; } - this.guest.score = parseInt(bufferHex.substring(26, 28), 10); + this.state.guest.score = parseInt(bufferHex.substring(26, 28), 10); if (guestBinaryInfo[0] === '1') { - this.guest.score += 100; + this.state.guest.score += 100; } - this.guest.hits = parseInt(buffer.subarray(23, 24).toString('hex'), 10); - this.home.hits = parseInt(buffer.subarray(22, 23).toString('hex'), 10); + this.state.guest.hits = parseInt(buffer.subarray(23, 24).toString('hex'), 10); + this.state.home.hits = parseInt(buffer.subarray(22, 23).toString('hex'), 10); - this.batter = buffer.subarray(21, 22).toString('hex'); - if (this.batter === 'aa') { - this.batter = ''; + this.state.batter = buffer.subarray(21, 22).toString('hex'); + if (this.state.batter === 'aa') { + this.state.batter = ''; } - this.home.pitcher = parseInt(buffer.subarray(26, 27).toString('hex'), 10); - this.guest.pitcher = parseInt(buffer.subarray(24, 25).toString('hex'), 10); + this.state.home.pitcher = parseInt(buffer.subarray(26, 27).toString('hex'), 10); + this.state.guest.pitcher = parseInt(buffer.subarray(24, 25).toString('hex'), 10); const baseBinaryInfo = buffer.at(19).toString(2).padStart(8, '0'); - this.bases.first = baseBinaryInfo[3] === '1'; - this.bases.second = baseBinaryInfo[2] === '1'; - this.bases.third = baseBinaryInfo[1] === '1'; + this.state.bases.first = baseBinaryInfo[3] === '1'; + this.state.bases.second = baseBinaryInfo[2] === '1'; + this.state.bases.third = baseBinaryInfo[1] === '1'; - this.outs = parseInt(bufferHex.substring(37, 38), 10); - this.strikes = parseInt(bufferHex.substring(36, 37), 10); - this.balls = parseInt(bufferHex.substring(39, 40), 10); + this.state.outs = parseInt(bufferHex.substring(37, 38), 10); + this.state.strikes = parseInt(bufferHex.substring(36, 37), 10); + this.state.balls = parseInt(bufferHex.substring(39, 40), 10); const binaryFlags = buffer.at(12).toString(2).padStart(8, '0'); - this.hit = binaryFlags[4] === '1'; + this.state.hit = binaryFlags[4] === '1'; } else if (seqNum === '22') { const guestInningStartIndex = 11; const homeInningStartIndex = 21; @@ -198,11 +202,11 @@ class Console { .subarray(guestInningStartIndex + i, guestInningStartIndex + i + 1) .toString('hex'); if (guestInningScore !== '0a') { - this.innings[i + 1].guest.score = parseInt(guestInningScore, 10); + this.state.innings[i + 1].guest.score = parseInt(guestInningScore, 10); } const homeInningScore = buffer.subarray(homeInningStartIndex + i, homeInningStartIndex + i + 1).toString('hex'); if (homeInningScore !== '0a') { - this.innings[i + 1].home.score = parseInt(homeInningScore, 10); + this.state.innings[i + 1].home.score = parseInt(homeInningScore, 10); } } } @@ -214,51 +218,51 @@ class Console { */ parseFootball(buffer) { if (!this.sportInitialized) { - this.time = {}; + this.state = { + time: {}, + playClock: {}, + home: {}, + guest: {}, + }; - this.playClock = {}; - - this.home = {}; - - this.guest = {}; this.sportInitialized = true; } const bufferHex = buffer.toString('hex'); - this.time.minutes = buffer.subarray(5, 6).toString('hex'); - this.time.seconds = buffer.subarray(6, 7).toString('hex'); - this.time.tenths = bufferHex.substring(15, 16); + this.state.time.minutes = buffer.subarray(5, 6).toString('hex'); + this.state.time.seconds = buffer.subarray(6, 7).toString('hex'); + this.state.time.tenths = bufferHex.substring(15, 16); - this.time.showTenths = bufferHex.at(8) === '1'; - this.time.display = `${this.time.minutes}:${this.time.seconds}`; + this.state.time.showTenths = bufferHex.at(8) === '1'; + this.state.time.display = `${this.state.time.minutes}:${this.state.time.seconds}`; - this.playClock.seconds = bufferHex.substring(16, 18); + this.state.playClock.seconds = bufferHex.substring(16, 18); const seqNum = bufferHex.substring(20, 22); if (seqNum === '11') { const homeBinaryInfo = buffer.at(12).toString(2).padStart(8, '0'); const guestBinaryInfo = buffer.at(14).toString(2).padStart(8, '0'); - this.home.score = parseInt(bufferHex.substring(22, 24), 10); + this.state.home.score = parseInt(bufferHex.substring(22, 24), 10); if (homeBinaryInfo[0] === '1') { - this.home.score += 100; + this.state.home.score += 100; } - this.guest.score = parseInt(bufferHex.substring(26, 28), 10); + this.state.guest.score = parseInt(bufferHex.substring(26, 28), 10); if (guestBinaryInfo[0] === '1') { - this.guest.score += 100; + this.state.guest.score += 100; } - this.home.poss = homeBinaryInfo[7] === '1'; - this.guest.poss = guestBinaryInfo[7] === '1'; + this.state.home.poss = homeBinaryInfo[7] === '1'; + this.state.guest.poss = guestBinaryInfo[7] === '1'; - this.home.timeouts = parseInt(bufferHex.substring(40, 41), 10); - this.guest.timeouts = parseInt(bufferHex.substring(41, 42), 10); + this.state.home.timeouts = parseInt(bufferHex.substring(40, 41), 10); + this.state.guest.timeouts = parseInt(bufferHex.substring(41, 42), 10); - this.quarter = parseInt(bufferHex.substring(31, 32), 10); - this.down = parseInt(bufferHex.substring(39, 40), 10); - this.yardsToGo = parseInt(buffer.subarray(18, 19).toString('hex'), 10); - this.ballOn = parseInt(buffer.subarray(21, 22).toString('hex'), 10); + this.state.quarter = parseInt(bufferHex.substring(31, 32), 10); + this.state.down = parseInt(bufferHex.substring(39, 40), 10); + this.state.yardsToGo = parseInt(buffer.subarray(18, 19).toString('hex'), 10); + this.state.ballOn = parseInt(buffer.subarray(21, 22).toString('hex'), 10); } } @@ -269,45 +273,45 @@ class Console { parseVolleyball(buffer) { const bufferHex = buffer.toString('hex'); if (!this.sportInitialized) { - this.time = {}; - - this.home = {}; - - this.guest = {}; + this.state = { + time: {}, + home: {}, + guest: {}, + }; this.sportInitialized = true; } - this.time.minutes = bufferHex.substring(10, 12); - this.time.seconds = bufferHex.substring(12, 14); - this.time.tenths = bufferHex.substring(15, 16); + this.state.time.minutes = bufferHex.substring(10, 12); + this.state.time.seconds = bufferHex.substring(12, 14); + this.state.time.tenths = bufferHex.substring(15, 16); - this.time.showTenths = bufferHex.at(8) === '1'; - this.time.display = `${this.time.minutes}:${this.time.seconds}`; + this.state.time.showTenths = bufferHex.at(8) === '1'; + this.state.time.display = `${this.state.time.minutes}:${this.state.time.seconds}`; const seqNum = bufferHex.substring(20, 22); if (seqNum === '11') { const homeBinaryInfo = buffer.at(12).toString(2).padStart(8, '0'); const guestBinaryInfo = buffer.at(14).toString(2).padStart(8, '0'); - this.home.score = parseInt(bufferHex.substring(22, 24), 10); + this.state.home.score = parseInt(bufferHex.substring(22, 24), 10); if (homeBinaryInfo[0] === '1') { - this.home.score += 100; + this.state.home.score += 100; } - this.guest.score = parseInt(bufferHex.substring(26, 28), 10); + this.state.guest.score = parseInt(bufferHex.substring(26, 28), 10); if (guestBinaryInfo[0] === '1') { - this.guest.score += 100; + this.state.guest.score += 100; } - this.home.serve = homeBinaryInfo[7] === '1'; - this.guest.serve = guestBinaryInfo[7] === '1'; + this.state.home.serve = homeBinaryInfo[7] === '1'; + this.state.guest.serve = guestBinaryInfo[7] === '1'; - this.home.gamesWon = parseInt(buffer.subarray(16, 17).toString('hex'), 10); - this.guest.gamesWon = parseInt(buffer.subarray(17, 18).toString('hex'), 10); + this.state.home.gamesWon = parseInt(buffer.subarray(16, 17).toString('hex'), 10); + this.state.guest.gamesWon = parseInt(buffer.subarray(17, 18).toString('hex'), 10); - this.home.timeouts = parseInt(bufferHex.substring(40, 41), 10); - this.guest.timeouts = parseInt(bufferHex.substring(41, 42), 10); + this.state.home.timeouts = parseInt(bufferHex.substring(40, 41), 10); + this.state.guest.timeouts = parseInt(bufferHex.substring(41, 42), 10); - this.game = parseInt(bufferHex.substring(31, 32), 10); + this.state.game = parseInt(bufferHex.substring(31, 32), 10); } } @@ -318,71 +322,77 @@ class Console { parseBasketball(buffer) { const bufferHex = buffer.toString('hex'); if (!this.sportInitialized) { - this.time = {}; - - this.shotClock = {}; - - this.home = {}; - - this.guest = {}; + this.state = { + time: {}, + shotClock: {}, + home: {}, + guest: {}, + }; this.sportInitialized = true; } - this.time.minutes = bufferHex.substring(10, 12); - this.time.seconds = bufferHex.substring(12, 14); - this.time.tenths = bufferHex.substring(15, 16); + this.state.time.minutes = bufferHex.substring(10, 12); + this.state.time.seconds = bufferHex.substring(12, 14); + this.state.time.tenths = bufferHex.substring(15, 16); - this.time.showTenths = bufferHex.at(8) === '1'; - this.time.display = `${this.time.minutes}:${this.time.seconds}`; + this.state.time.showTenths = bufferHex.at(8) === '1'; + this.state.time.display = `${this.state.time.minutes}:${this.state.time.seconds}`; - if (this.time.showTenths) { - this.time.display += `.${this.time.tenths}`; + if (this.state.time.showTenths) { + this.state.time.display += `.${this.state.time.tenths}`; } - this.shotClock.seconds = bufferHex.substring(16, 18); - if (this.shotClock.seconds === 'aa') { - this.shotClock.seconds = '00'; + this.state.shotClock.seconds = bufferHex.substring(16, 18); + if (this.state.shotClock.seconds === 'aa') { + this.state.shotClock.seconds = '00'; } - this.shotClock.tenths = bufferHex.substring(19, 20); - if (this.shotClock.tenths === 'a') { - this.shotClock.tenths = '0'; + this.state.shotClock.tenths = bufferHex.substring(19, 20); + if (this.state.shotClock.tenths === 'a') { + this.state.shotClock.tenths = '0'; } - this.shotClock.display = `${this.shotClock.seconds}.${this.shotClock.tenths}`; + this.state.shotClock.display = `${this.state.shotClock.seconds}.${this.state.shotClock.tenths}`; const seqNum = bufferHex.substring(20, 22); if (seqNum === '11') { const homeBinaryInfo = buffer.at(12).toString(2).padStart(8, '0'); const guestBinaryInfo = buffer.at(14).toString(2).padStart(8, '0'); - this.home.score = parseInt(bufferHex.substring(22, 24), 10); + this.state.home.score = parseInt(bufferHex.substring(22, 24), 10); if (homeBinaryInfo[0] === '1') { - this.home.score += 100; + this.state.home.score += 100; } - this.guest.score = parseInt(bufferHex.substring(26, 28), 10); + this.state.guest.score = parseInt(bufferHex.substring(26, 28), 10); if (guestBinaryInfo[0] === '1') { - this.guest.score += 100; + this.state.guest.score += 100; } - this.home.poss = homeBinaryInfo[7] === '1'; - this.guest.poss = guestBinaryInfo[7] === '1'; + this.state.home.poss = homeBinaryInfo[7] === '1'; + this.state.guest.poss = guestBinaryInfo[7] === '1'; - this.home.doubleBonus = homeBinaryInfo[3] === '1'; - this.home.bonus = homeBinaryInfo[4] === '1'; + this.state.home.doubleBonus = homeBinaryInfo[3] === '1'; + this.state.home.bonus = homeBinaryInfo[4] === '1'; - this.guest.doubleBonus = guestBinaryInfo[3] === '1'; - this.guest.bonus = guestBinaryInfo[4] === '1'; + this.state.guest.doubleBonus = guestBinaryInfo[3] === '1'; + this.state.guest.bonus = guestBinaryInfo[4] === '1'; - this.home.fouls = parseInt(bufferHex.substring(32, 34), 10); - this.guest.fouls = parseInt(bufferHex.substring(34, 36), 10); + this.state.home.fouls = parseInt(bufferHex.substring(32, 34), 10); + this.state.guest.fouls = parseInt(bufferHex.substring(34, 36), 10); - this.home.timeouts = parseInt(bufferHex.substring(40, 41), 10); - this.guest.timeouts = parseInt(bufferHex.substring(41, 42), 10); + this.state.home.timeouts = parseInt(bufferHex.substring(40, 41), 10); + this.state.guest.timeouts = parseInt(bufferHex.substring(41, 42), 10); - this.period = parseInt(bufferHex.substring(31, 32), 10); + this.state.period = parseInt(bufferHex.substring(31, 32), 10); } } + + toJSON() { + return { + sport: this.sport, + ...this.state, + }; + } } module.exports = {