From 8fc99983ce30747ae7282c485899aea81bc9d26e Mon Sep 17 00:00:00 2001 From: nsensfel Date: Wed, 7 Mar 2018 13:30:22 +0100 Subject: Still working on that JSON stuff... --- src/battlemap/src/Send/AddChar.elm | 139 +++++++++++++++++++ src/battlemap/src/Send/Send.elm | 26 +++- src/battlemap/src/Send/SetMap.elm | 69 ++++++++++ src/battlemap/src/Struct/ServerReply.elm | 26 ++++ src/battlemap/src/Update/HandleServerReply.elm | 37 +---- .../src/Update/HandleServerReply/AddChar.elm | 149 --------------------- .../src/Update/HandleServerReply/SetMap.elm | 70 ---------- 7 files changed, 261 insertions(+), 255 deletions(-) create mode 100644 src/battlemap/src/Send/AddChar.elm create mode 100644 src/battlemap/src/Send/SetMap.elm create mode 100644 src/battlemap/src/Struct/ServerReply.elm delete mode 100644 src/battlemap/src/Update/HandleServerReply/AddChar.elm delete mode 100644 src/battlemap/src/Update/HandleServerReply/SetMap.elm diff --git a/src/battlemap/src/Send/AddChar.elm b/src/battlemap/src/Send/AddChar.elm new file mode 100644 index 0000000..762d859 --- /dev/null +++ b/src/battlemap/src/Send/AddChar.elm @@ -0,0 +1,139 @@ +module Send.AddChar exposing (decode) + +-- Elm ------------------------------------------------------------------------- +import Dict + +import Json.Decode +import Json.Decode.Pipeline + +-- Battlemap ------------------------------------------------------------------- +import Data.Weapons + +import Struct.Attributes +import Struct.Character +import Struct.Error +import Struct.Model +import Struct.ServerReply +import Struct.WeaponSet + +-------------------------------------------------------------------------------- +-- TYPES ----------------------------------------------------------------------- +-------------------------------------------------------------------------------- +type alias CharAtt = + { + con : Int, + dex : Int, + int : Int, + min : Int, + spe : Int, + str : Int + } + +type alias Location = + { + x : Int, + y : Int + } + +type alias CharData = + { + ix : Int, + nam : String, + ico : String, + prt : String, + lc : Location, + hea : Int, + pla : String, + ena : Bool, + att : CharAtt, + awp : Int, + swp : Int + } + +-------------------------------------------------------------------------------- +-- LOCAL ----------------------------------------------------------------------- +-------------------------------------------------------------------------------- +attributes_decoder : (Json.Decode.Decoder CharAtt) +attributes_decoder = + (Json.Decode.Pipeline.decode + CharAtt + |> (Json.Decode.Pipeline.required "con" Json.Decode.int) + |> (Json.Decode.Pipeline.required "dex" Json.Decode.int) + |> (Json.Decode.Pipeline.required "int" Json.Decode.int) + |> (Json.Decode.Pipeline.required "min" Json.Decode.int) + |> (Json.Decode.Pipeline.required "spe" Json.Decode.int) + |> (Json.Decode.Pipeline.required "str" Json.Decode.int) + ) + +location_decoder : (Json.Decode.Decoder Location) +location_decoder = + (Json.Decode.Pipeline.decode + Location + |> (Json.Decode.Pipeline.required "x" Json.Decode.int) + |> (Json.Decode.Pipeline.required "y" Json.Decode.int) + ) + +char_decoder : (Json.Decode.Decoder CharData) +char_decoder = + (Json.Decode.Pipeline.decode + CharData + |> (Json.Decode.Pipeline.required "ix" Json.Decode.int) + |> (Json.Decode.Pipeline.required "nam" Json.Decode.string) + |> (Json.Decode.Pipeline.required "ico" Json.Decode.string) + |> (Json.Decode.Pipeline.required "prt" Json.Decode.string) + |> (Json.Decode.Pipeline.required "lc" location_decoder) + |> (Json.Decode.Pipeline.required "hea" Json.Decode.int) + |> (Json.Decode.Pipeline.required "pla" Json.Decode.string) + |> (Json.Decode.Pipeline.required "ena" Json.Decode.bool) + |> (Json.Decode.Pipeline.required "att" attributes_decoder) + |> (Json.Decode.Pipeline.required "awp" Json.Decode.int) + |> (Json.Decode.Pipeline.required "swp" Json.Decode.int) + ) + +-------------------------------------------------------------------------------- +-- EXPORTED -------------------------------------------------------------------- +-------------------------------------------------------------------------------- +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) -> + (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) + ) + ) + ) + ) + ) + + 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/Send/SetMap.elm b/src/battlemap/src/Send/SetMap.elm new file mode 100644 index 0000000..132216c --- /dev/null +++ b/src/battlemap/src/Send/SetMap.elm @@ -0,0 +1,69 @@ +module Send.SetMap exposing (decode) + +-- Elm ------------------------------------------------------------------------- +import Dict +import Json.Decode + +-- Battlemap ------------------------------------------------------------------- +import Data.Tiles + +import Struct.Battlemap +import Struct.Model +import Struct.ServerReply +import Struct.Tile + +-------------------------------------------------------------------------------- +-- TYPES ----------------------------------------------------------------------- +-------------------------------------------------------------------------------- +type alias MapData = + { + w : Int, + h : Int, + t : (List Int) + } + +-------------------------------------------------------------------------------- +-- LOCAL ----------------------------------------------------------------------- +-------------------------------------------------------------------------------- +deserialize_tile : Int -> Int -> Int -> Struct.Tile.Type +deserialize_tile map_width index id = + (Struct.Tile.new + (index % map_width) + (index // map_width) + (Data.Tiles.get_icon id) + (Data.Tiles.get_cost id) + ) + +-------------------------------------------------------------------------------- +-- EXPORTED -------------------------------------------------------------------- +-------------------------------------------------------------------------------- +decode : (Struct.Model.Type -> (Json.Decode.Decoder Struct.ServerReply.Type)) +decode model input = + case + (Json.Decode.decodeString + (Json.Decode.map3 MapData + (Json.Decode.field "w" Json.Decode.int) + (Json.Decode.field "h" Json.Decode.int) + (Json.Decode.field + "t" + (Json.Decode.list Json.Decode.int) + ) + ) + input + ) + of + (Result.Ok map_data) -> + (Result.Ok + (Struct.ServerReply.SetMap + (Struct.Battlemap.new + map_data.w + map_data.h + (List.indexedMap + (deserialize_tile map_data.w) + map_data.t + ) + ) + ) + ) + + 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 + ) diff --git a/src/battlemap/src/Update/HandleServerReply/AddChar.elm b/src/battlemap/src/Update/HandleServerReply/AddChar.elm deleted file mode 100644 index 2fa4195..0000000 --- a/src/battlemap/src/Update/HandleServerReply/AddChar.elm +++ /dev/null @@ -1,149 +0,0 @@ -module Update.HandleServerReply.AddChar exposing (apply_to) - --- Elm ------------------------------------------------------------------------- -import Dict - -import Json.Decode -import Json.Decode.Pipeline - --- Battlemap ------------------------------------------------------------------- -import Data.Weapons - -import Struct.Attributes -import Struct.Character -import Struct.Error -import Struct.Model -import Struct.WeaponSet - --------------------------------------------------------------------------------- --- TYPES ----------------------------------------------------------------------- --------------------------------------------------------------------------------- -type alias CharAtt = - { - con : Int, - dex : Int, - int : Int, - min : Int, - spe : Int, - str : Int - } - -type alias Location = - { - x : Int, - y : Int - } - -type alias CharData = - { - ix : Int, - nam : String, - ico : String, - prt : String, - lc : Location, - hea : Int, - pla : String, - ena : Bool, - att : CharAtt, - awp : Int, - swp : Int - } - --------------------------------------------------------------------------------- --- LOCAL ----------------------------------------------------------------------- --------------------------------------------------------------------------------- -attributes_decoder : (Json.Decode.Decoder CharAtt) -attributes_decoder = - (Json.Decode.Pipeline.decode - CharAtt - |> (Json.Decode.Pipeline.required "con" Json.Decode.int) - |> (Json.Decode.Pipeline.required "dex" Json.Decode.int) - |> (Json.Decode.Pipeline.required "int" Json.Decode.int) - |> (Json.Decode.Pipeline.required "min" Json.Decode.int) - |> (Json.Decode.Pipeline.required "spe" Json.Decode.int) - |> (Json.Decode.Pipeline.required "str" Json.Decode.int) - ) - -location_decoder : (Json.Decode.Decoder Location) -location_decoder = - (Json.Decode.Pipeline.decode - Location - |> (Json.Decode.Pipeline.required "x" Json.Decode.int) - |> (Json.Decode.Pipeline.required "y" Json.Decode.int) - ) - -char_decoder : (Json.Decode.Decoder CharData) -char_decoder = - (Json.Decode.Pipeline.decode - CharData - |> (Json.Decode.Pipeline.required "ix" Json.Decode.int) - |> (Json.Decode.Pipeline.required "nam" Json.Decode.string) - |> (Json.Decode.Pipeline.required "ico" Json.Decode.string) - |> (Json.Decode.Pipeline.required "prt" Json.Decode.string) - |> (Json.Decode.Pipeline.required "lc" location_decoder) - |> (Json.Decode.Pipeline.required "hea" Json.Decode.int) - |> (Json.Decode.Pipeline.required "pla" Json.Decode.string) - |> (Json.Decode.Pipeline.required "ena" Json.Decode.bool) - |> (Json.Decode.Pipeline.required "att" attributes_decoder) - |> (Json.Decode.Pipeline.required "awp" Json.Decode.int) - |> (Json.Decode.Pipeline.required "swp" Json.Decode.int) - ) - --------------------------------------------------------------------------------- --- EXPORTED -------------------------------------------------------------------- --------------------------------------------------------------------------------- -apply_to : Struct.Model.Type -> String -> Struct.Model.Type -apply_to model serialized_char = - case - (Json.Decode.decodeString - char_decoder - serialized_char - ) - 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.Err msg) -> - (Struct.Model.invalidate - model - (Struct.Error.new - Struct.Error.Programming - ("Could not deserialize character: " ++ msg) - ) - ) diff --git a/src/battlemap/src/Update/HandleServerReply/SetMap.elm b/src/battlemap/src/Update/HandleServerReply/SetMap.elm deleted file mode 100644 index 88eed11..0000000 --- a/src/battlemap/src/Update/HandleServerReply/SetMap.elm +++ /dev/null @@ -1,70 +0,0 @@ -module Update.HandleServerReply.SetMap exposing (apply_to) - --- Elm ------------------------------------------------------------------------- -import Dict -import Json.Decode - --- Battlemap ------------------------------------------------------------------- -import Data.Tiles - -import Struct.Battlemap -import Struct.Model -import Struct.Tile - --------------------------------------------------------------------------------- --- TYPES ----------------------------------------------------------------------- --------------------------------------------------------------------------------- -type alias MapData = - { - w : Int, - h : Int, - t : (List Int) - } - --------------------------------------------------------------------------------- --- LOCAL ----------------------------------------------------------------------- --------------------------------------------------------------------------------- -deserialize_tile : Int -> Int -> Int -> Struct.Tile.Type -deserialize_tile map_width index id = - (Struct.Tile.new - (index % map_width) - (index // map_width) - (Data.Tiles.get_icon id) - (Data.Tiles.get_cost id) - ) - --------------------------------------------------------------------------------- --- EXPORTED -------------------------------------------------------------------- --------------------------------------------------------------------------------- -apply_to : Struct.Model.Type -> String -> Struct.Model.Type -apply_to model serialized_map = - case - (Json.Decode.decodeString - (Json.Decode.map3 MapData - (Json.Decode.field "w" Json.Decode.int) - (Json.Decode.field "h" Json.Decode.int) - (Json.Decode.field - "t" - (Json.Decode.list Json.Decode.int) - ) - ) - serialized_map - ) - 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 - ) - ) - } - (Dict.empty) - ) - - _ -> model -- cgit v1.2.3-70-g09d2