summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authornsensfel <SpamShield0@noot-noot.org>2017-11-17 17:05:54 +0100
committernsensfel <SpamShield0@noot-noot.org>2017-11-17 17:05:54 +0100
commit55d3257033e7ca3818425e280bdee9aa6f24fbcb (patch)
tree840feab928232bfe20991a8c4cf50f5c0a993676 /src/database_shim.erl
parentf4bd9fdf0e9555837d5c1306fb629372c9a4c0f1 (diff)
Using a ets dict to simulate DBs.
Diffstat (limited to 'src/database_shim.erl')
-rw-r--r--src/database_shim.erl104
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.