summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authornsensfel <SpamShield0@noot-noot.org>2018-03-05 13:51:14 +0100
committernsensfel <SpamShield0@noot-noot.org>2018-03-05 13:51:14 +0100
commit6a82ead205f4463ee34cc4fcbc06f1b3e7b1dd1a (patch)
tree6b9fa780865044b8c57800d48fe251e4dff2d6f6 /src/struct/turn_result.erl
parent10b1058e56079232728e3fc959709bc784e58b5b (diff)
Starting to work on the client diff messages.
Diffstat (limited to 'src/struct/turn_result.erl')
-rw-r--r--src/struct/turn_result.erl148
1 files changed, 148 insertions, 0 deletions
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}
+ ]
+ }
+ ).