switch to a nested state object

This commit is contained in:
2023-11-24 11:32:45 -06:00
parent 190b8f838a
commit f2ad8b424c
+192 -182
View File
@@ -46,100 +46,104 @@ class Console {
*/ */
parseBaseball(buffer) { parseBaseball(buffer) {
if (!this.sportInitialized) { if (!this.sportInitialized) {
this.time = {}; this.state = {
time: {},
this.home = {}; home: {},
guest: {},
this.guest = {}; bases: {
this.bases = {}; first: false,
this.innings = { second: false,
1: { third: false,
home: {
score: 0,
},
guest: {
score: 0,
},
}, },
innings: {
1: {
home: {
score: 0,
},
guest: {
score: 0,
},
},
2: { 2: {
home: { home: {
score: 0, score: 0,
},
guest: {
score: 0,
},
}, },
guest: {
score: 0,
},
},
3: { 3: {
home: { home: {
score: 0, score: 0,
},
guest: {
score: 0,
},
}, },
guest: {
score: 0,
},
},
4: { 4: {
home: { home: {
score: 0, score: 0,
},
guest: {
score: 0,
},
}, },
guest: {
score: 0,
},
},
5: { 5: {
home: { home: {
score: 0, score: 0,
},
guest: {
score: 0,
},
}, },
guest: {
score: 0,
},
},
6: { 6: {
home: { home: {
score: 0, score: 0,
},
guest: {
score: 0,
},
}, },
guest: {
score: 0,
},
},
7: { 7: {
home: { home: {
score: 0, score: 0,
},
guest: {
score: 0,
},
}, },
guest: {
score: 0,
},
},
8: { 8: {
home: { home: {
score: 0, score: 0,
},
guest: {
score: 0,
},
}, },
guest: {
score: 0,
},
},
9: { 9: {
home: { home: {
score: 0, score: 0,
},
guest: {
score: 0,
},
}, },
guest: {
score: 0,
},
},
10: { 10: {
home: { home: {
score: 0, score: 0,
}, },
guest: { guest: {
score: 0, score: 0,
},
}, },
}, },
}; };
@@ -148,47 +152,47 @@ class Console {
const bufferHex = buffer.toString('hex'); const bufferHex = buffer.toString('hex');
this.time.minutes = buffer.subarray(5, 6).toString('hex'); this.state.time.minutes = buffer.subarray(5, 6).toString('hex');
this.time.seconds = buffer.subarray(6, 7).toString('hex'); this.state.time.seconds = buffer.subarray(6, 7).toString('hex');
this.time.tenths = bufferHex.substring(15, 16); this.state.time.tenths = bufferHex.substring(15, 16);
this.time.showTenths = bufferHex.at(8) === '1'; this.state.time.showTenths = bufferHex.at(8) === '1';
this.time.display = `${this.time.minutes}:${this.time.seconds}`; this.state.time.display = `${this.state.time.minutes}:${this.state.time.seconds}`;
const seqNum = bufferHex.substring(20, 22); const seqNum = bufferHex.substring(20, 22);
if (seqNum === '11') { if (seqNum === '11') {
const homeBinaryInfo = buffer.at(12).toString(2).padStart(8, '0'); const homeBinaryInfo = buffer.at(12).toString(2).padStart(8, '0');
const guestBinaryInfo = buffer.at(14).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') { 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') { 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.state.guest.hits = parseInt(buffer.subarray(23, 24).toString('hex'), 10);
this.home.hits = parseInt(buffer.subarray(22, 23).toString('hex'), 10); this.state.home.hits = parseInt(buffer.subarray(22, 23).toString('hex'), 10);
this.batter = buffer.subarray(21, 22).toString('hex'); this.state.batter = buffer.subarray(21, 22).toString('hex');
if (this.batter === 'aa') { if (this.state.batter === 'aa') {
this.batter = ''; this.state.batter = '';
} }
this.home.pitcher = parseInt(buffer.subarray(26, 27).toString('hex'), 10); this.state.home.pitcher = parseInt(buffer.subarray(26, 27).toString('hex'), 10);
this.guest.pitcher = parseInt(buffer.subarray(24, 25).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'); const baseBinaryInfo = buffer.at(19).toString(2).padStart(8, '0');
this.bases.first = baseBinaryInfo[3] === '1'; this.state.bases.first = baseBinaryInfo[3] === '1';
this.bases.second = baseBinaryInfo[2] === '1'; this.state.bases.second = baseBinaryInfo[2] === '1';
this.bases.third = baseBinaryInfo[1] === '1'; this.state.bases.third = baseBinaryInfo[1] === '1';
this.outs = parseInt(bufferHex.substring(37, 38), 10); this.state.outs = parseInt(bufferHex.substring(37, 38), 10);
this.strikes = parseInt(bufferHex.substring(36, 37), 10); this.state.strikes = parseInt(bufferHex.substring(36, 37), 10);
this.balls = parseInt(bufferHex.substring(39, 40), 10); this.state.balls = parseInt(bufferHex.substring(39, 40), 10);
const binaryFlags = buffer.at(12).toString(2).padStart(8, '0'); 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') { } else if (seqNum === '22') {
const guestInningStartIndex = 11; const guestInningStartIndex = 11;
const homeInningStartIndex = 21; const homeInningStartIndex = 21;
@@ -198,11 +202,11 @@ class Console {
.subarray(guestInningStartIndex + i, guestInningStartIndex + i + 1) .subarray(guestInningStartIndex + i, guestInningStartIndex + i + 1)
.toString('hex'); .toString('hex');
if (guestInningScore !== '0a') { 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'); const homeInningScore = buffer.subarray(homeInningStartIndex + i, homeInningStartIndex + i + 1).toString('hex');
if (homeInningScore !== '0a') { 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) { parseFootball(buffer) {
if (!this.sportInitialized) { if (!this.sportInitialized) {
this.time = {}; this.state = {
time: {},
playClock: {},
home: {},
guest: {},
};
this.playClock = {};
this.home = {};
this.guest = {};
this.sportInitialized = true; this.sportInitialized = true;
} }
const bufferHex = buffer.toString('hex'); const bufferHex = buffer.toString('hex');
this.time.minutes = buffer.subarray(5, 6).toString('hex'); this.state.time.minutes = buffer.subarray(5, 6).toString('hex');
this.time.seconds = buffer.subarray(6, 7).toString('hex'); this.state.time.seconds = buffer.subarray(6, 7).toString('hex');
this.time.tenths = bufferHex.substring(15, 16); this.state.time.tenths = bufferHex.substring(15, 16);
this.time.showTenths = bufferHex.at(8) === '1'; this.state.time.showTenths = bufferHex.at(8) === '1';
this.time.display = `${this.time.minutes}:${this.time.seconds}`; 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); const seqNum = bufferHex.substring(20, 22);
if (seqNum === '11') { if (seqNum === '11') {
const homeBinaryInfo = buffer.at(12).toString(2).padStart(8, '0'); const homeBinaryInfo = buffer.at(12).toString(2).padStart(8, '0');
const guestBinaryInfo = buffer.at(14).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') { 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') { if (guestBinaryInfo[0] === '1') {
this.guest.score += 100; this.state.guest.score += 100;
} }
this.home.poss = homeBinaryInfo[7] === '1'; this.state.home.poss = homeBinaryInfo[7] === '1';
this.guest.poss = guestBinaryInfo[7] === '1'; this.state.guest.poss = guestBinaryInfo[7] === '1';
this.home.timeouts = parseInt(bufferHex.substring(40, 41), 10); this.state.home.timeouts = parseInt(bufferHex.substring(40, 41), 10);
this.guest.timeouts = parseInt(bufferHex.substring(41, 42), 10); this.state.guest.timeouts = parseInt(bufferHex.substring(41, 42), 10);
this.quarter = parseInt(bufferHex.substring(31, 32), 10); this.state.quarter = parseInt(bufferHex.substring(31, 32), 10);
this.down = parseInt(bufferHex.substring(39, 40), 10); this.state.down = parseInt(bufferHex.substring(39, 40), 10);
this.yardsToGo = parseInt(buffer.subarray(18, 19).toString('hex'), 10); this.state.yardsToGo = parseInt(buffer.subarray(18, 19).toString('hex'), 10);
this.ballOn = parseInt(buffer.subarray(21, 22).toString('hex'), 10); this.state.ballOn = parseInt(buffer.subarray(21, 22).toString('hex'), 10);
} }
} }
@@ -269,45 +273,45 @@ class Console {
parseVolleyball(buffer) { parseVolleyball(buffer) {
const bufferHex = buffer.toString('hex'); const bufferHex = buffer.toString('hex');
if (!this.sportInitialized) { if (!this.sportInitialized) {
this.time = {}; this.state = {
time: {},
this.home = {}; home: {},
guest: {},
this.guest = {}; };
this.sportInitialized = true; this.sportInitialized = true;
} }
this.time.minutes = bufferHex.substring(10, 12); this.state.time.minutes = bufferHex.substring(10, 12);
this.time.seconds = bufferHex.substring(12, 14); this.state.time.seconds = bufferHex.substring(12, 14);
this.time.tenths = bufferHex.substring(15, 16); this.state.time.tenths = bufferHex.substring(15, 16);
this.time.showTenths = bufferHex.at(8) === '1'; this.state.time.showTenths = bufferHex.at(8) === '1';
this.time.display = `${this.time.minutes}:${this.time.seconds}`; this.state.time.display = `${this.state.time.minutes}:${this.state.time.seconds}`;
const seqNum = bufferHex.substring(20, 22); const seqNum = bufferHex.substring(20, 22);
if (seqNum === '11') { if (seqNum === '11') {
const homeBinaryInfo = buffer.at(12).toString(2).padStart(8, '0'); const homeBinaryInfo = buffer.at(12).toString(2).padStart(8, '0');
const guestBinaryInfo = buffer.at(14).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') { 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') { if (guestBinaryInfo[0] === '1') {
this.guest.score += 100; this.state.guest.score += 100;
} }
this.home.serve = homeBinaryInfo[7] === '1'; this.state.home.serve = homeBinaryInfo[7] === '1';
this.guest.serve = guestBinaryInfo[7] === '1'; this.state.guest.serve = guestBinaryInfo[7] === '1';
this.home.gamesWon = parseInt(buffer.subarray(16, 17).toString('hex'), 10); this.state.home.gamesWon = parseInt(buffer.subarray(16, 17).toString('hex'), 10);
this.guest.gamesWon = parseInt(buffer.subarray(17, 18).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.state.home.timeouts = parseInt(bufferHex.substring(40, 41), 10);
this.guest.timeouts = parseInt(bufferHex.substring(41, 42), 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) { parseBasketball(buffer) {
const bufferHex = buffer.toString('hex'); const bufferHex = buffer.toString('hex');
if (!this.sportInitialized) { if (!this.sportInitialized) {
this.time = {}; this.state = {
time: {},
this.shotClock = {}; shotClock: {},
home: {},
this.home = {}; guest: {},
};
this.guest = {};
this.sportInitialized = true; this.sportInitialized = true;
} }
this.time.minutes = bufferHex.substring(10, 12); this.state.time.minutes = bufferHex.substring(10, 12);
this.time.seconds = bufferHex.substring(12, 14); this.state.time.seconds = bufferHex.substring(12, 14);
this.time.tenths = bufferHex.substring(15, 16); this.state.time.tenths = bufferHex.substring(15, 16);
this.time.showTenths = bufferHex.at(8) === '1'; this.state.time.showTenths = bufferHex.at(8) === '1';
this.time.display = `${this.time.minutes}:${this.time.seconds}`; this.state.time.display = `${this.state.time.minutes}:${this.state.time.seconds}`;
if (this.time.showTenths) { if (this.state.time.showTenths) {
this.time.display += `.${this.time.tenths}`; this.state.time.display += `.${this.state.time.tenths}`;
} }
this.shotClock.seconds = bufferHex.substring(16, 18); this.state.shotClock.seconds = bufferHex.substring(16, 18);
if (this.shotClock.seconds === 'aa') { if (this.state.shotClock.seconds === 'aa') {
this.shotClock.seconds = '00'; this.state.shotClock.seconds = '00';
} }
this.shotClock.tenths = bufferHex.substring(19, 20); this.state.shotClock.tenths = bufferHex.substring(19, 20);
if (this.shotClock.tenths === 'a') { if (this.state.shotClock.tenths === 'a') {
this.shotClock.tenths = '0'; 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); const seqNum = bufferHex.substring(20, 22);
if (seqNum === '11') { if (seqNum === '11') {
const homeBinaryInfo = buffer.at(12).toString(2).padStart(8, '0'); const homeBinaryInfo = buffer.at(12).toString(2).padStart(8, '0');
const guestBinaryInfo = buffer.at(14).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') { 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') { if (guestBinaryInfo[0] === '1') {
this.guest.score += 100; this.state.guest.score += 100;
} }
this.home.poss = homeBinaryInfo[7] === '1'; this.state.home.poss = homeBinaryInfo[7] === '1';
this.guest.poss = guestBinaryInfo[7] === '1'; this.state.guest.poss = guestBinaryInfo[7] === '1';
this.home.doubleBonus = homeBinaryInfo[3] === '1'; this.state.home.doubleBonus = homeBinaryInfo[3] === '1';
this.home.bonus = homeBinaryInfo[4] === '1'; this.state.home.bonus = homeBinaryInfo[4] === '1';
this.guest.doubleBonus = guestBinaryInfo[3] === '1'; this.state.guest.doubleBonus = guestBinaryInfo[3] === '1';
this.guest.bonus = guestBinaryInfo[4] === '1'; this.state.guest.bonus = guestBinaryInfo[4] === '1';
this.home.fouls = parseInt(bufferHex.substring(32, 34), 10); this.state.home.fouls = parseInt(bufferHex.substring(32, 34), 10);
this.guest.fouls = parseInt(bufferHex.substring(34, 36), 10); this.state.guest.fouls = parseInt(bufferHex.substring(34, 36), 10);
this.home.timeouts = parseInt(bufferHex.substring(40, 41), 10); this.state.home.timeouts = parseInt(bufferHex.substring(40, 41), 10);
this.guest.timeouts = parseInt(bufferHex.substring(41, 42), 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 = { module.exports = {