summaryrefslogtreecommitdiff
blob: 0409e61605b38966917a89fd563b285d22d43ad4 (plain)
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
module Comm.Send exposing (commit)

-- Elm -------------------------------------------------------------------------
import Http

import Json.Decode

-- Extension -------------------------------------------------------------------
import Comm.Okay
import Comm.SetBattles

import Struct.Event
import Struct.ServerReply

--------------------------------------------------------------------------------
-- TYPES -----------------------------------------------------------------------
--------------------------------------------------------------------------------

--------------------------------------------------------------------------------
-- LOCAL -----------------------------------------------------------------------
--------------------------------------------------------------------------------
internal_decoder : (
      Int ->
      String ->
      (Json.Decode.Decoder Struct.ServerReply.Type)
   )
internal_decoder ix reply_type =
   case reply_type of
      "okay" -> (Comm.Okay.decoder)
      "set_battles" -> (Comm.SetBattles.decoder ix)
      other ->
         (Json.Decode.fail
            (
               "Unknown server command \""
               ++ other
               ++ "\""
            )
         )

decoder : Int -> (Json.Decode.Decoder Struct.ServerReply.Type)
decoder ix =
   (Json.Decode.field "msg" Json.Decode.string)
   |> (Json.Decode.andThen (internal_decoder ix))

--------------------------------------------------------------------------------
-- EXPORTED --------------------------------------------------------------------
--------------------------------------------------------------------------------
commit : Int -> String -> (Cmd Struct.Event.Type)
commit ix query =
   (Http.send
      Struct.Event.ServerReplied
      (Http.get query (Json.Decode.list (decoder ix)))
   )