-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSTH_Core.lua
More file actions
138 lines (113 loc) · 3.58 KB
/
Copy pathSTH_Core.lua
File metadata and controls
138 lines (113 loc) · 3.58 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
130
131
132
133
134
135
136
137
138
SamisTrialHelperAddon = SamisTrialHelperAddon or {}
local STH = SamisTrialHelperAddon
STH.settings = {}
STH.defaults = {
enableDebug = false,
clearOnZoneChange = true,
offsetX = 100,
offsetY = 200,
timeoutDuration = 8,
warningDuration = 60,
alertDuration = 15,
warningColour = "FFAA00",
alertColour = "FF0000",
defaultTextColour = "FFFFFF",
wipeOnZoneChange = false,
debug = false,
backgroundColor = "000000",
backgroundOpacity = 0.8,
showTitle = true,
listenToSayChannel = false,
listenToGroupChannel = true,
whisperPlayer = false,
}
STH.uncollectedItems = {}
STH.lastMessage = nil
STH.lastRequestRecord = nil
STH.pendingMessages = {}
function STH:RemoveUncollectedItemRecord(record)
if not record then return end
STH.uncollectedItems[record.playerName] = nil
end
function STH:CreateUncollectedItemRecord(fromDisplayName, fromName, itemLinks)
return {
playerName = fromDisplayName or fromName,
itemLinks = itemLinks,
requested = false,
}
end
local function buildMessageChunks(uncollectedItems, fromDisplayName)
local MAX_LENGTH = 350
local suffix = ""
if STH.settings.whisperPlayer then
prefix = string.format("/w %s ", fromDisplayName)
suffix = string.format(". [SamisTrialHelper]")
chatChannel = CHAT_CHANNEL_WHISPER
else
prefix = "/g "
suffix = string.format(" from %s. [SamisTrialHelper]", fromDisplayName)
chatChannel = CHAT_CHANNEL_PARTY
end
local available = MAX_LENGTH - #"I need these items: " - #suffix
local chunks = {}
local currentItems = {}
local currentLen = 0
for _, link in ipairs(uncollectedItems) do
local addLen = (#currentItems > 0 and 2 or 0) + #link
if currentLen + addLen <= available then
table.insert(currentItems, link)
currentLen = currentLen + addLen
else
table.insert(chunks, table.concat(currentItems, ", "))
currentItems = { link }
currentLen = #link
end
end
if #currentItems > 0 then
table.insert(chunks, table.concat(currentItems, ", "))
end
local messages = {}
for _, itemStr in ipairs(chunks) do
table.insert(messages, string.format("%s%s%s", prefix,
itemStr, suffix))
end
return messages
end
local function onChannelMessageSent(_, _, _, _, _, fromDisplayName)
if fromDisplayName ~= GetDisplayName() then return end
if #STH.pendingMessages == 0 then
EVENT_MANAGER:UnregisterForEvent("SamisTrialHelper_MessageQueue", EVENT_CHAT_MESSAGE_CHANNEL)
return
end
local next = table.remove(STH.pendingMessages, 1)
CHAT_SYSTEM:StartTextEntry(next.text, next.channel)
if #STH.pendingMessages == 0 then
EVENT_MANAGER:UnregisterForEvent("SamisTrialHelper_MessageQueue", EVENT_CHAT_MESSAGE_CHANNEL)
end
end
function STH:SendGroupMessage(record)
if not record.requested then
local fromDisplayName = record.playerName
local uncollectedItems = record.itemLinks
local messages = buildMessageChunks(uncollectedItems, fromDisplayName)
STH.lastMessage = messages[1]
STH.lastRequestRecord = record
local chatChannel
if STH.settings.whisperPlayer then
chatChannel = CHAT_CHANNEL_WHISPER
else
chatChannel = CHAT_CHANNEL_PARTY
end
STH.pendingMessages = {}
for i = 2, #messages do
table.insert(STH.pendingMessages, {
text = string.format("%s", messages[i]),
channel = chatChannel,
})
end
if #STH.pendingMessages > 0 then
EVENT_MANAGER:RegisterForEvent("SamisTrialHelper_MessageQueue", EVENT_CHAT_MESSAGE_CHANNEL, onChannelMessageSent)
end
CHAT_SYSTEM:StartTextEntry(string.format("%s", messages[1]), chatChannel)
end
end