-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.lua
More file actions
executable file
·154 lines (142 loc) · 4.84 KB
/
functions.lua
File metadata and controls
executable file
·154 lines (142 loc) · 4.84 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
local ADDON_NAME, ns = ...
local L = ns.L
local sounds = ns.data.sounds
local soundTypeAliases = ns.data.soundTypeAliases
local specialSoundTypes = ns.data.specialSoundTypes
local channels = {"PARTY", "RAID", "INSTANCE", "BATTLEGROUND", "GUILD", "OFFICER"}
function ns:SetOptionDefaults()
PHA_options = PHA_options or {}
for option, default in pairs(ns.data.defaults) do
ns:SetOptionDefault(PHA_options, option, default)
end
end
function ns:Trim(value)
local a = value:match("^%s*()")
local b = value:match("()%s*$", a)
return value:sub(a, b - 1)
end
function ns:Capitalize(value)
return value:gsub("(%l)(%w*)", function(first, rest)
return first:upper() .. rest:lower()
end)
end
function ns:GetRace()
local _, raceName, _ = UnitRace("player")
return raceName:lower():gsub("%s+", "")
end
function ns:GetGender()
local gender = UnitSex("player")
-- because this is for getting voice files, we assume female if not male
return gender == 2 and "male" or "female"
end
function ns:GetChannel()
local partyMembers = GetNumSubgroupMembers()
local raidMembers = IsInRaid() and GetNumGroupMembers() or 0
if IsInInstance() then
return "INSTANCE"
elseif raidMembers > 1 then
return "RAID"
elseif partyMembers > 0 then
return "PARTY"
end
return nil
end
function ns:GetSoundMatch(soundType)
-- Exact match
for index = 1, #sounds do
local sound = sounds[index]
if sound.type == soundType then
return sound.type
end
end
-- Alias exact match
if soundTypeAliases[soundType] then
return soundTypeAliases[soundType]
end
-- Fuzzy match
for index = 1, #sounds do
local sound = sounds[index]
if sound.type:match(soundType) or soundType:match(sound.type) then
return sound.type
end
end
-- Alias fuzzy match
for alias, sound in pairs(soundTypeAliases) do
if alias:match(soundType) or soundType:match(alias) then
return sound
end
end
return nil
end
function ns:GetSoundTypeIndex(soundType)
for index = 1, #sounds do
local sound = sounds[index]
if sound.type == soundType then
return index
end
end
return nil
end
function ns:SendSound(soundType, channel, target)
if not ns.data.toggles.sentSound then
ns:Toggle("sentSound", ns.data.timeout)
local soundsIndex = ns:GetSoundTypeIndex(soundType)
local sound = sounds[soundsIndex]
local soundType = sound.type
if specialSoundTypes[soundType] then
soundType = soundType .. ":" .. ns.race .. ":" .. ns.gender
end
local response = C_ChatInfo.SendAddonMessage(ns.name, soundType, channel, target)
if ns:OptionValue(PHA_options, "allowActions") and sound.action ~= nil then
sound.action()
end
return
end
local timeRemaining = math.max(ns.data.toggles.sentSound - GetServerTime(), 1)
ns:PrettyPrint(L.SendWarning:format(timeRemaining, timeRemaining ~= 1 and "s" or ""))
end
function ns:ReceivedSound(receivedType, sender, channel)
local soundType, race, gender = strsplit(":", receivedType)
local soundsIndex = ns:GetSoundTypeIndex(soundType)
local sound = sounds[soundsIndex]
local soundID = sound.id
if specialSoundTypes[sound.type] then
soundID = specialSoundTypes[sound.type][race] and specialSoundTypes[sound.type][race][gender] or specialSoundTypes[sound.type][gender]
end
ns:PlaySound(PHA_options, type(soundID) == "function" and soundID() or soundID, ns:OptionValue(PHA_options, "soundChannel"))
if ns:OptionValue(PHA_options, "allowReactions") and ns.characterName ~= sender and sound.reaction ~= nil then
sound.reaction()
end
C_GamePad.SetVibration("Low", 0.2)
ns:PrettyPrint(L.Received:format(sender, sound.type, ns:OptionValue(PHA_options, "sound") and "" or " (" .. _G.MUTED:lower() .. ")", channel == "WHISPER" and "you" or "the " .. channel:lower()))
end
function ns:ProcessSound(message)
local channel = ns:GetChannel()
local soundType = ns:OptionValue(PHA_options, "defaultSound")
local target
local a, b = strsplit(" ", message)
if b and b ~= "" then
soundType = ns:GetSoundMatch(a:lower())
if ns:Contains(channels, b:upper()) then
channel = b:upper()
else
target = b
channel = "WHISPER"
end
elseif a and a ~= "" then
local sound = ns:GetSoundMatch(a:lower())
if sound then
soundType = sound
elseif ns:Contains(channels, a:upper()) then
channel = a:upper()
else
target = a
channel = "WHISPER"
end
end
if channel then
ns:SendSound(soundType, channel, target)
else
ns:PrettyPrint(L.SendFailed)
end
end