Mercurial > wow > askmrrobot
comparison Shopping.lua @ 139:c229c759a125 v65
Update to support multiple setups per spec.
author | yellowfive |
---|---|
date | Mon, 05 Nov 2018 16:06:00 -0800 |
parents | a0894ffebd15 |
children | 44ce007189e0 |
comparison
equal
deleted
inserted
replaced
138:45e467335a1b | 139:c229c759a125 |
---|---|
3 local AceGUI = LibStub("AceGUI-3.0") | 3 local AceGUI = LibStub("AceGUI-3.0") |
4 | 4 |
5 local _frameShop | 5 local _frameShop |
6 local _panelContent | 6 local _panelContent |
7 local _cboPlayers | 7 local _cboPlayers |
8 local _selectedPlayer | 8 local _selectedSetup |
9 | 9 |
10 local _specs = { | 10 local _specs = { |
11 [1] = true, | 11 [1] = true, |
12 [2] = true, | 12 [2] = true, |
13 [3] = true, | 13 [3] = true, |
14 [4] = true, | 14 [4] = true, |
15 } | 15 } |
16 | 16 |
17 local _chk1 | |
18 local _chk2 | |
19 local _chk3 | |
20 local _chk4 | |
21 local _isAhOpen = false | 17 local _isAhOpen = false |
22 | 18 |
23 local function incrementTableItem(tbl, key, inc) | 19 local function incrementTableItem(tbl, key, inc) |
24 tbl[key] = tbl[key] and tbl[key] + inc or inc | 20 tbl[key] = tbl[key] and tbl[key] + inc or inc |
25 end | 21 end |
26 | 22 |
27 local function onShopFrameClose(widget) | 23 local function onShopFrameClose(widget) |
28 AceGUI:Release(widget) | 24 AceGUI:Release(widget) |
29 _frameShop = nil | 25 _frameShop = nil |
30 _cboPlayers = nil | 26 _cboPlayers = nil |
31 _chk1 = nil | |
32 _chk2 = nil | |
33 _chk3 = nil | |
34 _chk4 = nil | |
35 _panelContent = nil | 27 _panelContent = nil |
36 end | 28 end |
37 | 29 |
38 function Amr:HideShopWindow() | 30 function Amr:HideShopWindow() |
39 if not _frameShop then return end | 31 if not _frameShop then return end |
40 _frameShop:Hide() | 32 _frameShop:Hide() |
41 end | 33 end |
42 | 34 |
43 local function onPlayerChange(widget, eventName, value) | 35 local function onPlayerChange(widget, eventName, value) |
44 _selectedPlayer = value | 36 _selectedSetup = value |
45 Amr:RefreshShoppingUi() | 37 Amr:RefreshShoppingUi() |
46 end | 38 end |
47 | 39 |
48 local function onSpecClick(widget) | 40 local function onSpecClick(widget) |
49 local spec = widget:GetUserData("spec") | 41 local spec = widget:GetUserData("spec") |
93 _cboPlayers = AceGUI:Create("AmrUiDropDown") | 85 _cboPlayers = AceGUI:Create("AmrUiDropDown") |
94 _cboPlayers:SetWidth(400) | 86 _cboPlayers:SetWidth(400) |
95 _frameShop:AddChild(_cboPlayers) | 87 _frameShop:AddChild(_cboPlayers) |
96 _cboPlayers:SetPoint("TOPLEFT", _frameShop.content, "TOPLEFT", 0, -30) | 88 _cboPlayers:SetPoint("TOPLEFT", _frameShop.content, "TOPLEFT", 0, -30) |
97 | 89 |
98 -- spec pickers | |
99 _chk1 = AceGUI:Create("AmrUiCheckBox") | |
100 _chk1:SetUserData("spec", 1) | |
101 _chk1:SetCallback("OnClick", onSpecClick) | |
102 _frameShop:AddChild(_chk1) | |
103 _chk1:SetPoint("TOPLEFT", _cboPlayers.frame, "BOTTOMLEFT", 0, -20) | |
104 | |
105 _chk2 = AceGUI:Create("AmrUiCheckBox") | |
106 _chk2:SetUserData("spec", 2) | |
107 _chk2:SetCallback("OnClick", onSpecClick) | |
108 _frameShop:AddChild(_chk2) | |
109 _chk2:SetPoint("LEFT", _chk1.frame, "RIGHT", 30, 0) | |
110 | |
111 _chk3 = AceGUI:Create("AmrUiCheckBox") | |
112 _chk3:SetUserData("spec", 3) | |
113 _chk3:SetCallback("OnClick", onSpecClick) | |
114 _frameShop:AddChild(_chk3) | |
115 _chk3:SetPoint("LEFT", _chk2.frame, "RIGHT", 30, 0) | |
116 | |
117 _chk4 = AceGUI:Create("AmrUiCheckBox") | |
118 _chk4:SetUserData("spec", 4) | |
119 _chk4:SetCallback("OnClick", onSpecClick) | |
120 _frameShop:AddChild(_chk4) | |
121 _chk4:SetPoint("LEFT", _chk3.frame, "RIGHT", 30, 0) | |
122 | |
123 _panelContent = AceGUI:Create("AmrUiPanel") | 90 _panelContent = AceGUI:Create("AmrUiPanel") |
124 _panelContent:SetLayout("None") | 91 _panelContent:SetLayout("None") |
125 _panelContent:SetTransparent() | 92 _panelContent:SetTransparent() |
126 _frameShop:AddChild(_panelContent) | 93 _frameShop:AddChild(_panelContent) |
127 _panelContent:SetPoint("TOPLEFT", _chk1.frame, "BOTTOMLEFT", 0, -10) | 94 _panelContent:SetPoint("TOPLEFT", _cboPlayers.frame, "BOTTOMLEFT", 0, -10) |
128 _panelContent:SetPoint("BOTTOMRIGHT", _frameShop.content, "BOTTOMRIGHT") | 95 _panelContent:SetPoint("BOTTOMRIGHT", _frameShop.content, "BOTTOMRIGHT") |
129 | 96 |
130 -- update shopping list data | 97 -- update shopping list data |
131 local player = Amr:ExportCharacter() | 98 local player = Amr:ExportCharacter() |
132 Amr:UpdateShoppingData(player) | 99 Amr:UpdateShoppingData(player) |
133 | 100 |
134 -- fill player list | 101 -- fill player list |
135 local playerList = {} | 102 local playerList = {} |
136 for name, data in pairs(Amr.db.global.Shopping) do | 103 local firstData = nil |
137 table.insert(playerList, { text = name, value = name }) | 104 for name, v in pairs(Amr.db.global.Shopping2) do |
105 for setupName, data in pairs(v.setups) do | |
106 table.insert(playerList, { text = name .. " " .. setupName, value = name .. "@" .. setupName }) | |
107 if not firstData then | |
108 firstData = name .. "@" .. setupName | |
109 end | |
110 end | |
138 end | 111 end |
139 _cboPlayers:SetItems(playerList) | 112 _cboPlayers:SetItems(playerList) |
140 | 113 |
141 -- set default selected player | 114 -- set default selected player |
142 if not _selectedPlayer then | 115 local playerData = Amr.db.global.Shopping2[player.Name .. "-" .. player.Realm] |
143 _selectedPlayer = player.Name .. "-" .. player.Realm | 116 if playerData and playerData.setups then |
144 end | 117 _selectedSetup = Amr:GetActiveSetupLabel() |
145 _cboPlayers:SelectItem(_selectedPlayer) | 118 if not _selectedSetup then |
119 Amr:PickFirstSetupForSpec() | |
120 _selectedSetup = Amr:GetActiveSetupLabel() | |
121 end | |
122 if _selectedSetup and not playerData.setups[_selectedSetup] then | |
123 _selectedSetup = nil | |
124 else | |
125 _selectedSetup = player.Name .. "-" .. player.Realm .. "@" .. _selectedSetup | |
126 end | |
127 end | |
128 | |
129 if not _selectedSetup then | |
130 if playerData and playerData.setups then | |
131 for k,v in pairs(playerData.setups) do | |
132 _selectedSetup = player.Name .. "-" .. player.Realm .. "@" .. k | |
133 break | |
134 end | |
135 else | |
136 _selectedSetup = firstData | |
137 end | |
138 end | |
139 _cboPlayers:SelectItem(_selectedSetup) | |
146 | 140 |
147 Amr:RefreshShoppingUi() | 141 Amr:RefreshShoppingUi() |
148 | 142 |
149 -- set event on dropdown after UI has been initially rendered | 143 -- set event on dropdown after UI has been initially rendered |
150 _cboPlayers:SetCallback("OnChange", onPlayerChange) | 144 _cboPlayers:SetCallback("OnChange", onPlayerChange) |
145 | |
146 -- set a timer to refresh a bit after opening b/c sometimes some item info isn't available | |
147 Amr.Wait(2, function() | |
148 if _frameShop then | |
149 Amr:RefreshShoppingUi() | |
150 end | |
151 end) | |
151 else | 152 else |
152 _frameShop:Show() | 153 _frameShop:Show() |
153 Amr:RefreshShoppingUi() | 154 Amr:RefreshShoppingUi() |
154 end | 155 end |
155 | 156 |
268 end | 269 end |
269 end | 270 end |
270 | 271 |
271 function Amr:RefreshShoppingUi() | 272 function Amr:RefreshShoppingUi() |
272 | 273 |
273 local posToCheck = { _chk1, _chk2, _chk3, _chk4 } | |
274 local chk | |
275 | |
276 -- reset spec checkboxes | |
277 for specPos = 1,4 do | |
278 chk = posToCheck[specPos] | |
279 chk:SetVisible(false) | |
280 chk:SetChecked(false) | |
281 end | |
282 | |
283 -- clear out any previous data | 274 -- clear out any previous data |
284 _panelContent:ReleaseChildren() | 275 _panelContent:ReleaseChildren() |
285 | 276 |
286 local data = Amr.db.global.Shopping[_selectedPlayer] | 277 local parts = { strsplit("@", _selectedSetup) } |
278 | |
279 local data = Amr.db.global.Shopping2[parts[1]].setups[parts[2]] | |
287 if not data then | 280 if not data then |
288 _panelContent:SetLayout("None") | 281 _panelContent:SetLayout("None") |
289 | 282 |
290 local lbl = AceGUI:Create("AmrUiLabel") | 283 local lbl = AceGUI:Create("AmrUiLabel") |
291 _panelContent:AddChild(lbl) | 284 _panelContent:AddChild(lbl) |
296 else | 289 else |
297 local allStuff = { gems = {}, enchants = {}, materials = {} } | 290 local allStuff = { gems = {}, enchants = {}, materials = {} } |
298 local hasStuff = false | 291 local hasStuff = false |
299 local visited = {} | 292 local visited = {} |
300 | 293 |
301 for specPos = 1,4 do | 294 for inventoryId, stuff in pairs(data) do |
302 -- set labels on checkboxes | 295 hasStuff = true |
303 if data.specs[specPos] and data.specs[specPos] ~= 0 then | 296 if not visited[inventoryId] then |
304 chk = posToCheck[specPos] | 297 if stuff.gems then |
305 chk:SetText(L.SpecsShort[data.specs[specPos]]) | 298 for itemId, count in pairs(stuff.gems) do |
306 chk:SetVisible(true) | 299 incrementTableItem(allStuff.gems, itemId, count) |
307 chk:SetChecked(_specs[specPos]) | 300 end |
301 end | |
308 | 302 |
309 -- gather up all stuff for checked specs | 303 if stuff.enchants then |
310 if _specs[specPos] then | 304 for itemId, count in pairs(stuff.enchants) do |
311 hasStuff = true | 305 incrementTableItem(allStuff.enchants, itemId, count) |
312 | |
313 for inventoryId, stuff in pairs(data.stuff[specPos]) do | |
314 if not visited[inventoryId] then | |
315 if stuff.gems then | |
316 for itemId, count in pairs(stuff.gems) do | |
317 incrementTableItem(allStuff.gems, itemId, count) | |
318 end | |
319 end | |
320 | |
321 if stuff.enchants then | |
322 for itemId, count in pairs(stuff.enchants) do | |
323 incrementTableItem(allStuff.enchants, itemId, count) | |
324 end | |
325 end | |
326 | |
327 if stuff.materials then | |
328 for itemId, count in pairs(stuff.materials) do | |
329 incrementTableItem(allStuff.materials, itemId, count) | |
330 end | |
331 end | |
332 | |
333 -- make sure not to count the same physical item twice | |
334 if inventoryId ~= -1 then | |
335 visited[inventoryId] = true | |
336 end | |
337 end | |
338 end | 306 end |
339 end | 307 end |
340 | 308 |
341 end | 309 if stuff.materials then |
310 for itemId, count in pairs(stuff.materials) do | |
311 incrementTableItem(allStuff.materials, itemId, count) | |
312 end | |
313 end | |
342 | 314 |
315 -- make sure not to count the same physical item twice | |
316 if inventoryId ~= -1 then | |
317 visited[inventoryId] = true | |
318 end | |
319 end | |
343 end | 320 end |
344 | 321 |
345 if hasStuff then | 322 if hasStuff then |
346 -- remove what we already own | 323 -- remove what we already own |
347 local owned = { counts = {}, loaded = {} } | 324 local owned = { counts = {}, loaded = {} } |
417 end | 394 end |
418 | 395 |
419 return ret | 396 return ret |
420 end | 397 end |
421 | 398 |
422 -- look at both gear sets and find stuff that a player needs to acquire to gem/enchant their gear | 399 -- look at all gear sets and find stuff that a player needs to acquire to gem/enchant their gear for each setup |
423 function Amr:UpdateShoppingData(player) | 400 function Amr:UpdateShoppingData(player) |
424 | 401 |
425 local required = { | 402 local required = { |
426 stuff = {}, | 403 setups = {} |
427 specs = player.Specs | |
428 } | 404 } |
429 | 405 |
430 for i, spec in ipairs(required.specs) do | 406 for i, setup in ipairs(Amr.db.char.GearSetups) do |
431 local gear = Amr.db.char.GearSets[i] | 407 local gear = setup.Gear |
432 if gear then | 408 if gear then |
433 required.stuff[i] = getShoppingData(player, gear) | 409 required.setups[setup.Label] = getShoppingData(player, gear) |
434 else | 410 end |
435 required.stuff[i] = {} | 411 end |
436 end | 412 |
437 end | 413 Amr.db.global.Shopping2[player.Name .. "-" .. player.Realm] = required |
438 | |
439 Amr.db.global.Shopping[player.Name .. "-" .. player.Realm] = required | |
440 end | 414 end |
441 | 415 |
442 Amr:AddEventHandler("AUCTION_HOUSE_SHOW", function() | 416 Amr:AddEventHandler("AUCTION_HOUSE_SHOW", function() |
443 _isAhOpen = true | 417 _isAhOpen = true |
444 if Amr.db.profile.options.shopAh then | 418 if Amr.db.profile.options.shopAh then |