1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
|
-module(database_shim).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% TYPES %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% EXPORTS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-export
(
[
generate_db/1,
fetch/2,
commit/4
]
).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% LOCAL FUNCTIONS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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_random_characters
(
0,
0,
_CharactersPerPlayer,
_TotalCharacterCount,
Result
) ->
Result;
generate_random_characters
(
MaxPlayerID,
0,
CharactersPerPlayer,
TotalCharacterCount,
Result
) ->
generate_random_characters
(
(MaxPlayerID - 1),
CharactersPerPlayer,
CharactersPerPlayer,
TotalCharacterCount,
Result
);
generate_random_characters
(
MaxPlayerID,
PlayerCharacterCount,
CharactersPerPlayer,
TotalCharacterCount,
Result
) ->
NewCharacter =
character:random
(
TotalCharacterCount,
list_to_binary(integer_to_list(MaxPlayerID))
),
generate_random_characters
(
MaxPlayerID,
(PlayerCharacterCount - 1),
CharactersPerPlayer,
(TotalCharacterCount + 1),
[NewCharacter|Result]
).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% EXPORTED FUNCTIONS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
generate_db (Heir) ->
Pid = self(),
spawn(fun () -> create_db(Heir), Pid ! ok, receive ok -> ok end end),
receive
ok -> ok
end,
BattlemapWidth = roll:between(16, 64),
BattlemapHeight = roll:between(16, 64),
Battlemap = battlemap:random(0, BattlemapWidth, BattlemapHeight),
Characters = generate_random_characters(1, 7, 8, 0, []),
PlayersAsList = [<<"0">>, <<"1">>],
BattlemapInstance =
battlemap_instance:random
(
<<"0">>,
PlayersAsList,
Battlemap,
Characters
),
add_to_db({battlemap_instance_db, <<"0">>}, 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, _Owner, ObjectID, Value) ->
add_to_db({DB, ObjectID}, Value).
|