Skip to content

Instantly share code, notes, and snippets.

@wolfflow
Created January 25, 2013 15:31
Show Gist options
  • Save wolfflow/4635276 to your computer and use it in GitHub Desktop.
Save wolfflow/4635276 to your computer and use it in GitHub Desktop.
BaconJS Konami code timed sequence
arrayEquals = (a,b) -> $(a).not(b).length == 0 && $(b).not(a).length == 0
always = (value) -> (_) -> value
allKeyUps = $(document).asEventStream("keyup")
keyCodeIs = (keyCode) -> (event) -> event.keyCode is keyCode
keyUps = (keyCode) -> allKeyUps.filter keyCodeIs(keyCode)
keyState = (keyCode, value) -> keyUps(keyCode).map(always(value))
arrowKeyState = Bacon.mergeAll([
keyState(38, "UP"),
keyState(40, "DOWN"),
keyState(37, "LEFT"),
keyState(39, "RIGHT"),
keyState(66, "B"),
keyState(65, "A")
])
code = ["UP", "UP", "DOWN", "DOWN", "LEFT", "RIGHT", "LEFT", "RIGHT", "B", "A", "B", "A"]
buf = arrowKeyState.bufferWithTime(2500)
buf2 = arrowKeyState.bufferWithCount(code.length)
seqObserver = buf.concat(buf2)
seqObserver.onValue((x)-> console.log "KONAMI CODE DONE!" if arrayEquals(x,code))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment