| summaryrefslogtreecommitdiff | 
diff options
| author | nsensfel <SpamShield0@noot-noot.org> | 2018-09-13 16:51:08 +0200 | 
|---|---|---|
| committer | nsensfel <SpamShield0@noot-noot.org> | 2018-09-13 16:51:08 +0200 | 
| commit | 9ec5806bc721734923ff4c93f7ef1f97a6a03248 (patch) | |
| tree | c80b737a8bf0cc6e311c4fe7256f68e7ea8e158a /src/Comm/Send.elm | |
Starting an browser extension for TO...
Diffstat (limited to 'src/Comm/Send.elm')
| -rw-r--r-- | src/Comm/Send.elm | 67 | 
1 files changed, 67 insertions, 0 deletions
| diff --git a/src/Comm/Send.elm b/src/Comm/Send.elm new file mode 100644 index 0000000..dd9dc28 --- /dev/null +++ b/src/Comm/Send.elm @@ -0,0 +1,67 @@ +module Comm.Send exposing (try_sending) + +-- Elm ------------------------------------------------------------------------- +import Http + +import Json.Decode +import Json.Encode + +-- Extension ------------------------------------------------------------------- +import Comm.Okay +import Comm.SetBattles + +import Struct.Event +import Struct.ServerReply +import Struct.Model + +-------------------------------------------------------------------------------- +-- TYPES ----------------------------------------------------------------------- +-------------------------------------------------------------------------------- + +-------------------------------------------------------------------------------- +-- LOCAL ----------------------------------------------------------------------- +-------------------------------------------------------------------------------- +internal_decoder : String -> (Json.Decode.Decoder Struct.ServerReply.Type) +internal_decoder reply_type = +   case reply_type of +      "okay" -> (Comm.Okay.decode) +      "set_battles" -> (Comm.SetBattles.decode) +      other -> +         (Json.Decode.fail +            ( +               "Unknown server command \"" +               ++ other +               ++ "\"" +            ) +         ) + +decode : (Json.Decode.Decoder Struct.ServerReply.Type) +decode = +   (Json.Decode.field "msg" Json.Decode.string) +   |> (Json.Decode.andThen (internal_decoder)) + +-------------------------------------------------------------------------------- +-- EXPORTED -------------------------------------------------------------------- +-------------------------------------------------------------------------------- +-- TODO: turn this into a multi-server version. +try_sending : ( +      Struct.Model.Type -> +      String -> +      (Struct.Model.Type -> (Maybe Json.Encode.Value)) -> +      (Maybe (Cmd Struct.Event.Type)) +   ) +try_sending model recipient try_encoding_fun = +   case (try_encoding_fun model) of +      (Just serial) -> +         (Just +            (Http.send +               Struct.Event.ServerReplied +               (Http.post +                  recipient +                  (Http.jsonBody serial) +                  (Json.Decode.list (decode)) +               ) +            ) +         ) + +      Nothing -> Nothing | 


