Skip to content

Instantly share code, notes, and snippets.

@jinjor
Last active February 3, 2017 03:23
Show Gist options
  • Select an option

  • Save jinjor/8cf79904c004dd924e7ce3313135795f to your computer and use it in GitHub Desktop.

Select an option

Save jinjor/8cf79904c004dd924e7ce3313135795f to your computer and use it in GitHub Desktop.
<html>
<script src="konami.js"></script>
<script>
var app = Elm.Konami.worker();
app.ports.succeed.subscribe(function() {
alert('success!');
});
</script>
</html>
port module Konami exposing (..)
import Char
import Keyboard exposing (KeyCode)
main =
Platform.program
{ init = init
, update = update
, subscriptions = subscriptions
}
-- PORT
port succeed : () -> Cmd msg
-- COMMAND
up = 38
down = 40
left = 37
right = 39
b = Char.toCode 'B'
a = Char.toCode 'A'
command : List KeyCode
command =
[ up, up, down, down, left, right, left, right, b, a ]
-- MODEL
type alias Model = List KeyCode
init : (Model, Cmd Msg)
init =
command ! []
-- UPDATE
type Msg
= Key KeyCode
update : Msg -> Model -> (Model, Cmd Msg)
update msg model =
case msg of
Key keyCode ->
case model of
key :: tail ->
if key == keyCode then
(tail, if tail == [] then succeed () else Cmd.none)
else
command ! []
_ ->
model ! []
-- SUBSCRIPTIONS
subscriptions : Model -> Sub Msg
subscriptions model =
Keyboard.downs Key
@jinjor
Copy link
Author

jinjor commented Feb 3, 2017

This is an example usage of Platform.program.

elm-make Konami.elm --output=konami.js

This app handles Konami Code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment