| 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
 | -module(map_update).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% TYPES %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-include("../../../include/yaws_api.hrl").
-record
(
   input,
   {
      player_id :: binary(),
      session_token :: binary(),
      map_id :: binary(),
      w :: non_neg_integer(),
      h :: non_neg_integer(),
      t :: list(list(non_neg_integer()))
   }
).
-record
(
   query_state,
   {
      map :: map_map:type()
   }
).
-type input() :: #input{}.
-type query_state() :: #query_state{}.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% 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),
   MapID = maps:get(<<"mid">>, JSONReqMap),
   MapWidth = maps:get(<<"w">>, JSONReqMap),
   MapHeight = maps:get(<<"h">>, JSONReqMap),
   MapContent = maps:get(<<"t">>, JSONReqMap),
   true = (MapWidth > 0),
   true = (MapHeight > 0),
   true = (length(MapContent) == (MapWidth * MapHeight)),
   true =
      lists:all
      (
         fun (T) ->
            [M|[V|B]] = T,
            (
               (M > 0)
               and (V >= 0)
               and ((length(B) rem 2) == 0)
               and
               lists:all
               (
                  fun (Bo) ->
                     %% FIXME: this does not prevent "Error" tiles.
                     (Bo >= 0)
                  end,
                  B
               )
            )
         end,
         MapContent
      ),
   #input
   {
      player_id = PlayerID,
      session_token = SessionToken,
      map_id = MapID,
      w = MapWidth,
      h = MapHeight,
      t = MapContent
   }.
-spec fetch_data (input()) -> query_state().
fetch_data (Input) ->
   PlayerID = Input#input.player_id,
   MapID = Input#input.map_id,
   Map = shr_timed_cache:fetch(map_db, PlayerID, MapID),
   #query_state
   {
      map = Map
   }.
-spec update_data (query_state(), input()) -> query_state().
update_data (QueryState, Input) ->
   QueryState#query_state
   {
      map =
         map_map:update_from_list
         (
            QueryState#query_state.map,
            Input#input.w,
            Input#input.h,
            Input#input.t
         )
   }.
-spec commit_update (query_state(), input()) -> 'ok'.
commit_update (QueryState, Input) ->
   PlayerID = Input#input.player_id,
   MapID = Input#input.map_id,
   Map = QueryState#query_state.map,
   Query =
      shr_db_query:new
      (
         map_db,
         MapID,
         {user, PlayerID},
         [
            shr_db_query:set_field
            (
               map_map:get_height_field(),
               Input#input.h
            ),
            shr_db_query:set_field
            (
               map_map:get_width_field(),
               Input#input.w
            ),
            shr_db_query:set_field
            (
               map_map:get_tile_instances_field(),
               map_map:get_tile_instances(Map)
            )
         ]
      ),
   shr_database:commit(Query),
   shr_timed_cache:update(map_db, PlayerID, MapID, Map),
   'ok'.
-spec generate_reply () -> binary().
generate_reply () ->
   jiffy:encode([shr_okay:generate()]).
-spec handle (binary()) -> binary().
handle (Req) ->
   Input = parse_input(Req),
   shr_security:assert_identity
   (
      Input#input.player_id,
      Input#input.session_token
   ),
   shr_security:lock_queries(Input#input.player_id),
   QueryState = fetch_data(Input),
   Update = update_data(QueryState, Input),
   commit_update(Update, Input),
   shr_security:unlock_queries(Input#input.player_id),
   generate_reply().
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% EXPORTED FUNCTIONS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
out(A) ->
   {
      content,
      "application/json; charset=UTF-8",
      handle(A#arg.clidata)
   }.
 |