/
Tutor.elm
63 lines (50 loc) · 1.66 KB
/
Tutor.elm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
module Tutor exposing (Model, Msg, init, update, view, subscriptions)
import Array
import String
import Char
import Random
import Html exposing (..)
import Html.App as Html
import Html.Attributes exposing (..)
import Html.Events exposing (..)
import Keyboard
import Sources exposing (sources)
type alias Model =
{ sourceText : String
, attempt : String
, score: Int
}
type Msg
= Keypress Keyboard.KeyCode
| SetSentence Int
init : (Model, Cmd Msg)
init = (Model "" "" 0, Random.generate SetSentence (Random.int 0 ((Array.length sources) - 1) ) )
update : Msg -> Model -> (Model, Cmd Msg, Int)
update msg model =
case msg of
Keypress code ->
let
letter = String.fromChar ( Char.fromCode code )
len = String.length model.attempt
in
({ model |
attempt = model.attempt ++ letter
, score = model.score + if letter == (String.slice len (len + 1) model.sourceText) then 1 else -1
}, Cmd.none, model.score)
SetSentence n ->
({ model | sourceText = Maybe.withDefault "" (Array.get n sources) }, Cmd.none, model.score)
view : Model -> Html Msg
view model =
div [] (
[ span [] (List.map2 (\a b -> if a == b
then span [style [ ("color", "#0f0") ]] [text (String.fromChar a)]
else span [style [ ("color", "#f00") ]] [text (String.fromChar a)]
)
(String.toList model.sourceText) (String.toList model.attempt)
)]
++ [span [] [text (String.dropLeft (String.length model.attempt) model.sourceText)]]
++ [p [] [(span [] [text "Score: "]), (span [] [text (toString model.score)])]]
)
subscriptions : Model -> Sub Msg
subscriptions model =
Keyboard.presses Keypress