-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProfessionProfessor.lua
238 lines (195 loc) · 7.13 KB
/
ProfessionProfessor.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
local isVerbose = false -- non-verbose by default
local userNotifiedOfNewVersion = false
ProfessionProfessor = LibStub("AceAddon-3.0"):NewAddon("ProfessionProfessor", "AceEvent-3.0", "AceComm-3.0", "AceConsole-3.0", "AceSerializer-3.0")
-- Create shorthand reference for less typing
prof = ProfessionProfessor
local aceGUI = LibStub("AceGUI-3.0")
local allowedTradeskills = {
"Alchemy",
"Blacksmithing",
"Enchanting",
"Engineering",
"Leatherworking",
"Tailoring",
"Jewelcrafting",
"Cooking",
"Inscription"
}
-- AceAddon :OnInitialize()
function prof:OnInitialize()
self.version = GetAddOnMetadata("ProfessionProfessor", "Version")
self.db = LibStub("AceDB-3.0"):New("ProfessionProfessorDB")
-- Set up default options
if not self.db.char.options then
self.db.char.options = {}
self.db.char.options['verbose'] = isVerbose
else
isVerbose = self.db.char.options['verbose']
end
-- Console commands
prof:RegisterChatCommand("pro", "consoleCommands")
-- Event registration
-- as or 3.0.3 Enchanting counts as a normal tradeskill
prof:RegisterEvent("TRADE_SKILL_UPDATE", "tradeSkillUpdate")
-- Version checking
prof:RegisterComm("PROFPRO_VERSION", "onCommReceived")
prof:SendCommMessage("PROFPRO_VERSION", prof.version, "GUILD")
end
local function verbosePrint(message)
if isVerbose then
prof:Print(message)
end
end
local function isTradeskillSupported(value)
for i,v in ipairs(allowedTradeskills) do
if v == value then
return true
end
end
return false
end
function prof:toggleVerbose()
if self.db.char.options and self.db.char.options['verbose'] ~= nil then
isVerbose = not self.db.char.options['verbose']
self.db.char.options['verbose'] = isVerbose
prof:Print("Turned verbose mode " .. (isVerbose and 'On' or 'Off'))
end
end
function prof:tradeSkillUpdate()
-- Add in a check if the tradeskill has been linked (we don't want to index someone elses recipes!)
local isLink, linkedPlayer = IsTradeSkillLinked()
if isLink then
verbosePrint("Recipe is a linked skill from player " .. linkedPlayer)
-- break out early
return
end
updateProfessionDB()
end
function prof:onCommReceived(prefix, payload, distribution, sender)
if prefix == "PROFPRO_VERSION" then
-- Only notify the user once of a newer version
if userNotifiedOfNewVersion then
return
end
local rMajor, rMinor, rPatch = string.match(payload, "(%d+)%.(%d+)%.(%d+)")
local cMajor, cMinor, cPatch = string.match(prof.version, "(%d+)%.(%d+)%.(%d+)")
if rMajor > cMajor or (rMinor > cMinor and rMajor == cMajor) or (rPatch > cPatch and rMinor == cMinor and rMajor == cMajor) then
prof:Print("A newer version (".. payload ..") is available, please update!")
userNotifiedOfNewVersion = true
end
end
end
function updateProfessionDB()
local localisedName = GetTradeSkillLine()
local numSkills = GetNumTradeSkills()
if not isTradeskillSupported(localisedName) then
return
end
if type(prof.db.char.professions) == "table" then
if prof.db.char.professions[localisedName] and prof.db.char.professions[localisedName]["numSkills"] >= numSkills then
verbosePrint("No new recipes found for " .. localisedName)
return
end
else
prof.db.char.professions = {}
end
verbosePrint("Updating database for " .. localisedName)
local learnedIds = {}
local amount = 0
for i=1,numSkills do
local _,skillType = GetTradeSkillInfo(i)
-- Skip the headers, only check real skills
if skillType ~= "header" then
local itemLink, id = getTradeSkillItemId(i)
verbosePrint("Adding " .. itemLink .. ' [' .. id .. ']')
table.insert(learnedIds, id)
amount = amount + 1
end
end
if amount > 0 then
previousAmount = prof.db.char.professions[localisedName] and prof.db.char.professions[localisedName]["realNumSkills"] or 0
prof.db.char.professions[localisedName] = {
["numSkills"] = numSkills,
["realNumSkills"] = amount,
["learnedIds"] = learnedIds
}
newlyLearnedAmount = amount - previousAmount
prof:Print(newlyLearnedAmount .. " new " .. (newlyLearnedAmount > 1 and "recipes" or "recipe") .. " found for " .. localisedName)
end
end
function getTradeSkillItemId(i)
local itemLink
local returnid
itemLink = GetTradeSkillRecipeLink(i)
returnid = itemLink:match("enchant:(%d+)")
if returnid == nil then
returnid = itemLink:match("item:(%d+)")
end
return itemLink, returnid
end
function prof:consoleCommands(input)
if not input or input:trim() == "" then
if self.db.char.professions and next(self.db.char.professions) ~= nil then
showJson()
else
self:Print("Please open your professions windows to update the database, so there is something to export")
end
elseif input == "reset" then
self.db.char.professions = {}
self:Print("Data has been reset")
elseif input == "print" then
if self.db.char.professions then
local output = "You have " .. countTable(self.db.char.professions) .. " saved"
for k,v in pairs(self.db.char.professions) do
output = output .. ", " .. k .. "(" .. self.db.char.professions[k]['realNumSkills'] .. ") "
end
self:Print(output)
end
elseif input == "verbose" then
self:toggleVerbose()
end
end
function showJson()
-- Create a container frame
local f = aceGUI:Create("Frame")
f:SetCallback("OnClose",function(widget) aceGUI:Release(widget) end)
f:SetTitle("Profession Professor Skill Checker - Export -- " .. prof.version)
f:SetLayout("Flow")
local editBox = aceGUI:Create("MultiLineEditBox")
editBox:SetWidth(800)
editBox:SetHeight(500)
editBox:SetText(convertTableToString())
editBox:SetLabel("Copy text and paste into your discord channel with the Profession Professor bot and using the /upload slash command")
editBox:SetNumLines(20)
editBox:DisableButton(true)
f:AddChild(editBox)
end
function countTable(tableName)
local count = 0
for _ in pairs(tableName) do
count = count + 1
end
return count
end
function convertTableToString()
local name,_ = UnitName("player")
local faction,_ = UnitFactionGroup("player")
local text = name .. ";" .. faction .. ";" .. GetRealmName() .. ";"
for k,v in pairs(prof.db.char.professions) do
text = text .. k .. ";"
text = text .. getSkillString(k)
end
return text
end
function getSkillString(key)
local profession = prof.db.char.professions[key]
local realNumSkills = profession['realNumSkills']
local list = profession['learnedIds']
local text = ""
for i = 1,realNumSkills do
if list[i] ~= nil then
text = text .. list[i] .. ";"
end
end
return text
end