Mercurial > wow > askmrrobot
comparison Export.lua @ 57:01b63b8ed811 v21
total rewrite to version 21
author | yellowfive |
---|---|
date | Fri, 05 Jun 2015 11:05:15 -0700 |
parents | |
children | 69db1c3025ac |
comparison
equal
deleted
inserted
replaced
56:75431c084aa0 | 57:01b63b8ed811 |
---|---|
1 local Amr = LibStub("AceAddon-3.0"):GetAddon("AskMrRobot") | |
2 local L = LibStub("AceLocale-3.0"):GetLocale("AskMrRobot", true) | |
3 local AceGUI = LibStub("AceGUI-3.0") | |
4 | |
5 local _lastExport = nil | |
6 local _txt = nil | |
7 | |
8 local function createLabel(container, text, width) | |
9 local lbl = AceGUI:Create("AmrUiLabel") | |
10 lbl:SetWidth(width or 800) | |
11 lbl:SetText(text) | |
12 lbl:SetFont(Amr.CreateFont("Regular", 14, Amr.Colors.Text)) | |
13 container:AddChild(lbl) | |
14 return lbl | |
15 end | |
16 | |
17 local function onSplashClose() | |
18 Amr:HideCover() | |
19 Amr.db.char.FirstUse = false | |
20 end | |
21 | |
22 -- render a splash screen with first-time help | |
23 local function renderSplash(container) | |
24 local panel = Amr:RenderCoverChrome(container, 700, 450) | |
25 | |
26 local lbl = createLabel(panel, L.ExportSplashTitle, 650) | |
27 lbl:SetJustifyH("CENTER") | |
28 lbl:SetFont(Amr.CreateFont("Bold", 24, Amr.Colors.TextHeaderActive)) | |
29 lbl:SetPoint("TOP", panel.content, "TOP", 0, -10) | |
30 | |
31 local lbl2 = createLabel(panel, L.ExportSplashSubtitle, 650) | |
32 lbl2:SetJustifyH("CENTER") | |
33 lbl2:SetFont(Amr.CreateFont("Bold", 18, Amr.Colors.TextTan)) | |
34 lbl2:SetPoint("TOP", lbl.frame, "BOTTOM", 0, -20) | |
35 | |
36 lbl = createLabel(panel, L.ExportSplash1, 650) | |
37 lbl:SetFont(Amr.CreateFont("Regular", 14, Amr.Colors.Text)) | |
38 lbl:SetPoint("TOPLEFT", lbl2.frame, "BOTTOMLEFT", 0, -70) | |
39 | |
40 lbl2 = createLabel(panel, L.ExportSplash2, 650) | |
41 lbl2:SetFont(Amr.CreateFont("Regular", 14, Amr.Colors.Text)) | |
42 lbl2:SetPoint("TOPLEFT", lbl.frame, "BOTTOMLEFT", 0, -15) | |
43 | |
44 lbl = createLabel(panel, L.ExportSplash3, 650) | |
45 lbl:SetFont(Amr.CreateFont("Regular", 14, Amr.Colors.Text)) | |
46 lbl:SetPoint("TOPLEFT", lbl2.frame, "BOTTOMLEFT", 0, -15) | |
47 | |
48 local btn = AceGUI:Create("AmrUiButton") | |
49 btn:SetText(L.ExportSplashClose) | |
50 btn:SetBackgroundColor(Amr.Colors.Green) | |
51 btn:SetFont(Amr.CreateFont("Bold", 16, Amr.Colors.White)) | |
52 btn:SetWidth(120) | |
53 btn:SetHeight(28) | |
54 btn:SetPoint("BOTTOM", panel.content, "BOTTOM", 0, 20) | |
55 btn:SetCallback("OnClick", onSplashClose) | |
56 panel:AddChild(btn) | |
57 end | |
58 | |
59 -- renders the main UI for the Export tab | |
60 function Amr:RenderTabExport(container) | |
61 | |
62 local lbl = createLabel(container, L.ExportTitle) | |
63 lbl:SetFont(Amr.CreateFont("Bold", 24, Amr.Colors.TextHeaderActive)) | |
64 lbl:SetPoint("TOPLEFT", container.content, "TOPLEFT", 0, -40) | |
65 | |
66 local lbl2 = createLabel(container, L.ExportHelp1) | |
67 lbl2:SetPoint("TOPLEFT", lbl.frame, "BOTTOMLEFT", 0, -10) | |
68 | |
69 lbl = createLabel(container, L.ExportHelp2) | |
70 lbl:SetPoint("TOPLEFT", lbl2.frame, "BOTTOMLEFT", 0, -10) | |
71 | |
72 lbl2 = createLabel(container, L.ExportHelp3) | |
73 lbl2:SetPoint("TOPLEFT", lbl.frame, "BOTTOMLEFT", 0, -10) | |
74 | |
75 lbl = createLabel(container, L.ExportHelp4) | |
76 lbl:SetPoint("TOPLEFT", lbl2.frame, "BOTTOMLEFT", 0, -10) | |
77 | |
78 _txt = AceGUI:Create("AmrUiTextarea") | |
79 _txt:SetWidth(800) | |
80 _txt:SetHeight(300) | |
81 _txt:SetPoint("TOP", lbl.frame, "BOTTOM", 0, -20) | |
82 _txt:SetFont(Amr.CreateFont("Regular", 12, Amr.Colors.Text)) | |
83 container:AddChild(_txt) | |
84 | |
85 local data = self:ExportCharacter() | |
86 local txt = Amr.Serializer:SerializePlayerData(data, true) | |
87 _txt:SetText(txt) | |
88 _txt:SetFocus(true) | |
89 | |
90 -- update shopping list data | |
91 Amr:UpdateShoppingData(data) | |
92 | |
93 -- show help splash if first time a user is using this | |
94 if Amr.db.char.FirstUse then | |
95 Amr:ShowCover(renderSplash) | |
96 AceGUI:ClearFocus() | |
97 end | |
98 end | |
99 | |
100 function Amr:ReleaseTabExport() | |
101 end | |
102 | |
103 function Amr:GetExportText() | |
104 return _txt:GetText() | |
105 end | |
106 | |
107 | |
108 -- use some local variables to deal with the fact that a user can close the bank before a scan completes | |
109 local _lastBankBagId = nil | |
110 local _lastBankSlotId = nil | |
111 | |
112 local function scanBag(bagId, isBank, bagTable, bagItemsWithCount) | |
113 local numSlots = GetContainerNumSlots(bagId) | |
114 for slotId = 1, numSlots do | |
115 local _, itemCount, _, _, _, _, itemLink = GetContainerItemInfo(bagId, slotId) | |
116 if itemLink ~= nil then | |
117 local itemData = Amr.Serializer.ParseItemLink(itemLink) | |
118 if itemData ~= nil then | |
119 -- only add equippable items to bag data | |
120 if IsEquippableItem(itemLink) or Amr.SetTokenIds[itemData.id] then | |
121 if isBank then | |
122 _lastBankBagId = bagId | |
123 _lastBankSlotId = slotId | |
124 end | |
125 | |
126 table.insert(bagTable, itemLink) | |
127 end | |
128 | |
129 -- all items and counts, used for e.g. shopping list and reagents, etc. | |
130 if bagItemsWithCount then | |
131 if bagItemsWithCount[itemData.id] then | |
132 bagItemsWithCount[itemData.id] = bagItemsWithCount[itemData.id] + itemCount | |
133 else | |
134 bagItemsWithCount[itemData.id] = itemCount | |
135 end | |
136 end | |
137 end | |
138 end | |
139 end | |
140 end | |
141 | |
142 -- get the player's current gear and save it, also returns the data from GetPlayerData for efficiency | |
143 local function getEquipped() | |
144 local data = Amr.Serializer:GetPlayerData(Amr.db.char.SubSpecs) | |
145 local spec = GetActiveSpecGroup() | |
146 | |
147 Amr.db.char.Equipped[spec] = data.Equipped[spec] | |
148 Amr.db.char.SubSpecs[spec] = data.SubSpecs[spec] | |
149 | |
150 return data | |
151 end | |
152 | |
153 local function scanBags() | |
154 | |
155 local bagItems = {} | |
156 local itemsAndCounts = {} | |
157 | |
158 scanBag(BACKPACK_CONTAINER, false, bagItems, itemsAndCounts) -- backpack | |
159 for bagId = 1, NUM_BAG_SLOTS do | |
160 scanBag(bagId, false, bagItems, itemsAndCounts) | |
161 end | |
162 | |
163 Amr.db.char.BagItems = bagItems | |
164 Amr.db.char.BagItemsAndCounts = itemsAndCounts | |
165 end | |
166 | |
167 -- scan the player's bank and save the contents, must be at the bank | |
168 local function scanBank() | |
169 | |
170 local bankItems = {} | |
171 local itemsAndCounts = {} | |
172 | |
173 scanBag(BANK_CONTAINER, true, bankItems, itemsAndCounts) | |
174 scanBag(REAGENTBANK_CONTAINER, true, bankItems, itemsAndCounts) | |
175 for bagId = NUM_BAG_SLOTS + 1, NUM_BAG_SLOTS + NUM_BANKBAGSLOTS do | |
176 scanBag(bagId, true, bankItems, itemsAndCounts) | |
177 end | |
178 | |
179 -- see if the scan completed before the window closed, otherwise we don't overwrite with partial data | |
180 if _lastBankBagId ~= nil then | |
181 local itemLink = GetContainerItemLink(_lastBankBagId, _lastBankSlotId) | |
182 if itemLink ~= nil then --still open | |
183 Amr.db.char.BankItems = bankItems | |
184 Amr.db.char.BankItemsAndCounts = itemsAndCounts | |
185 end | |
186 end | |
187 | |
188 end | |
189 | |
190 -- scan the player's void storage and save the contents, must be at void storage | |
191 local function scanVoid() | |
192 | |
193 if IsVoidStorageReady() then | |
194 local voidItems = {} | |
195 local VOID_STORAGE_MAX = 80 | |
196 local VOID_STORAGE_PAGES = 2 | |
197 | |
198 for page = 1,VOID_STORAGE_PAGES do | |
199 for i = 1,VOID_STORAGE_MAX do | |
200 local itemId = GetVoidItemInfo(page, i) | |
201 if itemId then | |
202 local itemLink = GetVoidItemHyperlinkString(((page - 1) * VOID_STORAGE_MAX) + i); | |
203 if itemLink then | |
204 tinsert(voidItems, itemLink) | |
205 end | |
206 end | |
207 end | |
208 end | |
209 | |
210 Amr.db.char.VoidItems = voidItems | |
211 end | |
212 | |
213 end | |
214 | |
215 local function getRepStanding(factionId) | |
216 local name, description, standingId, _ = GetFactionInfoByID(factionId) | |
217 return standingId - 1; -- our rep enum correspond to what the armory returns, are 1 less than what the game returns | |
218 end | |
219 | |
220 local function getReputations() | |
221 local reps = {} | |
222 | |
223 local repList = {1375,1376,1270,1269,1341,1337,1387,1388,1435} | |
224 for i, repId in pairs(repList) do | |
225 local standing = getRepStanding(repId) | |
226 if standing >= 0 then | |
227 reps[repId] = standing | |
228 end | |
229 end | |
230 | |
231 return reps | |
232 end | |
233 | |
234 -- Returns a data object containing all information about the current player needed for an export: | |
235 -- gear, spec, reputations, bag, bank, and void storage items. | |
236 function Amr:ExportCharacter() | |
237 | |
238 local data = getEquipped() | |
239 scanBags() | |
240 | |
241 -- get extra data that is not necessary for the base serializer, but that we add here for completeness | |
242 data.Equipped = Amr.db.char.Equipped | |
243 data.Reputations = getReputations() | |
244 data.BagItems = Amr.db.char.BagItems | |
245 data.BankItems = Amr.db.char.BankItems | |
246 data.VoidItems = Amr.db.char.VoidItems | |
247 | |
248 return data | |
249 end | |
250 | |
251 function Amr:InitializeExport() | |
252 Amr:AddEventHandler("UNIT_INVENTORY_CHANGED", function(unitID) | |
253 if unitID and unitID ~= "player" then return end | |
254 getEquipped() | |
255 end) | |
256 end | |
257 | |
258 Amr:AddEventHandler("BANKFRAME_OPENED", scanBank) | |
259 Amr:AddEventHandler("PLAYERBANKSLOTS_CHANGED", scanBank) | |
260 | |
261 Amr:AddEventHandler("VOID_STORAGE_OPEN", scanVoid) | |
262 Amr:AddEventHandler("VOID_STORAGE_CONTENTS_UPDATE", scanVoid) | |
263 Amr:AddEventHandler("VOID_STORAGE_DEPOSIT_UPDATE", scanVoid) | |
264 Amr:AddEventHandler("VOID_STORAGE_UPDATE", scanVoid) |