| summaryrefslogtreecommitdiff | 
diff options
| -rw-r--r-- | src/battle/attack.erl | 34 | ||||
| -rw-r--r-- | src/reply/add_char.erl | 5 | ||||
| -rw-r--r-- | src/struct/battle_action.erl | 2 | ||||
| -rw-r--r-- | src/struct/direction.erl | 23 | ||||
| -rw-r--r-- | src/struct/location.erl | 14 | ||||
| -rw-r--r-- | src/struct/turn_result.erl | 148 | 
6 files changed, 208 insertions, 18 deletions
| diff --git a/src/battle/attack.erl b/src/battle/attack.erl index 9baf798..f1ef048 100644 --- a/src/battle/attack.erl +++ b/src/battle/attack.erl @@ -21,6 +21,9 @@        | 'nothing'     ). +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%% EXPORTS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  -export_type  (     [ @@ -32,9 +35,7 @@        attack_order_with_parry/0     ]  ). -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -%% EXPORTS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +  -export  (     [ @@ -44,6 +45,13 @@     ]  ). +-export +( +   [ +      encode/1 +   ] +). +  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  %% LOCAL FUNCTIONS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% @@ -278,3 +286,23 @@ get_sequence (AttackRange, AttackerWeapon, DefenderWeapon) ->        true ->           [First, Counter, Second]     end. + +-spec encode (attack_desc()) -> binary(). +% This shouldn't be a possibility. Types in this module are a mess... +encode ({AttackCategory, AttackEffect}) -> +   jiffy:encode +   ( +      { +         [ +            <<"attack">>, +            list_to_binary +            ( +               io_lib:format +               ( +                  "~p", +                  [{AttackCategory, AttackEffect}] +               ) +            ) +         ] +      } +   ). diff --git a/src/reply/add_char.erl b/src/reply/add_char.erl index 8518372..a920a2a 100644 --- a/src/reply/add_char.erl +++ b/src/reply/add_char.erl @@ -38,7 +38,7 @@ attributes_as_json (Attributes) ->     -> binary().  encode (IX, CharacterInstance, PlayerID) ->     Character = character_instance:get_character(CharacterInstance), -   {X, Y} = character_instance:get_location(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), @@ -55,8 +55,7 @@ encode (IX, CharacterInstance, PlayerID) ->                 <<"hea">>,                 character_instance:get_current_health(CharacterInstance)              }, -            {<<"lcx">>, X}, -            {<<"lcy">>, Y}, +            {<<"lc">>, location:encode(Location)},              {<<"pla">>, OwnerID},              {                 <<"ena">>, diff --git a/src/struct/battle_action.erl b/src/struct/battle_action.erl index d6bda92..b55a92e 100644 --- a/src/struct/battle_action.erl +++ b/src/struct/battle_action.erl @@ -49,7 +49,7 @@  -spec decode_mov_action (map()) -> struct().  decode_mov_action (JSONMap) ->     PathInBinary = maps:get(<<"p">>, JSONMap), -   Path = lists:map(fun direction:from_binary/1, PathInBinary), +   Path = lists:map(fun direction:decode/1, PathInBinary),     #move { path = Path }. diff --git a/src/struct/direction.erl b/src/struct/direction.erl index 074cadf..84ae272 100644 --- a/src/struct/direction.erl +++ b/src/struct/direction.erl @@ -12,8 +12,8 @@  -export  (     [ -      from_binary/1, -      to_binary/1 +      decode/1, +      encode/1     ]  ). @@ -24,13 +24,14 @@  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  %% EXPORTED FUNCTIONS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% --spec from_binary (binary()) -> enum(). -from_binary (<<"U">>) -> up; -from_binary (<<"D">>) -> down; -from_binary (<<"L">>) -> left; -from_binary (<<"R">>) -> right. +-spec decode (binary()) -> enum(). +decode (<<"U">>) -> up; +decode (<<"D">>) -> down; +decode (<<"L">>) -> left; +decode (<<"R">>) -> right. -to_binary (up) -> <<"U">>; -to_binary (down) -> <<"D">>; -to_binary (left) -> <<"L">>; -to_binary (right) -> <<"R">>. +-spec encode (enum()) -> binary(). +encode (up) -> <<"U">>; +encode (down) -> <<"D">>; +encode (left) -> <<"L">>; +encode (right) -> <<"R">>. diff --git a/src/struct/location.erl b/src/struct/location.erl index 624fe56..462dc46 100644 --- a/src/struct/location.erl +++ b/src/struct/location.erl @@ -13,6 +13,14 @@  -export  (     [ +      decode/1, +      encode/1 +   ] +). + +-export +( +   [        apply_direction/2,        dist/2     ] @@ -43,3 +51,9 @@ apply_direction (down, {X, Y}) ->  -spec dist(type(), type()) -> non_neg_integer().  dist ({OX, OY}, {DX, DY}) ->     (abs(DY - OY) + abs(DX - OX)). + +-spec encode (type()) -> list(non_neg_integer()). +encode ({X, Y}) -> [X, Y]. + +-spec decode (list(non_neg_integer)) -> type(). +decode ([X, Y]) when (is_integer(X) and is_integer(Y)) -> validate({X, Y}). diff --git a/src/struct/turn_result.erl b/src/struct/turn_result.erl new file mode 100644 index 0000000..d8ca2be --- /dev/null +++ b/src/struct/turn_result.erl @@ -0,0 +1,148 @@ +-module(turn_result). + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%% TYPES %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%%% +-record +( +   switched_weapon, +   { +      character_instance_ix :: character_instance:id() +   } +). + +-record +( +   moved, +   { +      character_instance_ix :: character_instance:id(), +      path :: [direction:enum()], +      new_location :: location:type() +   } +). + +-record +( +   attacked, +   { +      attacker_ix :: character_instance:id(), +      defender_ix :: character_instance:id(), +      sequence :: list(attack:attack_desc()) +   } +). + +-opaque struct() :: (#switched_weapon{} | #moved{} | #attacked{}). + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%% EXPORTS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +-export_type([struct/0]). + +-export +( +   [ +      new_character_switched_weapons/1, +      new_character_moved/3, +      new_character_attacked/3 +   ] +). + +-export +( +   [ +      encode/1 +   ] +). + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%% LOCAL FUNCTIONS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%% EXPORTED FUNCTIONS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +-spec new_character_switched_weapons (character_instance:id()) -> struct(). +new_character_switched_weapons (CharacterInstanceIX) -> +   #switched_weapon { character_instance_ix = CharacterInstanceIX }. + +-spec new_character_moved +   ( +      character_instance:id(), +      list(direction:enum()), +      location:type() +   ) +   -> struct(). +new_character_moved (CharacterInstanceIX, Path, NewLocation) -> +   #moved +   { +      character_instance_ix = CharacterInstanceIX, +      path = Path, +      new_location = NewLocation +   }. + +-spec new_character_attacked +   ( +      character_instance:id(), +      character_instance:id(), +      list(attack:attack_desc()) +   ) +   -> struct(). +new_character_attacked (AttackerIX, DefenderIX, AttackSequence) -> +   #attacked +   { +      attacker_ix = AttackerIX, +      defender_ix = DefenderIX, +      sequence = AttackSequence +   }. + +-spec encode (struct()) -> binary(). +encode (TurnResult) when is_record(TurnResult, switched_weapon) -> +   CharacterInstanceIX = TurnResult#switched_weapon.character_instance_ix, + +   jiffy:encode +   ( +      { +         [ +            {<<"t">>, <<"swp">>}, +            {<<"ix">>, CharacterInstanceIX} +         ] +      } +   ); +encode (TurnResult) when is_record(TurnResult, moved) -> +   CharacterInstanceIX = TurnResult#moved.character_instance_ix, +   Path = TurnResult#moved.path, +   NewLocation = TurnResult#moved.new_location, + +   EncodedPath = lists:map(fun direction:encode/1, Path), +   EncodedNewLocation = location:encode(NewLocation), + +   jiffy:encode +   ( +      { +         [ +            {<<"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, +   Sequence = TurnResult#attacked.sequence, + +   EncodedSequence = lists:map(fun attack:encode/1, Sequence), + +   jiffy:encode +   ( +      { +         [ +            {<<"t">>, <<"atk">>}, +            {<<"aix">>, AttackerIX}, +            {<<"dix">>, DefenderIX}, +            {<<"seq">>, EncodedSequence} +         ] +      } +   ). | 


