Commit 15923e89 authored by Aral Balkan's avatar Aral Balkan
Browse files

Codes now all start with a word from the HHGTTG and include a hashid at the...

Codes now all start with a word from the HHGTTG and include a hashid at the end that can be decoded to give the index of the user (an indication of how early they supported us)
parent 1131fb5d
shorthash = require 'shorthash'
uuid = require 'uuid'
fs = require 'fs'
assert = require 'assert'
#
# Implementation of hash sieve algorithm that I read
# about at http://www.shamasis.net/2009/09/fast-algorithm-to-find-unique-items-in-javascript-array/
# in CoffeeScript with slightly more literate code :)
#
Array.prototype.unique = ->
hashSieve = {}
arrayOfUniqueValues = [];
for item in @
hashSieve[item] = item
for item of hashSieve
arrayOfUniqueValues.push item
return arrayOfUniqueValues
class CodeGenerator
secret: ''
folder: null
folderExists: false
constructor: ->
# We put codes generated in each session into their own timestamped folder so we
# do not accidentally overwrite earlier ones
@folder = 'codes/' + Date.now()
if fs.existsSync('secret.txt')
@secret = fs.readFileSync 'secret.txt'
generateUniqueCodes: (numCodes) =>
codes = []
i = numCodes
while i
uniqueCode = uuid.v4() + @secret + '-' + Date.now()
shortUniqueCode = shorthash.unique(uniqueCode)
codes.push shortUniqueCode
i--
codes = codes.unique()
# Just to make sure.
assert.equal codes.length, numCodes, 'All codes must be unique.'
return codes
generateUniqueCodesAndSaveToFile: (numCodes, fileNameToSaveTo) =>
codes = @generateUniqueCodes(numCodes)
if not @folderExists
fs.mkdirSync @folder
@folderExists = true
fileNameJSON = @folder + '/' + fileNameToSaveTo + '.json'
fs.writeFileSync fileNameJSON, JSON.stringify(codes)
module.exports = CodeGenerator
\ No newline at end of file
This source diff could not be displayed because it is too large. You can view the blob instead.
This source diff could not be displayed because it is too large. You can view the blob instead.
This diff is collapsed.
This source diff could not be displayed because it is too large. You can view the blob instead.
assert = require 'assert'
fs = require 'fs'
Picker = (require 'RandomSelection').Picker
Hashids = require 'hashids'
secret = 'M&dGJ=Vv%H7gV@6Giv(pk7v38aMQ7G;vUNm2^Vb@$Ep9i7P9}r'
hashids = new Hashids secret
#
# Load all the unique words from The Hitchhiker’s Guide to the Galaxy
#
hhgttg = JSON.parse(fs.readFileSync('all-unique-words-in-the-hitchhikers-guide.json'))
hhgttgPicker = new Picker(hhgttg)
#
# Implementation of hash sieve algorithm that I read
# about at http://www.shamasis.net/2009/09/fast-algorithm-to-find-unique-items-in-javascript-array/
# in CoffeeScript with slightly more literate code :)
#
Array.prototype.unique = ->
hashSieve = {}
arrayOfUniqueValues = [];
for item in @
hashSieve[item] = item
for item of hashSieve
arrayOfUniqueValues.push item
return arrayOfUniqueValues
#
# Generates unique codes (based on the secret, above)
#
generateCodes = (fromId, toId) ->
numCodes = toId - fromId + 1
codes = []
count = 0
for i in [fromId..toId]
count++
process.stdout.write 'Generating code #' + count + ' (ID: '+i+') \r'
code = hashids.encode i
code = hhgttgPicker.pick() + '-' + code
codes.push code
codes = codes.unique()
assert.equal numCodes, codes.length, 'All codes should be unique.'
console.log 'Generated ' + numCodes + ' codes (from ID ' + fromId + ' to ID ' + toId + ')'
return codes
generateCodesAndSaveToFile = (fromId, toId, fileName) =>
codes = generateCodes fromId, toId
fs.writeFileSync fileName + '.json', JSON.stringify(codes)
#
# Generate waystone codes
#
teamFriendsAndFamilyCodes = generateCodesAndSaveToFile 1, 100, 'waystone-friends-and-family-access-codes'
tier1000Codes = generateCodesAndSaveToFile 101, 1100, 'waystone-tier-1000-access-codes'
tier100Codes = generateCodesAndSaveToFile 1101, 2100, 'waystone-tier-100-access-codes'
CodeGenerator = require './CodeGenerator.coffee'
codeGenerator = new CodeGenerator()
codeGenerator.generateUniqueCodesAndSaveToFile 1000, 'tier-100'
codeGenerator.generateUniqueCodesAndSaveToFile 1000, 'tier-1000'
......@@ -12,7 +12,10 @@
"author": "Aral Balkan",
"license": "Closed Source (this needs to be private)",
"dependencies": {
"RandomSelection": "^1.0.1",
"assert": "^1.1.2",
"hashids": "1.0.1",
"random-word": "^1.0.0",
"shorthash": "0.0.2",
"uuid": "^2.0.1"
}
......
assert = require 'assert'
fs = require 'fs'
#
# Implementation of hash sieve algorithm that I read
# about at http://www.shamasis.net/2009/09/fast-algorithm-to-find-unique-items-in-javascript-array/
# in CoffeeScript with slightly more literate code :)
#
Array.prototype.unique = ->
hashSieve = {}
arrayOfUniqueValues = [];
for item in @
hashSieve[item] = item
for item of hashSieve
arrayOfUniqueValues.push item
return arrayOfUniqueValues
#
# Test that codes really are unique.
#
a = JSON.parse(fs.readFileSync('codes/1414632363426/tier-100.json'))
b = JSON.parse(fs.readFileSync('codes/1414632363426/tier-1000.json'))
c = a.concat b
c = c.unique()
assert.equal c.length, a.length + b.length, 'Codes should be unique across both tiers'
console.log 'All codes are unique across the $100 and $1,000 tiers.'
\ No newline at end of file
assert = require 'assert'
CodeGenerator = require './CodeGenerator.coffee'
#
# Implementation of hash sieve algorithm that I read
# about at http://www.shamasis.net/2009/09/fast-algorithm-to-find-unique-items-in-javascript-array/
# in CoffeeScript with slightly more literate code :)
#
Array.prototype.unique = ->
hashSieve = {}
arrayOfUniqueValues = [];
for item in @
hashSieve[item] = item
for item of hashSieve
arrayOfUniqueValues.push item
return arrayOfUniqueValues
#
# Test that codes really are unique.
#
codeGenerator = new CodeGenerator()
numCodes = 10000
numTimes = 1000
while numTimes
process.stdout.write 'Running test #' + numTimes + ' \r'
codes = codeGenerator.generateUniqueCodes(numCodes)
codes = codes.unique()
assert.equal codes.length, numCodes, 'There should be ' + numCodes + ' unique codes.'
numTimes--
#
# And that our hash sieve is working :)
#
testArray =['hello', 'there', 'hello', 'again']
testArray = testArray.unique()
assert.equal testArray.length, 3, 'There should be 3 items.'
assert.equal testArray[2], 'again', 'Third item should be “again”'
console.log '\nTESTS OK.'
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment