summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'src/background/src/Comm/Send.elm')
-rw-r--r--src/background/src/Comm/Send.elm66
1 files changed, 66 insertions, 0 deletions
diff --git a/src/background/src/Comm/Send.elm b/src/background/src/Comm/Send.elm
new file mode 100644
index 0000000..3fc30ae
--- /dev/null
+++ b/src/background/src/Comm/Send.elm
@@ -0,0 +1,66 @@
+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.decoder)
+ "set_battles" -> (Comm.SetBattles.decoder)
+ other ->
+ (Json.Decode.fail
+ (
+ "Unknown server command \""
+ ++ other
+ ++ "\""
+ )
+ )
+
+decoder : (Json.Decode.Decoder Struct.ServerReply.Type)
+decoder =
+ (Json.Decode.field "msg" Json.Decode.string)
+ |> (Json.Decode.andThen (internal_decoder))
+
+--------------------------------------------------------------------------------
+-- EXPORTED --------------------------------------------------------------------
+--------------------------------------------------------------------------------
+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 (decoder))
+ )
+ )
+ )
+
+ Nothing -> Nothing