summaryrefslogtreecommitdiff |
diff options
author | nsensfel <SpamShield0@noot-noot.org> | 2018-03-09 16:48:56 +0100 |
---|---|---|
committer | nsensfel <SpamShield0@noot-noot.org> | 2018-03-09 16:48:56 +0100 |
commit | 8357273cf79511002f561a800b8d773d13b7fffa (patch) | |
tree | 895460a54746a944c49db90c8583fc1eed3c8f2c | |
parent | f70077b581408825ae4810983c8b1704e0a41289 (diff) |
Got the TurnResults JSON to work, I think.
-rw-r--r-- | src/battlemap/src/Send/AddChar.elm | 121 | ||||
-rw-r--r-- | src/battlemap/src/Send/Send.elm | 2 | ||||
-rw-r--r-- | src/battlemap/src/Send/TurnResults.elm | 27 | ||||
-rw-r--r-- | src/battlemap/src/Struct/Attack.elm | 36 | ||||
-rw-r--r-- | src/battlemap/src/Struct/Attributes.elm | 21 | ||||
-rw-r--r-- | src/battlemap/src/Struct/Character.elm | 107 | ||||
-rw-r--r-- | src/battlemap/src/Struct/Direction.elm | 17 | ||||
-rw-r--r-- | src/battlemap/src/Struct/Location.elm | 10 | ||||
-rw-r--r-- | src/battlemap/src/Struct/ServerReply.elm | 1 | ||||
-rw-r--r-- | src/battlemap/src/Struct/TurnResult.elm | 80 |
10 files changed, 271 insertions, 151 deletions
diff --git a/src/battlemap/src/Send/AddChar.elm b/src/battlemap/src/Send/AddChar.elm index 66a837f..be3fea7 100644 --- a/src/battlemap/src/Send/AddChar.elm +++ b/src/battlemap/src/Send/AddChar.elm @@ -4,132 +4,37 @@ module Send.AddChar exposing (decode) import Dict import Json.Decode -import Json.Decode.Pipeline -- Battlemap ------------------------------------------------------------------- import Data.Weapons -import Struct.Attributes import Struct.Character import Struct.Model import Struct.ServerReply -import Struct.WeaponSet +import Struct.Weapon -------------------------------------------------------------------------------- -- 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) - ) +weapon_getter : Struct.Model.Type -> Struct.Weapon.Ref -> Struct.Weapon.Type +weapon_getter model ref = + case (Dict.get ref model.weapons) of + (Just w) -> w + Nothing -> Data.Weapons.none -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) - ) - -internal_decoder : Struct.Model.Type -> CharData -> Struct.ServerReply.Type -internal_decoder model char_data = - (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) - ) - ) - ) - ) +internal_decoder : Struct.Character.Type -> Struct.ServerReply.Type +internal_decoder char = (Struct.ServerReply.AddCharacter char) -------------------------------------------------------------------------------- -- EXPORTED -------------------------------------------------------------------- -------------------------------------------------------------------------------- decode : (Struct.Model.Type -> (Json.Decode.Decoder Struct.ServerReply.Type)) -decode model = (Json.Decode.map (internal_decoder model) char_decoder) +decode model = + (Json.Decode.map + (internal_decoder) + (Struct.Character.decoder (weapon_getter model)) + ) diff --git a/src/battlemap/src/Send/Send.elm b/src/battlemap/src/Send/Send.elm index da26864..c8f7f5e 100644 --- a/src/battlemap/src/Send/Send.elm +++ b/src/battlemap/src/Send/Send.elm @@ -9,6 +9,7 @@ import Json.Encode -- Battlemap ------------------------------------------------------------------- import Send.SetMap import Send.AddChar +import Send.TurnResults import Struct.Event import Struct.ServerReply @@ -30,6 +31,7 @@ internal_decoder model reply_type = case reply_type of "add_char" -> (Send.AddChar.decode model) "set_map" -> (Send.SetMap.decode model) + "turn_results" -> (Send.TurnResults.decode) other -> (Json.Decode.fail ( diff --git a/src/battlemap/src/Send/TurnResults.elm b/src/battlemap/src/Send/TurnResults.elm new file mode 100644 index 0000000..5d2c748 --- /dev/null +++ b/src/battlemap/src/Send/TurnResults.elm @@ -0,0 +1,27 @@ +module Send.TurnResults exposing (decode) + +-- Elm ------------------------------------------------------------------------- +import Json.Decode + +-- Battlemap ------------------------------------------------------------------- +import Struct.ServerReply +import Struct.TurnResult + +-------------------------------------------------------------------------------- +-- TYPES ----------------------------------------------------------------------- +-------------------------------------------------------------------------------- +-------------------------------------------------------------------------------- +-- LOCAL ----------------------------------------------------------------------- +-------------------------------------------------------------------------------- +internal_decoder : (List Struct.TurnResult.Type) -> Struct.ServerReply.Type +internal_decoder trl = (Struct.ServerReply.TurnResults trl) + +-------------------------------------------------------------------------------- +-- EXPORTED -------------------------------------------------------------------- +-------------------------------------------------------------------------------- +decode : (Json.Decode.Decoder Struct.ServerReply.Type) +decode = + (Json.Decode.map + (internal_decoder) + (Json.Decode.field "cnt" (Json.Decode.list Struct.TurnResult.decoder)) + ) diff --git a/src/battlemap/src/Struct/Attack.elm b/src/battlemap/src/Struct/Attack.elm index 5159f7d..cb7acf8 100644 --- a/src/battlemap/src/Struct/Attack.elm +++ b/src/battlemap/src/Struct/Attack.elm @@ -1,6 +1,7 @@ -module Struct.Attack exposing (Type, Order, Precision) +module Struct.Attack exposing (Type, Order, Precision, decoder) -- Elm ------------------------------------------------------------------------- +import Json.Decode -- Battlemap ------------------------------------------------------------------- @@ -21,6 +22,7 @@ type alias Type = { order : Order, precision : Precision, + critical : Bool, parried : Bool, damage : Int } @@ -28,6 +30,38 @@ type alias Type = -------------------------------------------------------------------------------- -- LOCAL ----------------------------------------------------------------------- -------------------------------------------------------------------------------- +order_from_string : String -> Order +order_from_string str = + case str of + "f" -> First + "s" -> Second + _ -> Counter + +precision_from_string : String -> Precision +precision_from_string str = + case str of + "h" -> Hit + "g" -> Graze + _ -> Miss + +order_decoder : (Json.Decode.Decoder Order) +order_decoder = (Json.Decode.map (order_from_string) (Json.Decode.string)) + +precision_decoder : (Json.Decode.Decoder Precision) +precision_decoder = + (Json.Decode.map (precision_from_string) (Json.Decode.string)) + +decoder : (Json.Decode.Decoder Type) +decoder = + (Json.Decode.map5 + Type + (Json.Decode.field "ord" (order_decoder)) + (Json.Decode.field "pre" (precision_decoder)) + (Json.Decode.field "cri" (Json.Decode.bool)) + (Json.Decode.field "par" (Json.Decode.bool)) + (Json.Decode.field "dmg" (Json.Decode.int)) + ) + -------------------------------------------------------------------------------- -- EXPORTED -------------------------------------------------------------------- diff --git a/src/battlemap/src/Struct/Attributes.elm b/src/battlemap/src/Struct/Attributes.elm index 277a3ec..407856d 100644 --- a/src/battlemap/src/Struct/Attributes.elm +++ b/src/battlemap/src/Struct/Attributes.elm @@ -13,9 +13,16 @@ module Struct.Attributes exposing mod_mind, mod_speed, mod_strength, - new + new, + decoder ) +-- Elm ------------------------------------------------------------------------- +import Json.Decode +import Json.Decode.Pipeline + +-- Battlemap ------------------------------------------------------------------- + -------------------------------------------------------------------------------- -- TYPES ----------------------------------------------------------------------- -------------------------------------------------------------------------------- @@ -113,3 +120,15 @@ new con dex int min spe str = speed = spe, strength = str } + +decoder : (Json.Decode.Decoder Type) +decoder = + (Json.Decode.Pipeline.decode + Type + |> (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) + ) diff --git a/src/battlemap/src/Struct/Character.elm b/src/battlemap/src/Struct/Character.elm index 46ac8b2..450c605 100644 --- a/src/battlemap/src/Struct/Character.elm +++ b/src/battlemap/src/Struct/Character.elm @@ -2,7 +2,6 @@ module Struct.Character exposing ( Type, Ref, - new, get_ref, get_player_id, get_icon_id, @@ -15,18 +14,39 @@ module Struct.Character exposing is_enabled, set_enabled, get_weapons, - set_weapons + set_weapons, + decoder ) +-- Elm ------------------------------------------------------------------------- +import Json.Decode +import Json.Decode.Pipeline + -- Battlemap ------------------------------------------------------------------- import Struct.Attributes import Struct.Location import Struct.Statistics +import Struct.Weapon import Struct.WeaponSet -------------------------------------------------------------------------------- -- TYPES ----------------------------------------------------------------------- -------------------------------------------------------------------------------- +type alias PartiallyDecoded = + { + ix : Int, + nam : String, + ico : String, + prt : String, + lc : Struct.Location.Type, + hea : Int, + pla : String, + ena : Bool, + att : Struct.Attributes.Type, + awp : Int, + swp : Int + } + type alias Type = { id : String, @@ -47,46 +67,34 @@ type alias Ref = String -------------------------------------------------------------------------------- -- LOCAL ----------------------------------------------------------------------- -------------------------------------------------------------------------------- +finish_decoding : ( + (Struct.Weapon.Ref -> Struct.Weapon.Type) -> + PartiallyDecoded -> + Type + ) +finish_decoding get_weapon add_char = + let + active_weapon = (get_weapon add_char.awp) + secondary_weapon = (get_weapon add_char.swp) + weapon_set = (Struct.WeaponSet.new active_weapon secondary_weapon) + in + { + id = (toString add_char.ix), + name = add_char.nam, + icon = add_char.ico, + portrait = add_char.prt, + location = add_char.lc, + health = add_char.hea, + attributes = add_char.att, + statistics = (Struct.Statistics.new add_char.att weapon_set), + player_id = add_char.pla, + enabled = add_char.ena, + weapons = weapon_set + } -------------------------------------------------------------------------------- -- EXPORTED -------------------------------------------------------------------- -------------------------------------------------------------------------------- -new : ( - String -> -- id - String -> -- name - String -> -- icon - String -> -- portrait - Struct.Location.Type -> -- location - Int -> -- health - String -> -- player_id - Bool -> -- enabled - Struct.Attributes.Type -> - Struct.WeaponSet.Type -> - Type - ) -new - id name icon portrait - location health - player_id enabled - attributes weapons = - { - id = id, - name = name, - icon = icon, - portrait = portrait, - location = location, - health = health, - attributes = attributes, - statistics = - (Struct.Statistics.new - attributes - weapons - ), - player_id = player_id, - enabled = enabled, - weapons = weapons - } - get_ref : Type -> Ref get_ref c = c.id @@ -133,3 +141,26 @@ set_weapons weapons char = weapons ) } + +decoder : ( + (Struct.Weapon.Ref -> Struct.Weapon.Type) -> + (Json.Decode.Decoder Type) + ) +decoder get_weapon = + (Json.Decode.map + (finish_decoding get_weapon) + (Json.Decode.Pipeline.decode + PartiallyDecoded + |> (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" (Struct.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" (Struct.Attributes.decoder)) + |> (Json.Decode.Pipeline.required "awp" Json.Decode.int) + |> (Json.Decode.Pipeline.required "swp" Json.Decode.int) + ) + ) diff --git a/src/battlemap/src/Struct/Direction.elm b/src/battlemap/src/Struct/Direction.elm index c8eb28e..600138c 100644 --- a/src/battlemap/src/Struct/Direction.elm +++ b/src/battlemap/src/Struct/Direction.elm @@ -1,4 +1,9 @@ -module Struct.Direction exposing (Type(..), opposite_of, to_string) +module Struct.Direction exposing (Type(..), opposite_of, to_string, decoder) + +-- Elm ------------------------------------------------------------------------- +import Json.Decode + +-- Battlemap ------------------------------------------------------------------- -------------------------------------------------------------------------------- -- TYPES ----------------------------------------------------------------------- @@ -13,6 +18,14 @@ type Type = -------------------------------------------------------------------------------- -- LOCAL ----------------------------------------------------------------------- -------------------------------------------------------------------------------- +from_string : String -> Type +from_string str = + case str of + "R" -> Right + "L" -> Left + "U" -> Up + "D" -> Down + _ -> None -------------------------------------------------------------------------------- -- EXPORTED -------------------------------------------------------------------- @@ -35,3 +48,5 @@ to_string dir = Down -> "D" None -> "N" +decoder : (Json.Decode.Decoder Type) +decoder = (Json.Decode.map (from_string) Json.Decode.string) diff --git a/src/battlemap/src/Struct/Location.elm b/src/battlemap/src/Struct/Location.elm index ad9a811..ab20fac 100644 --- a/src/battlemap/src/Struct/Location.elm +++ b/src/battlemap/src/Struct/Location.elm @@ -1,6 +1,8 @@ module Struct.Location exposing (..) -- Elm ------------------------------------------------------------------------- +import Json.Decode +import Json.Decode.Pipeline -- Battlemap ------------------------------------------------------------------- import Struct.Direction @@ -47,3 +49,11 @@ dist loc_a loc_b = + (abs (loc_a.y - loc_b.y)) ) + +decoder : (Json.Decode.Decoder Type) +decoder = + (Json.Decode.Pipeline.decode + Type + |> (Json.Decode.Pipeline.required "x" Json.Decode.int) + |> (Json.Decode.Pipeline.required "y" Json.Decode.int) + ) diff --git a/src/battlemap/src/Struct/ServerReply.elm b/src/battlemap/src/Struct/ServerReply.elm index 8dec96b..dcbd043 100644 --- a/src/battlemap/src/Struct/ServerReply.elm +++ b/src/battlemap/src/Struct/ServerReply.elm @@ -6,7 +6,6 @@ module Struct.ServerReply exposing (Type(..)) import Struct.Battlemap import Struct.Character import Struct.TurnResult -import Struct.Model -------------------------------------------------------------------------------- -- TYPES ----------------------------------------------------------------------- diff --git a/src/battlemap/src/Struct/TurnResult.elm b/src/battlemap/src/Struct/TurnResult.elm index 4b2b059..dd7c9ef 100644 --- a/src/battlemap/src/Struct/TurnResult.elm +++ b/src/battlemap/src/Struct/TurnResult.elm @@ -1,6 +1,15 @@ -module Struct.TurnResult exposing (Type(..), Attack, Movement, WeaponSwitch) +module Struct.TurnResult exposing + ( + Type(..), + Attack, + Movement, + WeaponSwitch, + decoder + ) -- Elm ------------------------------------------------------------------------- +import Json.Decode +import Json.Decode.Pipeline -- Battlemap ------------------------------------------------------------------- import Struct.Direction @@ -38,6 +47,75 @@ type Type = -- LOCAL ----------------------------------------------------------------------- -------------------------------------------------------------------------------- +movement_decoder : (Json.Decode.Decoder Movement) +movement_decoder = + (Json.Decode.map3 + Movement + (Json.Decode.field "ix" Json.Decode.int) + (Json.Decode.field + "p" + (Json.Decode.list (Struct.Direction.decoder)) + ) + (Json.Decode.field + "nlc" + (Struct.Location.decoder) + ) + ) + +attack_decoder : (Json.Decode.Decoder Attack) +attack_decoder = + (Json.Decode.map3 + Attack + (Json.Decode.field "aix" Json.Decode.int) + (Json.Decode.field "dix" Json.Decode.int) + (Json.Decode.field + "seq" + (Json.Decode.list (Struct.Attack.decoder)) + ) + ) + +weapon_switch_decoder : (Json.Decode.Decoder WeaponSwitch) +weapon_switch_decoder = + (Json.Decode.map + WeaponSwitch + (Json.Decode.field "ix" Json.Decode.int) + ) + + +internal_decoder : String -> (Json.Decode.Decoder Type) +internal_decoder kind = + case kind of + "swp" -> + (Json.Decode.map + (\x -> (SwitchedWeapon x)) + (weapon_switch_decoder) + ) + + "mv" -> + (Json.Decode.map + (\x -> (Moved x)) + (movement_decoder) + ) + + "atk" -> + (Json.Decode.map + (\x -> (Attacked x)) + (attack_decoder) + ) + + other -> + (Json.Decode.fail + ( + "Unknown kind of turn result: \"" + ++ other + ++ "\"." + ) + ) + -------------------------------------------------------------------------------- -- EXPORTED -------------------------------------------------------------------- -------------------------------------------------------------------------------- +decoder : (Json.Decode.Decoder Type) +decoder = + (Json.Decode.field "t" Json.Decode.string) + |> (Json.Decode.andThen internal_decoder) |