annotate Modules/CraftingRules.lua @ 105:eb0493400ff0

Ticket 32 - Based on the patch from lrdx, I have chaned ItemAuditor to depend on DataStore directly instead of going through Altoholic.
author Asa Ayers <Asa.Ayers@Gmail.com>
date Mon, 23 Aug 2010 14:50:07 -0700
parents e6292f1a0cf3
children f1a013c64270
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@100 12 local print = function(message, ...)
Asa@100 13 ItemAuditor:Print(message, ...)
Asa@100 14 end
Asa@100 15
Asa@100 16 local Options = {
Asa@100 17 header_result = {
Asa@100 18 type = 'header',
Asa@100 19 name = 'Default Result',
Asa@100 20 order = 9,
Asa@100 21 },
Asa@100 22 veto = {
Asa@100 23 type = "toggle",
Asa@100 24 name = "Veto unknown",
Asa@100 25 desc = "Vetos any items you don't have a rule for",
Asa@100 26 get = function() return ItemAuditor.db.char.rules_default_veto end,
Asa@100 27 set = function(info, value) ItemAuditor.db.char.rules_default_veto = value end,
Asa@100 28 order = 10,
Asa@100 29 },
Asa@100 30 }
Asa@100 31
Asa@100 32 local function generateRuleOptions(name)
Asa@100 33 local opt = {
Asa@100 34 name = name,
Asa@100 35 type = 'group',
Asa@100 36 args = {
Asa@100 37 items = {
Asa@100 38 type = "input",
Asa@100 39 name = "Item(s)",
Asa@100 40 desc = "Items this rule should match. Separate items with commas.",
Asa@100 41 multiline = true,
Asa@100 42 get = function()
Asa@100 43 return ItemAuditor.db.char.rules[name].search
Asa@100 44 end,
Asa@100 45 set = function(info, value)
Asa@100 46 ItemAuditor.db.char.rules[name].search = value
Asa@100 47 end,
Asa@100 48 order = 0,
Asa@100 49 },
Asa@100 50 header_result = {
Asa@100 51 type = 'header',
Asa@100 52 name = 'Rule',
Asa@100 53 order = 9,
Asa@100 54 },
Asa@100 55 veto = {
Asa@100 56 type = "toggle",
Asa@100 57 name = "Veto",
Asa@100 58 desc = "Veto any item that matches this rule",
Asa@100 59 get = function()
Asa@100 60 return (ItemAuditor.db.char.rules[name].target == -1)
Asa@100 61 end,
Asa@100 62 set = function(info, value)
Asa@100 63 if value then
Asa@100 64 value = -1
Asa@100 65 else
Asa@100 66 value = 0
Asa@100 67 end
Asa@100 68 ItemAuditor.db.char.rules[name].target = value
Asa@100 69 end,
Asa@100 70 order = 10,
Asa@100 71 },
Asa@100 72 auction_threshold = {
Asa@100 73 type = "range",
Asa@100 74 name = "Number to craft",
Asa@100 75 desc = "",
Asa@100 76 min = 0,
Asa@100 77 max = 1000,
Asa@100 78 softMax = 50,
Asa@100 79 step = 1,
Asa@100 80 get = function() return max(0, ItemAuditor.db.char.rules[name].target) end,
Asa@100 81 set = function(info, value)
Asa@100 82 ItemAuditor.db.char.rules[name].target = value
Asa@100 83 end,
Asa@100 84 disabled = function() return ItemAuditor.db.char.rules[name].target == -1 end,
Asa@100 85 order = 11,
Asa@100 86 },
Asa@100 87 header_delete = {
Asa@100 88 type = 'header',
Asa@100 89 name = '',
Asa@100 90 order = 19,
Asa@100 91 },
Asa@100 92 header_delete = {
Asa@100 93 type = 'execute',
Asa@100 94 name = 'Delete Rule',
Asa@100 95 func = function()
Asa@100 96 ItemAuditor.db.char.rules[name] = nil
Asa@100 97 Options['rule_'..name] = nil
Asa@100 98 end,
Asa@100 99 order = 20,
Asa@100 100 },
Asa@100 101 },
Asa@100 102 }
Asa@100 103
Asa@100 104
Asa@100 105 return opt
Asa@100 106 end
Asa@100 107
Asa@100 108 --[[
Asa@100 109 This had to be separated because set refers to Options and generateRuleOptions
Asa@100 110 ]]
Asa@100 111 Options.new = {
Asa@100 112 type = "input",
Asa@100 113 name = "Create New Rule",
Asa@100 114 desc = "",
Asa@100 115 get = function()
Asa@100 116 return ""
Asa@100 117 end,
Asa@100 118 set = function(info, name)
Asa@100 119 ItemAuditor.db.char.rules[name] = {
Asa@100 120 search = name,
Asa@100 121 target = 0,
Asa@100 122 }
Asa@100 123 Options['rule_'..name] = generateRuleOptions(name)
Asa@100 124 end,
Asa@100 125 order = 0,
Asa@100 126 }
Asa@100 127
Asa@100 128 local function generateDefaultGroups()
Asa@100 129 local defaultGroups = {
Asa@100 130 ['Glyphs'] = {
Asa@100 131 search = 'Glyph of',
Asa@100 132 target = 0,
Asa@100 133 },
Asa@100 134 ['Epic Gems'] = {
Asa@100 135 search = "Cardinal Ruby, Ametrine, King's Amber, Eye of Zul, Majestic Zircon, Dreadstone",
Asa@100 136 target = 0,
Asa@100 137 },
Asa@100 138 ['Rare Gems'] = {
Asa@100 139 search = "Scarlet Ruby, Monarch Topaz, Autumn's Glow, Forest Emerald, Sky Sapphire, Twilight Opal",
Asa@100 140 target = 0,
Asa@100 141 },
Asa@100 142 }
Asa@100 143
Asa@100 144 for name, rule in pairs(defaultGroups) do
Asa@100 145 ItemAuditor.db.char.rules[name] = {
Asa@100 146 search = rule.search,
Asa@100 147 target = rule.target,
Asa@100 148 }
Asa@100 149 Options['rule_'..name] = generateRuleOptions(name)
Asa@100 150 end
Asa@100 151 end
Asa@100 152
Asa@100 153 local rules
Asa@100 154 function CraftingRules:OnInitialize()
Asa@100 155 rules = ItemAuditor.db.char.rules
Asa@100 156 local count = 0
Asa@100 157 for name, _ in pairs(rules) do
Asa@100 158 Options['rule_'..name] = generateRuleOptions(name)
Asa@100 159 count = count + 1
Asa@100 160 end
Asa@100 161
Asa@100 162 if count == 0 then
Asa@100 163 generateDefaultGroups()
Asa@100 164 end
Asa@100 165 end
Asa@100 166
Asa@100 167 local function runRule(rule, itemName, itemID)
Asa@100 168 local searches = {strsplit(',', rule.search:upper())}
Asa@100 169
Asa@100 170 for _, search in pairs(searches) do
Asa@100 171 search = search:trim()
Asa@100 172
Asa@100 173 if string.find(itemName, search) ~= nil or itemID == search then
Asa@100 174 return rule.target
Asa@100 175 end
Asa@100 176 end
Asa@100 177 return 0
Asa@100 178 end
Asa@100 179
Asa@100 180 local function Decide(data)
Asa@100 181
Asa@100 182 local match_rule = nil
Asa@100 183 local match_num = 0
Asa@100 184
Asa@100 185 local itemName = data.name:upper()
Asa@100 186 local itemID = tostring(Utils.GetItemID(data.link))
Asa@100 187 for name, rule in pairs(rules) do
Asa@100 188 local result = runRule(rule, itemName, itemID)
Asa@100 189 if result == -1 then
Asa@100 190 return result, name
Asa@100 191 elseif result > match_num then
Asa@100 192 match_rule = name
Asa@100 193 match_num = result
Asa@100 194 end
Asa@100 195 end
Asa@100 196
Asa@100 197 if match_rule == nil and ItemAuditor.db.char.rules_default_veto then
Asa@100 198 return -1
Asa@100 199 end
Asa@100 200 return match_num, match_rule
Asa@100 201 end
Asa@100 202
Asa@100 203 Crafting.RegisterCraftingDecider('Crafting Rules', Decide, Options)