Mercurial > wow > itemauditor
comparison Modules/Crafting.lua @ 59:4ec321eb0dfe
Adding a crafing tab to the ItemAuditor window.
author | Asa Ayers <Asa.Ayers@Gmail.com> |
---|---|
date | Sun, 25 Jul 2010 15:51:13 -0700 |
parents | |
children | 069f39f62104 |
comparison
equal
deleted
inserted
replaced
58:bdf3aba93aa9 | 59:4ec321eb0dfe |
---|---|
1 local addonName, addonTable = ...; | |
2 local ItemAuditor = _G[addonName] | |
3 | |
4 local AceGUI = LibStub("AceGUI-3.0") | |
5 local ScrollingTable = LibStub("ScrollingTable") | |
6 | |
7 local validateMoney = ItemAuditor.validateMoney | |
8 local parseMoney = ItemAuditor.parseMoney | |
9 | |
10 local realData = {} | |
11 | |
12 local function displayMoney(rowFrame, cellFrame, data, cols, row, realrow, column, fShow, table, ...) | |
13 if fShow == true then | |
14 local money = data[realrow][column] | |
15 if money then | |
16 cellFrame.text:SetText(ItemAuditor:FormatMoney(tonumber(money))) | |
17 else | |
18 cellFrame.text:SetText("") | |
19 end | |
20 | |
21 end | |
22 end | |
23 | |
24 local craftingCols = { | |
25 { name= "Item", width = 200, defaultsort = "desc", | |
26 ['DoCellUpdate'] = function(rowFrame, cellFrame, data, cols, row, realrow, column, fShow, table, ...) | |
27 if fShow == true then | |
28 local data = realData[realrow] | |
29 cellFrame.text:SetText(data.link) | |
30 end | |
31 end, | |
32 }, | |
33 { name= "Cost Each", width = 100, align = "RIGHT", | |
34 ['DoCellUpdate'] = displayMoney, | |
35 }, | |
36 { name= "Est Sale Each", width = 100, align = "RIGHT", | |
37 ['DoCellUpdate'] = displayMoney, | |
38 }, | |
39 { name= "Decided By", width = 100, align = "RIGHT", | |
40 | |
41 }, | |
42 { name= "craft", width = 50, align = "RIGHT", | |
43 | |
44 }, | |
45 { name= "Total Profit", width = 100, align = "RIGHT", | |
46 ['DoCellUpdate'] = displayMoney, | |
47 }, | |
48 } | |
49 | |
50 local function ExportToSkillet() | |
51 local index = 1 | |
52 local data = ItemAuditor:GetCraftingRow(index) | |
53 while data do | |
54 local skillString = select(3, string.find(data.recipeLink, "^|%x+|H(.+)|h%[.+%]")) | |
55 local _, skillId = strsplit(":", skillString) | |
56 | |
57 ItemAuditor:AddToQueue(skillId,tradeSkillIndex, data.queue) | |
58 | |
59 local data = ItemAuditor:GetCraftingRow(index) | |
60 end | |
61 end | |
62 | |
63 local craftingContent = false | |
64 local craftingTable = false | |
65 local function ShowCrafting(container) | |
66 if craftingContent == false then | |
67 local window = container.frame | |
68 craftingContent = CreateFrame("Frame",nil,window) | |
69 craftingContent:SetBackdropColor(0, 0, 1, 0.5) | |
70 craftingContent:SetBackdropBorderColor(1, 0, 0, 1) | |
71 | |
72 craftingContent:SetPoint("TOPLEFT", window, 10, -50) | |
73 craftingContent:SetPoint("BOTTOMRIGHT",window, -10, 10) | |
74 | |
75 craftingTable = ScrollingTable:CreateST(craftingCols, 22, nil, nil, craftingContent ) | |
76 | |
77 IAcc = craftingContent | |
78 IAccWindow = window | |
79 craftingTable.frame:SetPoint("TOPLEFT",craftingContent, 0,0) | |
80 craftingTable.frame:SetPoint("BOTTOMRIGHT", craftingContent, 0, 30) | |
81 | |
82 craftingTable:RegisterEvents({ | |
83 ["OnEnter"] = function (rowFrame, cellFrame, data, cols, row, realrow, column, scrollingTable, ...) | |
84 if realrow then | |
85 local data = realData[realrow] | |
86 | |
87 GameTooltip:SetOwner(rowFrame, "ANCHOR_CURSOR") | |
88 GameTooltip:SetHyperlink(data.link) | |
89 GameTooltip:Show() | |
90 end | |
91 end, | |
92 ["OnLeave"] = function (rowFrame, cellFrame, data, cols, row, realrow, column, scrollingTable, ...) | |
93 GameTooltip:Hide() | |
94 end, | |
95 }); | |
96 | |
97 | |
98 btnProcess = CreateFrame("Button", nil, craftingContent, "UIPanelButtonTemplate") | |
99 btnProcess:SetText("Process") | |
100 btnProcess:SetSize(100, 25) | |
101 btnProcess:SetPoint("BOTTOMRIGHT", craftingContent, 0, 0) | |
102 btnProcess:RegisterForClicks("LeftButtonUp"); | |
103 btnProcess:SetScript("OnClick", function (self, button, down) | |
104 local data = ItemAuditor:GetCraftingRow(1) | |
105 ItemAuditor:Print('Crafting %sx%s', data.link, data.queue) | |
106 DoTradeSkill(data.tradeSkillIndex, data.queue) | |
107 end) | |
108 | |
109 btnProcess:SetScript("OnEnter", function() | |
110 local data = ItemAuditor:GetCraftingRow(1) | |
111 if data then | |
112 GameTooltip:SetOwner(this, "ANCHOR_CURSOR") | |
113 GameTooltip:SetText(format('Create %sx%s', data.link, data.queue)) | |
114 GameTooltip:Show() | |
115 end | |
116 end) | |
117 | |
118 btnProcess:SetScript("OnLeave", function() | |
119 GameTooltip:Hide() | |
120 end) | |
121 | |
122 btnSkillet = CreateFrame("Button", nil, craftingContent, "UIPanelButtonTemplate") | |
123 btnSkillet:SetText("Queue in Skillet") | |
124 btnSkillet:SetSize(125, 25) | |
125 btnSkillet:SetPoint("BOTTOMRIGHT", btnProcess, 'BOTTOMLEFT', 0, 0) | |
126 btnSkillet:RegisterForClicks("LeftButtonUp"); | |
127 btnSkillet:SetScript("OnClick", function (self, button, down) | |
128 ExportToSkillet() | |
129 end) | |
130 | |
131 end | |
132 craftingContent:Show() | |
133 | |
134 if container.parent then | |
135 local width = 80 | |
136 for i, data in pairs(craftingCols) do | |
137 width = width + data.width | |
138 end | |
139 container.parent:SetWidth(width); | |
140 end | |
141 | |
142 ItemAuditor:RegisterEvent("TRADE_SKILL_SHOW", function() | |
143 if craftingContent and craftingContent:IsVisible() then | |
144 ItemAuditor:UpdateCraftingTable() | |
145 end | |
146 end) | |
147 ItemAuditor:UpdateCraftingTable() | |
148 | |
149 return craftingContent | |
150 end | |
151 | |
152 | |
153 | |
154 ItemAuditor:RegisterTab('Crafting', 'tab_crafting', ShowCrafting) | |
155 function ItemAuditor:DisplayCrafting() | |
156 self:CreateFrame('tab_crafting') | |
157 end | |
158 | |
159 local craftingDeciders = {} | |
160 | |
161 function ItemAuditor:RegisterCraftingDecider(name, decider) | |
162 craftingDeciders[name] = decider | |
163 end | |
164 | |
165 local lastWinnder = "" | |
166 local function Decide(data) | |
167 local newDecision = 0 | |
168 for name, decider in pairs(craftingDeciders) do | |
169 if name ~= lastWinner then | |
170 newDecision = decider(data) | |
171 if newDecision > data.queue then | |
172 data.queue = newDecision | |
173 lastWinner = name | |
174 return Decide(data) | |
175 elseif newDecision < 0 then | |
176 lastWinner = "" | |
177 return 'VETO: '..name, 0 | |
178 end | |
179 end | |
180 end | |
181 | |
182 winner = lastWinner | |
183 lastWinner = "" | |
184 | |
185 return winner, data.queue | |
186 end | |
187 | |
188 local function isProfitable(data) | |
189 if data.profit > 0 and data.profit > ItemAuditor:GetCraftingThreshold() then | |
190 return 1 | |
191 end | |
192 return -1 | |
193 end | |
194 ItemAuditor:RegisterCraftingDecider('Is Profitable', isProfitable) | |
195 | |
196 local function tableFilter(self, row, ...) | |
197 -- column 5 is how many should be crafted | |
198 return row[5] > 0 | |
199 end | |
200 | |
201 local tableData = {} | |
202 function ItemAuditor:UpdateCraftingTable() | |
203 if LSW == nil then | |
204 self:Print("This feature requires LilSparky's Workshop.") | |
205 return | |
206 end | |
207 if Skillet == nil then | |
208 self:Print("This feature requires Skillet.") | |
209 return | |
210 end | |
211 if GetAuctionBuyout ~= nil then | |
212 elseif AucAdvanced and AucAdvanced.Version then | |
213 else | |
214 self:Print("This feature requires Auctionator, Auctioneer, AuctionLite, or AuctionMaster.") | |
215 return | |
216 end | |
217 wipe(realData) | |
218 wipe(tableData) | |
219 | |
220 local profitableItems = {} | |
221 local profitableIndex = 1 | |
222 local numChecked = 0 | |
223 local row = 1 | |
224 | |
225 for i = 1, GetNumTradeSkills() do | |
226 local itemLink = GetTradeSkillItemLink(i) | |
227 local itemId = Skillet:GetItemIDFromLink(itemLink) | |
228 | |
229 --Figure out if its an enchant or not | |
230 _, _, _, _, altVerb = GetTradeSkillInfo(i) | |
231 if LSW.scrollData[itemId] ~= nil and altVerb == 'Enchant' then | |
232 -- Ask LSW for the correct scroll | |
233 itemId = LSW.scrollData[itemId]["scrollID"] | |
234 end | |
235 | |
236 local recipeLink = GetTradeSkillRecipeLink(i) | |
237 local stackSize = 1 | |
238 if recipeLink ~= nil and itemId ~= nil then | |
239 local skillName, skillType, numAvailable, isExpanded, altVerb = GetTradeSkillInfo(i) | |
240 local itemName, itemLink= GetItemInfo(itemId) | |
241 | |
242 local count = Altoholic:GetItemCount(itemId) | |
243 local reagents = {} | |
244 local totalCost = 0 | |
245 for reagentId = 1, GetTradeSkillNumReagents(i) do | |
246 local reagentName, _, reagentCount = GetTradeSkillReagentInfo(i, reagentId); | |
247 local reagentLink = GetTradeSkillReagentItemLink(i, reagentId) | |
248 | |
249 reagents[reagentId] = { | |
250 name = reagentName, | |
251 count = reagentCount, | |
252 price = self:GetReagentCost(reagentLink, reagentCount), | |
253 } | |
254 totalCost = totalCost + self:GetReagentCost(reagentLink, reagentCount) | |
255 end | |
256 | |
257 local data = { | |
258 recipeLink = recipeLink, | |
259 link = itemLink, | |
260 name = itemName, | |
261 count = count, | |
262 price = (self:GetAuctionPrice(itemLink) or 0), | |
263 cost = totalCost, | |
264 profit = (self:GetAuctionPrice(itemLink) or 0) - totalCost, | |
265 reagents = reagents, | |
266 count = count, | |
267 tradeSkillIndex = i, | |
268 queue = 0, | |
269 winner = "", | |
270 } | |
271 | |
272 data.winner, data.queue = Decide(data) | |
273 data.queue = data.queue - count | |
274 | |
275 -- If a tradeskill makes 5 at a time and something asks for 9, we should only | |
276 -- craft twice to get 10. | |
277 data.queue = ceil(data.queue / GetTradeSkillNumMade(i)) | |
278 | |
279 realData[row] = data | |
280 row = row + 1 | |
281 end | |
282 end | |
283 table.sort(realData, function(a, b) return a.profit*a.queue > b.profit*b.queue end) | |
284 for key, data in pairs(realData) do | |
285 tableData[key] = { | |
286 data.name, | |
287 data.cost, | |
288 data.price, | |
289 data.winner, | |
290 data.queue, | |
291 data.profit*data.queue, | |
292 } | |
293 end | |
294 | |
295 craftingTable:SetData(tableData, true) | |
296 craftingTable:SetFilter(tableFilter) | |
297 end | |
298 | |
299 function ItemAuditor:GetCraftingRow(row) | |
300 if craftingTable then | |
301 for _, index in pairs(craftingTable.sorttable) do | |
302 local tableRow = tableData[index] | |
303 if tableFilter(nil, tableRow) then | |
304 row = row - 1 | |
305 if row == 0 then | |
306 return realData[index] | |
307 end | |
308 end | |
309 end | |
310 elseif realData then | |
311 return realData[row] | |
312 end | |
313 return nil | |
314 end |