Skip to content

Instantly share code, notes, and snippets.

@rollie42
Created May 4, 2022 10:59
Show Gist options
  • Save rollie42/4fad6b50e9394b132565ae9d946edd88 to your computer and use it in GitHub Desktop.
Save rollie42/4fad6b50e9394b132565ae9d946edd88 to your computer and use it in GitHub Desktop.
// This is strictly for aesthetic purposes - it can be ignored
const mask = "01101001010010100111101010001110101011010101011110100101010101010101010010101010011010101"
export const serializeSettings = (settings) => {
const vals = []
vals.push(settings.language === 'English')
vals.push(settings.ordering === 'Alphabetical')
vals.push(settings.keyboardInterface.level > 1)
vals.push(settings.keyboardInterface.level > 0)
for (const animal of settings.content) {
vals.push(animal.active)
}
let arr = BitArray.from(vals)
// console.log('Orig arr', arr)
arr = arr['^'](BitArray.from(mask.slice(0, vals.length)))
// console.log('Masked arr', arr)
const encoded = encode(arr.buffer).replaceAll("+", "-").replaceAll("/", "_")
return encoded
}
export const deserializeSettings = (encoded) => {
const buffer = decode(encoded.replaceAll("-", "+").replaceAll("_", "/"))
const vals = []
for(const b of new Uint8Array(buffer)) {
for (let i = 0; i < 8; i++) {
vals.push(!!(b & (1 << i)))
}
}
let arr = BitArray.from(vals)
//console.log('Masked arr', arr)
arr = arr['^'](BitArray.from(mask.slice(0, vals.length)))
//console.log('Orig arr', arr)
const settings = {
}
const itr = arr.values()
const v = () => itr.next().value
settings.language = v() ? 'English' : 'Japanese'
settings.ordering = v() ? 'Alphabetical' : 'Shuffle'
const targetLevel = v() * 2 + v()
settings.keyboardInterface = keyboardOptions.find(o => o.level === targetLevel)
settings.content = Object.values(animals).map(animal => ({...animal, active: v()}))
return settings
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment