annotate Modules/CraftingRules.lua @ 137:526036e4358f

Ticket 42 - In order to fix an issue with having multiple items that are the same base item with different enchants, all costs are now tracked against the base item instead of the exact item.
author Asa Ayers <Asa.Ayers@Gmail.com>
date Sat, 02 Oct 2010 19:21:56 -0700
parents 706b173b0fa1
children 090e466fd956
rev   line source
Asa@100 1 local ItemAuditor = select(2, ...)
Asa@100 2 local CraftingRules = ItemAuditor:NewModule("CraftingRules")
Asa@100 3 print('CraftingRules')
Asa@100 4 local Crafting = ItemAuditor:GetModule("Crafting")
Asa@100 5 local Utils = ItemAuditor:GetModule("Utils")
Asa@100 6
Asa@100 7 ItemAuditor.DB_defaults.char.rules_default_veto = false
Asa@100 8 ItemAuditor.DB_defaults.char.rules = {
Asa@100 9
Asa@100 10 }
Asa@100 11
Asa@107 12 local function mergeDefaultValues(rule)
Asa@107 13 if nil == rule.search then
Asa@107 14 rule.search = ""
Asa@107 15 end
Asa@107 16 if nil == rule.target then
Asa@107 17 rule.target = 0
Asa@107 18 end
Asa@107 19 if nil == rule.skip_singles then
Asa@107 20 rule.skip_singles = false
Asa@107 21 end
Asa@108 22 if nil == rule.bonus_queue then
Asa@108 23 rule.bonus_queue = 0
Asa@108 24 end
Asa@107 25 return rule
Asa@107 26 end
Asa@107 27
Asa@100 28 local print = function(message, ...)
Asa@100 29 ItemAuditor:Print(message, ...)
Asa@100 30 end
Asa@100 31
Asa@100 32 local Options = {
Asa@100 33 header_result = {
Asa@100 34 type = 'header',
Asa@100 35 name = 'Default Result',
Asa@100 36 order = 9,
Asa@100 37 },
Asa@100 38 veto = {
Asa@100 39 type = "toggle",
Asa@100 40 name = "Veto unknown",
Asa@100 41 desc = "Vetos any items you don't have a rule for",
Asa@100 42 get = function() return ItemAuditor.db.char.rules_default_veto end,
Asa@100 43 set = function(info, value) ItemAuditor.db.char.rules_default_veto = value end,
Asa@100 44 order = 10,
Asa@100 45 },
Asa@100 46 }
Asa@100 47
Asa@100 48 local function generateRuleOptions(name)
Asa@100 49 local opt = {
Asa@100 50 name = name,
Asa@100 51 type = 'group',
Asa@100 52 args = {
Asa@100 53 items = {
Asa@100 54 type = "input",
Asa@100 55 name = "Item(s)",
Asa@100 56 desc = "Items this rule should match. Separate items with commas.",
Asa@100 57 multiline = true,
Asa@100 58 get = function()
Asa@100 59 return ItemAuditor.db.char.rules[name].search
Asa@100 60 end,
Asa@100 61 set = function(info, value)
Asa@100 62 ItemAuditor.db.char.rules[name].search = value
Asa@100 63 end,
Asa@100 64 order = 0,
Asa@100 65 },
Asa@100 66 header_result = {
Asa@100 67 type = 'header',
Asa@100 68 name = 'Rule',
Asa@100 69 order = 9,
Asa@100 70 },
Asa@100 71 veto = {
Asa@100 72 type = "toggle",
Asa@100 73 name = "Veto",
Asa@100 74 desc = "Veto any item that matches this rule",
Asa@100 75 get = function()
Asa@100 76 return (ItemAuditor.db.char.rules[name].target == -1)
Asa@100 77 end,
Asa@100 78 set = function(info, value)
Asa@100 79 if value then
Asa@100 80 value = -1
Asa@100 81 else
Asa@100 82 value = 0
Asa@100 83 end
Asa@100 84 ItemAuditor.db.char.rules[name].target = value
Asa@100 85 end,
Asa@100 86 order = 10,
Asa@100 87 },
Asa@100 88 auction_threshold = {
Asa@100 89 type = "range",
Asa@100 90 name = "Number to craft",
Asa@100 91 desc = "",
Asa@100 92 min = 0,
Asa@100 93 max = 1000,
Asa@100 94 softMax = 50,
Asa@100 95 step = 1,
Asa@100 96 get = function() return max(0, ItemAuditor.db.char.rules[name].target) end,
Asa@100 97 set = function(info, value)
Asa@100 98 ItemAuditor.db.char.rules[name].target = value
Asa@100 99 end,
Asa@100 100 disabled = function() return ItemAuditor.db.char.rules[name].target == -1 end,
Asa@100 101 order = 11,
Asa@100 102 },
Asa@107 103 skip_singles = {
Asa@107 104 type = "toggle",
Asa@107 105 name = "Skip Singles",
Asa@107 106 desc = "If only one of the item will be crafted, skip it",
Asa@107 107 disabled = function() return ItemAuditor.db.char.rules[name].target <= 1 end,
Asa@107 108 get = function()
Asa@107 109 return ItemAuditor.db.char.rules[name].skip_singles and (ItemAuditor.db.char.rules[name].target > 1)
Asa@107 110 end,
Asa@107 111 set = function(info, value)
Asa@107 112 ItemAuditor.db.char.rules[name].skip_singles = value
Asa@107 113 end,
Asa@107 114 order = 12,
Asa@107 115 },
Asa@108 116 bonus_queue = {
Asa@108 117 type = "range",
Asa@108 118 name = "Bonus Queue",
Asa@108 119 desc = "If don't have any of an item, this number will be added to the number to craft.",
Asa@108 120 min = 0,
Asa@108 121 max = 1000,
Asa@108 122 softMax = 50,
Asa@108 123 step = 1,
Asa@108 124 get = function() return ItemAuditor.db.char.rules[name].bonus_queue end,
Asa@108 125 set = function(info, value)
Asa@108 126 ItemAuditor.db.char.rules[name].bonus_queue = value
Asa@108 127 end,
Asa@108 128 order = 13,
Asa@108 129 },
Asa@100 130 header_delete = {
Asa@100 131 type = 'header',
Asa@100 132 name = '',
Asa@100 133 order = 19,
Asa@100 134 },
Asa@100 135 header_delete = {
Asa@100 136 type = 'execute',
Asa@100 137 name = 'Delete Rule',
Asa@100 138 func = function()
Asa@100 139 ItemAuditor.db.char.rules[name] = nil
Asa@100 140 Options['rule_'..name] = nil
Asa@100 141 end,
Asa@100 142 order = 20,
Asa@100 143 },
Asa@100 144 },
Asa@100 145 }
Asa@100 146
Asa@100 147
Asa@100 148 return opt
Asa@100 149 end
Asa@100 150
Asa@100 151 --[[
Asa@100 152 This had to be separated because set refers to Options and generateRuleOptions
Asa@100 153 ]]
Asa@100 154 Options.new = {
Asa@100 155 type = "input",
Asa@100 156 name = "Create New Rule",
Asa@100 157 desc = "",
Asa@100 158 get = function()
Asa@100 159 return ""
Asa@100 160 end,
Asa@100 161 set = function(info, name)
Asa@100 162 ItemAuditor.db.char.rules[name] = {
Asa@100 163 search = name,
Asa@100 164 target = 0,
Asa@100 165 }
Asa@100 166 Options['rule_'..name] = generateRuleOptions(name)
Asa@100 167 end,
Asa@100 168 order = 0,
Asa@100 169 }
Asa@100 170
Asa@100 171 local function generateDefaultGroups()
Asa@100 172 local defaultGroups = {
Asa@100 173 ['Glyphs'] = {
Asa@100 174 search = 'Glyph of',
Asa@100 175 target = 0,
Asa@100 176 },
Asa@100 177 ['Epic Gems'] = {
Asa@100 178 search = "Cardinal Ruby, Ametrine, King's Amber, Eye of Zul, Majestic Zircon, Dreadstone",
Asa@100 179 target = 0,
Asa@100 180 },
Asa@100 181 ['Rare Gems'] = {
Asa@100 182 search = "Scarlet Ruby, Monarch Topaz, Autumn's Glow, Forest Emerald, Sky Sapphire, Twilight Opal",
Asa@100 183 target = 0,
Asa@100 184 },
Asa@100 185 }
Asa@100 186
Asa@100 187 for name, rule in pairs(defaultGroups) do
Asa@107 188 ItemAuditor.db.char.rules[name] = mergeDefaultValues({
Asa@100 189 search = rule.search,
Asa@100 190 target = rule.target,
Asa@107 191 })
Asa@100 192 Options['rule_'..name] = generateRuleOptions(name)
Asa@100 193 end
Asa@100 194 end
Asa@100 195
Asa@100 196 local rules
Asa@100 197 function CraftingRules:OnInitialize()
Asa@100 198 rules = ItemAuditor.db.char.rules
Asa@100 199 local count = 0
Asa@107 200 for name, rule in pairs(rules) do
Asa@107 201 mergeDefaultValues(rule)
Asa@100 202 Options['rule_'..name] = generateRuleOptions(name)
Asa@100 203 count = count + 1
Asa@100 204 end
Asa@100 205
Asa@100 206 if count == 0 then
Asa@100 207 generateDefaultGroups()
Asa@100 208 end
Asa@100 209 end
Asa@100 210
Asa@107 211 local function runRule(rule, itemName, itemID, data)
Asa@100 212 local searches = {strsplit(',', rule.search:upper())}
Asa@108 213 local bonus = 0
Asa@108 214 if data.count == 0 then
Asa@108 215 bonus = rule.bonus_queue
Asa@108 216 end
Asa@100 217
Asa@100 218 for _, search in pairs(searches) do
Asa@100 219 search = search:trim()
Asa@100 220
Asa@100 221 if string.find(itemName, search) ~= nil or itemID == search then
Asa@107 222 if rule.skip_singles and data.count+1 == rule.target then
Asa@107 223 return data.count
Asa@107 224 end
Asa@108 225 return rule.target + bonus
Asa@100 226 end
Asa@100 227 end
Asa@100 228 return 0
Asa@100 229 end
Asa@100 230
Asa@100 231 local function Decide(data)
Asa@100 232
Asa@100 233 local match_rule = nil
Asa@100 234 local match_num = 0
Asa@100 235
Asa@100 236 local itemName = data.name:upper()
Asa@100 237 local itemID = tostring(Utils.GetItemID(data.link))
Asa@100 238 for name, rule in pairs(rules) do
Asa@107 239 local result = runRule(rule, itemName, itemID, data)
Asa@100 240 if result == -1 then
Asa@100 241 return result, name
Asa@100 242 elseif result > match_num then
Asa@100 243 match_rule = name
Asa@100 244 match_num = result
Asa@100 245 end
Asa@100 246 end
Asa@100 247
Asa@100 248 if match_rule == nil and ItemAuditor.db.char.rules_default_veto then
Asa@100 249 return -1
Asa@100 250 end
Asa@100 251 return match_num, match_rule
Asa@100 252 end
Asa@100 253
Asa@100 254 Crafting.RegisterCraftingDecider('Crafting Rules', Decide, Options)