-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMain.elm
260 lines (203 loc) · 6.25 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
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
port module Main exposing (..)
import Random
import Html exposing (Html, div, text, span, h1, ul, li)
import Html.Attributes exposing (class, style, attribute)
import Html.Events exposing (onClick)
import Json.Encode as Encode
import Json.Decode as Decode
import Gameroom exposing (..)
import Gameroom.Context exposing (Context)
-- Main
main : Program Never (Model Problem Guess) (Msg Problem Guess)
main =
gameWith
[ basePath "/the-capitalist"
, unicodeIcon "💰"
, name "The Capitalist"
, subheading "Oh, not that kind, though, more like a person who knows the capital of a lot of countries.."
, instructions "Find the capital of the country!"
, clearWinner 100
, responsiblePorts { outgoing = outgoing, incoming = incoming }
]
spec
-- Types, encoders and decoders
type alias Problem =
{ question : String
, answers : List String
, correct : Int
}
problemEncoder : Problem -> Encode.Value
problemEncoder problem =
Encode.object
[ ( "question", Encode.string problem.question )
, ( "answers"
, List.map Encode.string problem.answers
|> Encode.list
)
, ( "correct", Encode.int problem.correct )
]
problemDecoder : Decode.Decoder Problem
problemDecoder =
Decode.map3 Problem
(Decode.field "question" Decode.string)
(Decode.field "answers" (Decode.list Decode.string))
(Decode.field "correct" Decode.int)
type alias Guess =
Int
guessEncoder : Guess -> Encode.Value
guessEncoder =
Encode.int
guessDecoder : Decode.Decoder Guess
guessDecoder =
Decode.int
-- Game spec
spec : Spec Problem Guess
spec =
{ view = view
, evaluate = evaluate
, problemGenerator = problemGenerator
, guessEncoder = guessEncoder
, guessDecoder = guessDecoder
, problemEncoder = problemEncoder
, problemDecoder = problemDecoder
}
view : Context Guess -> Problem -> Html Guess
view context problem =
-- not used, only here to show a more concise version of the game view
div []
[ div []
[ h1 [] [ text problem.question ]
, ul []
(List.indexedMap
(\index answer ->
-- events map to raw guesses (in this case, an Int)
li [ onClick index ] [ text answer ]
)
problem.answers
)
]
]
styledView : Context Guess -> Problem -> Html Guess
styledView context problem =
div [ style containerStyle ]
[ div [ style contentStyle ]
[ h1 [] [ text problem.question ]
, ul
[ style listStyle
]
(List.indexedMap
(\index answer ->
let
isGuessedBySelf =
context.ownGuess == (Just index)
isMarkedCorrect =
(index == problem.correct) && (isGuessedBySelf || context.isRoundOver)
in
li
[ onClick index
, style <|
listItemBaseStyle isGuessedBySelf isMarkedCorrect
]
[ text answer ]
)
problem.answers
)
]
]
evaluate : Problem -> Guess -> Float
evaluate problem guess =
-- a correct guess maps to a higher evaluation
if (guess == problem.correct) then
100
else
0
problemGenerator : Random.Generator Problem
problemGenerator =
generatorFromList
{ question = "🇱🇻 Latvia"
, answers = [ "Tallin", "Riga", "Vilnius", "Moscow" ]
, correct = 1
}
-- a list of problems identical to the record above
problems
-- Ports
port outgoing : Encode.Value -> Cmd msg
port incoming : (Encode.Value -> msg) -> Sub msg
-- Problem DB
problems : List Problem
problems =
[ { question = "🇭🇺 Hungary"
, answers = [ "Budapest", "Pécs", "Mosonmagyaróvár", "Garmisch-Partenkirchen" ]
, correct = 0
}
, { question = "🇧🇳 Brunei"
, answers = [ "Munich", "Copenhagen", "Jakarta", "Bandar Seri Begawan" ]
, correct = 3
}
, { question = "🇸🇮 Slovenia"
, answers = [ "Munich", "Ljubljana", "Jakarta", "Bandar Seri Begawan" ]
, correct = 1
}
, { question = "🇫🇷 France"
, answers = [ "Bordeaux", "Paris", "Paris, NY" ]
, correct = 1
}
, { question = "🇩🇪 Germany"
, answers = [ "Bordeaux", "Berlin, NH", "Berlin", "Stuttgart" ]
, correct = 2
}
, { question = "🇩🇰 Denmark"
, answers = [ "Ansterdam", "Aarhus", "Copenhagen", "Christiania" ]
, correct = 2
}
]
-- Styles
containerStyle : List ( String, String )
containerStyle =
[ ( "width", "100%" )
, ( "height", "100%" )
, ( "display", "flex" )
, ( "align-items", "center" )
, ( "justify-content", "center" )
]
contentStyle : List ( String, String )
contentStyle =
[ ( "margin", "0" )
, ( "width", "auto" )
, ( "max-width", "440px" )
, ( "text-align", "center" )
]
listStyle : List ( String, String )
listStyle =
[ ( "list-style", "none" )
, ( "padding-left", "0" )
]
listItemBaseStyle : Bool -> Bool -> List ( String, String )
listItemBaseStyle isGuessedBySelf isMarkedCorrect =
[ ( "margin", "12px" )
, ( "display", "inline-block" )
, ( "border-width", "1px" )
, ( "border-style", "solid" )
, ( "cursor", "pointer" )
, ( "padding", "8px 16px" )
, ( "border-radius", "6px" )
]
++ [ ( "border-color"
, if isGuessedBySelf then
"#333333"
else
"transparent"
)
, ( "background-color"
, if isMarkedCorrect then
"#333333"
else
"transparent"
)
, ( "color"
, if isMarkedCorrect then
"#FFFFFF"
else
"#333333"
)
]