| summaryrefslogtreecommitdiff | 
diff options
| -rw-r--r-- | src/battle/query/btl_load.erl | 159 | ||||
| -rw-r--r-- | src/battle/struct/btl_map.erl | 100 | ||||
| -rw-r--r-- | src/map/query/map_load.erl | 93 | ||||
| -rw-r--r-- | src/shared/shr_security.erl | 33 | 
4 files changed, 385 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) +   }. diff --git a/src/map/query/map_load.erl b/src/map/query/map_load.erl new file mode 100644 index 0000000..0f9c685 --- /dev/null +++ b/src/map/query/map_load.erl @@ -0,0 +1,93 @@ +-module(map_load). + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%% TYPES %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +-include("../../../include/yaws_api.hrl"). + +-record +( +   input, +   { +      player_id :: binary(), +      session_token :: binary(), +      map_id :: binary() +   } +). + +-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), + +   #input +   { +      player_id = PlayerID, +      session_token = SessionToken, +      map_id = MapID +   }. + +-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 generate_reply(query_state()) -> binary(). +generate_reply (QueryState) -> +   Map = QueryState#query_state.map, + +   SetMap = map_set_map:generate(Map), +   Output = jiffy:encode(SetMap), + +   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). + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%% EXPORTED FUNCTIONS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +out(A) -> +   { +      content, +      "application/json; charset=UTF-8", +      handle(A#arg.clidata) +   }. diff --git a/src/shared/shr_security.erl b/src/shared/shr_security.erl new file mode 100644 index 0000000..724b241 --- /dev/null +++ b/src/shared/shr_security.erl @@ -0,0 +1,33 @@ +-module(shr_security). + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%% TYPES %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%% EXPORTS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +-export +( +   [ +      assert_identity/2, +      lock_queries/1, +      unlock_queries/1 +   ] +). + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%% LOCAL FUNCTIONS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%% EXPORTED FUNCTIONS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +-spec assert_identity (any(), any()) -> 'unimplemented'. +assert_identity (_PlayerID, _SessionToken) -> unimplemented. + +-spec lock_queries (any()) -> 'unimplemented'. +lock_queries (_PlayerID) -> unimplemented. + +-spec unlock_queries (any()) -> 'unimplemented'. +unlock_queries (_PlayerID) -> unimplemented. | 


