| summaryrefslogtreecommitdiff | 
diff options
| author | nsensfel <SpamShield0@noot-noot.org> | 2018-03-06 17:08:00 +0100 | 
|---|---|---|
| committer | nsensfel <SpamShield0@noot-noot.org> | 2018-03-06 17:08:00 +0100 | 
| commit | 98203d4d0034dab5db72737bcfb92017a11f3245 (patch) | |
| tree | 9c4fd2a8904468d7ee69d86e39ce27051cc8ca2d /src | |
| parent | e5bc1b2d1bfcf5f36bd4f0d567e4ec8e0fb22e85 (diff) | |
I might have been using JSON the wrong way.refactoring
Diffstat (limited to 'src')
| -rw-r--r-- | src/query/character_turn.erl | 11 | ||||
| -rw-r--r-- | src/reply/add_char.erl | 76 | ||||
| -rw-r--r-- | src/reply/set_map.erl | 23 | ||||
| -rw-r--r-- | src/struct/attack.erl | 23 | ||||
| -rw-r--r-- | src/struct/location.erl | 20 | ||||
| -rw-r--r-- | src/struct/turn_result.erl | 55 | 
6 files changed, 96 insertions, 112 deletions
| diff --git a/src/query/character_turn.erl b/src/query/character_turn.erl index c4f9883..a692be1 100644 --- a/src/query/character_turn.erl +++ b/src/query/character_turn.erl @@ -151,14 +151,15 @@ update_cache (Battle, Input) ->     )     -> binary().  generate_reply (ClientUpdate) -> -   %% TODO     jiffy:encode     (        [ -         [ -            <<"raw">>, -            lists:map(fun turn_result:encode/1, ClientUpdate) -         ] +         { +            [ +               {<<"msg">>, <<"turn_results">>}, +               {<<"cnt">>, lists:map(fun turn_result:encode/1, ClientUpdate)} +            ] +         }        ]     ). diff --git a/src/reply/add_char.erl b/src/reply/add_char.erl index a920a2a..b3ef128 100644 --- a/src/reply/add_char.erl +++ b/src/reply/add_char.erl @@ -29,58 +29,46 @@ attributes_as_json (Attributes) ->        ]     }. --spec encode +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%% EXPORTED FUNCTIONS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +-spec generate     (        non_neg_integer(),        character_instance:struct(),        player:id()     ) -   -> binary(). -encode (IX, CharacterInstance, PlayerID) -> +   -> {list(any())}. +generate (IX, CharacterInstance, PlayerID) ->     Character = character_instance:get_character(CharacterInstance),     Location = character_instance:get_location(CharacterInstance),     Attributes = character:get_attributes(Character),     {ActiveWeapon, SecondaryWeapon} = character:get_weapon_ids(Character),     OwnerID = character:get_owner_id(Character), -   jiffy:encode -   ( -      { -         [ -            {<<"ix">>, IX}, -            {<<"nam">>, character:get_name(Character)}, -            {<<"ico">>, character:get_icon(Character)}, -            {<<"prt">>, character:get_portrait(Character)}, -            { -               <<"hea">>, -               character_instance:get_current_health(CharacterInstance) -            }, -            {<<"lc">>, location:encode(Location)}, -            {<<"pla">>, OwnerID}, -            { -               <<"ena">>, -               ( -                  character_instance:get_is_active(CharacterInstance) -                  and -                  (OwnerID == PlayerID) -               ) -            }, -            {<<"att">>, attributes_as_json(Attributes)}, -            {<<"awp">>, ActiveWeapon}, -            {<<"swp">>, SecondaryWeapon} -         ] -      } -   ). - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -%% EXPORTED FUNCTIONS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% --spec generate -   ( -      non_neg_integer(), -      character_instance:struct(), -      player:id() -   ) -   -> list(binary()). -generate (IX, CharacterInstance, PlayerID) -> -   [<<"add_char">>, encode(IX, CharacterInstance, PlayerID)]. +   { +      [ +         {<<"msg">>, <<"add_char">>}, +         {<<"ix">>, IX}, +         {<<"nam">>, character:get_name(Character)}, +         {<<"ico">>, character:get_icon(Character)}, +         {<<"prt">>, character:get_portrait(Character)}, +         { +            <<"hea">>, +            character_instance:get_current_health(CharacterInstance) +         }, +         {<<"lc">>, location:encode(Location)}, +         {<<"pla">>, OwnerID}, +         { +            <<"ena">>, +            ( +               character_instance:get_is_active(CharacterInstance) +               and +               (OwnerID == PlayerID) +            ) +         }, +         {<<"att">>, attributes_as_json(Attributes)}, +         {<<"awp">>, ActiveWeapon}, +         {<<"swp">>, SecondaryWeapon} +      ] +   }. diff --git a/src/reply/set_map.erl b/src/reply/set_map.erl index 0941239..6a7cd39 100644 --- a/src/reply/set_map.erl +++ b/src/reply/set_map.erl @@ -12,22 +12,17 @@  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  %% LOCAL FUNCTIONS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% --spec encode (battlemap:struct()) -> binary(). -encode (Battlemap) -> -   jiffy:encode -   ( -      { -         [ -            {<<"w">>, battlemap:get_width(Battlemap)}, -            {<<"h">>, battlemap:get_height(Battlemap)}, -            {<<"t">>, array:sparse_to_list(battlemap:get_tile_ids(Battlemap))} -         ] -      } -   ).  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  %% EXPORTED FUNCTIONS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% --spec generate (battlemap:struct()) -> list(binary()). +-spec generate (battlemap:struct()) -> {list(any())}.  generate (Battlemap) -> -   [<<"set_map">>, encode(Battlemap)]. +   { +      [ +         {<<"msg">>, <<"set_map">>}, +         {<<"w">>, battlemap:get_width(Battlemap)}, +         {<<"h">>, battlemap:get_height(Battlemap)}, +         {<<"t">>, array:sparse_to_list(battlemap:get_tile_ids(Battlemap))} +      ] +   }. diff --git a/src/struct/attack.erl b/src/struct/attack.erl index 7b81adc..7f6b302 100644 --- a/src/struct/attack.erl +++ b/src/struct/attack.erl @@ -281,7 +281,7 @@ get_sequence (AttackRange, AttackerWeapon, DefenderWeapon) ->           [First, Counter, Second]     end. --spec encode (struct()) -> binary(). +-spec encode (struct()) -> {list(any())}.  % This shouldn't be a possibility. Types in this module are a mess...  encode (Attack) ->     Order = Attack#attack.order, @@ -290,15 +290,12 @@ encode (Attack) ->     IsParry = Attack#attack.is_parry,     Damage = Attack#attack.damage, -   jiffy:encode -   ( -      { -         [ -            {<<"ord">>, encode_order(Order)}, -            {<<"pre">>, encode_precision(Precision)}, -            {<<"cri">>, IsCritical}, -            {<<"par">>, IsParry}, -            {<<"dmg">>, Damage} -         ] -      } -   ). +   { +      [ +         {<<"ord">>, encode_order(Order)}, +         {<<"pre">>, encode_precision(Precision)}, +         {<<"cri">>, IsCritical}, +         {<<"par">>, IsParry}, +         {<<"dmg">>, Damage} +      ] +   }. diff --git a/src/struct/location.erl b/src/struct/location.erl index 0d5367d..b8e2bf3 100644 --- a/src/struct/location.erl +++ b/src/struct/location.erl @@ -52,8 +52,20 @@ apply_direction (down, {X, Y}) ->  dist ({OX, OY}, {DX, DY}) ->     (abs(DY - OY) + abs(DX - OX)). --spec encode (type()) -> list(non_neg_integer()). -encode ({X, Y}) -> [X, Y]. +-spec encode (type()) -> {list(any())}. +encode ({X, Y}) -> +   { +      [ +         {<<"x">>, X}, +         {<<"y">>, Y} +      ] +   }. --spec decode (list(non_neg_integer())) -> type(). -decode ([X, Y]) when (is_integer(X) and is_integer(Y)) -> validate({X, Y}). +-spec decode (map()) -> type(). +decode (Map) -> +   X = maps:get(<<"x">>, Map), +   Y = maps:get(<<"y">>, Map), + +   true = (is_integer(X) and is_integer(Y)), + +   validate({X, Y}). diff --git a/src/struct/turn_result.erl b/src/struct/turn_result.erl index c5cafcd..5f796ca 100644 --- a/src/struct/turn_result.erl +++ b/src/struct/turn_result.erl @@ -96,19 +96,16 @@ new_character_attacked (AttackerIX, DefenderIX, AttackSequence) ->        sequence = AttackSequence     }. --spec encode (struct()) -> binary(). +-spec encode (struct()) -> {list(any())}.  encode (TurnResult) when is_record(TurnResult, switched_weapon) ->     CharacterInstanceIX = TurnResult#switched_weapon.character_instance_ix, -   jiffy:encode -   ( -      { -         [ -            {<<"t">>, <<"swp">>}, -            {<<"ix">>, CharacterInstanceIX} -         ] -      } -   ); +   { +      [ +         {<<"t">>, <<"swp">>}, +         {<<"ix">>, CharacterInstanceIX} +      ] +   };  encode (TurnResult) when is_record(TurnResult, moved) ->     CharacterInstanceIX = TurnResult#moved.character_instance_ix,     Path = TurnResult#moved.path, @@ -117,17 +114,14 @@ encode (TurnResult) when is_record(TurnResult, moved) ->     EncodedPath = lists:map(fun direction:encode/1, Path),     EncodedNewLocation = location:encode(NewLocation), -   jiffy:encode -   ( -      { -         [ -            {<<"t">>, <<"mv">>}, -            {<<"ix">>, CharacterInstanceIX}, -            {<<"p">>, EncodedPath}, -            {<<"nlc">>, EncodedNewLocation} -         ] -      } -   ); +   { +      [ +         {<<"t">>, <<"mv">>}, +         {<<"ix">>, CharacterInstanceIX}, +         {<<"p">>, EncodedPath}, +         {<<"nlc">>, EncodedNewLocation} +      ] +   };  encode (TurnResult) when is_record(TurnResult, attacked) ->     AttackerIX = TurnResult#attacked.attacker_ix,     DefenderIX = TurnResult#attacked.defender_ix, @@ -135,17 +129,14 @@ encode (TurnResult) when is_record(TurnResult, attacked) ->     EncodedSequence = lists:map(fun attack:encode/1, Sequence), -   jiffy:encode -   ( -      { -         [ -            {<<"t">>, <<"atk">>}, -            {<<"aix">>, AttackerIX}, -            {<<"dix">>, DefenderIX}, -            {<<"seq">>, EncodedSequence} -         ] -      } -   ); +   { +      [ +         {<<"t">>, <<"atk">>}, +         {<<"aix">>, AttackerIX}, +         {<<"dix">>, DefenderIX}, +         {<<"seq">>, EncodedSequence} +      ] +   };  encode (Other) ->     io:format("~n invalid encode param\"~p\"~n", [Other]),     true = Other. | 


