summaryrefslogtreecommitdiff |
diff options
Diffstat (limited to 'src/database_shim.erl')
-rw-r--r-- | src/database_shim.erl | 104 |
1 files changed, 84 insertions, 20 deletions
diff --git a/src/database_shim.erl b/src/database_shim.erl index 3e88c34..4110918 100644 --- a/src/database_shim.erl +++ b/src/database_shim.erl @@ -1,26 +1,90 @@ -module(database_shim). --export([fetch/2]). +-export +( + [ + generate_db/1, + fetch/2 + ] +). -include("timed_cache_data.hrl"). -fetch(battlemap_db, Object_ID) -> - Width = (rand:uniform(54) + 10), - Height = (rand:uniform(54) + 10), - io:format +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%% LOCAL %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +create_db (_Heir) -> + ets:new ( - "~nGenerating new Battlemap ~p of size (~p, ~p)...~n", - [Object_ID, Width, Height] + db_shim, + [ + set, + public, + named_table, + {keypos, 1}, + {read_concurrency, true} + ] + ). + +add_to_db (ID, Val) -> + ets:store(db_shim, {ID, Val}). + +generate_char_instances (Battlemap, Characters) -> + lists:map + ( + fun (Char) -> + { + Char#character.id, + #character_instance + { + x = rand:uniform(Battlemap#battlemap.width - 1), + y = rand:uniform(Battlemap#battlemap.height - 1), + team = (rand:uniform(2) - 1) + } + } + end, + Characters + ). + +generate_map_instance (CharInts) -> + #battlemap_instance + { + id = <<"0">>, + chars = dict:from_list(CharInts), + curr_player = <<"0">>, + rem_chars = [], + last_turn = [] + }. + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%% EXPORTED %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +generate_db (Heir) -> + Pid = self(), + spawn(fun () -> create_db(Heir), Pid ! ok, receive ok -> ok end end), + receive + ok -> ok + end, + Battlemap = battlemap_shim:generate(), + Characters = character_shim:generate(rand:uniform(14) + 2), + CharacterInsts = generate_char_instances(Battlemap, Characters), + BattlemapInstance = generate_map_instance(CharacterInsts), + add_to_db({battlemap_db, Battlemap#battlemap.id}, Battlemap), + lists:map + ( + fun (Char) -> + add_to_db({character_sb, Char#character.id}, Char) + end, + Characters ), - Result = - #battlemap { - id = Object_ID, - width = Width, - height = Height, - content = battlemap_shim:generate(Width, Height) - }, - {ok, - { - character_shim:generate(Width, Height) - } - }; -fetch(battlemap_db, Object_ID) -> + add_to_db + ( + {battlemap_instance_db, BattlemapInstance#battlemap_instance.id}, + BattlemapInstance + ). + +fetch (DB, Object_ID) -> + ets:first(db_shim), %% It appears the db does not exist... + case ets:lookup(db_shim, {DB, Object_ID}) of + [{_Key, Value}] -> {ok, Value}; + [] -> nothing + end. |