summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/battlemap.erl24
-rw-r--r--src/battlemap_instance.erl33
-rw-r--r--src/character_instance.erl20
-rw-r--r--src/database_shim.erl13
-rw-r--r--www/handler/battlemap/character_turn.yaws93
-rw-r--r--www/handler/battlemap/load_state.yaws8
6 files changed, 157 insertions, 34 deletions
diff --git a/src/battlemap.erl b/src/battlemap.erl
new file mode 100644
index 0000000..209a4ab
--- /dev/null
+++ b/src/battlemap.erl
@@ -0,0 +1,24 @@
+-module(battlemap).
+-export
+(
+ [
+ cross/4
+ ]
+).
+
+-include("timed_cache_data.hrl").
+
+calc_new_loc (X, Y, [], _Points, _Map, _OtherChars) ->
+ {X, Y};
+calc_new_loc (X, Y, [Step|Path], Points, Map, OtherChars) ->
+ case Step of
+ <<"U">> -> calc_new_loc(X, (Y - 1), Path, Points, Map, OtherChars);
+ <<"D">> -> calc_new_loc(X, (Y + 1), Path, Points, Map, OtherChars);
+ <<"L">> -> calc_new_loc((X - 1), Y, Path, Points, Map, OtherChars);
+ <<"R">> -> calc_new_loc((X + 1), Y, Path, Points, Map, OtherChars);
+ _ -> calc_new_loc(X, Y, Path, Points, Map, OtherChars)
+ end.
+
+cross (Battlemap, CharInst, Path, OtherChars) ->
+ {X, Y} = character_instance:get_location(CharInst),
+ {ok, calc_new_loc(X, Y, Path, 99, Battlemap, OtherChars)}.
diff --git a/src/battlemap_instance.erl b/src/battlemap_instance.erl
new file mode 100644
index 0000000..ef573eb
--- /dev/null
+++ b/src/battlemap_instance.erl
@@ -0,0 +1,33 @@
+-module(battlemap_instance).
+-export
+(
+ [
+ get_char_instances/1,
+ get_char_instance/2,
+ set_char_instance/3
+ ]
+).
+
+-include("timed_cache_data.hrl").
+
+get_char_instances (BattlemapInstance) ->
+ lists:map
+ (
+ fun ({_K, V}) -> V end,
+ dict:to_list(BattlemapInstance#battlemap_instance.chars)
+ ).
+
+get_char_instance (BattlemapInstance, CharInstID) ->
+ {ok, dict:fetch(CharInstID, BattlemapInstance#battlemap_instance.chars)}.
+
+set_char_instance (BattlemapInstance, CharInstID, CharInst) ->
+ BattlemapInstance#battlemap_instance
+ {
+ chars =
+ dict:store
+ (
+ CharInstID,
+ CharInst,
+ BattlemapInstance#battlemap_instance.chars
+ )
+ }.
diff --git a/src/character_instance.erl b/src/character_instance.erl
new file mode 100644
index 0000000..d8d7455
--- /dev/null
+++ b/src/character_instance.erl
@@ -0,0 +1,20 @@
+-module(character_instance).
+-export
+(
+ [
+ set_location/3,
+ get_location/1
+ ]
+).
+
+-include("timed_cache_data.hrl").
+
+set_location (CharInst, X, Y) ->
+ CharInst#character_instance
+ {
+ x = X,
+ y = Y
+ }.
+
+get_location (CharInst) ->
+ {CharInst#character_instance.x, CharInst#character_instance.y}.
diff --git a/src/database_shim.erl b/src/database_shim.erl
index 8b6cea4..0a46c00 100644
--- a/src/database_shim.erl
+++ b/src/database_shim.erl
@@ -3,7 +3,8 @@
(
[
generate_db/1,
- fetch/2
+ fetch/2,
+ commit/3
]
).
@@ -84,9 +85,13 @@ generate_db (Heir) ->
BattlemapInstance
).
-fetch (DB, Object_ID) ->
- io:format("~ndb_shim lookup: ~p.~n", [{DB, Object_ID}]),
- case ets:lookup(db_shim, {DB, Object_ID}) of
+fetch (DB, ObjectID) ->
+ io:format("~ndb_shim lookup: ~p.~n", [{DB, ObjectID}]),
+ case ets:lookup(db_shim, {DB, ObjectID}) of
[{_Key, Value}] -> {ok, Value};
[] -> nothing
end.
+
+commit (DB, ObjectID, Value) ->
+ add_to_db({DB, ObjectID}, Value),
+ timed_cache:invalidate(DB, ObjectID).
diff --git a/www/handler/battlemap/character_turn.yaws b/www/handler/battlemap/character_turn.yaws
index ffcf0f9..40540f6 100644
--- a/www/handler/battlemap/character_turn.yaws
+++ b/www/handler/battlemap/character_turn.yaws
@@ -1,34 +1,69 @@
<erl>
+-record
+(
+ input,
+ {
+ session_token,
+ player_id,
+ battlemap_id,
+ instance_id,
+ char_id,
+ path,
+ target_id
+ }
+).
+
+-include("/tmp/timed_cache_data.hrl").
+
+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),
+ path = maps:get(<<"path">>, JSONReqMap),
+ target_id = maps:get(<<"target_id">>, JSONReqMap)
+ }.
+
handle (Req) ->
- io:format("~nReceived~p...", [Req]),
- JSON_Req_Map = jiffy:decode(Req, [return_maps]),
- UserToken = maps:get(<<"user_token">>, JSON_Req_Map),
- io:format("~nCharacter Turn for ~p...", [UserToken]),
-%% ok = users_manager:ping(UserToken),
- jiffy:encode
- (
- {
- [
- {<<"types">>, [<<"STATUS">>]},
- {
- <<"data">>,
- [
- jiffy:encode
- (
- {
- [
- {
- <<"status">>,
- <<"OK">>
- }
- ]
- }
- )
- ]
- }
- ]
- }
- ).
+ %%%% Parse
+ Input = parse_input(Req),
+ %%%% Fetch
+ Battlemap = timed_cache:fetch(battlemap_db, Input#input.battlemap_id),
+ BattlemapInstance =
+ timed_cache:fetch
+ (
+ battlemap_instance_db,
+ <<"0">>
+ ),
+ {ok, CharInst} =
+ battlemap_instance:get_char_instance
+ (
+ BattlemapInstance,
+ Input#input.char_id
+ ),
+ %%%% Calc
+ {ok, {X, Y}} =
+ battlemap:cross
+ (
+ Battlemap,
+ CharInst,
+ Input#input.path,
+ battlemap_instance:get_char_instances(BattlemapInstance)
+ ),
+ NewBattlemapInstance =
+ battlemap_instance:set_char_instance
+ (
+ BattlemapInstance,
+ Input#input.char_id,
+ character_instance:set_location(CharInst, X, Y)
+ ),
+ %%%% Commit
+ database_shim:commit(battlemap_instance_db, <<"0">>, NewBattlemapInstance),
+ %%%% Reply
+ jiffy:encode([[<<"okay">>]]).
out(A) ->
{
diff --git a/www/handler/battlemap/load_state.yaws b/www/handler/battlemap/load_state.yaws
index b5a7fc7..d68c6a4 100644
--- a/www/handler/battlemap/load_state.yaws
+++ b/www/handler/battlemap/load_state.yaws
@@ -1,5 +1,5 @@
<erl>
--record(input, {player_id, battlemap_id, instance_id}).
+-record(input, {session_token, player_id, battlemap_id, instance_id}).
-include("/tmp/timed_cache_data.hrl").
@@ -7,6 +7,7 @@ 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)
@@ -65,7 +66,9 @@ generate_reply (Battlemap, _BattlemapInstance, Characters) ->
).
handle (Req) ->
+ %%%% Parse
Input = parse_input(Req),
+ %%%% Fetch
Battlemap = timed_cache:fetch(battlemap_db, Input#input.battlemap_id),
BattlemapInstance =
timed_cache:fetch
@@ -85,6 +88,9 @@ handle (Req) ->
end,
dict:to_list(BattlemapInstance#battlemap_instance.chars)
),
+ %%%% Calc
+ %%%% Commit
+ %%%% Reply
generate_reply(Battlemap, BattlemapInstance, Characters).
out(A) ->