comparison LibModuleDBShare-1.0/LibModuleDBShare-1.0.lua @ 27:0739db0c99ac

Created a namespace in member DBs to store group data in.
author Andrew Knoll <andrewtknoll@gmail.com>
date Sat, 16 Mar 2013 22:56:03 -0400
parents 4bc47e7b549d
children 085d93d62782
comparison
equal deleted inserted replaced
26:4bc47e7b549d 27:0739db0c99ac
37 error("Usage: LibModuleDBShare:NewGroup(groupName, groupDescription, initialDB, usesDualSpec): 'groupName' must be a string.", 2); 37 error("Usage: LibModuleDBShare:NewGroup(groupName, groupDescription, initialDB, usesDualSpec): 'groupName' must be a string.", 2);
38 elseif type(groupDescription) ~= "string" then 38 elseif type(groupDescription) ~= "string" then
39 error("Usage: LibModuleDBShare:NewGroup(groupName, groupDescription, initialDB, usesDualSpec): 'groupDescription' must be a string.", 2); 39 error("Usage: LibModuleDBShare:NewGroup(groupName, groupDescription, initialDB, usesDualSpec): 'groupDescription' must be a string.", 2);
40 elseif type(LibModuleDBShare.groups[groupName]) ~= "nil" then 40 elseif type(LibModuleDBShare.groups[groupName]) ~= "nil" then
41 error("LibModuleDBShare:NewGroup(groupName, groupDescription, initialDB, usesDualSpec): group '"..groupName.."' already exists.", 2); 41 error("LibModuleDBShare:NewGroup(groupName, groupDescription, initialDB, usesDualSpec): group '"..groupName.."' already exists.", 2);
42 elseif type(initialDB) ~= "table" or not AceDB.db_registry[initial] then 42 elseif type(initialDB) ~= "table" or not AceDB.db_registry[initialDB] then
43 error("LibModuleDBShare:NewGroup(groupName, groupDescription, initialDB, usesDualSpec): 'initalDB must be an AceDB-3.0 database.", 2); 43 error("LibModuleDBShare:NewGroup(groupName, groupDescription, initialDB, usesDualSpec): 'initalDB' must be an AceDB-3.0 database.", 2);
44 end 44 end
45 -- create group 45 -- create group
46 local group = {} 46 local group = {}
47 group.name = groupName; 47 group.name = groupName;
48 group.members = {}; 48 group.members = {};
68 -- add all profiles from initialDB to syncDB 68 -- add all profiles from initialDB to syncDB
69 for i, profile in pairs(initialDB:GetProfiles()) do 69 for i, profile in pairs(initialDB:GetProfiles()) do
70 group.syncDB:SetProfile(profile); 70 group.syncDB:SetProfile(profile);
71 end 71 end
72 group.syncDB:SetProfile(initialDB:GetCurrentProfile()); 72 group.syncDB:SetProfile(initialDB:GetCurrentProfile());
73 group.members[initialDB] = true; 73 group.members[initialDB] = initialDB:GetNamespace(MAJOR, true) or initialDB:RegisterNamespace(MAJOR);
74 if type(initialDB.char.logoutTimestamp) == "number" then 74 if type(group.members[initialDB].char.logoutTimestamp) == "number" then
75 group.profileTimestamp = initialDB.char.logoutTimestamp; 75 group.profileTimestamp = group.members[initialDB].char.logoutTimestamp;
76 else 76 else
77 group.profileTimestamp = 0; 77 group.profileTimestamp = 0;
78 end 78 end
79 -- add methods and callbacks 79 -- add methods and callbacks
80 for k, v in pairs(DBGroup) do 80 for k, v in pairs(DBGroup) do
94 -- @param groupName The name of the DB group to retrieve. 94 -- @param groupName The name of the DB group to retrieve.
95 -- @usage 95 -- @usage
96 -- local myAddonDBGroup = LibStub("LibModuleDBShare-1.0"):GetGroup("MyAddonGroupName") 96 -- local myAddonDBGroup = LibStub("LibModuleDBShare-1.0"):GetGroup("MyAddonGroupName")
97 -- @return the DB group object, or nil if not found 97 -- @return the DB group object, or nil if not found
98 function LibModuleDBShare:GetGroup(groupName) 98 function LibModuleDBShare:GetGroup(groupName)
99 assert(type(groupName) == "string", "Usage: LibModuleDBShare:GetGroup(groupName): 'groupName' must be a string."); 99 if type(groupName) ~= "string" then
100 error("Usage: LibModuleDBShare:GetGroup(groupName): 'groupName' must be a string.", 2);
101 end
100 return LibModuleDBShare.groups[groupName]; 102 return LibModuleDBShare.groups[groupName];
101 end 103 end
102 104
103 --- Adds a database to the group. 105 --- Adds a database to the group.
104 -- @param newDB The database to add. 106 -- @param newDB The database to add.
117 self.squelchCallbacks = true; 119 self.squelchCallbacks = true;
118 for i, profile in pairs(newDB:GetProfiles()) do 120 for i, profile in pairs(newDB:GetProfiles()) do
119 self.syncDB:SetProfile(profile); 121 self.syncDB:SetProfile(profile);
120 end 122 end
121 -- set current profile based on timestamps 123 -- set current profile based on timestamps
122 if type(newDB.char.logoutTimestamp) == "number" and newDB.char.logoutTimestamp > self.profileTimestamp then 124 local namespace = newDB:GetNamespace(MAJOR, true) or newDB:RegisterNamespace(MAJOR);
125 if type(namespace.char.logoutTimestamp) == "number" and namespace.char.logoutTimestamp > self.profileTimestamp then
123 self.squelchCallbacks = false; 126 self.squelchCallbacks = false;
124 self.syncDB:SetProfile(newDB:GetCurrentProfile()); 127 self.syncDB:SetProfile(newDB:GetCurrentProfile());
125 self.profileTimestamp = newDB.character.logoutTimestamp; 128 self.profileTimestamp = namespace.character.logoutTimestamp;
126 else 129 else
127 self.syncDB:SetProfile(syncProfile); 130 self.syncDB:SetProfile(syncProfile);
128 newDB:SetProfile(syncProfile); 131 newDB:SetProfile(syncProfile);
129 self.squelchCallbacks = false; 132 self.squelchCallbacks = false;
130 end 133 end
131 -- add to members list 134 -- add to members list
132 self.members[newDB] = true; 135 self.members[newDB] = namespace;
133 newDB.RegisterCallback(self, "OnDatabaseShutdown", "OnDatabaseShutdown"); 136 newDB.RegisterCallback(self, "OnDatabaseShutdown", "OnDatabaseShutdown");
134 end 137 end
135 138
136 -- callback handlers (new profiles are handled by OnProfileChanged) 139 -- callback handlers (new profiles are handled by OnProfileChanged)
137 140
162 end 165 end
163 166
164 local timestamp = nil; 167 local timestamp = nil;
165 168
166 function DBGroup:OnDatabaseShutdown(callback, db) 169 function DBGroup:OnDatabaseShutdown(callback, db)
167 if not timestamp then -- ensures uniform timestamps so 170 if not timestamp then -- ensure uniform timestamps to minimize
168 timestamp = time(); 171 timestamp = time(); -- calls to SetProfile in NewGroup
169 end 172 end
170 db.char.logoutTimestamp = timestamp; 173 self.members[db].char.logoutTimestamp = timestamp;
171 end 174 end