summaryrefslogtreecommitdiff
blob: c38cfdee15393dba296bf0169251166b7a145bde (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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
-module(btl_join).

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% TYPES %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-include("../../../include/yaws_api.hrl").

-type mode() :: (attack | defend | {invalid, binary()}).

-record
(
   input,
   {
      player_id :: shr_player:id(),
      session_token :: binary(),
      mode :: mode(),
      summary_ix :: non_neg_integer(),
      size :: non_neg_integer(),
      roster_ixs :: list(non_neg_integer()),
      map_id :: ataxia_id:type()
   }
).


-type input() :: #input{}.
-type defend_query_state() :: 'ok'.
-type attack_query_state() :: 'ok'.

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

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% LOCAL FUNCTIONS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-spec parse_input (binary()) -> input().
parse_input (Req) ->
   JSONReqMap = jiffy:decode(Req, [return_maps]),
   PlayerID = maps:get(<<"pid">>, JSONReqMap),
   SessionToken =  maps:get(<<"stk">>, JSONReqMap),
   SummaryIX = maps:get(<<"six">>, JSONReqMap),

   Mode =
      case maps:get(<<"m">>, JSONReqMap) of
         <<"a">> -> attack;
         <<"d">> -> defend;
         V -> {invalid, V}
      end,

   true = ((Mode == attack) or (Mode == defend)),

   Size =
      case maps:get(<<"s">>, JSONReqMap) of
         <<"s">> -> 8;
         <<"m">> -> 16;
         <<"l">> -> 24;
         _ -> 0
      end,

   Roster = maps:get(<<"r">>, JSONReqMap),
   MapID = maps:get(<<"map_id">>, JSONReqMap),

   #input
   {
      player_id = PlayerID,
      session_token = SessionToken,
      mode = Mode,
      size = Size,
      summary_ix = SummaryIX,
      roster_ixs = Roster,
      map_id = MapID
   }.

-spec authenticate_user (input()) -> ('ok' | 'error').
authenticate_user (Input) ->
   PlayerID = Input#input.player_id,
   SessionToken = Input#input.session_token,

   Player = shr_timed_cache:fetch(player_db, any, PlayerID),

   case shr_security:credentials_match(SessionToken, Player) of
      true -> ok;
      _ -> error
   end.

-spec handle_attack (input()) -> 'ok'.
handle_attack (Input) ->
   PlayerID = Input#input.player_id,
   SelectedCharacterIXs = Input#input.roster_ixs,
   SummaryIX = Input#input.summary_ix,
   PlayerDBUser = ataxia_security:user_from_id(PlayerID),
   PartySize = length(SelectedCharacterIXs),

   % TODO: be less brutal if none is found.
   {ok, AvailablePendingBattle, AvailablePendingBattleID} =
      ataxia_client:update_and_fetch_any
      (
         pending_battle_db,
         PlayerDBUser,
         ataxic:update_lock
         (
            ataxic:apply_function
            (
               ataxia_lock,
               locked,
               [
                  ataxic:constant(PlayerDBUser),
                  ataxic:constant(60)
               ]
            )
         ),
         ataxic:land
         (
            [
               ataxic:ge
               (
                  ataxic:field
                  (
                     ataxia_entry:get_value_field(),
                     ataxic:field
                     (
                        btl_pending_battle:get_free_slots_field(),
                        ataxic:current_value()
                     )
                  ),
                  ataxic:constant(PartySize)
               ),
               ataxic:neg
               (
                  ataxic:field
                  (
                     btl_pending_battle:get_player_ids_field(),
                     ataxic:apply_function
                     (
                        lists,
                        member,
                        [
                           ataxic:constant(PlayerID),
                           ataxic:current_value()
                        ]
                     )
                  )
               )
            ]
         )
      ),

   bnt_join_battle:attempt
   (
      PlayerID,
      SummaryIX,
      SelectedCharacterIXs,
      AvailablePendingBattleID,
      AvailablePendingBattle
   ),

   ok.
-spec handle_defend (input()) -> 'ok'.
handle_defend (Input) ->
   PlayerID = Input#input.player_id,
   SelectedCharacterIXs = Input#input.roster_ixs,
   SummaryIX = Input#input.summary_ix,
   MapID = Input#input.map_id,

   bnt_join_battle:generate(PlayerID, SummaryIX, MapID, SelectedCharacterIXs),

   ok.

-spec fetch_attack_data (input()) -> attack_query_state().
fetch_attack_data (_Input) -> ok. % TODO

-spec fetch_defend_data (input()) -> defend_query_state().
fetch_defend_data (_Input) -> ok. % TODO

-spec authorize_attack (attack_query_state(), input()) -> 'ok'.
authorize_attack (_QueryState, _Input) -> ok. % TODO

-spec authorize_defend (defend_query_state(), input()) -> 'ok'.
authorize_defend (_QueryState, _Input) -> ok. % TODO

-spec handle (binary()) -> binary().
handle (Req) ->
   Input = parse_input(Req),
   case authenticate_user(Input) of
      ok ->
         case Input#input.mode of
            attack ->
               QueryState = fetch_attack_data(Input),
               ok = authorize_attack(QueryState, Input),
               handle_attack(Input);

            defend ->
               QueryState = fetch_defend_data(Input),
               ok = authorize_defend(QueryState, Input),
               handle_defend(Input)
         end,
         jiffy:encode([shr_okay:generate()]);

      error -> jiffy:encode([shr_disconnected:generate()])
   end.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% EXPORTED FUNCTIONS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
out(A) ->
   {
      content,
      "application/json; charset=UTF-8",
      handle(A#arg.clidata)
   }.