summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'src/struct')
-rw-r--r--src/struct/battle_action.erl2
-rw-r--r--src/struct/direction.erl23
-rw-r--r--src/struct/location.erl14
-rw-r--r--src/struct/turn_result.erl148
4 files changed, 175 insertions, 12 deletions
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}
+ ]
+ }
+ ).