IVR Script in lua - lua

I've created an ivr script in lua. In this script goto statement is not working. Please someone tell me what is the problem in this code. If I erase ::chandu:: and elseif (digits == "0") then goto chandu then it is running fine. but adding goto statement call is not connecting or I can say script is not running.
.
i = 0
j = 0
while (session:ready() == true) do
session:setAutoHangup(false);
repeat
::chandu::
digits = session:playAndGetDigits(1, 3, 3, 3000, "#", "/var/sipxdata/mediaserver/data/ivrscript/sampleLua/prompts/coral.wav", "/var/sipxdata/mediaserver/data/ivrscript/sampleLua/prompts/coral.wav", "\\d+");
if (digits == "1") then
repeat
digits = session:playAndGetDigits(1, 3, 3, 3000, "#", "/var/sipxdata/mediaserver/data/ivrscript/sampleLua/prompts/SPG_greet_long.wav","/var/sipxdata/mediaserver/data/ivrscript/sampleLua/prompts/SPG_greet_long.wav", "\\d+");
if (digits == "1") then
language = "de"
session:execute("set","transfer_ringback=$${us-ring}");
session:execute("bridge","sofia/coraltele.com/819#192.168.22.1");
session:hangup();
elseif (digits == "2") then
language = "de"
session:execute("set","transfer_ringback=$${us-ring}");
session:execute("bridge","sofia/coraltele.com/826#192.168.22.1");
session:hangup();
elseif (digits == "9") then
i = i + 1
elseif (digits == "0") then
goto chandu
else
language = "de"
session:execute("set","transfer_ringback=$${us-ring}");
session:execute("bridge","sofia/coraltele.com/898#192.168.22.1");
session:hangup();
end
until i == 3
elseif(digits == "0") then
j = j + 1
else
language = "de"
session:execute("set","transfer_ringback=$${us-ring}");
session:execute("bridge","sofia/coraltele.com/898#192.168.1.240");
session:hangup();
end
until j == 2
language = "de"
session:execute("set","transfer_ringback=$${us-ring}");
session:execute("bridge","sofia/coraltele.com/898#192.168.1.240");
session:hangup();
end

Finally I've created a script having completely working now.
i = 0
j = 0
while (session:ready() == true) do
session:setAutoHangup(false);
repeat
digits = session:playAndGetDigits(1, 3, 3, 3000, "#", "/var/sipxdata/mediaserver/data/ivrscript/sampleLua/prompts/coral.wav", "shout://translate.google.com/translate_tts?tl=en&q=This+option+is+not+valid", "\d+");
if (digits == "1") then
repeat
digits = session:playAndGetDigits(1, 3, 3, 3000, "#", "/var/sipxdata/mediaserver/data/ivrscript/sampleLua/prompts/corporate_greet_long.wav","shout://translate.google.com/translate_tts?tl=en&q=This+option+is+not+valid", "\\d+");
if (digits == "1") then
language = "de"
session:execute("set","transfer_ringback=$${us-ring}");
session:execute("bridge","sofia/coraltele.com/819#192.168.1.240");
session:hangup();
destroy(0);
elseif (digits == "2") then
language = "de"
session:execute("set","transfer_ringback=$${us-ring}");
session:execute("bridge","sofia/coraltele.com/826#192.168.1.240");
session:hangup();
destroy(0);
elseif (digits == "9") then
i = i + 1
elseif(digits == "0") then
i = 0
j = 0
break
else
language = "de"
session:execute("set","transfer_ringback=$${us-ring}");
session:execute("bridge","sofia/coraltele.com/898#192.168.1.240");
session:hangup();
destroy(0); --Destroys the session and releases resources.
--This is done for you when your script ends,
--but if your script contains an infinite loop
--you can use this to terminate the session.
end
until i == 3
elseif (digits == "2") then
repeat
digits = session:playAndGetDigits(1, 3, 3, 3000, "#", "/var/sipxdata/mediaserver/data/ivrscript/sampleLua/prompts/support_greet_long.wav","shout://translate.google.com/translate_tts?tl=en&q=This+option+is+not+valid", "\\d+");
if (digits == "1") then
language = "de"
session:execute("set","transfer_ringback=$${us-ring}");
session:execute("bridge","sofia/coraltele.com/825#192.168.1.240");
session:hangup();
destroy(0);
elseif (digits == "2") then
language = "de"
session:execute("set","transfer_ringback=$${us-ring}");
session:execute("bridge","sofia/coraltele.com/830#192.168.1.240");
session:hangup();
destroy(0);
elseif(digits == "9") then
i = i + 1
elseif(digits == "0") then
i = 0
j = 0
break
else
language = "de"
session:execute("set","transfer_ringback=$${us-ring}");
session:execute("bridge","sofia/coraltele.com/898#192.168.1.240");
session:hangup();
destroy(0);
end
until i == 3
elseif (digits == "3") then
repeat
digits = session:playAndGetDigits(1, 3, 3, 3000, "#", "/var/sipxdata/mediaserver/data/ivrscript/sampleLua/prompts/SPG_greet_long.wav","shout://translate.google.com/translate_tts?tl=en&q=This+option+is+not+valid", "\\d+");
if (digits == "1") then
language = "de"
session:execute("set","transfer_ringback=$${us-ring}");
session:execute("bridge","sofia/coraltele.com/808#192.168.1.240");
session:hangup();
destroy(0);
elseif (digits == "2") then
language = "de"
session:execute("set","transfer_ringback=$${us-ring}");
session:execute("bridge","sofia/coraltele.com/878#192.168.1.240");
session:hangup();
destroy(0);
elseif (digits == "9") then
i = i + 1
elseif(digits == "0") then
i = 0
j = 0
break
else
language = "de"
session:execute("set","transfer_ringback=$${us-ring}");
session:execute("bridge","sofia/coraltele.com/898#192.168.1.240");
session:hangup();
destroy(0);
end
until i == 3
elseif (digits == "4") then
repeat
digits = session:playAndGetDigits(1, 3, 3, 3000, "#", "/var/sipxdata/mediaserver/data/ivrscript/sampleLua/prompts/SPG_greet_long.wav","shout://translate.google.com/translate_tts?tl=en&q=This+option+is+not+valid", "\\d+");
if (digits == "1") then
language = "de"
session:execute("set","transfer_ringback=$${us-ring}");
session:execute("bridge","sofia/coraltele.com/890#192.168.1.240");
session:hangup();
destroy(0);
elseif (digits == "2") then
language = "de"
session:execute("set","transfer_ringback=$${us-ring}");
session:execute("bridge","sofia/coraltele.com/891#192.168.1.240");
session:hangup();
destroy(0);
elseif (digits == "9") then
i = i + 1
elseif(digits == "0") then
i = 0
j = 0
break
else
language = "de"
session:execute("set","transfer_ringback=$${us-ring}");
session:execute("bridge","sofia/coraltele.com/898#192.168.1.240");
session:hangup();
destroy(0);
end
until i == 3
elseif (digits == "5") then
repeat
digits = session:playAndGetDigits(1, 3, 3, 3000, "#", "/var/sipxdata/mediaserver/data/ivrscript/sampleLua/prompts/SPG_greet_long.wav","shout://translate.google.com/translate_tts?tl=en&q=This+option+is+not+valid", "\\d+");
if (digits == "1") then
language = "de"
session:execute("set","transfer_ringback=$${us-ring}");
session:execute("bridge","sofia/coraltele.com/851#192.168.1.240");
session:hangup();
destroy(0);
elseif (digits == "2") then
language = "de"
session:execute("set","transfer_ringback=$${us-ring}");
session:execute("bridge","sofia/coraltele.com/872#192.168.1.240");
session:hangup();
destroy(0);
elseif (digits == "9") then
i = i + 1
elseif(digits == "0") then
i = 0
j = 0
break
else
language = "de"
session:execute("set","transfer_ringback=$${us-ring}");
session:execute("bridge","sofia/coraltele.com/898#192.168.1.240");
session:hangup();
destroy(0);
end
until i == 3
elseif(digits == "0") then
j = j + 1
else
language = "de"
session:execute("set","transfer_ringback=$${us-ring}");
session:execute("bridge","sofia/coraltele.com/898#192.168.1.240");
session:hangup();
destroy(0);
end
until j == 2
end

Related

Is there a simpler way than to pass a 40-object table?

I'm running these requests for numbers with five sub-cases which have four sub-cases each. There are over 200 of these cases, of which just one is shown below, so it would be several thousand lines of code. (This is example code, the numbers would not all be the same, of course.)
elseif case1 then
if u <= 0 then if w == 0 then return B(2,3), B(3,4)
elseif w == 1 then return B(2,3), B(3,4)
elseif w == 2 then return B(2,3), B(3,4)
elseif w >= 3 then return B(2,3), B(3,4)
elseif if u == 1 then return LimitCheck(if w == 0 then return B(2,3), B(3,4)
elseif w == 1 then return B(2,3), B(3,4)
elseif w == 2 then return B(2,3), B(3,4)
elseif w >= 3 then return B(2,3), B(3,4)
elseif if u == 2 then return LimitCheck(if w == 0 then return B(2,3), B(3,4)
elseif w == 1 then return B(2,3), B(3,4)
elseif w == 2 then return B(2,3), B(3,4)
elseif w >= 3 then return B(2,3), B(3,4)
elseif if u == 3 then return LimitCheck(if w == 0 then return B(2,3), B(3,4)
elseif w == 1 then return B(2,3), B(3,4)
elseif w == 2 then return B(2,3), B(3,4)
elseif w >= 3 then return B(2,3), B(3,4)
elseif if u == 4 then return LimitCheck(if w == 0 then return B(2,3), B(3,4)
elseif w == 1 then return B(2,3), B(3,4)
elseif w == 2 then return B(2,3), B(3,4)
elseif w >= 3 then return B(2,3), B(3,4)
I thought I'd be clever and compartmentalize the code that repeats, let sub-functions take apart the cases:
local function LimitCheck(arg)
if w <= 0 then return arg[1], arg[2]
elseif w == 1 then return arg[3], arg[4]
elseif w == 2 then return arg[5], arg[6]
elseif w >= 3 then return arg[7], arg[8]
end
end
local function Limits(arg)
if u == 0 then return LimitCheck({arg[1],arg[2],arg[3],arg[4],arg[5],arg[6],arg[7],arg[8],})
elseif u == 1 then return LimitCheck({arg[9],arg[10],arg[11],arg[12],arg[13],arg[14],arg[15],arg[16],})
elseif u == 2 then return LimitCheck({arg[17],arg[18],arg[19],arg[20],arg[21],arg[22],arg[23],arg[24],})
elseif u == 3 then return LimitCheck({arg[25],arg[26],arg[27],arg[28],arg[29],arg[30],arg[31],arg[32],})
elseif u == 4 then return LimitCheck({arg[33],arg[34],arg[35],arg[36],arg[37],arg[38],arg[39],arg[40]})
end
end
And then have the main function return what it needs to know in one bulk:
elseif case1 then
return Limits({B(5,6),B(7,8), B(5,6),B(5,6), B(5,6),B(5,6), B(5,6),B(5,6),
B(5,6),B(5,6), B(5,6),B(5,6), B(5,6),B(5,6), B(5,6),B(5,6),
B(5,6),B(5,6), B(5,6),B(5,6), B(5,6),B(5,6), B(5,6),B(5,6),
B(5,6),B(5,6), B(5,6),B(5,6), B(5,6),B(5,6), B(5,6),B(5,6),
B(5,6),B(5,6), B(5,6),B(5,6), B(5,6),B(5,6), B(5,6),B(5,6)})
Problem is, this is a 40-entry table object that is passed every time, quite a bulky undertaking for a request that may happen 100 times a second. And it returns a bunch of stuff that isn't used.
I wonder if there is a resource-saving way to do this.
Here's an example of how to do this using a lookup table.
You can hardcode it or build it from code if the structure allows it.
local LUT = {
[0] = {
[0] = "a",
[1] = "b",
[2] = "c",
[3] = "d",
},
[1] = {
[0] = "e",
[1] = "f",
[2] = "g",
[3] = "h",
},
[2] = {
[0] = "i",
[1] = "j",
[2] = "k",
[3] = "l",
},
[3] = {
[0] = "m",
[1] = "n",
[2] = "o",
[3] = "p",
},
[4] = {
[0] = "q",
[1] = "r",
[2] = "s",
[3] = "t",
},
}
function lookup(u, w)
-- we only accept u <= 5
if math.type(u) ~= "integer" or u > 4
-- and w >= 0
or math.type(w) ~= "integer" or w < 0 then
return
end
return LUT[math.max(0, u)][math.min(3, w)]
end
print(lookup(-5, 3))

Continue playSound() Cheat Engine Lua

In Cheat Engine, to play a wav sound file we can use playSound().
I am trying to play sound for Morse code:
test = '.... . .-.. .-.. ---/.-- --- .-. .-.. -..'
for i= 1, #test do
chr = string.sub(test, i, i)
if chr == '.' then
playSound(findTableFile("dot.wav"))
elseif chr == '-' then
playSound(findTableFile("dash.wav"))
elseif chr == 's' then
playSound(findTableFile("shortpause.wav"))
elseif chr == ' ' then
playSound(findTableFile("mediumpause.wav"))
elseif chr == '/' then
playSound(findTableFile("longpause.wav"))
end
end
But sound playing only the first 'chr'. How to play all characters 'chr' by their defined sound?.
Problem-solve:
test = '.... . .-.. .-.. ---/.-- --- .-. .-.. -..'
function playMorse()
for i= 1, #test do
chr = string.sub(test, i, i)
if chr == '.' then
playSound(findTableFile("dot.wav"))
sleep(300)
elseif chr == '-' then
playSound(findTableFile("dash.wav"))
sleep(300)
elseif chr == 's' then
playSound(findTableFile("shortpause.wav"))
sleep(300)
elseif chr == ' ' then
playSound(findTableFile("mediumpause.wav"))
sleep(300)
elseif chr == '/' then
playSound(findTableFile("longpause.wav"))
sleep(300)
end
end
end
playMorse()

Need Addon help fixing Name Detection in Chat

The following code is suppose to change a user name say Player in any text example: '[Player2]: Hi Player' into '[Player2]: Hi >Player<' with Player in red and >< symbols in yellow. Problem is its not detecting other players saying it and is just plain coloring them by class and warning when I say my name when it should be the other way around.
I understand there is likely Global Leaks and such as this is a beta phase addon at the moment and I am constantly working on rewriting functions to make them better but for now I am trying to get everything working first. If you can simplify the Name detection and change it without messing with links in WoW I would take the advice and I will throw a comment line in the code to show your name for credit and in the main addon page. Otherwise I just need the fix to find out why its not working as intended.
Here is the code:
local _PlayerName2 = CM2.StripSpecial(UnitName("player"), true)
_PlayerName2 = _PlayerName2:lower()
local _PlayerName, _ = UnitName("player")
--Detect if the current character said their own name. Block Warnings.
local throwWarn = true
if ChatAuthor == curPlayer then
--Player said something.
throwWarn = false
--CM2.Print("Disabled Warnings")
end
if text == nil then
print("Text Nil, Check URL Detection Code")
end
--Color own name in your chat. Really only sender name because all other references will
--be tied to the alert.
if throwWarn == false and text:find(" "..curPlayer.." ") ~= nil then
text = CM2.far(text, curPlayer, CM2.ColorName(CM2_Nick[plainPlayerName][2], plainPlayerName))
end
if throwWarn == false and text:find(case_insensitive_pattern(plainPlayerName)) ~= nil then
text = CM2.far(text, plainPlayerName, CM2.ColorName(CM2_Nick[plainPlayerName][2], plainPlayerName))
end
--Detect and alert when playername is spoken
local pos = 0
if throwWarn == true and text:find(" ".._PlayerName.." ") ~= nil then
--Detect if the Original Player name was said.
text = text:gsub(" ".._PlayerName.." ", " "..CHATMOD_COLOR["YELLOW"]..">"..CHATMOD_COLOR["RED"].._PlayerName..CHATMOD_COLOR["YELLOW"].."<\124r ")
UIErrorsFrame:AddMessage(text, red, green, blue, nil, UIERRORS_HOLD_TIME)
PlaySound("FriendJoinGame")
elseif throwWarn == true and text:find(" "..case_insensitive_pattern(plainPlayerName).." ") ~= nil then
--Detect if the stripped down Player Name was said.
text = text:gsub(" ".._PlayerName2.." ", " "..CHATMOD_COLOR["YELLOW"]..">"..CHATMOD_COLOR["RED"].._PlayerName2..CHATMOD_COLOR["YELLOW"].."<\124r ")
UIErrorsFrame:AddMessage(text, red, green, blue, nil, UIERRORS_HOLD_TIME)
PlaySound("FriendJoinGame")
end
if text == nil then
print("Text Nil, Check playername highlighter Code")
end
-- Color Nicks
if CM2_Options["ColorNicks"] then
-- Hold Original unmodified words for later use
temp2 = text
temp2 = CM2.StripSpecial(temp2)
temp = CM2.StripSpecial(text)
temp = string.gsub(temp, "%]%[", " ")
temp = string.gsub(temp, "[^a-zA-Z0-9%s]", "")
words = CM2.GetWords(temp)
words2 = CM2.GetWords(temp:lower())
for word = 1, #words do
-- Cant be the player name or it locks up the client... Go figure...
if words[word] ~= UnitName("player") and words[word] ~= plainPlayerName then
--print(words[word]:lower())
if CM2_Nick[words[word]:lower()] ~= nil then
--{level, class, guild, realm, name} Nick Layout. Name is the unfiltered name.
local newWord = CM2.ColorName(CM2_Nick[words[word]:lower()][2], words[word]:lower())
word2find = words[word]
pos = temp2:find(word2find) or temp:find(words2[word])
if newWord ~= nil then
--replace with find code.
text = CM2.far(text, word2find, newWord)
end
end
end
end
end
Function CM2.far:
function CM2.far(str, fstr, rstr)
if (str and type(str) == "string") and (fstr and type(fstr) == "string") and (rstr and type(rstr) == "string") then
--And space at the end so gfind will find till the end.
--In case a match is there.
str = str .. " "
local pos = nil
for x in str:gmatch("[^%:]"..case_insensitive_pattern(fstr).."[^%:]") do
if pos ~= nil and pos ~= str:find("[^%:]"..case_insensitive_pattern(fstr).."[^%:]", pos+1) then
str = str:sub(1, pos) .. rstr .. str:sub(pos + #fstr + 1)
--print(str)
pos = str:find("[^%:]"..case_insensitive_pattern(fstr).."[^%:]", pos+1)
elseif pos == nil then
pos = str:find("[^%:]"..case_insensitive_pattern(fstr).."[^%:]")
str = str:sub(1, pos) .. rstr .. str:sub(pos + #fstr + 1)
pos = str:find("[^%:]"..case_insensitive_pattern(fstr).."[^%:]", pos+1)
--print(str)
end
end
str = str:sub(1, #str - 1)
return str
end
end
Function CM2.StripSpecial:
function CM2.StripSpecial(msg, player)
PlayerStripped = 0
--Strips out all special characters such as Ö and the like.
--Should only be used for being returned to a temp string unless replacement is required.
if msg ~= nil and type(msg) == "string" then
for x=1, #msg do
local CharVal = string.byte(string.sub(msg,x,x+1), -1)
--local StrTab = {}
--for a=1, #msg do
-- StrTab:Insert(
--print("Debug: "..string.byte(string.sub(msg,x,x+1)))
--print(CharVal)
if CharVal ~= nil then
if 146 <= CharVal and CharVal <= 150 then
msg = StringReplace(msg, x, "O")
if player then
PlayerStripped = PlayerStripped + 1
end
elseif 178 <= CharVal and CharVal <= 182 then
msg = StringReplace(msg, x, "o")
if player then
PlayerStripped = PlayerStripped + 1
end
elseif 128 <= CharVal and CharVal <= 134 then
msg = StringReplace(msg, x, "A")
if player then
PlayerStripped = PlayerStripped + 1
end
elseif 160 <= CharVal and CharVal <= 166 then
msg = StringReplace(msg, x, "a")
if player then
PlayerStripped = PlayerStripped + 1
end
elseif 136 <= CharVal and CharVal <= 139 then
msg = StringReplace(msg, x, "E")
if player then
PlayerStripped = PlayerStripped + 1
end
elseif 168 <= CharVal and CharVal <= 171 then
msg = StringReplace(msg, x, "e")
if player then
PlayerStripped = PlayerStripped + 1
end
elseif 153 <= CharVal and CharVal <= 156 then
msg = StringReplace(msg, x, "U")
if player then
PlayerStripped = PlayerStripped + 1
end
elseif 185 <= CharVal and CharVal <= 188 then
msg = StringReplace(msg, x, "u")
if player then
PlayerStripped = PlayerStripped + 1
end
elseif 140 <= CharVal and CharVal <= 143 then
msg = StringReplace(msg, x, "I")
if player then
PlayerStripped = PlayerStripped + 1
end
elseif 172 <= CharVal and CharVal <= 175 then
msg = StringReplace(msg, x, "i")
if player then
PlayerStripped = PlayerStripped + 1
end
elseif 135 == CharVal then
msg = StringReplace(msg, x, "C")
if player then
PlayerStripped = PlayerStripped + 1
end
elseif 167 == CharVal then
msg = StringReplace(msg, x, "c")
if player then
PlayerStripped = PlayerStripped + 1
end
elseif 144 == CharVal then
msg = StringReplace(msg, x, "D")
if player then
PlayerStripped = PlayerStripped + 1
end
elseif 176 == CharVal then
msg = StringReplace(msg, x, "o")
if player then
PlayerStripped = PlayerStripped + 1
end
elseif 152 == CharVal then
msg = StringReplace(msg, x, "O")
if player then
PlayerStripped = PlayerStripped + 1
end
elseif 184 == CharVal then
msg = StringReplace(msg, x, "o")
if player then
PlayerStripped = PlayerStripped + 1
end
end
end
end
end
return msg
end
Function CM2.GetWords:
function CM2.GetWords(str)
if type(str) == "string" then
local results = {}
for word in string.gmatch(str, "%S+", 9) do
table.insert(results, word)
end
return results
end
end
Function case_insensitive_pattern:
function case_insensitive_pattern(pattern)
-- find an optional '%' (group 1) followed by any character (group 2)
local p = pattern:gsub("(%%?)(.)", function(percent, letter)
--print(percent, letter)
if percent ~= "" or (not letter:match("%a")) then
if letter ~= "-" then
-- if the '%' matched, or `letter` is not a letter, return "as is"
return percent .. letter
elseif letter == "-" then
return "%-"
end
else
if letter == "[" then
return "%["
elseif letter == "]" then
--print("hi")
return "%]"
end
-- else, return a case-insensitive character class of the matched letter
return string.format("[%s%s]", letter:lower(), letter:upper())
end
end)
return p
end
Function CM2.ColorName:
function CM2.ColorName(str, word)
--Using the class and word provided precolor it for chat.
if str == "MONK" then
word = "\124cff00ff96" .. CM2_Nick[word][5] .. "|r"
elseif str == "DEATH KNIGHT" then
word = "\124cffc41f3b" .. CM2_Nick[word][5] .. "|r"
elseif str == "DRUID" then
word = "\124cffff7d0a" .. CM2_Nick[word][5] .. "|r"
elseif str == "HUNTER" then
word = "\124cffabd473" .. CM2_Nick[word][5] .. "|r"
elseif str == "MAGE" then
word = "\124cff69ccf0" .. CM2_Nick[word][5] .. "|r"
elseif str == "PALADIN" then
word = "\124cfff58cba" .. CM2_Nick[word][5] .. "|r"
elseif str == "PRIEST" then
word = "\124cffffffff" .. CM2_Nick[word][5] .. "|r"
elseif str == "ROGUE" then
word = "\124cfffff569" .. CM2_Nick[word][5] .. "|r"
elseif str == "SHAMAN" then
word = "\124cff0070de" .. CM2_Nick[word][5] .. "|r"
elseif str == "WARLOCK" then
word = "\124cff9482c9" .. CM2_Nick[word][5] .. "|r"
elseif str == "WARRIOR" then
word = "\124cffc79c6e" .. CM2_Nick[word][5] .. "|r"
end
return word
end
Edit 1: Managed to fix self name warning but noticed that saying your full matching player name in chat is not being colored. I can deal with this myself. No changes we able to be made yet to fix the other players saying the players name warning yet.

Issue with lua code in world of warcraft

-- Color Nicks
if CM2_Options["ColorNicks"] then
-- Hold Original unmodified words for later use
local temp2 = ChatMod2_GetWords(text)
local temp = ChatMod2_StripSpecial(text)
temp = string.gsub(temp, "[^a-zA-Z0-9%s]", "")
temp = temp:lower()
local words = ChatMod2_GetWords(temp)
for word = 1, #words do
-- Cant be the player name or it locks up the client... Go figure...
if words[word] ~= UnitName("player") then
print(temp)
if CM2_Nick[words[word]] ~= nil then
--{level, class, guild, realm, name} Nick Layout. Name is the unfiltered name.
local newWord = ChatMod2_ColorName(CM2_Nick[words[word]][2], words[word])
text = text:gsub(temp2[word], newWord)
end
end
end
end
The above is causing World Of Warcraft to fail to load into the game. Because I lack the debugging ability from the program itself does anyone see why here?
Below are the other functions mentioned in the above code:
function ChatMod2_GetWords(str)
local results = {}
for word in string.gmatch(str, "%S+", 9) do
table.insert(results, word)
end
return results
end
function ChatMod2_ColorName(str, word)
--Using the class and word provided precolor it for chat.
if str == "MONK" then
word = "\124cff00ff96" .. CM2_Nick[word][5] .. "|r"
elseif str == "DEATH KNIGHT" then
word = "\124cffc41f3b" .. CM2_Nick[word][5] .. "|r"
elseif str == "DRUID" then
word = "\124cffff7d0a" .. CM2_Nick[word][5] .. "|r"
elseif str == "HUNTER" then
word = "\124cffabd473" .. CM2_Nick[word][5] .. "|r"
elseif str == "MAGE" then
word = "\124cff69ccf0" .. CM2_Nick[word][5] .. "|r"
elseif str == "PALADIN" then
word = "\124cfff58cba" .. CM2_Nick[word][5] .. "|r"
elseif str == "PRIEST" then
word = "\124cffffffff" .. CM2_Nick[word][5] .. "|r"
elseif str == "ROGUE" then
word = "\124cfffff569" .. CM2_Nick[word][5] .. "|r"
elseif str == "SHAMAN" then
word = "\124cff0070de" .. CM2_Nick[word][5] .. "|r"
elseif str == "WARLOCK" then
word = "\124cff9482c9" .. CM2_Nick[word][5] .. "|r"
elseif str == "WARRIOR" then
word = "\124cffc79c6e" .. CM2_Nick[word][5] .. "|r"
end
return word
end
function ChatMod2_StripSpecial(msg)
--Strips out all special characters such as Ö and the like.
--Should only be used for being returned to a temp string unless replacement is required.
if msg ~= nil and type(msg) == "string" then
for x=1, #msg do
local CharVal = string.byte(string.sub(msg,x,x+1), -1)
--local StrTab = {}
--for a=1, #msg do
-- StrTab:Insert(
--print("Debug: "..string.byte(string.sub(msg,x,x+1)))
--print(CharVal)
if CharVal ~= nil then
if 146 <= CharVal and CharVal <= 150 then
msg = StringReplace(msg, x, "O")
elseif 178 <= CharVal and CharVal <= 182 then
msg = StringReplace(msg, x, "o")
elseif 128 <= CharVal and CharVal <= 134 then
msg = StringReplace(msg, x, "A")
elseif 160 <= CharVal and CharVal <= 166 then
msg = StringReplace(msg, x, "a")
elseif 136 <= CharVal and CharVal <= 139 then
msg = StringReplace(msg, x, "E")
elseif 168 <= CharVal and CharVal <= 171 then
msg = StringReplace(msg, x, "e")
elseif 153 <= CharVal and CharVal <= 156 then
msg = StringReplace(msg, x, "U")
elseif 185 <= CharVal and CharVal <= 188 then
msg = StringReplace(msg, x, "u")
elseif 140 <= CharVal and CharVal <= 143 then
msg = StringReplace(msg, x, "I")
elseif 172 <= CharVal and CharVal <= 175 then
msg = StringReplace(msg, x, "i")
elseif 135 == CharVal then
msg = StringReplace(msg, x, "C")
elseif 167 == CharVal then
msg = StringReplace(msg, x, "c")
elseif 144 == CharVal then
msg = StringReplace(msg, x, "D")
elseif 176 == CharVal then
msg = StringReplace(msg, x, "o")
elseif 152 == CharVal then
msg = StringReplace(msg, x, "O")
elseif 184 == CharVal then
msg = StringReplace(msg, x, "o")
end
end
end
end
return msg
end
I appreciate any and all help.
ChatMod2_GetWords is clearly fine.
ChatMod2_ColorName on the other hand will explode if CM2_Nick doesn't have a word entry or if that entry doesn't have an index 5.
I haven't looked at ChatMod2_StripSpecial much yet.

lua loops stuck after 15 numbers

My code is designed to read digits and turn them into Chinese pinyin:
function digitconverter (digit)
if digit == "0" then
cnumber = "ying2 "
elseif digit == "1" then
cnumber = "yi1 "
elseif digit == "2" then
cnumber = "er2 "
elseif digit == "3" then
cnumber = "san1 "
elseif digit == "4" then
cnumber = "si4 "
elseif digit == "5" then
cnumber = "wu3 "
elseif digit == "6" then
cnumber = "liu4 "
elseif digit == "7" then
cnumber = "qi1 "
elseif digit == "8" then
cnumber = "ba1 "
elseif digit == "9" then
cnumber = "jiu3 "
end
return cnumber
end
print("Enter a number to be converted:")
repeat
strnumber = io.read("*line")
number = tonumber(strnumber)
if number ~= nil then
continue = true
else
print("Invalid input. Please try again:")
continue = false
end
until continue == true
nlength = #strnumber
digits = {}
for d in string.gmatch(number, "%d") do
digits[#digits + 1] = d
end
convnumber = ""
for d=1,nlength do
convnumber = convnumber .. digitconverter(digits[d])
end
print(convnumber)
io.read()
If I enter over 15 digits, it gets stuck (for lack of a better term). It WILL convert every digit, but the 16th will be random and the 17th and on will repeat another random one. I've been over it and I can't figure out where it's getting hung up. Thoughts?
You're iterating through the digits of number, not strnumber. The problem is when you get to too many digits, the string representation is going to be in scientific notation:
strnumber = '1234567890123456789'
number = tonumber(strnumber)
print(number) --> 1.2345678901235e+018
Side note: Lua is based on hashtables, which gives you (barring hash collisions) constant time lookup. So your digit converter can be simply written as a map:
local digitmap = {
["0"] = "ying2 ",
["1"] = "yi1 ",
["2"] = "er2 ",
["3"] = "san1 ",
["4"] = "si4 ",
["5"] = "wu3 ",
["6"] = "liu4 ",
["7"] = "qi1 ",
["8"] = "ba1 ",
["9"] = "jiu3 ",
}
Also, building strings like this is very inefficient:
for d=1,nlength do
convnumber = convnumber .. digitconverter(digits[d])
end
You're generating tons of intermediate strings, which requires a lot of allocations and produces a lot of garbage. It's much faster to put all the values you need to concatenate into a table, then call table.concat. Another advantage is that you can specify a delimiter (right now, you're hard coding the delimiter into your string table).
Using those techniques, we can rewrite your code like this:
local digitmap = {
['0'] = 'ying2',
['1'] = 'yi1',
['2'] = 'er2',
['3'] = 'san1',
['4'] = 'si4',
['5'] = 'wu3',
['6'] = 'liu4',
['7'] = 'qi1',
['8'] = 'ba1',
['9'] = 'jiu3',
}
print('Enter a number to be converted:')
while true do
strnumber = io.read('*line')
if not strnumber:match('%D') then
break
end
print('Invalid input. Please try again:')
end
local digits = {}
for digit in string.gmatch(strnumber, '%d') do
digits[#digits + 1] = digitmap[digit]
end
print(table.concat(digits, ' '))
Probably, you want to scan strnumber instead of number in the following line of your code:
for d in string.gmatch(number, "%d") do
Your number variable contains a numerical value in double format with 15-16 decimal digits.

Resources