-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProfessions.lua
More file actions
130 lines (112 loc) · 4.59 KB
/
Copy pathProfessions.lua
File metadata and controls
130 lines (112 loc) · 4.59 KB
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
local ADDON_PREFIX = "YYProfessions"
local AceComm = LibStub("AceComm-3.0")
local AceSerializer = LibStub:GetLibrary("AceSerializer-3.0")
local function getProfessionsAndSkill()
local validName = {
["Engineering"] = true,
["Enchanting"] = true,
["Alchemy"] = true,
["Blacksmithing"] = true,
["Find Herbs"] = true,
["Leatherworking"] = true,
["Find Minerals"] = true,
["Skinning"] = true,
["Tailoring"] = true,
-- ["Fishing"] = true,
-- ["First Aid"] = true,
-- ["Cooking"] = true,
}
-- Mapping table for name replacements
local nameReplacement = {
["Find Herbs"] = "Herbalism",
["Find Minerals"] = "Mining",
}
local professions = {}
-- Get all professions learned on character
for i = 1, GetNumSpellTabs() do
local offset, numSlots = select(3, GetSpellTabInfo(i))
for j = offset+1, offset+numSlots do
local spellName, spellSubName, spellID = GetSpellBookItemName(j, BOOKTYPE_SPELL)
if validName[spellName] then
local displayName = nameReplacement[spellName] or spellName
table.insert(professions, {displayName, spellSubName, spellID})
end
end
end
return professions
end
local function SerializeProfessionData()
if YippYappGuildTools_ProfessionsDB then
local serializedString = AceSerializer:Serialize(YippYappGuildTools_ProfessionsDB)
return serializedString
else
print(string.format(localeTable.failedToSerialize))
return nil
end
end
local function SendProfessionDataToGuild()
local serializedData = SerializeProfessionData()
if serializedData then
AceComm:SendCommMessage(ADDON_PREFIX, serializedData, "GUILD")
end
end
function RequestLatestProfessionData()
AceComm:SendCommMessage(ADDON_PREFIX, "request", "GUILD")
end
function InitializeProfessionsFeature(YippYappGuildTools_ProfessionsDB)
local characterInfo = GetCharacterInfo()
local professionInfo = getProfessionsAndSkill()
-- Character name acts as a unique key
local characterName = characterInfo.name
-- Check if the character already exists in YippYappGuildTools_ProfessionsDB
if YippYappGuildTools_ProfessionsDB[characterName] then
-- Update existing character data
YippYappGuildTools_ProfessionsDB[characterName].professions = professionInfo
YippYappGuildTools_ProfessionsDB[characterName].lastUpdated = characterInfo.lastUpdated
YippYappGuildTools_ProfessionsDB[characterName].level = characterInfo.level
else
-- Add new character data
YippYappGuildTools_ProfessionsDB[characterName] = {
name = characterName,
level = characterInfo.level,
class = characterInfo.class,
lastUpdated = characterInfo.lastUpdated,
professions = professionInfo,
}
end
UpdateProfessionsContent(YippYappGuildTools_ProfessionsDB)
SendProfessionDataToGuild()
end
-- Helper function to compare tables (simplified and specific for your use case)
function AreTablesEqual(table1, table2)
if #table1 ~= #table2 then return false end
for i, v in ipairs(table1) do
if table2[i] ~= v then return false end
end
return true
end
-- Registering the addon communication channel
AceComm:RegisterComm(ADDON_PREFIX, function(prefix, message, distribution, sender)
local playerName = UnitName("player")
if sender == playerName then
return -- Avoid processing messages from self
end
if message == "request" then
-- Handle the request for profession data
SendProfessionDataToGuild()
else
local success, incomingData = AceSerializer:Deserialize(message)
if success then
for characterName, professionInfo in pairs(incomingData) do
-- If the character does not exist in the local DB, or the incoming data is more recent, update it
if not YippYappGuildTools_ProfessionsDB[characterName] or (YippYappGuildTools_ProfessionsDB[characterName].lastUpdated < professionInfo.lastUpdated) then
YippYappGuildTools_ProfessionsDB[characterName] = professionInfo
end
end
-- UpdateProfessionsContent
UpdateProfessionsContent(YippYappGuildTools_ProfessionsDB)
else
print(string.format(localeTable.failedToDeserialize, sender))
end
end
end)