summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authornsensfel <SpamShield0@noot-noot.org>2018-07-12 17:13:00 +0200
committernsensfel <SpamShield0@noot-noot.org>2018-07-12 17:13:00 +0200
commitb853df7a1c3efef6b84b90fe8c492611564f8b53 (patch)
tree524925ca4a81809ca1dc70b7a4a719fcbb8c04d3 /src/battle
parent0b2562792eb35c35b573fd9a79d1c73576e0d536 (diff)
'tis always better with the missing files.
Diffstat (limited to 'src/battle')
-rw-r--r--src/battle/query/btl_load.erl159
-rw-r--r--src/battle/struct/btl_map.erl100
2 files changed, 259 insertions, 0 deletions
diff --git a/src/battle/query/btl_load.erl b/src/battle/query/btl_load.erl
new file mode 100644
index 0000000..7eac627
--- /dev/null
+++ b/src/battle/query/btl_load.erl
@@ -0,0 +1,159 @@
+-module(btl_load).
+
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%% 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_map(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),
+ shr_security:assert_identity
+ (
+ Input#input.player_id,
+ Input#input.session_token
+ ),
+ shr_security:lock_queries(Input#input.player_id),
+ QueryState = fetch_data(Input),
+ shr_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)
+ }.
diff --git a/src/battle/struct/btl_map.erl b/src/battle/struct/btl_map.erl
new file mode 100644
index 0000000..ef34833
--- /dev/null
+++ b/src/battle/struct/btl_map.erl
@@ -0,0 +1,100 @@
+-module(btl_map).
+
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%% TYPES %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+-type id() :: binary().
+
+-record
+(
+ map,
+ {
+ id :: id(),
+ width :: integer(),
+ height :: integer(),
+ tile_class_ids :: array:array(btl_tile:class_id())
+ }
+).
+
+-opaque type() :: #map{}.
+
+-export_type([type/0, id/0]).
+
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%% EXPORTS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%%%% Accessors
+-export
+(
+ [
+ get_id/1,
+ get_width/1,
+ get_height/1,
+ get_tile_class_ids/1,
+ get_tile_class_id/2
+ ]
+).
+
+-export
+(
+ [
+ from_list/4
+ ]
+).
+
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%% LOCAL FUNCTIONS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+-spec location_to_array_index
+ (
+ non_neg_integer(),
+ btl_location:type()
+ )
+ -> ('error' | non_neg_integer()).
+location_to_array_index (ArrayWidth, {X, Y}) ->
+ if
+ (X < 0) -> error;
+ (Y < 0) -> error;
+ (X >= ArrayWidth) -> error;
+ true -> ((Y * ArrayWidth) + X)
+ end.
+
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%% EXPORTED FUNCTIONS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%%%% Accessors
+-spec get_id (type()) -> id().
+get_id (Battlemap) -> Battlemap#map.id.
+
+-spec get_width (type()) -> integer().
+get_width (Battlemap) -> Battlemap#map.width.
+
+-spec get_height (type()) -> integer().
+get_height (Battlemap) -> Battlemap#map.height.
+
+-spec get_tile_class_ids (type()) -> array:array(btl_tile:class_id()).
+get_tile_class_ids (Battlemap) -> Battlemap#map.tile_class_ids.
+
+-spec get_tile_class_id (btl_location:type(), type()) -> btl_tile:class_id().
+get_tile_class_id (Location, Battlemap) ->
+ TileIX = location_to_array_index(Battlemap#map.width, Location),
+ array:get(TileIX, Battlemap#map.tile_class_ids).
+
+-spec from_list
+ (
+ non_neg_integer(),
+ non_neg_integer(),
+ non_neg_integer(),
+ list(non_neg_integer())
+ )
+ -> type().
+from_list (ID, Width, Height, List) ->
+ TileClassIDs = lists:map(fun btl_tile:class_id_from_int/1, List),
+
+ #map
+ {
+ id = list_to_binary(integer_to_list(ID)),
+ width = Width,
+ height = Height,
+ tile_class_ids = array:from_list(TileClassIDs)
+ }.