comparison Modules/CraftingRules.lua @ 100:e6292f1a0cf3

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