| 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/io/database_shim.erl | |
| parent | 80358376b9300a0d73cb8b62dfa9fdd65240ca66 (diff) | |
Trying to tidy up this mess.
Diffstat (limited to 'src/io/database_shim.erl')
| -rw-r--r-- | src/io/database_shim.erl | 94 | 
1 files changed, 94 insertions, 0 deletions
| diff --git a/src/io/database_shim.erl b/src/io/database_shim.erl new file mode 100644 index 0000000..243051b --- /dev/null +++ b/src/io/database_shim.erl @@ -0,0 +1,94 @@ +-module(database_shim). +-export +( +   [ +      generate_db/1, +      fetch/2, +      commit/3 +   ] +). + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%% LOCAL %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +create_db (_Heir) -> +   ets:new +   ( +      db_shim, +      [ +         set, +         public, +         named_table, +         {keypos, 1}, +         {read_concurrency, true} +      ] +   ), +   io:format("~ndb_shim ets created.~n"). + +add_to_db (ID, Val) -> +   io:format("~nadd to db_shim: ~p.~n", [{ID, Val}]), +   ets:insert(db_shim, {ID, Val}). + +generate_char_instances (Battlemap, Characters) -> +   lists:map +   ( +      fun (Char) -> +         { +            character:get_id(Char), +            character_instance:new_instance_of +            ( +               Char, +               (rand:uniform(2) - 1), % team, +               { +                  rand:uniform(battlemap:get_width(Battlemap) - 1), % X +                  rand:uniform(battlemap:get_heigth(Battlemap) - 1)  % Y +               } +            ) +         } +      end, +      Characters +   ). + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%% EXPORTED %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +generate_db (Heir) -> +   Pid = self(), +   spawn(fun () -> create_db(Heir), Pid ! ok, receive ok -> ok end end), +   receive +      ok -> ok +   end, +   Players = [<<"0">>, <<"1">>], +   Battlemap = battlemap_shim:generate_random(), +   Characters = character_shim:generate_random(rand:uniform(12) + 4), +   CharacterInsts = generate_char_instances(Battlemap, Characters), +   BattlemapInstance = +      battlemap_instance_shim:generate_random +      ( +         CharacterInsts, +         Players +      ), +   add_to_db({battlemap_db, battlemap:get_id(Battlemap)}, Battlemap), +   lists:map +   ( +      fun (Char) -> +         add_to_db({character_db, character:get_id(Char)}, Char) +      end, +      Characters +   ), +   add_to_db +   ( +      {battlemap_instance_db, battlemap_instance:get_id(BattlemapInstance)}, +      BattlemapInstance +   ). + +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). | 


