summaryrefslogtreecommitdiff |
diff options
author | nsensfel <SpamShield0@noot-noot.org> | 2017-11-28 17:14:02 +0100 |
---|---|---|
committer | nsensfel <SpamShield0@noot-noot.org> | 2017-11-28 17:14:02 +0100 |
commit | f539b7072c357339328d9bfd54f1f1ed51828586 (patch) | |
tree | b6205dd79c78090831e812aceac177d2a9f35d28 /src/query/load_state.erl | |
parent | 80358376b9300a0d73cb8b62dfa9fdd65240ca66 (diff) |
Trying to tidy up this mess.
Diffstat (limited to 'src/query/load_state.erl')
-rw-r--r-- | src/query/load_state.erl | 123 |
1 files changed, 123 insertions, 0 deletions
diff --git a/src/query/load_state.erl b/src/query/load_state.erl new file mode 100644 index 0000000..f2818bd --- /dev/null +++ b/src/query/load_state.erl @@ -0,0 +1,123 @@ +-module(load_state). +-record(input, {session_token, player_id, battlemap_id, instance_id}). +-export([out/1]). + +parse_input (Req) -> + JSONReqMap = jiffy:decode(Req, [return_maps]), + #input + { + session_token = maps:get(<<"session_token">>, JSONReqMap), + player_id = maps:get(<<"player_id">>, JSONReqMap), + battlemap_id = maps:get(<<"battlemap_id">>, JSONReqMap), + instance_id = maps:get(<<"instance_id">>, JSONReqMap) + }. + +generate_set_map (Battlemap) -> + jiffy:encode + ( + { + [ + {<<"width">>, battlemap:get_width(Battlemap)}, + {<<"height">>, battlemap:get_height(Battlemap)}, + {<<"content">>, battlemap:list_tiles(Battlemap)} + ] + } + ). + +generate_add_char (Char, CharInstance, BattlemapInstance, PlayerID) -> + {X, Y} = character_instance:get_location(CharInstance), + CharID = character:get_id(Char), + jiffy:encode + ( + { + [ + {<<"id">>, character:get_id(Char)}, + {<<"name">>, character:get_name(Char)}, + {<<"icon">>, character:get_icon(Char)}, + {<<"portrait">>, character:get_portrait(Char)}, + {<<"loc_x">>, X}, + {<<"loc_y">>, Y}, + {<<"team">>, character_instance:get_owner(CharInstance)}, + {<<"mov_pts">>, character:get_movement_points(Char)}, + {<<"atk_rg">>, character:get_attack_range(Char)}, + { + <<"enabled">>, + battlemap_instance:can_play_char_instance + ( + BattlemapInstance, + PlayerID, + CharID + ) + } + ] + } + ). + +generate_reply (Battlemap, BattlemapInstance, Characters, PlayerID) -> + jiffy:encode + ( + [ + [ + <<"set_map">>, + generate_set_map(Battlemap) + ] + | + lists:map + ( + fun ({CharID, CharInstance}) -> + [ + <<"add_char">>, + generate_add_char + ( + CharID, + CharInstance, + BattlemapInstance, + PlayerID + ) + ] + end, + Characters + ) + ] + ). + +handle (Req) -> + %%%% Parse + Input = parse_input(Req), + %%%% Fetch + Battlemap = timed_cache:fetch(battlemap_db, Input#input.battlemap_id), + BattlemapInstance = + timed_cache:fetch + ( + battlemap_instance_db, + <<"0">> +% {Input#input.battlemap_id, Input#input.battlemap_instance_id} + ), + Characters = + lists:map + ( + fun ({CharID, CharInst}) -> + { + timed_cache:fetch(character_db, CharID), + CharInst + } + end, + battlemap_instance:list_characters(BattlemapInstance) + ), + %%%% Calc + %%%% Commit + %%%% Reply + generate_reply + ( + Battlemap, + BattlemapInstance, + Characters, + Input#input.player_id + ). + +out(A) -> + { + content, + "application/json; charset=UTF-8", + handle(A#arg.clidata) + }. |