-
Notifications
You must be signed in to change notification settings - Fork 0
/
question.elm
288 lines (245 loc) · 9.35 KB
/
question.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
module Question where
import Sukutomo exposing (Idol, Card, Song)
import Html exposing (..)
import Html.Attributes exposing (..)
import Html.Events exposing (onClick)
import String
import Json.Decode
port btnColor : String
type Quizz
= Idols
| Cards
| Songs
| All
type Action
= Restart
| GotIdols (Maybe (List Idol))
| GotRandomCards (Maybe (List Card))
| GotSongs (Maybe (List Song))
| Answer Question
| SongInfoReceived Json.Decode.Value
| ChangeQuizz Quizz
type alias Question =
{ question : QuestionKind
, answer : Maybe AnswerKind }
type QuestionKind
= IdolFood String Idol (List Idol)
| IdolLeastFood String Idol (List Idol)
| IdolHobby String Idol (List Idol)
| SongPlay (Maybe String) Int Song (List Song)
| SongCover String Song (List Song)
| CardAttribute String Card
| CardRarity String Card
| CardDetail String Idol (List Idol)
type AnswerKind
= IdolAnswer Idol
| StringAnswer String
| SongAnswer Song
getSongName : Song -> String
getSongName song =
case song.romaji_name of
Just name -> name
Nothing -> song.name
newQuestion : QuestionKind -> Question
newQuestion question =
{ question = question, answer = Nothing }
answerQuestion : AnswerKind -> Question -> Question
answerQuestion answer question =
{ question = question.question, answer = Just answer }
checkAnswer : Question -> Bool
checkAnswer question =
case question.answer of
Nothing -> True
Just answer ->
case question.question of
IdolFood _ idol _ ->
case answer of
IdolAnswer i -> idol.favorite_food == i.favorite_food
_ -> False
IdolLeastFood _ idol _ ->
case answer of
IdolAnswer i -> idol.least_favorite_food == i.least_favorite_food
_ -> False
IdolHobby _ idol _ ->
case answer of
IdolAnswer i -> idol.hobbies == i.hobbies
_ -> False
SongCover _ song _ ->
case answer of
SongAnswer s -> song.name == s.name
_ -> False
SongPlay _ _ song _ ->
case answer of
SongAnswer s -> song.name == s.name
_ -> False
CardAttribute _ card ->
case answer of
StringAnswer s -> s == card.attribute
_ -> False
CardRarity _ card ->
case answer of
StringAnswer s -> s == card.rarity
_ -> False
CardDetail _ idol _ ->
case answer of
IdolAnswer i -> i.name == idol.name
_ -> False
questionToHtml : Question -> String -> Html
questionToHtml question btnColor =
let s = case question.question of
IdolHobby s _ _->
text <| "Who likes " ++ (String.toLower s) ++ "?"
IdolFood s _ _ ->
text <| "Who likes " ++ (String.toLower s) ++ "?"
IdolLeastFood s _ _ ->
text <| "Who dislikes " ++ (String.toLower s) ++ "?"
SongPlay surl s _ _->
let formatItunesURL id =
"https://itunes.apple.com/lookup?id=" ++ toString s ++ "&callback=elm.ports.songInfo.send" in
let content =
case surl of
Nothing ->
node "script" [ src (formatItunesURL s) ] []
Just url ->
audio [controls True] [source [src url, type' "audio/mp4"] [] ] in
div [ class "row" ] [
div [ class "col-md-7" ] [
span [ class "song-question" ] [ text "What is this song?" ]
]
, div [ class "col-md-5" ] [
content
] ]
SongCover s _ _ ->
div [ class "row" ] [
div [ class "col-md-7" ] [
span [ class "question-with-image" ] [ text "What is this song?" ]
]
, div [ class "col-md-5" ] [
div [ class "song-image-question-detail"
, style [ ("background-image", "url('" ++ s ++ "')") ]
] []
]
]
CardAttribute transparent _ ->
div [ class "row" ] [
div [ class "col-md-7" ] [
span [ class "question-with-image" ] [ text "What is the attribute of this card?" ]
]
, div [ class "col-md-5" ] [
img [ class "image-with-question"
, src transparent] []
]
]
CardRarity transparent _ ->
div [ class "row" ] [
div [ class "col-md-7" ] [
span [ class "question-with-image" ] [ text "What is the rarity of this card?" ]
]
, div [ class "col-md-5" ] [
img [ class "image-with-question"
, src transparent] []
]
]
CardDetail image _ _ ->
div [ class "row" ] [
div [ class "col-md-7" ] [
span [ class "question-with-image" ] [ text "Who is the idol in this card?" ]
]
, div [ class "col-md-5" ] [
div [ class "image-question-detail"
, style [ ("background-image", "url('" ++ image ++ "')") ]
] []
]
]
in
div
[ class ("question text-" ++ btnColor) ]
[s]
stringOptions : Signal.Address Action -> Question -> List String -> List Html
stringOptions address question strings =
let format str =
a [href "#", onClick address (Answer <| (answerQuestion (StringAnswer str) question))] [text str] in
List.map format strings
imageOptions : Signal.Address Action -> Question -> List (String, String) -> List Html
imageOptions address question images =
let format (image, str) =
a [ href "#", onClick address (Answer <| (answerQuestion (StringAnswer str) question)) ] [
img [ src image, alt str, class "choice-image" ] []
] in
List.map format images
idolOptions : Signal.Address Action -> Question -> List Idol -> List Html
idolOptions address question idols =
let format element =
figure
[class "trivia_idol"
]
[img
[ src element.chibi
, onClick address (Answer <| (answerQuestion (IdolAnswer element) question))
] [text element.name]
, figcaption
[]
[ text element.name ]
]
in
let aux acc l =
case l of
[] -> acc
x::xs -> aux ((format x)::acc) xs
in
aux [] idols
songOptions : Signal.Address Action -> Question -> List Song -> List Html
songOptions address question songs =
let format element =
let name = getSongName element in
case question.question of
SongPlay _ song _ _ ->
figure
[class "trivia_song"
]
[img
[ src element.image
, onClick address (Answer <| (answerQuestion (SongAnswer element) question))
] [text name]
, figcaption
[]
[ text name ]
]
SongCover song _ _ ->
div
[class "trivia_song"
]
[span
[ onClick address (Answer <| (answerQuestion (SongAnswer element) question))
] [text name]
]
_ -> div [] []
in
let aux acc l =
case l of
[] -> acc
x::xs -> aux ((format x)::acc) xs
in
aux [] songs
optionsToHtml : Signal.Address Action -> Question -> List Html
optionsToHtml address question =
case question.question of
IdolHobby _ _ idols -> idolOptions address question idols
IdolFood _ _ idols -> idolOptions address question idols
IdolLeastFood _ _ idols -> idolOptions address question idols
SongCover _ _ songs -> songOptions address question songs
SongPlay _ _ _ songs -> songOptions address question songs
CardRarity _ _ -> imageOptions address question
[ ("https://i.schoolido.lu/static/N" ++ btnColor ++ ".png", "N")
, ("https://i.schoolido.lu/static/R" ++ btnColor ++ ".png", "R")
, ("https://i.schoolido.lu/static/SR" ++ btnColor ++ ".png", "SR")
, ("https://i.schoolido.lu/static/SSR" ++ btnColor ++ ".png", "SSR")
, ("https://i.schoolido.lu/static/UR" ++ btnColor ++ ".png", "UR")
]
CardAttribute _ _ -> imageOptions address question
[ ("https://i.schoolido.lu/static/Smile.png", "Smile")
, ("https://i.schoolido.lu/static/Pure.png", "Pure")
, ("https://i.schoolido.lu/static/Cool.png", "Cool")
, ("https://i.schoolido.lu/static/All.png", "All")
]
CardDetail _ _ idols -> idolOptions address question idols