| summaryrefslogtreecommitdiff | 
diff options
| author | nsensfel <SpamShield0@noot-noot.org> | 2018-03-07 13:30:22 +0100 | 
|---|---|---|
| committer | nsensfel <SpamShield0@noot-noot.org> | 2018-03-07 13:30:22 +0100 | 
| commit | 8fc99983ce30747ae7282c485899aea81bc9d26e (patch) | |
| tree | 660ac771592cafcd7447a24192876172abeb57f7 /src/battlemap | |
| parent | c4a3e939ca10aca673732340b0c47d663da30302 (diff) | |
Still working on that JSON stuff...
Diffstat (limited to 'src/battlemap')
| -rw-r--r-- | src/battlemap/src/Send/AddChar.elm (renamed from src/battlemap/src/Update/HandleServerReply/AddChar.elm) | 90 | ||||
| -rw-r--r-- | src/battlemap/src/Send/Send.elm | 26 | ||||
| -rw-r--r-- | src/battlemap/src/Send/SetMap.elm (renamed from src/battlemap/src/Update/HandleServerReply/SetMap.elm) | 33 | ||||
| -rw-r--r-- | src/battlemap/src/Struct/ServerReply.elm | 26 | ||||
| -rw-r--r-- | src/battlemap/src/Update/HandleServerReply.elm | 37 | 
5 files changed, 109 insertions, 103 deletions
| diff --git a/src/battlemap/src/Update/HandleServerReply/AddChar.elm b/src/battlemap/src/Send/AddChar.elm index 2fa4195..762d859 100644 --- a/src/battlemap/src/Update/HandleServerReply/AddChar.elm +++ b/src/battlemap/src/Send/AddChar.elm @@ -1,4 +1,4 @@ -module Update.HandleServerReply.AddChar exposing (apply_to) +module Send.AddChar exposing (decode)  -- Elm -------------------------------------------------------------------------  import Dict @@ -13,6 +13,7 @@ import Struct.Attributes  import Struct.Character  import Struct.Error  import Struct.Model +import Struct.ServerReply  import Struct.WeaponSet  -------------------------------------------------------------------------------- @@ -92,58 +93,47 @@ char_decoder =  --------------------------------------------------------------------------------  -- EXPORTED --------------------------------------------------------------------  -------------------------------------------------------------------------------- -apply_to : Struct.Model.Type -> String -> Struct.Model.Type -apply_to model serialized_char = -   case -      (Json.Decode.decodeString -         char_decoder -         serialized_char -      ) -   of +decode : (Struct.Model.Struct -> (Json.Decode.Decoder Struct.ServerReply.Type)) +decode model input = +   case (Json.Decode.decodeString char_decoder input) of        (Result.Ok char_data) -> -         (Struct.Model.add_character -            model -            (Struct.Character.new -               (toString char_data.ix) -               char_data.nam -               char_data.ico -               char_data.prt -               {x = lc.x, y = lc.y} -               char_data.hea -               char_data.pla -               char_data.ena -               (Struct.Attributes.new -                  char_data.att.con -                  char_data.att.dex -                  char_data.att.int -                  char_data.att.min -                  char_data.att.spe -                  char_data.att.str -               ) -               ( -                  case -                     ( -                        (Dict.get char_data.awp model.weapons), -                        (Dict.get char_data.swp model.weapons) -                     ) -                  of -                     ((Just wp_0), (Just wp_1)) -> -                        (Struct.WeaponSet.new wp_0 wp_1) - -                     _ -> -                        (Struct.WeaponSet.new -                           (Data.Weapons.none) -                           (Data.Weapons.none) +         (Result.Ok +            (Struct.ServerReply.AddCharacter +               (Struct.Character.new +                  (toString char_data.ix) +                  char_data.nam +                  char_data.ico +                  char_data.prt +                  {x = char_data.lc.x, y = char_data.lc.y} +                  char_data.hea +                  char_data.pla +                  char_data.ena +                  (Struct.Attributes.new +                     char_data.att.con +                     char_data.att.dex +                     char_data.att.int +                     char_data.att.min +                     char_data.att.spe +                     char_data.att.str +                  ) +                  ( +                     case +                        ( +                           (Dict.get char_data.awp model.weapons), +                           (Dict.get char_data.swp model.weapons)                          ) +                     of +                        ((Just wp_0), (Just wp_1)) -> +                           (Struct.WeaponSet.new wp_0 wp_1) + +                        _ -> +                           (Struct.WeaponSet.new +                              (Data.Weapons.none) +                              (Data.Weapons.none) +                           ) +                  )                 )              )           ) -      (Result.Err msg) -> -         (Struct.Model.invalidate -            model -            (Struct.Error.new -               Struct.Error.Programming -               ("Could not deserialize character: " ++ msg) -            ) -         ) +   other -> other diff --git a/src/battlemap/src/Send/Send.elm b/src/battlemap/src/Send/Send.elm index d8420b1..cd3d68e 100644 --- a/src/battlemap/src/Send/Send.elm +++ b/src/battlemap/src/Send/Send.elm @@ -1,4 +1,4 @@ -module Send.Send exposing (Reply, try_sending) +module Send.Send exposing (try_sending)  -- Elm -------------------------------------------------------------------------  import Http @@ -7,20 +7,34 @@ import Json.Decode  import Json.Encode  -- Battlemap ------------------------------------------------------------------- +import Send.SetMap +import Send.AddChar +  import Struct.Event +import Struct.ServerReply  import Struct.Model  --------------------------------------------------------------------------------  -- TYPES -----------------------------------------------------------------------  -------------------------------------------------------------------------------- -type alias Reply = (List String)  --------------------------------------------------------------------------------  -- LOCAL -----------------------------------------------------------------------  -------------------------------------------------------------------------------- -decoder : (Json.Decode.Decoder (List (List String))) -decoder = -   (Json.Decode.list (Json.Decode.list Json.Decode.string)) +internal_decoder : ( +      Struct.Model.Type -> +      String -> +      (Json.Decode.Decoder Struct.ServerReply.Type) +   ) +internal_decoder model reply_type = +   case reply_type of +      "add_char" -> (Send.AddChar.decode model) +      "set_map" -> (Send.SetMap.decode model) + +decode : Struct.Model.Type -> (Json.Decode.Decoder Struct.ServerReply.Type) +decode model = +   (Json.Decode.field "msg" Json.Decode.string) +   |> (Json.Decode.andThen (internal_decoder model))  --------------------------------------------------------------------------------  -- EXPORTED -------------------------------------------------------------------- @@ -40,7 +54,7 @@ try_sending model recipient try_encoding_fun =                 (Http.post                    recipient                    (Http.jsonBody serial) -                  (decoder) +                  (decode model)                 )              )           ) diff --git a/src/battlemap/src/Update/HandleServerReply/SetMap.elm b/src/battlemap/src/Send/SetMap.elm index 88eed11..132216c 100644 --- a/src/battlemap/src/Update/HandleServerReply/SetMap.elm +++ b/src/battlemap/src/Send/SetMap.elm @@ -1,4 +1,4 @@ -module Update.HandleServerReply.SetMap exposing (apply_to) +module Send.SetMap exposing (decode)  -- Elm -------------------------------------------------------------------------  import Dict @@ -9,6 +9,7 @@ import Data.Tiles  import Struct.Battlemap  import Struct.Model +import Struct.ServerReply  import Struct.Tile  -------------------------------------------------------------------------------- @@ -36,8 +37,8 @@ deserialize_tile map_width index id =  --------------------------------------------------------------------------------  -- EXPORTED --------------------------------------------------------------------  -------------------------------------------------------------------------------- -apply_to : Struct.Model.Type -> String -> Struct.Model.Type -apply_to model serialized_map = +decode : (Struct.Model.Type -> (Json.Decode.Decoder Struct.ServerReply.Type)) +decode model input =     case        (Json.Decode.decodeString           (Json.Decode.map3 MapData @@ -48,23 +49,21 @@ apply_to model serialized_map =                 (Json.Decode.list Json.Decode.int)              )           ) -         serialized_map +         input        )     of        (Result.Ok map_data) -> -         (Struct.Model.reset -            {model | -               battlemap = -                  (Struct.Battlemap.new -                     map_data.w -                     map_data.h -                     (List.indexedMap -                        (deserialize_tile map_data.w) -                        map_data.t -                     ) +         (Result.Ok +            (Struct.ServerReply.SetMap +               (Struct.Battlemap.new +                  map_data.w +                  map_data.h +                  (List.indexedMap +                     (deserialize_tile map_data.w) +                     map_data.t                    ) -            } -            (Dict.empty) +               ) +            )           ) -      _ -> model +      error -> error diff --git a/src/battlemap/src/Struct/ServerReply.elm b/src/battlemap/src/Struct/ServerReply.elm new file mode 100644 index 0000000..5849567 --- /dev/null +++ b/src/battlemap/src/Struct/ServerReply.elm @@ -0,0 +1,26 @@ +module Struct.ServerReply exposing (Type(..)) + +-- Elm ------------------------------------------------------------------------- + +-- Battlemap ------------------------------------------------------------------- +import Struct.Battlemap +import Struct.Character +import Struct.Model + + +-------------------------------------------------------------------------------- +-- TYPES ----------------------------------------------------------------------- +-------------------------------------------------------------------------------- + +type Type = +   Okay +   | AddCharacter Struct.Character.Type +   | SetMap Struct.Battlemap.Type + +-------------------------------------------------------------------------------- +-- LOCAL ----------------------------------------------------------------------- +-------------------------------------------------------------------------------- + +-------------------------------------------------------------------------------- +-- EXPORTED -------------------------------------------------------------------- +-------------------------------------------------------------------------------- diff --git a/src/battlemap/src/Update/HandleServerReply.elm b/src/battlemap/src/Update/HandleServerReply.elm index 3eb09c1..da5d95d 100644 --- a/src/battlemap/src/Update/HandleServerReply.elm +++ b/src/battlemap/src/Update/HandleServerReply.elm @@ -8,42 +8,13 @@ import Struct.Error  import Struct.Event  import Struct.Model -import Update.HandleServerReply.AddChar -import Update.HandleServerReply.SetMap -  --------------------------------------------------------------------------------  -- TYPES -----------------------------------------------------------------------  -------------------------------------------------------------------------------- -type ServerReply = -   (SetMap Update.HandleServerReply.SetMap.Type) -   | (AddChar Update.HandleServerReply.SetMap.Type) -   | (Other String)  --------------------------------------------------------------------------------  -- LOCAL -----------------------------------------------------------------------  -------------------------------------------------------------------------------- -apply_command: (List String) -> Struct.Model.Type -> Struct.Model.Type -apply_command cmd model = -   case -      cmd -   of -      ["set_map", data] -> -         (Update.HandleServerReply.SetMap.apply_to model data) - -      ["add_char", data] -> -         (Update.HandleServerReply.AddChar.apply_to model data) - -      _ -> -         (Struct.Model.invalidate -            model -            (Struct.Error.new -               Struct.Error.Programming -               ( -                  "Received invalid command from server:" -                  ++ (toString cmd) -               ) -            ) -         )  --------------------------------------------------------------------------------  -- EXPORTED -------------------------------------------------------------------- @@ -65,4 +36,10 @@ apply_to model query_result =           )        (Result.Ok commands) -> -         ((List.foldl (apply_command) model commands), Cmd.none) +         ( +            (Struct.Model.invalidate +               model +               (Struct.Error.new Struct.Error.Unimplemented "Network Comm.") +            ), +            Cmd.none +         ) | 


