-
Notifications
You must be signed in to change notification settings - Fork 5
/
ProfessionMaster.lua
376 lines (315 loc) · 11.3 KB
/
ProfessionMaster.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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
--[[
@author Esperanza - Everlook/EU-Alliance
@copyright ©2021 Profession Master Authors. All Rights Reserved.
Licensed under the GNU General Public License, Version 3.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.gnu.org/licenses/gpl-3.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS-IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
--]]
-- define addon name
local addonName = "Profession Master";
local addonVersion = GetAddOnMetadata("ProfessionMaster", "version");
local addonShortcut = "|cffDA8CFF[PM]|r ";
-- define addon
ProfessionMasterAddon = {};
ProfessionMasterAddon.__index = ProfessionMasterAddon;
-- prepare storage
if (not OwnProfessions) then OwnProfessions = {}; end
if (not Professions) then Professions = {}; end
if (not SyncTimes) then SyncTimes = {}; end
if (not Logs) then Logs = {}; end
if (not CharacterSets) then CharacterSets = {}; end
if (not BucketList) then BucketList = {}; end
if (not CharacterSettings) then CharacterSettings = {}; end
if (not Guildmates) then Guildmates = {}; end
--- Create new addon container.
function ProfessionMasterAddon:Create()
-- get wow build
local wowBuild = GetBuildInfo();
-- create addon and add empty holders
local addon = {
name = addonName,
version = addonVersion,
shortcut = addonShortcut,
debug = false,
trace = false,
frame = CreateFrame("Frame"),
logLevel = 0,
loaded = false,
isEra = string.find(wowBuild, "1.") == 1
};
setmetatable(addon, ProfessionMasterAddon);
-- clear types
addon.serviceTypes = {};
addon.services = {};
addon.viewTypes = {};
addon.modelTypes = {};
-- register events
addon:RegisterEvents();
return addon;
end
--- Register service.
-- @param ServiceType Service type definition.
-- @param name Name of service.
function ProfessionMasterAddon:RegisterService(ServiceType, name)
-- add to service types
self.serviceTypes[name] = ServiceType;
end
--- Check settings.
function ProfessionMasterAddon:CheckSettings()
-- check settings
if (not PMSettings) then
PMSettings = {};
end
if (not PMSettings.storageId) then
PMSettings.storageId = self:GenerateString(12);
end
if (not PMSettings.minimapButton) then
PMSettings.minimapButton = {
hide = false
};
end
end
--- Register view.
-- @param ViewType View type definition.
-- @param name Name of view.
function ProfessionMasterAddon:RegisterView(ViewType, name)
-- add to view types
self.viewTypes[name] = ViewType;
end
--- Register model.
-- @param ModelType Model type definition.
-- @param name Name of model.
function ProfessionMasterAddon:RegisterModel(ModelType, name)
-- add to model types
self.modelTypes[name] = ModelType;
end
--- Get service.
-- @param name Name of service.
function ProfessionMasterAddon:GetService(name)
-- check if service created
if (self.services[name] == nil) then
-- create service
local service = {};
setmetatable(service, self.serviceTypes[name]);
self.services[name] = service;
-- initialize service
service:Initialize();
end
-- get service
return self.services[name];
end
--- Create view.
-- @param name Name of view.
function ProfessionMasterAddon:CreateView(name)
-- get to view type
local viewType = self.viewTypes[name];
-- create view
local view = {};
setmetatable(view, viewType);
return view;
end
--- Get model.
-- @param name Name of model.
function ProfessionMasterAddon:GetModel(name)
-- get model type
return self.modelTypes[name];
end
--- Toggle debug flag.
function ProfessionMasterAddon:ToggleDebug()
if (self.logLevel == 1) then
self.logLevel = 0;
self:GetService("chat"):WriteBare("Debug-Mode disabled.");
elseif (self.logLevel == 0) then
self.logLevel = 1;
self:GetService("chat"):WriteBare("Debug-Mode enabled.");
end
end
--- Toggle trace flag.
function ProfessionMasterAddon:ToggleTrace()
if (self.logLevel == 2) then
self.logLevel = 0;
self:GetService("chat"):WriteBare("Trace-Mode disabled.");
elseif (self.logLevel == 0) then
self.logLevel = 2;
self:GetService("chat"):WriteBare("Trace-Mode enabled.");
end
end
--- Log debug message.
-- @param class Name of class to log within.
-- @param method Name of method to log within.
-- @param message Log message.
function ProfessionMasterAddon:LogDebug(class, method, message, ...)
-- check if trace not enabled
if (self.logLevel < 1) then
return;
end
-- add to logs
table.insert(Logs, date("%Y-%m-%dT%H-%M-%S") .. " " .. class .. ":" .. method .. " [Debug] " .. string.format(message, ...));
-- print out trace
--print("[Debug] " .. class .. ":" .. method .. " - " .. string.format(message, ...));
end
--- Log trace message.
-- @param class Name of class to log within.
-- @param method Name of method to log within.
-- @param message Log message.
function ProfessionMasterAddon:LogTrace(class, method, message, ...)
-- check if trace not enabled
if (self.logLevel < 2) then
return;
end
-- add to logs
table.insert(Logs, date("%Y-%m-%dT%H-%M-%S") .. " " .. class .. ":" .. method .. " [Trace] " .. string.format(message, ...));
-- print out trace
-- print("[Trace] " .. class .. ":" .. method .. " - " .. string.format(message, ...));
end
--- Register addon events.
function ProfessionMasterAddon:RegisterEvents()
-- check if russian
if (GetLocale() == "ruRU") then
print(addonShortcut .. "Авторы аддона оставляют за собой право не поддерживать игроков из стран, которые ведут себя мизантропически.");
end
-- register events
self.frame:RegisterEvent("ADDON_LOADED");
self.frame:RegisterEvent("CHAT_MSG_ADDON");
self.frame:RegisterEvent("PLAYER_LOGIN");
self.frame:RegisterEvent("TRADE_SKILL_UPDATE");
self.frame:RegisterEvent("CRAFT_UPDATE");
self.frame:RegisterEvent("GUILD_ROSTER_UPDATE");
self.frame:RegisterEvent("PLAYER_REGEN_DISABLED");
self.frame:RegisterEvent("PLAYER_REGEN_ENABLED");
self.frame:RegisterEvent("BAG_UPDATE");
-- handle on event
self.frame:SetScript("OnEvent", function(_self, event, prefix, message, channel, sender)
-- handle chat messaage
if (event == "ADDON_LOADED" and prefix == "ProfessionMaster") then
-- startup
self:CheckSettings();
-- handle chat messaage
elseif (event == "CHAT_MSG_ADDON") then
-- startup
self:GetService("message"):HandleMessage(prefix, message, sender);
-- handle login
elseif (event == "PLAYER_LOGIN") then
-- unregister event
_self:UnregisterEvent("PLAYER_LOGIN")
-- crete professions view
self.professionsView = self:CreateView("professions");
-- migrate data
self:Migrate();
-- create minimap icon
self:GetService("ui"):CreateMinimapIcon();
-- watch tooltip
self:GetService("tooltip"):WatchTooltip();
-- startup
self:GetService("timer"):Wait("PlayerLogin", 10, function()
-- show loaded message
self:GetService("chat"):Write("AddonLoaded");
-- broadcast version
self:GetService("message"):SendToGuild(self:GetModel("version-broadcast-message"):Create());
-- check welcome
self:GetService("own-professions"):CheckWelcome();
-- say hello to guild
self:GetService("professions"):SayHelloToGuild();
-- check missing reagents
self:GetService("inventory"):CheckMissingReagents();
end);
-- handle trade skill update
elseif (event == "TRADE_SKILL_UPDATE") then
self:GetService("own-professions"):GetTradeSkillProfessionData();
-- handle craft update
elseif (event == "GUILD_ROSTER_UPDATE") then
self:GetService("player"):RefreshGuildmates();
-- handle combat enter
elseif (event == "PLAYER_REGEN_DISABLED") then
self.inCombat = true;
if (self.professionsView.visible) then
self.professionsView:Hide();
end
-- handle combat leave
elseif (event == "PLAYER_REGEN_ENABLED") then
self.inCombat = nil;
-- handle bag update
elseif (event == "BAG_UPDATE") then
self:GetService("inventory"):CheckMissingReagents();
end
end);
end
-- prepare charset
local idCharset = {};
for i = 48, 57 do
table.insert(idCharset, string.char(i))
end
for i = 65, 90 do
table.insert(idCharset, string.char(i))
end
for i = 97, 122 do
table.insert(idCharset, string.char(i))
end
-- Generate random string.
function ProfessionMasterAddon:GenerateString(length)
-- build id
local id = {};
for i = 1, length do
table.insert(id, idCharset[math.random(1, #idCharset)]);
end
return table.concat(id, "");
end
-- Migrate data.
function ProfessionMasterAddon:Migrate()
-- check data version
if (PMSettings.storeVersion and PMSettings.storeVersion < 3) then
-- clear data
Professions = {};
OwnProfessions = {};
SyncTimes = {};
CharacterSettings = {};
PMSettings = {};
self:CheckSettings();
end
-- check data version
if (PMSettings.storeVersion and PMSettings.storeVersion < 5) then
-- check all professions
for professionId, profession in pairs(Professions) do
-- prepare valid skills
local validSkills = {};
-- iterate skills
for skillId, skill in pairs(profession) do
-- check skill
if (skill.name ~= nil and skill.itemId ~= nil) then
validSkills[skillId] = skill;
end
end
-- store valid skills
Professions[professionId] = validSkills;
end
-- check own professions
for characterName, professions in pairs(OwnProfessions) do
-- iterate all professions
for professionId, skills in pairs(professions) do
-- prepare valid skills
local validSkills = {};
-- iterate all skills
for skillIndex = 1, #skills do
-- get skill
local skill = skills[skillIndex];
-- check skill
if (skill.skillId ~= nil and skill.itemId ~= nil) then
table.insert(validSkills, skill);
end
end
-- store valid skills
professions[professionId] = validSkills;
end
end
end
-- set store version
PMSettings.storeVersion = 5;
end
-- create addon
_G.professionMaster = ProfessionMasterAddon:Create();