-
Notifications
You must be signed in to change notification settings - Fork 1
/
Bot.elm
78 lines (63 loc) · 2.16 KB
/
Bot.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
module Bot where
import Http
import Json
import Dict
import PureRandom
import Maybe
import Utils as U
import Birdhouse as BH
lakesJsonSigResp : Signal (Http.Response String)
lakesJsonSigResp = Http.sendGet . constant <| "lakes.json"
type Loc = { lat : Float, lon : Float }
type Lake = { name : String, loc : Loc }
toLoc : Json.Value -> Maybe Loc
toLoc v =
case v of
Json.Object d ->
case (Dict.get "lat" d, Dict.get "lon" d) of
(Just (Json.Number lat), Just (Json.Number lon)) -> Just { lat = lat, lon = lon }
otherwise -> Nothing
otherwise -> Nothing
toLake : Json.Value -> Maybe Lake
toLake v =
case v of
Json.Object d ->
case (Dict.get "name" d, U.concatMap toLoc (Dict.get "loc" d)) of
(Just (Json.String name), Just loc) -> Just { name = name, loc = loc }
otherwise -> Nothing
otherwise -> Nothing
toLakes : Json.Value -> [Lake]
toLakes v =
case v of
Json.Array ls -> Maybe.justs <| map toLake ls
otherwise -> []
toGeoUpdate : Lake -> BH.GeoUpdate (BH.StatusUpdate {})
toGeoUpdate { name, loc } = {
status = "Lake " ++ name,
lat = loc.lat,
long = loc.lon,
display_coordinates = True }
seed : PureRandom.Gen
seed = PureRandom.mkGen 1
pos : Int
pos = 0
-- port updates : Signal (Maybe (BH.GeoUpdate (BH.StatusUpdate {})))
port updates : Signal (Maybe { lat : Float, long : Float, display_coordinates : Bool, status : String })
port updates = lakesJsonSigResp
|> lift U.respToMaybe
|> lift (U.concatMap Json.fromString)
|> lift (U.map toLakes)
|> lift (U.map <| map toGeoUpdate . drop pos . fst . PureRandom.shuffle seed)
|> U.extract []
|> U.spool (every <| 3 * hour)
previews : Signal Element
previews = (\update -> case update of
Just t -> BH.preview t
Nothing -> empty) <~ updates
consPreview : Element -> ([Element], Int) -> ([Element], Int)
consPreview p (ps, n) =
let counter = if n == -1
then empty
else container 50 60 middle <| asText n
in ((p `beside` counter) :: ps, n + 1)
main = flow down . fst <~ foldp consPreview ([], -1) previews