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
|
-module(btl_load_state).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% TYPES %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-include("../../../include/yaws_api.hrl").
-record
(
input,
{
player_id :: btl_player:id(),
session_token :: binary(),
battle_id :: binary()
}
).
-record
(
query_state,
{
battle :: btl_battle: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),
BattleID = maps:get(<<"bid">>, JSONReqMap),
#input
{
player_id = PlayerID,
session_token = SessionToken,
battle_id = BattleID
}.
-spec fetch_data (input()) -> query_state().
fetch_data (Input) ->
PlayerID = Input#input.player_id,
BattleID = Input#input.battle_id,
Battle = shr_timed_cache:fetch(battle_db, PlayerID, BattleID),
#query_state
{
battle = Battle
}.
-spec generate_reply(query_state(), input()) -> binary().
generate_reply (QueryState, Input) ->
PlayerID = Input#input.player_id,
Battle = QueryState#query_state.battle,
Players = btl_battle:get_players(Battle),
PlayerIX =
shr_array_util:first
(
fun (Player) ->
(btl_player:get_id(Player) == PlayerID)
end,
Players
),
true = (PlayerIX >= 0),
SetTimeline =
btl_set_timeline:generate
(
btl_battle:get_encoded_last_turns_effects(Battle)
),
SetMap = btl_set_map:generate(btl_battle:get_battlemap(Battle)),
AddCharList =
array:sparse_to_list
(
array:map
(
fun (IX, Character) ->
btl_add_char:generate(IX, Character, PlayerIX)
end,
btl_battle:get_characters(Battle)
)
),
AddWeaponList =
lists:map
(
fun (WeaponID) ->
btl_add_weapon:generate(shr_weapon:from_id(WeaponID))
end,
btl_battle:get_used_weapon_ids(Battle)
),
AddArmorList =
lists:map
(
fun (ArmorID) ->
btl_add_armor:generate(shr_armor:from_id(ArmorID))
end,
btl_battle:get_used_armor_ids(Battle)
),
AddTileList =
lists:map
(
fun (TileID) ->
btl_add_tile:generate(btl_tile:from_id(TileID))
end,
btl_battle:get_used_tile_ids(Battle)
),
OutputList =
(
AddTileList
++ [SetTimeline, SetMap | AddWeaponList]
++ AddArmorList
++ AddCharList
),
Output = jiffy:encode(OutputList),
Output.
-spec handle (binary()) -> binary().
handle (Req) ->
Input = parse_input(Req),
btl_security:assert_identity
(
Input#input.player_id,
Input#input.session_token
),
btl_security:lock_queries(Input#input.player_id),
QueryState = fetch_data(Input),
btl_security:unlock_queries(Input#input.player_id),
generate_reply(QueryState, Input).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% EXPORTED FUNCTIONS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
out(A) ->
{
content,
"application/json; charset=UTF-8",
handle(A#arg.clidata)
}.
|