Skip to content

Commit

Permalink
almost...
Browse files Browse the repository at this point in the history
  • Loading branch information
Fedreg committed Feb 20, 2017
1 parent 69a9e5c commit cdfbdcc
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 21 deletions.
62 changes: 42 additions & 20 deletions Main.elm
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type alias Model =
, bpm : Int
, score : Int
, gameOn : Bool
, playersTurn : Bool
}


Expand All @@ -48,13 +49,14 @@ type alias PlayBundle =


model =
{ noteOptions = ["CQ4", "D#Q4", "FQ4", "GQ4", "A#Q4", "CQ5"]
{ noteOptions = [ "cq4", "d#q4", "fq4", "gq4", "a#q4", "cq5" ]
, computerNotes = []
, playerNotes = []
, index = 0
, bpm = 80
, score = 0
, gameOn = False
, playersTurn = False
}


Expand All @@ -76,7 +78,7 @@ type Msg
| AddNoteToComputerNotes Int
| AcceptNotesFromPlayer Int
| CompareResults
| SendNotes
| SendNotes Int
| LightUpDiv Int

update msg model =
Expand All @@ -88,6 +90,7 @@ update msg model =
, index = 0
, playerNotes = []
, score = 0
, bpm = 80
, gameOn = True
}, Cmd.none )
:> update GenerateRandomNote
Expand All @@ -100,8 +103,9 @@ update msg model =
noteList =
[ index ]
in
( { model | computerNotes = model.computerNotes ++ noteList }, Cmd.none)
( { model | computerNotes = model.computerNotes ++ noteList, index = 0, playerNotes = []}, Cmd.none)
:> update (LightUpDiv index)
:> update (SendNotes index)

LightUpDiv id ->
(model, blink id)
Expand All @@ -111,43 +115,60 @@ update msg model =
noteList =
[ id ]
in
( { model | playerNotes = model.playerNotes ++ noteList }, Cmd.none )
( { model | playerNotes = model.playerNotes ++ noteList, playersTurn = True }, Cmd.none )
:> update (LightUpDiv id)
:> update (SendNotes id)
:> update CompareResults

CompareResults ->
if
getAt model.index model.playerNotes == getAt model.index model.playerNotes
model.playerNotes == model.computerNotes
then
({ model | score = model.score + 10, index = model.index + 1, bpm = model.bpm + 5 }, Cmd.none)
({ model | score = model.score + 10, index = model.index + 1, bpm = model.bpm + 10 }, Cmd.none)
:> update GenerateRandomNote

else
(model, Cmd.none)
:> update StartGame
--:> update StartGame


SendNotes ->
let
index =
getAt model.index model.computerNotes
SendNotes id ->
if model.playersTurn == False
then
let
_ = Debug.log "playersTurn False"
index =
getAt id model.computerNotes

note =
getAt (Maybe.withDefault 0 index) model.noteOptions
in
( { model | index = model.index +1}
, send (PlayBundle (noteSorter <| Maybe.withDefault "cq4" note) (tempo model.bpm)))
:> update (LightUpDiv (Maybe.withDefault 1 index))

else
let
_ = Debug.log "playersTurn True"
index =
getAt id model.playerNotes

note =
getAt (Maybe.withDefault 0 index) model.noteOptions
in
( { model | playersTurn = False}
, send (PlayBundle (noteSorter <| Maybe.withDefault "cq4" note) (tempo model.bpm)))

note =
getAt (Maybe.withDefault 0 index) model.noteOptions
in
( { model | index = model.index +1, bpm = model.bpm + 5}
, send (PlayBundle (noteSorter <| Maybe.withDefault "CQ4" note) (tempo model.bpm)))


subscriptions : Model -> Sub Msg
subscriptions model =
if model.gameOn == True && model.index < List.length model.computerNotes
then
let
speed =
tempo model.bpm
(tempo model.bpm) * 4
in
Time.every (speed * second) (always SendNotes)
Time.every (speed * second) (always (SendNotes model.index))
else Sub.none


Expand Down Expand Up @@ -269,7 +290,8 @@ view model =
, colorNoteDiv 6 "purple"
]
, div [] [text ("computerNotes: " ++ Basics.toString model.computerNotes)]
, div [] [text ("playerNotes: " ++ Basics.toString model.playerNotes)]
, div [] [text ("playerNotes: " ++ Basics.toString model.playerNotes)]
, div [] [text ("index: " ++ Basics.toString model.index)]
, score model
]

Expand Down
2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
function changeColor(id) {
var id = String(id);
var el = document.getElementById(id);
el.style.transition = "transform .3s";
el.style.transition = "transform .15s";
el.style.transform = "scale(2,2)";
setTimeout( function() {
el.style.transform = "scale(1,1)";
Expand Down

0 comments on commit cdfbdcc

Please sign in to comment.