vanilla-wow-addons – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 --[[
2 Bagnon Database
3 Functions for storing and accessing player inventory data
4 This is intended to be completely seperate from Bagnon_Forever
5  
6 BagnonDB has this format, which was adapted from KC_Items
7 Realm
8 Character
9 BagID = size,count,[link]
10 ItemSlot = link,[count]
11 Money
12 --]]
13  
14 --local globals
15 local currentPlayer = UnitName("player"); --the name of the current player that's logged on
16 local currentRealm = GetRealmName(); --what currentRealm we're on
17 local atBank; --is the current player at the bank or not
18  
19 function BagnonDB_OnEvent()
20 if(event == "BAG_UPDATE") then
21 BagnonDB_SaveBagData(arg1);
22 elseif(event == "PLAYERBANKSLOTS_CHANGED") then
23 BagnonDB_SaveBagData(-1);
24 elseif(event == "BANKFRAME_CLOSED") then
25 atBank = nil;
26 BagnonDB_SaveBankData();
27 elseif(event == "BANKFRAME_OPENED") then
28 atBank = 1;
29 BagnonDB_SaveBankData();
30 elseif(event == "PLAYER_MONEY") then
31 BagnonDB_SavePlayerMoney();
32 elseif(event == "PLAYER_LOGIN") then
33 BagnonDB_LoadVariables();
34 BagnonDB_SavePlayerMoney();
35 end
36 end
37  
38 function BagnonDB_LoadVariables()
39 --[[
40 BagnonDB settings are set to default under the following conditions
41 No saved variables (duh)
42 Versions that did not know about the wowVersion
43 Right after any WoW Patch
44 --]]
45 if(not (BagnonDB and BagnonDB.wowVersion and BagnonDB.wowVersion == GetBuildInfo()) ) then
46 BagnonDB = {
47 version = BAGNON_FOREVER_VERSION,
48 wowVersion = GetBuildInfo();
49 };
50 end
51  
52 if(not BagnonDB[currentRealm]) then
53 BagnonDB[currentRealm] = {};
54 end
55  
56 if(not BagnonDB[currentRealm][UnitName("player")]) then
57 BagnonDB[currentRealm][UnitName("player")] = {};
58 end
59  
60 if(BagnonDB.version ~= BAGNON_FOREVER_VERSION) then
61 BagnonDB_UpdateVersion();
62 end
63 end
64  
65 function BagnonDB_UpdateVersion()
66 BagnonDB.version = BAGNON_FOREVER_VERSION;
67  
68 DEFAULT_CHAT_FRAME:AddMessage(BAGNON_FOREVER_UPDATED, 1, 1, 0);
69 end
70  
71 --[[ Storage Functions ]]--
72  
73 --save all inventory data about the current player
74 function BagnonDB_SaveAllData()
75 local i;
76 --you know, this should probably be a constant
77 for i = -2, 10, 1 do
78 BagnonDB_SaveBagData(i);
79 end
80 BagnonDB_SavePlayerMoney();
81 end
82  
83 --save all bank data about the current player
84 function BagnonDB_SaveBankData()
85 BagnonDB_SaveBagData(-1);
86 local bagID;
87 for bagID = 5, 10, 1 do
88 BagnonDB_SaveBagData(bagID);
89 end
90 end
91  
92 --saves all the data about the current player's bag
93 function BagnonDB_SaveBagData(bagID)
94 --don't save bank data unless you're at the bank
95 if( Bagnon_IsBankBag(bagID) and not atBank ) then
96 return;
97 end
98  
99 local size;
100 if(bagID == KEYRING_CONTAINER) then
101 size = GetKeyRingSize();
102 else
103 size = GetContainerNumSlots(bagID);
104 end
105  
106 if(size > 0) then
107 local link, count;
108  
109 if(bagID > 0) then
110 link = BagnonDB_ToShortLink( GetInventoryItemLink("player", ContainerIDToInventoryID(bagID) ) );
111 end
112  
113 count = GetInventoryItemCount("player", ContainerIDToInventoryID(bagID));
114  
115 --save bag size
116 BagnonDB[currentRealm][currentPlayer][bagID] = {};
117 BagnonDB[currentRealm][currentPlayer][bagID].s = size .. "," .. count .. ",";
118  
119 if(link) then
120 BagnonDB[currentRealm][currentPlayer][bagID].s = BagnonDB[currentRealm][currentPlayer][bagID].s .. link;
121 end
122  
123 --save all item info
124 for index = 1, size, 1 do
125 BagnonDB_SaveItemData(bagID, index);
126 end
127 else
128 BagnonDB[currentRealm][currentPlayer][bagID] = nil;
129 end
130 end
131  
132 function BagnonDB_SaveItemData(bagID, itemSlot)
133 local texture, count = GetContainerItemInfo(bagID, itemSlot);
134 local data;
135  
136 if(texture) then
137 data = BagnonDB_ToShortLink( GetContainerItemLink(bagID, itemSlot) ) .. ",";
138 if(count > 1) then
139 data = data .. count;
140 end
141 end
142  
143 BagnonDB[currentRealm][currentPlayer][bagID][itemSlot] = data;
144 end
145  
146 function BagnonDB_SavePlayerMoney()
147 BagnonDB[currentRealm][currentPlayer].g = GetMoney();
148 end
149  
150 --[[ Access Functions ]]--
151  
152 --returns <player>'s current money
153 function BagnonDB_GetMoney(player)
154 return BagnonDB[currentRealm][player].g;
155 end
156  
157 --[[
158 returns the size, link, and count of the current bag
159 size is how many items the bag can hold
160 link is the item link of the bag, if available
161 count is how many items are in the bag, this is used by ammo and soul shard bags
162 --]]
163 function BagnonDB_GetBagData(player, bagID)
164 local data = BagnonDB[currentRealm][player][bagID];
165 if(data) then
166 local _, _, size, count, link = string.find(data.s, "([%w_:]+),([%w_:]+),([%w_:]*)");
167  
168 if(link and link ~="") then
169 return size, "item:" .. link, tonumber(count);
170 elseif(size) then
171 return size, nil, tonumber(count);
172 end
173 end
174 return 0;
175 end
176  
177 --returns the item link, (in the form of item:www:xxx:yyy:zzz), texture (full path) , and count of the item in the given slot
178 function BagnonDB_GetItemDataFromSlot(player, bagID, slot)
179 if( not( BagnonDB[currentRealm] and BagnonDB[currentRealm][player] and BagnonDB[currentRealm][player][bagID] )) then
180 return nil;
181 end
182  
183 local itemData = BagnonDB[currentRealm][player][bagID][slot];
184  
185 if(itemData) then
186 local _, _, shortLink, count = string.find(itemData, "([%w_:]+),([%w_:]*)");
187  
188 shortLink = "item:" .. shortLink;
189  
190 if(count == "") then
191 count = 1;
192 else
193 count = tonumber(count);
194 end
195  
196 local _, _, _, _, _, _, _, _, texture = GetItemInfo(shortLink);
197  
198 return shortLink, texture, count;
199 end
200  
201 return nil;
202 end
203  
204 --returns the item link, (in the form of item:www:xxx:yyy:zzz), texture (full path) , and count of the given item
205 function BagnonDB_GetItemData(item)
206 local itemData = BagnonDB[currentRealm][item:GetParent():GetParent().player][item:GetParent():GetID()][item:GetID()];
207  
208 if(itemData) then
209 local _, _, shortLink, count = string.find(itemData, "([%w_:]+),([%w_:]*)");
210  
211 if(shortLink ~= "") then
212 shortLink = "item:" .. shortLink;
213 else
214 shortLink = nil;
215 end
216  
217 if(count == "") then
218 count = 1;
219 else
220 count = tonumber(count);
221 end
222  
223 local _, _, _, _, _, _, _, _, texture = GetItemInfo(shortLink);
224  
225 return shortLink, texture, count;
226 end
227  
228 return nil;
229 end
230  
231 --return the full hyperlink of an item. This is for linking in chat
232 function BagnonDB_GetFullItemLink(item)
233 local link = ( BagnonDB_GetItemData(item) );
234  
235 if(link) then
236 local name, ilink, quality = GetItemInfo( link );
237 local r,g,b,hex = GetItemQualityColor( quality );
238  
239 return hex .. "|H".. link .. "|h[" .. name .. "]|h|r";
240 end
241 end
242  
243 --[[ Removal Functions ]]--
244  
245 --removes all saved data about the given player
246 function BagnonDB_RemovePlayer(player, realm)
247 if(BagnonDB[realm]) then
248 BagnonDB[realm][player] = nil;
249 end
250 end
251  
252 --[[ Conversion Functions ]]--
253  
254 --takes a full item hyperlink and returns the www:xxx:yyy:zzz form
255 function BagnonDB_ToShortLink(fullLink)
256 if(fullLink) then
257 local _, _, w, x, y, z = string.find(fullLink, "(%d+):(%d+):(%d+):(%d+)") ;
258 return w .. ":" .. x .. ":" .. y .. ":" .. z;
259 end
260 return nil;
261 end
262  
263 --takes an item link and returns its ID
264 function BagnonDB_ToID(link)
265 if(link) then
266 local _, _, id = string.find(link, "item:(%d+)");
267 return id;
268 end
269 end
270  
271 --[[ Create the Database Event Handler ]]--
272  
273 CreateFrame("Frame", "Bagnon_DB");
274 Bagnon_DB:Hide();
275  
276 Bagnon_DB:RegisterEvent("BAG_UPDATE");
277 Bagnon_DB:RegisterEvent("PLAYER_LOGIN");
278 Bagnon_DB:RegisterEvent("BANKFRAME_CLOSED");
279 Bagnon_DB:RegisterEvent("BANKFRAME_OPENED");
280 Bagnon_DB:RegisterEvent("PLAYERBANKSLOTS_CHANGED");
281 Bagnon_DB:RegisterEvent("PLAYER_MONEY");
282  
283 Bagnon_DB:SetScript("OnEvent", BagnonDB_OnEvent);