Last active
December 10, 2024 12:58
-
-
Save Tekunogosu/12f70171cb9a61405ed719a8efca618b to your computer and use it in GitHub Desktop.
Hackmud: Cleans QRCodes for T2 missions - May work on other things
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function (context, args) { | |
let qrstates = []; | |
let corruptChars = #fs.scripts.lib().corruption_chars.split('') | |
function buildQRPattern() { | |
// Convert string to array of characters | |
const chars = "<qr missing>".split(''); | |
// For each character, create a pattern that matches either the original | |
// character or any corruption character | |
const pattern = chars.map(char => { | |
// Escape special regex characters if present | |
const escapedChar = char.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); | |
// Create a character class that includes original char and corruption chars | |
return `[${escapedChar}${corruptChars.join('')}]`; | |
}).join(''); | |
return new RegExp(`^${pattern}$`); | |
} | |
let missingPattern = buildQRPattern() | |
function isCorrupt(char) { | |
return corruptChars.includes(char) | |
} | |
function printQRCodes() { | |
qrstates.forEach(qr => #D(qr.qr.join(''))) | |
} | |
// grab initial state | |
let first = args.s.call(args.arg) | |
if (first.includes("Shift operation is currently in progress.")) return first | |
let p = new RegExp('[<>]'); | |
let invalidChars = new RegExp('^[a-zA-Z0-9`]$') | |
first.filter((value, index) => { | |
if(missingPattern.test(value) || p.test(value)) return false | |
const qr = value.split('').filter(str => !invalidChars.test(str)) | |
qrstates.push({ | |
index: index, | |
qr: qr, | |
corruptedIndexes: qr.reduce((acc,value,index) => { | |
if (isCorrupt(qr[index])) acc.push(index) | |
return acc | |
}, []) | |
}) | |
return true | |
}) | |
// iterate until there are no more corrupted characters | |
while(qrstates.some(state => state.corruptedIndexes.length > 0)) { | |
let res = args.s.call(args.arg) | |
qrstates.forEach(state => { | |
// get the qr code from our call to the api | |
// and strip all unwanted characters. [a-zA-Z0-9`] | |
let newCode = res[state.index].split('').filter(str => !invalidChars.test(str)) | |
// iterate through the corrupted indexes and replace none corrupted chars | |
state.corruptedIndexes = state.corruptedIndexes.reduce((acc, value) => { | |
if (!isCorrupt(newCode[value])) | |
state.qr[value] = newCode[value] | |
else | |
acc.push(value) | |
return acc | |
}, []) | |
}) | |
} | |
printQRCodes() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment