-
Notifications
You must be signed in to change notification settings - Fork 1
/
Main.elm
234 lines (194 loc) · 5.87 KB
/
Main.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
port module Main exposing (..)
import Html
exposing
( Html
, text
, input
, select
, option
, div
, h1
, label
)
import Html.Attributes exposing (value)
import Data.Difficulty exposing (Difficulty)
import View.Question
import View.Button
import View.Form
import Request.Helpers exposing (queryString)
import Util exposing (onChange, (=>), appendIf)
import Http exposing (Error)
import Request.TriviaQuestions exposing (TriviaResult)
import SharedStyles exposing (..)
import Data.TriviaZipList exposing (TriviaZipList)
{ class } =
triviaNamespace
type alias GameResults =
{ score : Int
, total : Int
}
type alias Model =
{ amount : Int
, difficulty : Difficulty
, trivia : Maybe TriviaZipList
}
init : ( Model, Cmd Msg )
init =
( Model
5
Data.Difficulty.default
Nothing
, Cmd.none
)
type Msg
= Answer String
| UpdateAmount String
| ChangeDifficulty Difficulty
| Start
| GetQuestions (Result Error TriviaResult)
| SubmitAnswers
| SavedGameResults (List GameResults)
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case Debug.log "MSG: " msg of
Answer val ->
( model.trivia
|> Maybe.map (Data.TriviaZipList.answer val)
|> Maybe.map Data.TriviaZipList.next
|> Maybe.map (\t -> { model | trivia = Just t })
|> Maybe.withDefault model
, Cmd.none
)
UpdateAmount str ->
case String.toInt str of
Ok val ->
if val > 50 then
( { model | amount = 50 }
, Cmd.none
)
else
( { model | amount = val }
, Cmd.none
)
Err err ->
( model
, Cmd.none
)
ChangeDifficulty lvl ->
( { model | difficulty = lvl }
, Cmd.none
)
Start ->
let
apiUrl str =
"https://opentdb.com/api.php" ++ str
amount =
toString model.amount
flag =
not (Data.Difficulty.isAny model.difficulty)
difficultyValue =
"difficulty" => (String.toLower (Data.Difficulty.toString model.difficulty))
params =
[ "amount" => amount ]
request =
Http.get
(params
|> appendIf flag difficultyValue
|> queryString
|> apiUrl
)
Request.TriviaQuestions.decoder
in
( model, Http.send GetQuestions request )
GetQuestions res ->
case res of
Ok { questions } ->
( { model | trivia = Just (Data.TriviaZipList.fromList questions) }
, Cmd.none
)
Err err ->
( model, Cmd.none )
_ ->
( model, Cmd.none )
--
-- SubmitAnswers ->
-- let
-- length =
-- Array.length model.questions
--
-- score =
-- Array.foldl
-- (\{ userAnswer, correct } acc ->
-- case userAnswer of
-- Just v ->
-- if v == correct then
-- acc + 1
-- else
-- acc
--
-- Nothing ->
-- acc
-- )
-- 0
-- model.questions
--
-- res =
-- GameResults score length
-- in
-- ( model, output res )
--
-- SavedGameResults res ->
-- ( model, Cmd.none )
view : Model -> Html Msg
view { amount, trivia } =
div
[ class [ TriviaContainer ] ]
[ h1 [] [ text "Open Trivia" ]
, div
[ class [ ConfigForm ] ]
[ View.Form.group
[ label [] [ text "Amount of questions: " ]
, input
[ onChange UpdateAmount
, value (toString amount)
, Html.Attributes.class "form-control"
]
[]
]
, View.Form.group
[ label [] [ text "Difficulty: " ]
, select
[ onChange (ChangeDifficulty << Data.Difficulty.get)
, Html.Attributes.class "form-control"
]
(List.map
(\key ->
option
[]
[ text key ]
)
Data.Difficulty.keys
)
]
, View.Button.btn Start "Start"
]
, case trivia of
Just zipList ->
View.Question.view (Data.TriviaZipList.current zipList) Answer
Nothing ->
text ""
-- , View.Button.btn SubmitAnswers "Submit"
]
port output : GameResults -> Cmd msg
port incoming : (List GameResults -> msg) -> Sub msg
subscriptions : Model -> Sub Msg
subscriptions model =
incoming SavedGameResults
main : Program Never Model Msg
main =
Html.program
{ init = init
, update = update
, view = view
, subscriptions = subscriptions
}