Skip to content

Instantly share code, notes, and snippets.

@bevacqua
Created May 9, 2017 02:31
Show Gist options
  • Save bevacqua/4a06f4f2332663bd1d46952e528578d2 to your computer and use it in GitHub Desktop.
Save bevacqua/4a06f4f2332663bd1d46952e528578d2 to your computer and use it in GitHub Desktop.
'use strict'
const path = require(`path`)
const glob = require(`glob`)
const ponytars = glob.sync(`.bin/semipublic/ponytars/*.png`)
// app.get('/ponytar/:hash.png', getPonytar)
function getPonytar(req, res) {
const { hash } = req.params
const seed = seedFromHash(hash)
const random = randomFromSeed(seed)
const index = Math.floor(random() * ponytars.length)
const ponytar = ponytars[index]
res.sendFile(path.resolve(ponytar))
}
function randomFromSeed(seed) {
return () => {
const x = Math.sin(seed++) * 1000
return x - Math.floor(x)
}
}
function seedFromHash(hash) {
const rnotation = /[.e+]/g
const integer = parseInt(hash, 16).toString()
const seed = integer.toString().replace(rnotation, ``)
return seed / 100000
}
module.exports = getPonytar
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment