From 941f3748edf01d168fd134e2dc3105e3bb51f310 Mon Sep 17 00:00:00 2001 From: spencerkee <spencerkee@users.noreply.github.com> Date: Thu, 20 Mar 2025 22:21:18 +0000 Subject: [PATCH] Refactor --- app.js | 184 ++++++++++++++++++++++++++++++----------------------- index.html | 1 + 2 files changed, 105 insertions(+), 80 deletions(-) diff --git a/app.js b/app.js index 17cce35..94f05ee 100644 --- a/app.js +++ b/app.js @@ -37,6 +37,7 @@ const latencyDiv = document.getElementById("latency"); const latencyScan = document.getElementById("scan"); // buttons +const neutral = document.getElementById("neutral"); const up = document.getElementById("up"); const down = document.getElementById("down"); const left = document.getElementById("left"); @@ -74,8 +75,8 @@ const recordingButtons = [ const codeDataButtonIndexes = [ [14, 19], // Neutral - [15, 19], // Up - [15, 19], // Down + [14, 19], // Up + [14, 19], // Down [15, 19], // Left [15, 19], // Right [0, 5], // A @@ -137,7 +138,7 @@ recordingButtons.forEach((button, index) => { const sum = counts.reduce((partialSum, a) => partialSum + a, 0); let threshold = 0; if (index === 0) { - threshold = sum * 0.80; + threshold = sum * 0.90; } else { threshold = sum * 0.40; } @@ -147,71 +148,89 @@ recordingButtons.forEach((button, index) => { // where each element is [code.data, { count: count }] sortedRecordedData = sortedRecordedData.map(data => [data[0], data[1].count]); + let annotatedVals = sortedRecordedData.map(data => { + return [data[0].substring( + codeDataButtonIndexes[index][0], + codeDataButtonIndexes[index][1] + ), data[0], data[1], getButtonReadouts(data[0])]; + }); + + let filteredAnnotatedVals = annotatedVals.filter((relevantSlice, codeData, count, readouts) => { + if (codeDataButtonSubstrings[index].includes(relevantSlice)) { + console.log(`Found ${relevantSlice} in codeDataButtonSubstrings[${index}]`); + return false; + } + for (let i = 0; i < codeDataButtonSubstrings[index].length; i++) { + // Skip self. + if (i === index) { + continue + } + let sliceStartStop = codeDataButtonIndexes[i]; + // Check if indexes are irrelevant. + if (!sliceStartStop === codeDataButtonIndexes[index]) { + continue; + } + // Check if relevantSlice is in found for another button + if (codeDataButtonSubstrings[index].includes(relevantSlice)) { + console.log(`Found ${relevantSlice} in codeDataButtonSubstrings[${i}]`); + return false; + } + } + return true; + }); // Create accumulator and iterate over sortedRecordedData and filter out values // below the threshold let bestValues = []; let accumulator = 0; // TODO Replace this with a foreach - for (let i = 0; i < sortedRecordedData.length; i++) { + for (let i = 0; i < filteredAnnotatedVals.length; i++) { if (accumulator > threshold) { break; } - bestValues.push([sortedRecordedData[i][0], getButtonReadouts(sortedRecordedData[i][0])]); - accumulator += sortedRecordedData[i][1]; + bestValues.push(filteredAnnotatedVals[i][0]); + accumulator += filteredAnnotatedVals[i][2]; } - bestValues.forEach((codeData, buttonReadout) => { - console.log('me'); + // debugger; + // let stringBestValues = bestValues.map(a => a[0]); + bestValues.forEach(relevantSlice => { + console.log(`Adding ${relevantSlice} to codeDataButtonSubstrings[${index}]`); + codeDataButtonSubstrings[index].push(relevantSlice); }); + console.log(codeDataButtonSubstrings[index]); + }; +}); - let stringBestValues = bestValues.map(a => a[0]); - stringBestValues.forEach(codeData => { - let relevantSlice = codeData.substring( - codeDataButtonIndexes[index][0], - codeDataButtonIndexes[index][1] - ); - // Check if relevantSlice is in codeDataButtonSubstrings[index] - if (!codeDataButtonSubstrings[index].includes(relevantSlice)) { - codeDataButtonSubstrings[index].push(relevantSlice); - console.log(`Adding ${relevantSlice} to codeDataButtonSubstrings[${index}]`); - } - }); - - // if (index === 0) { - // // TODO Only add unique ones. - // codeDataButtonSubstrings[0].push(...bestValues.map(codeData => codeData[0])); - // let junk = analyze(codeDataButtonSubstrings[0]); - // debugger; - // } else { - - - - // let junk = analyze(codeDataButtonSubstrings[index]); - - // let minDiff = Infinity; - // let minDiffString = ""; - // bestValues.forEach(possibleInput => { - // // Find the string in codeDataButtonSubstrings[0] which differes from - // // possibleInput by the least characters - // codeDataButtonSubstrings[0].forEach(neutralCodeData => { - // let diff = 0; - // for (let i = 0; i < neutralCodeData.length; i++) { - // if (neutralCodeData[i] !== possibleInput[0][i]) { - // diff++; - // } - // } - // if (diff < minDiff) { - // minDiff = diff; - // minDiffString = neutralCodeData; - // } - // }); - // }); - - debugger; - } -} -}); +// if (index === 0) { +// // TODO Only add unique ones. +// codeDataButtonSubstrings[0].push(...bestValues.map(codeData => codeData[0])); +// let junk = analyze(codeDataButtonSubstrings[0]); +// debugger; +// } else { + + + +// let junk = analyze(codeDataButtonSubstrings[index]); + +// let minDiff = Infinity; +// let minDiffString = ""; +// bestValues.forEach(possibleInput => { +// // Find the string in codeDataButtonSubstrings[0] which differes from +// // possibleInput by the least characters +// codeDataButtonSubstrings[0].forEach(neutralCodeData => { +// let diff = 0; +// for (let i = 0; i < neutralCodeData.length; i++) { +// if (neutralCodeData[i] !== possibleInput[0][i]) { +// diff++; +// } +// } +// if (diff < minDiff) { +// minDiff = diff; +// minDiffString = neutralCodeData; +// } +// }); +// }); // let time; // recordNetural.onpointerdown = function () { @@ -309,6 +328,7 @@ select.onpointerup = () => { function checkButtonStatus() { const pressed = { + neutral: qr.neutral, left: pressedButtons.left || qr.left, right: pressedButtons.right || qr.right, up: pressedButtons.up || qr.up, @@ -318,7 +338,7 @@ function checkButtonStatus() { start: pressedButtons.start || qr.start, select: pressedButtons.select || qr.select, }; - + neutral.style = pressed.neutral ? "background-color: red;" : ""; up.style = pressed.up ? "background-color: red;" : ""; down.style = pressed.down ? "background-color: red;" : ""; left.style = pressed.left ? "background-color: red;" : ""; @@ -389,14 +409,14 @@ let codeDataButtonSubstrings = [ [], // Up [], // Down [ - "OP3%S", - "OP9OS", - "OOP8S", - "OOI3S", - "OP9+S", - "OP8$S", - "OP8$S", - "OP98S", + // "OP3%S", + // "OP9OS", + // "OOP8S", + // "OOI3S", + // "OP9+S", + // "OP8$S", + // "OP8$S", + // "OP98S", ], // Left [ // "OO$:S", @@ -446,19 +466,23 @@ function getButtonReadouts(codeData) { codeDataButtonIndexes[5][0], codeDataButtonIndexes[5][1] ); + if (codeDataButtonSubstrings[0].some((substring) => directionComponent === substring)) { + // console.log(directionComponent); + localQr.neutral = true; + } else { + // Left + if ( + codeDataButtonSubstrings[3].some((substring) => directionComponent === substring) + ) { + localQr.left = true; + } - // Left - if ( - codeDataButtonSubstrings[3].some((substring) => directionComponent === substring) - ) { - localQr.left = true; - } - - // Right - if ( - codeDataButtonSubstrings[4].some((substring) => directionComponent === substring) - ) { - localQr.right = true; + // Right + if ( + codeDataButtonSubstrings[4].some((substring) => directionComponent === substring) + ) { + localQr.right = true; + } } // A button @@ -541,11 +565,11 @@ function tick() { } // console.log(recordingButtonIsHeldArr); - console.log(recordedData[0]); + // console.log(recordedData[0]); // Log sorted recordedData[0] to the console - console.log( - Object.entries(recordedData[0]).sort((a, b) => b[1].count - a[1].count), - ); + // console.log( + // Object.entries(recordedData[0]).sort((a, b) => b[1].count - a[1].count), + // ); if (!datas[code.data]) { diff --git a/index.html b/index.html index 9587d95..1ab6a10 100644 --- a/index.html +++ b/index.html @@ -15,6 +15,7 @@ </div> <div style="float: right; width: 50%;"> <canvas id="nes-canvas" width="256" height="240" style="width: 100%"></canvas> + <button id="neutral">Neutral</button> <button id="up">Up</button> <button id="down">Down</button> <button id="left">Left</button> -- GitLab