summaryrefslogtreecommitdiff
blob: 7d53bcd5d3066c17b74eef5fe51a53647d9ea7c5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
-module(btl_character_turn_request).

-define(PLAYER_ID_FIELD, <<"pid">>).
-define(SESSION_TOKEN_FIELD, <<"stk">>).
-define(BATTLE_ID_FIELD, <<"bid">>).
-define(CHAR_IX_FIELD, <<"cix">>).
-define(ACTIONS_FIELD, <<"act">>).
-define(ACTIONS_MOVE_FIELD, <<"mov">>).
-define(ACTIONS_WEAPON_SWITCH_FIELD, <<"wps">>).
-define(ACTIONS_ATTACK_FIELD, <<"tar">>).

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% TYPES %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-record
(
   type,
   {
      player_id :: shr_player:id(),
      session_token :: binary(),
      battle_id :: binary(),
      character_ix :: non_neg_integer(),
      actions :: list(btl_action:type())
   }
).

-opaque type() :: #type{}.

-export_type([type/0]).

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% EXPORTS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-export
(
   [
      decode/1
   ]
).

-export
(
   [
      get_player_id/1,
      get_session_token/1,
      get_battle_id/1,
      get_character_ix/1,
      get_actions/1
   ]
).

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% LOCAL FUNCTIONS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-spec decode_actions (non_neg_integer(), map()) -> list(btl_action:type()).
decode_actions (CharacterIX, Act) ->
   S0Result = [],
   S1Result =
      case
         btl_action:maybe_decode_move
         (
            CharacterIX,
            maps:get(?ACTIONS_MOVE_FIELD, Act)
         )
      of
         [] -> S0Result;
         [Move] -> [Move|S0Result]
      end,

   S2Result =
      case
         btl_action:maybe_decode_attack
         (
            CharacterIX,
            maps:get(?ACTIONS_ATTACK_FIELD, Act)
         )
      of
         [] -> S1Result;
         [Atk] -> [Atk|S1Result]
      end,

   S3Result =
      case
         btl_action:maybe_decode_weapon_switch
         (
            CharacterIX,
            maps:get(?ACTIONS_WEAPON_SWITCH_FIELD, Act)
         )
      of
         [] -> S2Result;
         [Wps] -> [Wps|S2Result]
      end,

   lists:reverse(S3Result).

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% EXPORTED FUNCTIONS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-spec decode (map()) -> type().
decode (Map) ->
   CharacterIX = maps:get(?CHAR_IX_FIELD, Map),
   EncodedActions = maps:get(?ACTIONS_FIELD, Map),
   Actions = decode_actions(CharacterIX, EncodedActions),

   #type
   {
      player_id = maps:get(?PLAYER_ID_FIELD, Map),
      session_token = maps:get(?SESSION_TOKEN_FIELD, Map),
      battle_id = maps:get(?BATTLE_ID_FIELD, Map),
      character_ix = CharacterIX,
      actions = Actions
   }.

-spec get_player_id (type()) -> shr_player:id().
get_player_id (Request) -> Request#type.player_id.

-spec get_session_token (type()) -> binary().
get_session_token (Request) -> Request#type.session_token.

-spec get_battle_id (type()) -> binary().
get_battle_id (Request) -> Request#type.battle_id.

-spec get_character_ix (type()) -> non_neg_integer().
get_character_ix (Request) -> Request#type.character_ix.

-spec get_actions (type()) -> list(btl_action:type()).
get_actions (Request) -> Request#type.actions.