annotate Modules/QuickAuctions.lua @ 157:091bae7bfca0

Fixed a bug in calculating purchase/sale where the item ID was being used instead of the number of items that changed.
author Asa Ayers <Asa.Ayers@Gmail.com>
date Wed, 15 Dec 2010 22:49:28 -0800
parents 613fd4bc6999
children
rev   line source
Asa@63 1 local ItemAuditor = select(2, ...)
Asa@63 2 local QuickAuctions= ItemAuditor:NewModule("QuickAuctions")
Asa@64 3 local Crafting = ItemAuditor:GetModule("Crafting")
Asa@68 4 local Utils = ItemAuditor:GetModule("Utils")
Asa@89 5 local AuctionHouse = ItemAuditor:GetModule("AuctionHouse")
Asa@18 6
Asa@86 7 local PT = LibStub("LibPeriodicTable-3.1")
Asa@86 8
Asa@62 9 --[[
Asa@62 10 This is simply for compatibility while I change the QA API. Once
Asa@62 11 my changes get merged into the main project, this can go away.
Asa@62 12 ]]
Asa@62 13 if QAAPI ~= nil and QAAPI.GetGroupThreshold ~= nil and QAAPI.GetGroupConfig == nil then
Asa@62 14 function QAAPI:GetGroupConfig(groupName)
Asa@62 15 return QAAPI:GetGroupThreshold(groupName),
Asa@62 16 QAAPI:GetGroupPostCap(groupName),
Asa@62 17 QAAPI:GetGroupPerAuction(groupName)
Asa@62 18 end
Asa@62 19
Asa@62 20 function QAAPI:SetGroupConfig(groupName, key, value)
Asa@62 21 if key == 'threshold' then
Asa@62 22 return QAAPI:SetGroupThreshold(groupName, value)
Asa@62 23 end
Asa@62 24 end
Asa@62 25 end
Asa@62 26
Asa@62 27
Asa@62 28
Asa@63 29 function ItemAuditor:IsQACompatible()
Asa@62 30 return (QAAPI ~= nil and QAAPI.GetGroupConfig ~= nil)
Asa@18 31 end
Asa@18 32
Asa@63 33 function ItemAuditor:IsQAEnabled()
Asa@119 34 if ItemAuditor:IsQACompatible() then
Asa@119 35 local qam = GetAddOnInfo('QAManager')
Asa@119 36 if qam then
Asa@119 37 ItemAuditor.Options.args.qa_options.disabled = true
Asa@119 38 if ItemAuditor.db.char.use_quick_auctions then
Asa@119 39 ItemAuditor.db.char.use_quick_auctions = false
Asa@119 40 StaticPopupDialogs["ItemAuditor_QAOptionsReplaced"] = {
Asa@119 41 text = "The ability to have ItemAuditor adjust your QA thresholds is being moved to QAManager. If you have to use the options within ItemAuditor you can disable QAManager to restore them for now, but this option will change in the future.",
Asa@119 42 button1 = "OK",
Asa@119 43 timeout = 0,
Asa@119 44 whileDead = true,
Asa@119 45 hideOnEscape = true,
Asa@119 46 OnAccept = function()
Asa@119 47 -- StaticPopupDialogs["ItemAuditor_QAOptionsReplaced"] = nil
Asa@119 48 end,
Asa@119 49 }
Asa@119 50 StaticPopup_Show('ItemAuditor_QAOptionsReplaced')
Asa@119 51 end
Asa@119 52 return false
Asa@119 53 end
Asa@119 54 return ItemAuditor.db.char.use_quick_auctions
Asa@119 55 end
Asa@119 56 return false
Asa@18 57 end
Asa@18 58
Asa@63 59 function ItemAuditor:IsQADisabled()
Asa@18 60 return not self:IsQAEnabled()
Asa@18 61 end
Asa@18 62
Asa@63 63 function ItemAuditor:SetQAEnabled(info, value)
Asa@18 64 ItemAuditor.db.char.use_quick_auctions = value
Asa@18 65 end
Asa@18 66
Asa@63 67 function ItemAuditor:RefreshQAGroups()
Asa@63 68 if not ItemAuditor.IsQAEnabled() then
Asa@18 69 return
Asa@18 70 end
Asa@18 71 for groupName in pairs(QAAPI:GetGroups()) do
Asa@18 72 self:UpdateQAGroup(groupName)
Asa@18 73 end
Asa@18 74 end
Asa@18 75
Asa@63 76 function ItemAuditor:UpdateQAThreshold(link)
Asa@63 77 if not ItemAuditor.IsQAEnabled() then
Asa@18 78 return
Asa@18 79 end
Asa@18 80 _, link= GetItemInfo(link)
Asa@18 81
Asa@18 82 self:UpdateQAGroup(QAAPI:GetItemGroup(link))
Asa@18 83 end
Asa@18 84
Asa@19 85 local function calculateQAThreshold(copper)
Asa@19 86 if copper == 0 then
Asa@19 87 copper = 1
Asa@19 88 end
Asa@19 89
Asa@19 90 -- add my minimum profit margin
Asa@20 91 -- GetAuctionThreshold returns a percent as a whole number. This will convert 25 to 1.25
Asa@111 92 local min_by_percent = copper * (1+ItemAuditor:GetAuctionThreshold())
Asa@111 93 local min_by_value = copper + ItemAuditor.db.char.auction_threshold_value
Asa@111 94 copper = max(min_by_percent, min_by_value)
Asa@19 95
Asa@19 96 -- add AH Cut
Asa@63 97 local keep = 1 - ItemAuditor:GetAHCut()
Asa@19 98 return copper/keep
Asa@19 99 end
Asa@19 100
Asa@63 101 function ItemAuditor:UpdateQAGroup(groupName)
Asa@63 102 if not ItemAuditor.IsQAEnabled() then
Asa@18 103 return
Asa@18 104 end
Asa@18 105 if groupName then
Asa@18 106 local threshold = 0
Asa@18 107
Asa@18 108 for link in pairs(QAAPI:GetItemsInGroup(groupName)) do
Asa@18 109 local _, itemCost= ItemAuditor:GetItemCost(link, 0)
Asa@18 110
Asa@18 111 threshold = max(threshold, itemCost)
Asa@18 112 end
Asa@18 113
Asa@19 114 threshold = calculateQAThreshold(threshold)
Asa@18 115
Asa@62 116 QAAPI:SetGroupConfig(groupName, 'threshold', ceil(threshold))
Asa@18 117 end
Asa@18 118 end
Asa@18 119
Asa@59 120 local function isProfitable(data)
Asa@129 121 if ItemAuditor:IsQACompatible() then
Asa@59 122 local QAGroup = QAAPI:GetItemGroup(data.link)
Asa@59 123 if QAGroup ~= nil then
Asa@63 124 local currentInvested, _, currentCount = ItemAuditor:GetItemCost(data.link)
Asa@62 125 local threshold, postCap, perAuction = QAAPI:GetGroupConfig(QAGroup)
Asa@62 126 local stackSize = postCap * perAuction
Asa@59 127
Asa@59 128 -- bonus
Asa@71 129 stackSize = ceil(stackSize * (1+ItemAuditor.db.char.qa_extra))
Asa@77 130 local target = stackSize
Asa@77 131 stackSize = stackSize - currentCount
Asa@59 132
Asa@59 133 local newThreshold = ((data.cost*stackSize) + currentInvested) / (currentCount + stackSize)
Asa@59 134 newThreshold = calculateQAThreshold(newThreshold)
Asa@59 135
Asa@59 136 if newThreshold < data.price then
Asa@77 137 return target
Asa@59 138 end
Asa@59 139
Asa@59 140 return -1
Asa@59 141 end
Asa@59 142 end
Asa@59 143 return 0
Asa@59 144 end
Asa@112 145
Asa@112 146 local QADeciderOptions = {
Asa@112 147 extra = {
Asa@112 148 type = "range",
Asa@112 149 name = "Create Extra",
Asa@112 150 desc = "This is the amount of an item that should be created above what you sell in one post in QuickAuctions."..
Asa@112 151 "If you sell 4 stacks of 5 of an item and your extra is 25%, it will queue enough for you to have 25 of that item.",
Asa@112 152 min = 0.0,
Asa@112 153 max = 1.0,
Asa@112 154 step = 0.01,
Asa@112 155 isPercent = true,
Asa@112 156 get = function() return ItemAuditor.db.char.qa_extra end,
Asa@112 157 set = function(info, value)
Asa@112 158 ItemAuditor.db.char.qa_extra = value
Asa@112 159 end,
Asa@129 160 disabled = function() return not ItemAuditor:IsQACompatible() end,
Asa@112 161 order = 10,
Asa@112 162 },
Asa@112 163 }
Asa@112 164 Crafting.RegisterCraftingDecider('IA QuickAuctions', isProfitable, QADeciderOptions)
Asa@59 165
Asa@68 166
Asa@63 167 function ItemAuditor:Queue()
Asa@70 168 local dest, name = Crafting.GetQueueDestination()
Asa@68 169 local function Export(data)
Asa@70 170 ItemAuditor:Print(format("Adding %s x%s to %s queue. Profit: %s",
Asa@68 171 data.link,
Asa@68 172 data.queue,
Asa@70 173 name,
Asa@68 174 Utils.FormatMoney(data.profit)
Asa@68 175 ))
Asa@70 176 dest(data)
Asa@18 177 end
Asa@68 178 ItemAuditor:UpdateCraftingTable()
Asa@68 179 Crafting.Export(Export)
Asa@18 180 end
Asa@18 181
Asa@63 182 function ItemAuditor:GetReagentCost(link, total)
Asa@18 183 local totalCost = 0
Asa@153 184
Asa@153 185 if not link then
Asa@153 186 return 99990000
Asa@153 187 end
Asa@153 188
Asa@86 189 if PT:ItemInSet(link,"Tradeskill.Mat.BySource.Vendor") then
Asa@19 190 local _, _, _, _, _, _, _, _, _, _, itemVendorPrice = GetItemInfo (link);
Asa@19 191 totalCost = itemVendorPrice * total
Asa@19 192 total = 0
Asa@19 193 end
Asa@19 194
Asa@19 195
Asa@63 196 local investedTotal, investedPerItem, count = ItemAuditor:GetItemCost(link)
Asa@18 197
Asa@18 198 if count > 0 then
Asa@18 199 if total <= count then
Asa@18 200 totalCost = investedPerItem * total
Asa@18 201 total = 0
Asa@18 202 else
Asa@18 203 totalCost = investedTotal
Asa@18 204 total = total - count
Asa@18 205 end
Asa@18 206 end
Asa@18 207
Asa@18 208 -- If there is none on the auction house, this uses a large enough number
Asa@18 209 -- to prevent us from trying to make the item.
Asa@21 210 local ahPrice = (self:GetAuctionPrice(link) or 99990000)
Asa@21 211 -- local itemName, itemLink, itemRarity, itemLevel, itemMinLevel, itemType, _, _, _, _, itemVendorPrice = GetItemInfo (link);
Asa@18 212
Asa@18 213 return totalCost + (ahPrice * total)
Asa@18 214 end
Asa@18 215
Asa@63 216 function ItemAuditor:GetAuctionPrice(itemLink)
Asa@89 217 return AuctionHouse:GetAuctionPrice(itemLink)
Asa@21 218 end
Asa@21 219
Asa@63 220 function ItemAuditor:AddToQueue(skillId,skillIndex, toQueue)
Asa@18 221 if Skillet == nil then
Asa@18 222 self:Print("Skillet not loaded")
Asa@21 223 return
Asa@18 224 end
Asa@18 225 if Skillet.QueueCommandIterate ~= nil then
Asa@18 226 local queueCommand = Skillet:QueueCommandIterate(tonumber(skillId), toQueue)
Asa@18 227 Skillet:AddToQueue(queueCommand)
Asa@18 228 else
Asa@18 229 Skillet.stitch:AddToQueue(skillIndex, toQueue)
Asa@18 230 end
Asa@18 231 end
Asa@67 232
Asa@67 233 ItemAuditor.Options.args.qa_options = {
Asa@67 234 name = "QA Options",
Asa@67 235 desc = "Control how ItemAuditor integrates with QuickAuctions",
Asa@67 236 type = 'group',
Asa@67 237 disabled = function() return not ItemAuditor:IsQACompatible() end,
Asa@67 238 args = {
Asa@67 239 toggle_qa = {
Asa@67 240 type = "toggle",
Asa@67 241 name = "Enable Quick Auctions",
Asa@67 242 desc = "This will enable or disable Quick Auctions integration",
Asa@67 243 get = "IsQAEnabled",
Asa@67 244 set = "SetQAEnabled",
Asa@67 245 order = 0,
Asa@67 246 },
Asa@111 247 enable_percent = {
Asa@111 248 type = "toggle",
Asa@111 249 name = "Use percent to calculate threshold.",
Asa@111 250 get = function() return ItemAuditor.db.char.auction_threshold > 0 end,
Asa@111 251 set = function(info, value)
Asa@111 252 value = value and 0.15 or 0
Asa@111 253 ItemAuditor.db.char.auction_threshold = value
Asa@111 254 end,
Asa@111 255 order = 1,
Asa@111 256 },
Asa@67 257 auction_threshold = {
Asa@67 258 type = "range",
Asa@67 259 name = "Auction Threshold",
Asa@111 260 desc = "Don't sell items for less than this amount of profit.",
Asa@67 261 min = 0.0,
Asa@67 262 max = 1.0,
Asa@67 263 isPercent = true,
Asa@111 264 hidden = function() return ItemAuditor.db.char.auction_threshold == 0 end,
Asa@67 265 get = function() return ItemAuditor.db.char.auction_threshold end,
Asa@67 266 set = function(info, value)
Asa@67 267 ItemAuditor.db.char.auction_threshold = value
Asa@71 268 -- ItemAuditor:RefreshQAGroups()
Asa@71 269 end,
Asa@71 270 disabled = 'IsQADisabled',
Asa@111 271 order = 2,
Asa@111 272 },
Asa@111 273 enable_absolute = {
Asa@111 274 type = "toggle",
Asa@111 275 name = "Use value to calculate threshold.",
Asa@111 276 get = function() return ItemAuditor.db.char.auction_threshold_value > 0 end,
Asa@111 277 set = function(info, value)
Asa@111 278 value = value and 100000 or 0
Asa@111 279 ItemAuditor.db.char.auction_threshold_value = value
Asa@111 280 end,
Asa@111 281 order = 3,
Asa@111 282 },
Asa@111 283 auction_threshold_absolute = {
Asa@111 284 type = "input",
Asa@111 285 name = "Auction Threshold",
Asa@111 286 desc = "Don't sell items for less than this amount of profit.",
Asa@111 287 hidden = function() return ItemAuditor.db.char.auction_threshold_value == 0 end,
Asa@111 288 get = function() return
Asa@111 289 Utils.FormatMoney(ItemAuditor.db.char.auction_threshold_value , '', true)
Asa@111 290 end,
Asa@111 291 validate = function(info, value)
Asa@111 292 if not Utils.validateMoney(value) then
Asa@111 293 return "Invalid money format"
Asa@111 294 end
Asa@111 295 return true
Asa@111 296 end,
Asa@111 297 set = function(info, value)
Asa@111 298 ItemAuditor.db.char.auction_threshold_value = Utils.parseMoney(value)
Asa@111 299 end,
Asa@111 300 usage = "###g ##s ##c",
Asa@111 301 disabled = 'IsQADisabled',
Asa@111 302 order = 4,
Asa@111 303 },
Asa@67 304 refresh_qa = {
Asa@67 305 type = "execute",
Asa@67 306 name = "Refresh QA Thresholds",
Asa@67 307 desc = "Resets all Quick Auctions thresholds",
Asa@67 308 func = "RefreshQAGroups",
Asa@67 309 disabled = 'IsQADisabled',
Asa@111 310 order = 15,
Asa@67 311 },
Asa@67 312 }
Asa@67 313 }
Asa@67 314
Asa@67 315 ItemAuditor.Options.args.queue = {
Asa@67 316 type = "execute",
Asa@67 317 name = "queue",
Asa@67 318 desc = "Queue",
Asa@67 319 func = "Queue",
Asa@67 320 guiHidden = true,
Asa@67 321 }