-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLCMessage.lua
More file actions
129 lines (122 loc) · 3.32 KB
/
LCMessage.lua
File metadata and controls
129 lines (122 loc) · 3.32 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
129
--
-- The main function to use this script is at the bottom - LCMessage()
--
-- e.g. LCMessage("your message","PARTY",5);
-- This will output "your message" to the /p channel, provided it hasn't been done already in the last 5 seconds
--
--
-- Initiate the cache variable
--
local LCMessageLog = {}
--
-- See if the given message exists in the cache
-- for the supplied channel type
-- (boolean)
--
local function messageExists(msg,chan)
local foundMsg = false
for c,ms in pairs(LCMessageLog) do
if(c == chan) then
for k,v in pairs(ms) do
if(v.msg == msg) then foundMsg = true end
end
end
end
if(foundMsg==true) then return true end
return false
end
--
-- Return the array of data for the given message
-- (table) if found, (nil) if not
--
local function getMessageData(msg,chan)
for k,v in pairs(LCMessageLog[chan]) do
if(v.msg == msg) then return v end
end
return nil
end
--
-- Ensure a key exists in the cache for the supplied channel
--
local function createChanTable(chan)
local foundChan = false
for c,ms in pairs(LCMessageLog) do
if(c == chan) then
foundChan = true
end
end
if(foundChan==false) then LCMessageLog[chan] = {} end
end
local usableChans = {
PARTY = 1
,SAY = 1
,INSTANCE_CHAT = 1
,RAID = 1
,PRINT = 1
};
--
-- Get the channel to use
--
local function getChanName(chan)
chan = chan or 'nil';
if(chan ~= 'nil' and usableChans[chan] == nil) then chan = 'nil'; end
if(chan == 'nil') then
if(IsInRaid()) then
if(LCcfg.get('raidChat')=='SAY') then chan = 'SAY'; end
if(LCcfg.get('raidChat')=='YELL') then chan = 'YELL'; end
if(LCcfg.get('raidChat')=='PARTY' and IsInGroup()) then chan = 'PARTY'; end
if(LCcfg.get('raidChat')=='RAID') then chan = 'RAID'; end
elseif(IsInGroup()) then
if(LCcfg.get('instanceChat')=='SAY') then chan = 'SAY'; end
if(LCcfg.get('instanceChat')=='YELL') then chan = 'YELL'; end
if(LCcfg.get('instanceChat')=='PARTY' and IsInGroup()) then chan = 'PARTY'; end
if(LCcfg.get('instanceChat')=='INSTANCE_CHAT') then chan = 'INSTANCE_CHAT'; end
else
if(LCcfg.get('instanceChat')=='SAY') then chan = 'SAY'; end
if(LCcfg.get('instanceChat')=='YELL') then chan = 'YELL'; end
end
if(IsInGroup() and LCU.player.inInstance==nil) then chan = 'PARTY' end
end
return LCU.str(chan);
end
--
-- Output the message
-- then update cache
--
local function postMsg(msg,chan)
chan = getChanName(chan);
createChanTable(chan);
if(chan=='nil' or chan=='PRINT') then print(msg)
else
if (LCU.player.inInstance==nil or LCU.player.inInstance==false) and (chan==nil or chan=='SAY' or chan=='YELL' or chan=='CHANNEL') then
print(chan, 'not allowed: ', msg)
else
SendChatMessage(msg,chan)
end
end
local msgFound = false
for k,v in pairs(LCMessageLog[chan]) do
if(v.msg == msg) then
v.time = GetTime()
msgFound = true
end
end
if(msgFound == false) then
LCMessageLog[chan][#LCMessageLog[chan]+1] = {['msg']=msg,['time']=GetTime()}
end
end
--
-- Main function to be called externally
--
function LCMessage(msg,chan,minTimeBetweenCalls)
minTimeBetweenCalls = minTimeBetweenCalls or 1.5
chan = getChanName(chan);
local alreadyPosted = messageExists(msg,chan)
if(alreadyPosted==false) then
postMsg(msg,chan)
else
local msgD = getMessageData(msg,chan)
local tDiff = GetTime() - msgD.time;
if(tDiff > minTimeBetweenCalls) then postMsg(msg,chan) end
end
end