-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGuildRelay.lua
230 lines (182 loc) · 7.03 KB
/
GuildRelay.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
local COMM_PREFIX = "GuildRelay";
local COMM_ASK_FOR_MASTER = "WhoIsMaster";
local COMM_I_AM_MASTER = "IAmMaster:";
local COMM_ID_CHECK_PREFIX = "IdCheck:";
local COMM_VERSION_CHECK_PREFIX = "VersionCheck:";
local OUTDATED_VERSION_MESSAGE = "Your version of GuildRelay is outdated. GO UPDATE!"
local clientId = 0;
local relayMaster = nil;
local relayMasterId = 0;
local playerName = nil;
local relayChannelName = "GuildRelay";
local isInititalized = false;
RegisterAddonMessagePrefix(COMM_PREFIX);
local function debugMessage(message)
if guildRelayDebugMode then
DEFAULT_CHAT_FRAME:AddMessage(">GuildRelay DEBUG< " .. message, 1, 1, 1);
end
end
local function isRelayMaster()
if relayMaster == GetUnitName("player", false) then
debugMessage("This client is relay master");
return true;
else
debugMessage("This client is not relay master");
return false;
end
end
local function sendAddonBroadcast(message)
SendAddonMessage(COMM_PREFIX, message, "GUILD");
end
local function sendAddonWhisper(message, target)
SendAddonMessage(COMM_PREFIX, message, "WHISPER", target);
end
local function setDebugMode(mode)
guildRelayDebugMode = mode;
debugMessage("Set debug mode to " .. tostring(guildRelayDebugMode));
end
local function initalize()
debugMessage("Initializing GuildRelay channels");
sendAddonBroadcast(COMM_VERSION_CHECK_PREFIX .. GetAddOnMetadata("GuildRelay", "Version"))
clientId = GetTime() * 1000;
JoinTemporaryChannel(relayChannelName);
sendAddonBroadcast(COMM_ASK_FOR_MASTER);
relayMaster = GetUnitName("player", false);
relayMasterId = clientId;
isInititalized = true;
end
local function setRelayChannel(channelName)
LeaveChannelByName(relayChannelName);
relayChannelName = channelName;
JoinTemporaryChannel(relayChannelName);
debugMessage("Set relay channel to " .. relayChannelName);
end
local frame = CreateFrame("FRAME");
frame:RegisterEvent("CHAT_MSG_GUILD");
local function relayMessage(self, event, ...)
if not isInititalized then
initalize();
end
debugMessage("Recieved chat message to relay");
unitName, unitRealm = UnitName("player");
playerName = unitName .. "-" .. GetRealmName();
message, author, language, arg4, arg5, arg6, arg7, arg8, arg9, arg10, lineId, senderGuid = ...;
if isRelayMaster() then
number, name, id = GetChannelName(relayChannelName);
SendChatMessage("[" .. author .. "]: " .. message ,"CHANNEL" ,nil ,number);
end
end
frame:SetScript("OnEvent", relayMessage);
local frame1 = CreateFrame("FRAME");
frame1:RegisterEvent("PLAYER_LOGIN");
local function initiateRelay(self, event, ...)
DEFAULT_CHAT_FRAME:AddMessage("GuildRelay loaded", 1, 1, 1);
end
frame1:SetScript("OnEvent", initiateRelay);
local frame2 = CreateFrame("FRAME");
frame2:RegisterEvent("CHAT_MSG_ADDON");
local function handleComm(self, event, ...)
prefix, message, channel, sender = ...;
if prefix == COMM_PREFIX then
if message == COMM_ASK_FOR_MASTER then
debugMessage(COMM_ASK_FOR_MASTER);
if not (sender == playerName) then
debugMessage("sender: " .. sender .. " playerName: " .. playerName);
if isRelayMaster() then
sendAddonBroadcast(COMM_I_AM_MASTER .. clientId);
end
end
elseif string.sub(message, 1, string.len(COMM_I_AM_MASTER)) == COMM_I_AM_MASTER then
if not (sender == playerName) then
relayMaster = sender;
relayMasterId = tonumber(string.sub(message, string.len(COMM_I_AM_MASTER) + 1));
end
elseif string.sub(message, 1, string.len(COMM_ID_CHECK_PREFIX)) == COMM_ID_CHECK_PREFIX then
if not (sender == playerName) then
if tonumber(string.sub(message, string.len(COMM_ID_CHECK_PREFIX) + 1)) > relayMasterId then
relayMaster = sender;
relayMasterId = tonumber(string.sub(message, string.len(COMM_ID_CHECK_PREFIX) + 1));
end
end
elseif string.sub(message, 1, string.len(COMM_VERSION_CHECK_PREFIX)) == COMM_VERSION_CHECK_PREFIX then
if tonumber(string.sub(message, string.len(COMM_VERSION_CHECK_PREFIX) + 1)) > tonumber(GetAddOnMetadata("GuildRelay", "Version")) then
DEFAULT_CHAT_FRAME:AddMessage(">GuildRelay< " .. OUTDATED_VERSION_MESSAGE, 1, 0, 0);
else
debugMessage("Passed version check");
end
end
debugMessage("Addon message: " .. prefix .. message .. channel .. sender);
end
end
frame2:SetScript("OnEvent", handleComm);
local frame3 = CreateFrame("FRAME");
frame3:RegisterEvent("CHAT_MSG_CHANNEL_LEAVE");
local function onChannelEvent(self, event, ...)
debugMessage("channel event detected: " .. event);
arg1, name, arg3, channel, arg5, arg6, arg7, channelNumber, channelName = ...;
if channelName == relayChannelName then
debugMessage("Someone left the GuildRelay channel");
if name == relayMaster then
debugMessage("Relay Master left");
sendAddonBroadcast(COMM_ID_CHECK_PREFIX .. clientId);
relayMaster = GetUnitName("player", false);
relayMasterId = clientId;
end
end
end
frame3:SetScript("OnEvent", onChannelEvent);
local frame4 = CreateFrame("FRAME");
frame4:RegisterEvent("CHAT_MSG_CHANNEL");
local function onIncomingMessage(self, event, ...)
chatMessage, author, lang, channel, target, flag, zoneId, channelNumber, channelName, lineId, senderGuid = ...;
if channelName == relayChannelName then
if isRelayMaster() then
if not (string.sub(chatMessage, 1, 1) == "[") then
debugMessage("Incoming message detected, relaying...")
SendChatMessage(">GR< [" .. author .. "] " .. chatMessage, "GUILD");
end
end
end
end
frame4:SetScript("OnEvent", onIncomingMessage);
SLASH_GUILDRELAY1 = "/guildrelay";
SLASH_GUILDRELAY2 = "/gry";
function SlashCmdList.GUILDRELAY(message, editbox)
local command, rest = message:match("^(%S*)%s*(.-)$");
debugMessage("slash command detected: Command: " .. command .. " - rest: " .. rest);
if command == "setdebug" then
debugMessage("valid command: " .. command);
if rest == "true" then
setDebugMode(true);
elseif rest == "false" then
setDebugMode(false);
end
elseif command == "dump" then
debugMessage("valid command: " .. command);
DEFAULT_CHAT_FRAME:AddMessage("debugMode: " .. tostring(guildRelayDebugMode), 1, 1, 1);
DEFAULT_CHAT_FRAME:AddMessage("clientId: " .. clientId, 1, 1, 1);
DEFAULT_CHAT_FRAME:AddMessage("relayMaster: " .. tostring(relayMaster), 1, 1, 1);
DEFAULT_CHAT_FRAME:AddMessage("relayMasterId: " .. relayMasterId, 1, 1, 1);
DEFAULT_CHAT_FRAME:AddMessage("relayChannelName: " .. relayChannelName, 1, 1, 1);
DEFAULT_CHAT_FRAME:AddMessage("isInitialized: " .. tostring(isInititalized), 1, 1, 1);
DEFAULT_CHAT_FRAME:AddMessage("playerName: " .. tostring(playerName), 1, 1, 1);
else
DEFAULT_CHAT_FRAME:AddMessage("\"" .. command .. "\" - " .. "Invalid command", 1, 1, 1);
end
end
--[[
local function updateIsRelayMaster()
if not isInititalized then
initalize();
end
debugMessage("Updating relay master");
channelNumber, channelName, instanceID = GetChannelName(syncChannelName);
channelNumber = channelNumber + 3;
print(GetNumDisplayChannels());
name, owner, moderator, muted, active, enabled = GetChannelRosterInfo(channelNumber, 1);
if name == GetUnitName("player", false) then
isRelayMaster = true;
debugMessage("This client is now relay master");
end
end
--]]