-
Notifications
You must be signed in to change notification settings - Fork 1
/
Main.elm
478 lines (369 loc) · 10.6 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
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
module Main exposing (..)
import Html exposing (Html, program)
import Html.Attributes exposing (style)
import Json.Decode as Decode exposing (Decoder)
import Svg exposing (Svg, svg, rect)
import Svg.Attributes exposing (fill, width, height, x, y)
import Http
import Window
import Task
-- MAIN
main : Program Never Model Message
main =
program
{ init = init
, update = update
, view = view
, subscriptions = subscriptions
}
-- MODELS
type Line
= Broken
| Unbroken
type alias Hexagram =
( Line, Line, Line, Line, Line, Line )
type alias IChing =
List Hexagram
type alias Model =
{ iChing : IChing
, size : Window.Size
, line_height : Int
, line_width : Int
, hexagram_height : Int
, hexagram_width : Int
, rows : Int
, columns : Int
}
iChingURL : String
iChingURL =
"iching.json"
init : ( Model, Cmd Message )
init =
let
rows =
8
columns =
8
line_height : Int
line_height =
10
line_width : Int
line_width =
line_height * 2 * 6
hexagram_height : Int
hexagram_height =
line_height * 2 * 8
hexagram_width : Int
hexagram_width =
line_height * 2 * 8
getIChing : Cmd Message
getIChing =
iChingDecoder
|> Http.get iChingURL
|> Http.send ReceiveIChing
getSize : Cmd Message
getSize =
Window.size
|> Task.perform ReceiveSize
effects : Cmd Message
effects =
[ getSize, getIChing ]
|> Cmd.batch
size : Window.Size
size =
{ width = 0, height = 0 }
initialModel : Model
initialModel =
{ iChing = []
, size = size
, line_height = line_height
, line_width = line_width
, hexagram_height = hexagram_height
, hexagram_width = hexagram_width
, rows = rows
, columns = columns
}
in
( initialModel, effects )
-- MESSAGES
type Message
= NoOp
| ReceiveIChing (Result Http.Error IChing)
| ReceiveSize Window.Size
-- UPDATES
update : Message -> Model -> ( Model, Cmd Message )
update message model =
(case message of
ReceiveIChing result ->
receiveIChing result
ReceiveSize size ->
receiveSize size
NoOp ->
always <| model ! []
)
|> apply model
apply : a -> (a -> b) -> b
apply a fn =
fn a
{-| Takes a result containing the decoded IChing and puts it into the model,
logs the error if there is one
-}
receiveIChing : Result Http.Error IChing -> Model -> ( Model, Cmd Message )
receiveIChing result model =
case result of
Ok iChing ->
{ model | iChing = iChing } ! []
Err error ->
let
_ =
Debug.log "error fetching iChing" (error)
in
model ! []
{-| Takes the size and updates model sizes in order to resize rending to screen
-}
receiveSize : Window.Size -> Model -> ( Model, Cmd Message )
receiveSize size model =
let
base_size =
if size.height > size.width then
size.width
else
size.height
( newHexagramHeight, newHexagramWidth, newLineHeight, newLineWidth ) =
( base_size // 8
, base_size // 8
, (base_size // 128)
, (base_size // 32) * 3
)
in
{ model
| size = size
, line_height = newLineHeight
, line_width = newLineWidth
, hexagram_height = newHexagramHeight
, hexagram_width = newHexagramWidth
}
! []
{-| Decodes some JSON that represents the I Ching.
exampleJSON : String
exampleJSON =
"""
{
"hexagrams": {
"hexagram": [
{
"pattern": "966696"
}
]
}
}
"""
-- Would be equal to [(Unbroken, Broken, Broken, Broken, Unbroken, Broken)]
exampleIChing : IChing
exampleIChing =
exampleJSON
|> Decode.decodeString iChingDecoder
|> Result.withDefault []
-}
iChingDecoder : Decoder IChing
iChingDecoder =
Decode.at [ "pattern" ] Decode.string
|> Decode.map pattern2hexagram
|> Decode.list
|> Decode.at [ "hexagrams", "hexagram" ]
{-| Takes a string of length 6 containing "6" or "9" and returns a six tuple of
Lines. Invalid patterns will log an error and return a default hexagram.
-- Equal to (Unbroken, Broken, Broken, Broken, Unbroken, Broken)
exampleHexagram : Hexagram
exampleHexagram =
pattern2hexagram "966696"
-}
pattern2hexagram : String -> Hexagram
pattern2hexagram pattern =
case String.split "" pattern of
[ one, two, three, four, five, six ] ->
( one, two, three, four, five, six )
|> map6tuple string2line
pattern ->
let
_ =
Debug.log "Invalid pattern" (pattern)
in
( Broken, Broken, Broken, Broken, Broken, Broken )
{-| Takes a function and applies it to each element of a 6 tuple
-- Would be equal to ("1", "2", "3", "4", "5", "6")
exampleTuple : (String, String, String, String, String, String)
exampleTuple =
(0, 1, 2, 3, 4, 5)
|> map6tuple ((+) 1)
|> map6tuple toString
-}
map6tuple : (a -> b) -> ( a, a, a, a, a, a ) -> ( b, b, b, b, b, b )
map6tuple fn ( one, two, three, four, five, six ) =
( fn one, fn two, fn three, fn four, fn five, fn six )
string2line : String -> Line
string2line string =
case string of
"9" ->
Unbroken
"6" ->
Broken
unknown ->
let
_ =
Debug.log "Expected either 6 ot 9, found" unknown
in
Broken
{-| Converts a 6 tuple to a list, useful for converting a tuple that will be
mapped over by a view function
hexagram : Hexagram
hexagram =
( Broken, Unbroken, Broken, Unbroken, Broken, Unbroken )
viewHexagram : List (Svg Message)
viewHexagram =
hexagram
|> listFrom6tuple
|> List.indexedMap viewLine
-}
listFrom6tuple : ( a, a, a, a, a, a ) -> List a
listFrom6tuple ( one, two, three, four, five, six ) =
[ one, two, three, four, five, six ]
-- SUBSCRIPTIONS
subscriptions : Model -> Sub Message
subscriptions model =
Window.resizes ReceiveSize
-- VIEWS
view : Model -> Html Message
view model =
let
toPx n =
toString n ++ "px"
iChingHeight =
model.hexagram_height * 8
iChingWidth =
model.hexagram_width * 8
eigthHexagramHeight =
model.hexagram_height // 8
eigthHexagramWidth =
model.hexagram_width // 8
heightDifference =
abs (model.size.height - iChingHeight) // 2
widthDifference =
abs (model.size.width - iChingWidth) // 2
top =
toPx (heightDifference + eigthHexagramHeight)
left =
toPx (widthDifference + eigthHexagramWidth)
styles =
[ ( "position", "absolute" )
, ( "top", top )
, ( "left", left )
]
|> style
in
Html.div [ styles ]
[ viewHexagrams model
]
viewHexagrams : Model -> Html Message
viewHexagrams ({ hexagram_height, hexagram_width } as model) =
let
width_ : Svg.Attribute Message
width_ =
hexagram_width
* 8
|> (\a -> a - (hexagram_width // 4))
|> toString
|> width
height_ : Svg.Attribute Message
height_ =
hexagram_height
* 8
|> (\a -> a - (hexagram_height // 4))
|> toString
|> height
in
model.iChing
|> List.indexedMap (viewHexagram model)
|> svg [ width_, height_ ]
viewHexagram : Model -> Int -> Hexagram -> Svg Message
viewHexagram ({ hexagram_height, hexagram_width } as model) position hexagram =
let
x_ : Svg.Attribute Message
x_ =
position
% 8
|> (*) hexagram_width
|> toString
|> x
y_ : Svg.Attribute Message
y_ =
position
// 8
|> (*) hexagram_height
|> toString
|> y
width_ : Svg.Attribute Message
width_ =
hexagram_width
|> toString
|> width
height_ : Svg.Attribute Message
height_ =
hexagram_height
|> toString
|> height
in
hexagram
|> listFrom6tuple
|> List.indexedMap (viewLine model)
|> svg [ width_, height_, x_, y_ ]
viewLine : Model -> Int -> Line -> Svg Message
viewLine { line_height, line_width } position bar =
let
y_ : Svg.Attribute Message
y_ =
position
* 2
|> (*) line_height
|> toString
|> y
line_height_ : Svg.Attribute Message
line_height_ =
line_height
|> toString
|> height
line_width_ : Svg.Attribute Message
line_width_ =
line_width
|> toString
|> width
broken_line_width : Svg.Attribute Message
broken_line_width =
line_width
// 3
|> toString
|> width
x_offset : Svg.Attribute Message
x_offset =
line_width
// 3
|> (*) 2
|> toString
|> x
fill_ : Svg.Attribute Message
fill_ =
fill "black"
attributes : List (Svg.Attribute Message)
attributes =
[ line_height_, line_width_, y_ ]
bars : List (Svg Message)
bars =
if bar == Unbroken then
[ rect [ line_width_, line_height_, fill_ ] []
]
else
[ rect [ broken_line_width, line_height_, fill_ ] []
, rect [ broken_line_width, line_height_, fill_, x_offset ] []
]
in
svg attributes bars