containertables.inc.lua

changeset 0
cc66ad6b0d75
equal deleted inserted replaced
-1:000000000000 0:cc66ad6b0d75
1
2 --[[ I created this class for an easy way of managing the objects inside a container in the MOO. ]]
3
4 containertable = { };
5
6 function containertable:add(object)
7 local subt = self[(object and object._properties.name) or ""];
8 if not subt then subt = { };
9 self[(object and object._properties.name) or ""] = subt;
10 -- Also, all object's aliases need to point to subt
11 if object._tags then
12 for i, tag in ipairs(object._tags) do
13 self[tag] = subt;
14 end
15 end
16 end
17 table.insert(subt, object);
18 return object;
19 end
20
21 function containertable:remove(object)
22 local subt = self[(object._properties and object._properties.name) or ""];
23 if not subt then return false; end
24 for n, stored_object in pairs(subt) do
25 if object == stored_object then table.remove(subt, n); return object; end
26 end
27 return false;
28 end
29
30 function containertable:addalias(name, tag)
31 self[tag] = self[name];
32 end
33
34 function containertable:findobject(name)
35 local subt = self[name or ""];
36 if not subt then subt = self[""]; if not subt then return; end end
37 return subt[1];
38 end
39
40 function containertable:getallobjects()
41 all = {};
42 for tag, list in pairs(self) do
43 for n, object in ipairs(list) do
44 all[object] = true;
45 end
46 end
47 return all; -- We return a table, where the objects are the keys, and the values are true
48 end
49
50 function createcontainertable(t)
51 return setmetatable(t or {}, { __index = containertable });
52 end

mercurial