-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.lua
153 lines (129 loc) · 6.99 KB
/
main.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
KeystoneCompanion.loaded = false;
local print, colorise, devPrint = KeystoneCompanion.print, KeystoneCompanion.colorise, KeystoneCompanion.dev.print
local LibSerialize = LibStub:GetLibrary("LibSerialize");
local LibDataBroker = LibStub:GetLibrary("LibDataBroker-1.1");
local LibDBIcon = LibStub:GetLibrary("LibDBIcon-1.0")
local function ToggleUI()
if(KeystoneCompanion.UI.Frame:IsShown()) then
KeystoneCompanion.UI.Frame:Hide();
else
KeystoneCompanion.UI.Rerender();
KeystoneCompanion.UI.Frame:Show();
end
end
local dataBrokerObj = LibDataBroker:NewDataObject('Keystone Companion', {
type = 'launcher',
icon = 'Interface/AddOns/Keystone-Companion/assets/textures/addon-icon',
OnClick = function() ToggleUI() end,
OnTooltipShow = function(tooltip)
tooltip:AddLine("Keystone Companion", 1, 1, 1)
tooltip:AddLine("Click to open an overview of your party's keystones and dungeon items.", nil, nil, nil, true)
end
});
local function InitDataBrokerIcon()
KeystoneCompanionDB.libDBIcon = KeystoneCompanionDB.libDBIcon or {hide = false};
if(not LibDBIcon:GetMinimapButton('Keystone Companion')) then
LibDBIcon:Register('Keystone Companion', dataBrokerObj, KeystoneCompanionDB.libDBIcon);
end
end
KeystoneCompanion.EventFrame = CreateFrame('Frame', 'KeystoneCompanionEventFrame')
KeystoneCompanion.EventFrame:RegisterEvent('PLAYER_ENTERING_WORLD');
KeystoneCompanion.EventFrame:RegisterEvent('GROUP_ROSTER_UPDATE');
KeystoneCompanion.EventFrame:RegisterEvent('BAG_UPDATE');
KeystoneCompanion.EventFrame:RegisterEvent('PARTY_LEADER_CHANGED');
KeystoneCompanion.EventFrame:RegisterEvent('ROLE_CHANGED_INFORM');
KeystoneCompanion.EventFrame:SetScript('OnEvent', function(self, event, ...)
devPrint('event - ' .. event);
if event == 'PLAYER_ENTERING_WORLD' then
if (KeystoneCompanionDB.UI ~= nil) then
KeystoneCompanion.UI.Frame:SetPoint(KeystoneCompanionDB.UI.point, KeystoneCompanionDB.UI.relativeTo, KeystoneCompanionDB.UI.relativePoint, KeystoneCompanionDB.UI.offsetX, KeystoneCompanionDB.UI.offsetY)
else
KeystoneCompanion.UI.Frame:SetPoint('CENTER', UIParent, 'CENTER')
end
if(UnitInParty("player") or KeystoneCompanion.isDev()) then
devPrint('world loaded and player in party - Sending LOGON and UPDATE messages')
KeystoneCompanion.communication.SendMessage(KeystoneCompanion.communication.messageTypes.LOGON)
KeystoneCompanion.communication.SendMessage(KeystoneCompanion.communication.messageTypes.UPDATE, KeystoneCompanion.inventory:GetInventoryString())
C_Timer.NewTimer(1, function() KeystoneCompanion.communication:RequestKeystoneInfoFromLibOpenRaid() end);
end
KeystoneCompanion.inventory:ScanInventory();
KeystoneCompanion.loaded = true
if (KeystoneCompanion.isDev()) then
KeystoneCompanion.UI.Frame:Show();
KeystoneCompanion.UI.Rerender();
end
InitDataBrokerIcon();
elseif event == 'GROUP_ROSTER_UPDATE' and KeystoneCompanion.loaded == true then
local playersByName = {};
local homePartyPlayers = GetHomePartyInfo();
local numExistingPlayersInInventory = 0;
if(homePartyPlayers ~= nil) then
for _, v in ipairs(homePartyPlayers) do playersByName[v] = true end
end
for key, value in pairs(KeystoneCompanion.inventory) do
if(type(value) == "table" and key ~= 'self' and playersByName[key] == nil) then
KeystoneCompanion.inventory[key] = nil;
elseif(type(value) == "table" and key ~= 'self') then
numExistingPlayersInInventory = numExistingPlayersInInventory + 1;
end
end
--if we encountered no pre-existing player entries in inventory data at all, the player just joined a party,
--in which case LOGON and UPDATE messages should be sent.
if(numExistingPlayersInInventory == 0) then
KeystoneCompanion.communication.SendMessage(KeystoneCompanion.communication.messageTypes.LOGON);
KeystoneCompanion.communication.SendMessage(KeystoneCompanion.communication.messageTypes.UPDATE, KeystoneCompanion.inventory:GetInventoryString())
C_Timer.NewTimer(1, function() KeystoneCompanion.communication:RequestKeystoneInfoFromLibOpenRaid() end);
end
KeystoneCompanion.UI.Rerender();
elseif event == 'BAG_UPDATE' and KeystoneCompanion.loaded == true then
local currentInventoryData = LibSerialize:Serialize(KeystoneCompanion.inventory.self);
KeystoneCompanion.inventory:ScanInventory();
local inventoryDataAfterScan = LibSerialize:Serialize(KeystoneCompanion.inventory.self);
if(currentInventoryData ~= inventoryDataAfterScan) then
if(KeystoneCompanion.UI.Frame:IsShown()) then
KeystoneCompanion.UI.Rerender();
end
KeystoneCompanion.communication.SendMessage(KeystoneCompanion.communication.messageTypes.UPDATE, KeystoneCompanion.inventory:GetInventoryString())
end
elseif (event == 'PARTY_LEADER_CHANGED' or event == 'ROLE_CHANGED_INFORM') and KeystoneCompanion.loaded == true then
KeystoneCompanion.UI.Rerender();
end
end)
KeystoneCompanion.communication:RegisterMessageHandler(KeystoneCompanion.communication.messageTypes.LOGON, function(sender)
if(sender == UnitName('player')) then return end
devPrint('received LOGON message from ' .. sender)
KeystoneCompanion.communication.SendMessage(KeystoneCompanion.communication.messageTypes.UPDATE, KeystoneCompanion.inventory:GetInventoryString())
end)
KeystoneCompanion.communication:RegisterMessageHandler(KeystoneCompanion.communication.messageTypes.UPDATE, function(sender, _, data)
devPrint('received UPDATE message from ' .. sender)
if(sender == UnitName('player') and not KeystoneCompanion.isDev()) then return end
KeystoneCompanion.inventory:LoadString(sender, data)
if(KeystoneCompanion.UI.Frame:IsShown()) then
KeystoneCompanion.UI.Rerender();
end
end)
SLASH_KEYSTONECOMPANION1, SLASH_KEYSTONECOMPANION2 = '/keystonecompanion', '/kc';
function SlashCmdList.KEYSTONECOMPANION(msg, editBox)
local args = {}
for word in msg:gmatch('%S+') do args[#args+1] = word end
if(#args == 0) then
ToggleUI();
end
if(args[1] == 'dev') then
if(args[2] == 'on' or args[2] == 'enable') then
KeystoneCompanionDB.settings.DevMode = true;
print('Developer mode ' .. colorise('38ee45', 'enabled'));
elseif(args[2] == 'off' or args[2] == 'disables') then
KeystoneCompanionDB.settings.DevMode = false;
print('Developer mode ' .. colorise('00ffff', 'disabled'));
else
print('/kc dev [enable|on|disable|off]')
end
end
end
if(KeystoneCompanion.buildType == 'alpha') then
print('You\'re running an ' .. colorise('ff0000', 'Alpha') .. ' build of the addon. Features may be broken or only half finished in alpha versions!')
elseif(KeystoneCompanion.buildType == 'beta') then
print('You\'re running a ' .. colorise('ddca2e', 'Beta') .. ' version of the addon. Thank you for helping test, and please report any issues on the Github so they can be fixed before release. :)')
end
print('type ' .. colorise('ddca2e', '/kc') .. ' to open the KeystoneCompanion UI, or click the minimap button.')