Hi! i have an problem with a trivia bot! the problem is that at sometime when im running it the bot freezes the mainchat so that neither i or the users can write anything and the ptokax console starts to take all the processor usage! i hope i am making myself understood! i will post here the Trivia and wait for the help of someone! tkhx in advance! xokas over&out
The Script goes out like this:
-- TriviaBot Version 2.1
-- TriviaBot written by Leon a.k.a. Don Leon
-- Changes by Spud (Dream~Masters)
--
-- Version 2.0 Changes
--
-- Fixed TBPart message
-- Enabled confirmation message for save/load scores
-- Fixed character reveal function to reveal more that one character if word is long enough
-- Enabled auto reveal of answer feature
-- Removed "cheat" auto character hitting feature
-- Top Ten feature by aMutex and tr0f
-- Changed scoring
--
-- Version 2.1 Changes
-- Top Ten feature designed by Tezlo (Many many thanks for that)
-- Optional AutoLogin of all users
-- Fixed 100% CPU bug
-- Timed saving of scores
-- Moved various text from main to PM to save bandwidth
-- Users can't start Trivia
BotName = "TriviaBot"
ScriptVersion = "v2.1"
BotVersion = BotName.." "..ScriptVersion
-- This file is not needed it will be created if it not exists
strTrivScoreFile = "TriviaScores.dat"
-- This file is needed unless you want to run a Trivia with only two questions ;)
strDefTrivFile = "Trivia.dat"
strTrivFile = strDefTrivFile
-- This char will be the global separator? (Make it one that is not used in nicknames nor questions ;)
strGSep = "*"
-- This char is used to mask the unrevealed chars
strCMask = "@"
-- Default startup mode of Trivia!
lngDefMode = 0 -- (0 = game is played in chat!, 1 = game is played in chat but questions only show for players, 2 = game is played in pm with players)
lngMode = lngDefMode
-- Max number of questions to show when a Operator ask to see list Questions 0 = 0, -1 = Disabled
lngMaxShowList = 5
-- Max no of Hints per word, and always hit on a hidden char
lngMaxHints = 4
boolHintAlwaysHitHidden = 1 -- (nil = off, 1 = on)
-- How much time before a question end. Time in Trivia is measured in 15 seconds, 1 = 15 seconds, 2 = 30 seconds etc!
-- Rest is how long to rest after a question is solved depening on when the word is solved in the Timer loop it is ?15 secs to the setting!
lngMaxTime = 8 -- (Needs to be a multiple of 4 i.e. 4 / 8 / 12 / 16)
lngRestTime = 2
lngHintTime = lngMaxTime / 4
-- How often should the bot autosave the scores
boolAutoSaveScores = 1 -- (nil = off, 1 = on)
countSaveTurns = 10 -- After how many questions should the scores be saved
-- Should bot reveal the correct answer once time is up?
boolRevealAnswer = 1 -- (nil = off, 1 = on)
-- Should the bot display what guess made the word become a little clearer if played in PM!
boolShowGuessesInPM = 1 -- (nil = off, 1 = on)
-- Should the bot Auto Reveal Hints?
boolAutoHint = 1 -- (nil = off, 1 = on)
-- Should the bot auto login every user on connect ?
boolAutoLogin = 1 -- (nil = off, 1 = on)
-- TriggStart is what char the commands should begin with I use ! but you can use # or whatever
TriggStart = "!"
-- Theese are the different triggers I've tried to name them so that one easily understand what they do!
-- I include TriggStart and then the word within "" this word is not casesensitive!
JoinTrigg = TriggStart.."tbjoin"
PartTrigg = TriggStart.."tbpart"
StartTrigg = TriggStart.."tbstart"
StopTrigg = TriggStart.."tbstop"
WordTrigg = TriggStart.."tbword"
QuizTrigg = TriggStart.."tbquestion"
RestartTrigg = TriggStart.."tbrestart"
PointTrigg = TriggStart.."tbpoints"
ScoreTrigg = TriggStart.."tbscores"
PlayerTrigg = TriggStart.."tbplayers"
QuestionsTrigg = TriggStart.."tbquestions"
ReloadTrigg = TriggStart.."tbreload"
HelpTrigg = TriggStart.."tbhelp"
ExtraHelpTrigg = TriggStart.."tbhelp+"
SaveTrigg = TriggStart.."tbsavescores"
LoadTrigg = TriggStart.."tbloadscores"
ModeTrigg = TriggStart.."tbmode"
HintTrigg = TriggStart.."dica"
LoadTop = TriggStart.."tbtopten"
acount = 0
bcount = 0
scount = 0
guessArray = {}
lngGuessmax = 0
playerArray = {}
pointArray = {}
lngWord = 0
lngPassed = 0
lngHinted = 0
strWord = ""
strQuestion = ""
strSolved = ""
strBanner = " Escreve '"..StartTrigg.."' para iniciar o trivia, '"..StopTrigg.."' para parar o trivia, escreve '"..JoinTrigg.."' para te juntares ao jogo e '"..PartTrigg.."' para sair do jogo!"
strStart = "TriviaBot "..ScriptVersion.." iniciado! "..strBanner
strStopp = "TriviaBot "..ScriptVersion.." desligado "..strBanner
function Main()
frmHub:RegBot(BotName)
ReloadQuestions()
setlocale("swe")
LoadScores()
end
function NewUserConnected(curUser)
if (boolAutoLogin) then
if (not (playerArray[curUser.sName])) then
playerArray[curUser.sName] = 1
else
playerArray[curUser.sName] = nil
end
end
end
function OpConnected(curUser)
if (boolAutoLogin) then
if (not (playerArray[curUser.sName])) then
playerArray[curUser.sName] = 1
else
playerArray[curUser.sName] = nil
end
end
end
function UserDisconnected(curUser)
playerArray[curUser.sName] = nil
end
function OpDisconnected(curUser)
playerArray[curUser.sName] = nil
end
function DataArrival(curUser, data)
local boolPlaydata = nil
if (strsub(data, 1, 1) == "<") then
data = strsub(data, 1, (strlen(data) - 1))
if ((lngMode == 0) or (lngMode == 1)) then
boolPlaydata = 1
end
elseif (strsub(data, 1, 4) == "$To:") then
data = strsub(data, 1, (strlen(data) - 1))
local _, _, whoTo = strfind(data,"$To:%s+(%S+)")
if (whoTo == BotName) then
data = strsub(data, (15 + strlen(BotName) + strlen(curUser.sName)))
if (lngMode == 2) then
boolPlaydata = 1
end
end
end
local _, _, firstWord = strfind(data, "%b<>%s+(%S+)")
if (firstWord ~= nil) then
if ((strlower(firstWord) == strlower(HelpTrigg)) or (strlower(firstWord) == strlower(ExtraHelpTrigg))) then
showHelp(curUser)
elseif (strlower(firstWord) == strlower(SaveTrigg)) then
if (curUser.bOperator) then
SaveScores(curUser)
end
elseif (strlower(firstWord) == strlower(LoadTrigg)) then
if (curUser.bOperator) then
LoadScores(curUser)
end
elseif (strlower(firstWord) == strlower(LoadTop)) then
TopTen(user)
elseif (strlower(firstWord) == strlower(ModeTrigg)) then
if (curUser.bOperator) then
local _, _, ModeDigit = strfind(data, "%b<>%s+%S+%s+(%d+)")
if (ModeDigit ~= nil) then
lngMode = tonumber(ModeDigit)
if (lngMode == 0) then
SendToPlayers("O jogo ? jogado no main chat")
elseif (lngMode == 1) then
SendToPlayers("O jogo ? jogado no main chat mas as perguntas so sao visiveis pelos jogadores")
elseif (lngMode == 2) then
SendToPlayers("Jogo ? jogado em PM com os jogadores")
else
SendToPlayers("Modo n?o existente!Modo definido como standard de novo")
lngMode = lngDefMode
end
end
end
elseif (strlower(firstWord) == strlower(JoinTrigg)) then
if (not (playerArray[curUser.sName])) then
playerArray[curUser.sName] = 1
SendToPlayers(curUser.sName.." ? agora um jogador! N?o escrevam mais de 3 respostas em 10 segundos pra nao causarem lag por favor.")
else curUser:SendPM(BotName, "J? ?s um jogador e n?o sabias.")
end
elseif (strlower(firstWord) == strlower(PartTrigg)) then
if (playerArray[curUser.sName]) then
SendToPlayers(curUser.sName.." n?o est? a jogar agora!")
playerArray[curUser.sName] = nil
end
elseif (strlower(firstWord) == strlower(RestartTrigg)) then
if (curUser.bOperator) then
SendToPlayers("TriviaBot reinicidado. Todos os contadores a 0!")
SendToPlayers("Se salvares agora apagas os dados antigos!")
pointArray = {}
StopQuiz(1)
end
elseif ((strlower(firstWord) == strlower(PointTrigg)) and (playerArray[curUser.sName])) then
if (pointArray[curUser.sName]) then
SendToPlayers(curUser.sName.." tens "..pointArray[curUser.sName].." pontos!")
else
SendToPlayers(curUser.sName.." tens 0 pontos!")
end
elseif ((strlower(firstWord) == strlower(HintTrigg)) and (playerArray[curUser.sName])) then
if ((lngWord ~= 0) and strfind(strSolved, strCMask, 1, plain)) then
if (lngHinted < lngMaxHints) then
acount = strlen(strWord) / 4
for nn=1,acount do
local lngRepChar = random(1, strlen(strWord))
while 1 do
if (boolHintAlwaysHitHidden) then
if (strsub(strSolved, lngRepChar, lngRepChar) == strCMask) then
break
end
else
break
end
lngRepChar = random(1, strlen(strWord))
end
local strTurn = ""
for x=1, strlen(strWord) do
if (x == lngRepChar) then
strTurn = strTurn..strsub(strWord, x, x)
else
strTurn = strTurn..strsub(strSolved, x, x)
end
end
strSolved = strTurn
end
lngHinted = lngHinted + 1
lngHintTimeCount = lngHintTimeCount + lngHintTime
SendToPlayers("Dica: "..strSolved)
acount = ""
else
SendToPlayers("Todas as dicas usadas para esta frase!")
end
else
SendToPlayers("Dica n?o ? necessaria!")
end
elseif ((strlower(firstWord) == strlower(ScoreTrigg)) and (playerArray[curUser.sName])) then
curUser:SendPM(BotName, "Pontua?ao:")
curUser:SendPM(BotName, "---------------------------------------------------------")
for index, value in pointArray do
curUser:SendPM(BotName, index.." pontos: "..value)
end
curUser:SendPM(BotName, "---------------------------------------------------------")
elseif ((strlower(firstWord) == strlower(PlayerTrigg)) and (playerArray[curUser.sName])) then
curUser:SendPM(BotName, "Jogadores:")
curUser:SendPM(BotName, "---------------------------------------------------------")
for index, value in playerArray do
curUser:SendPM(BotName, index)
end
curUser:SendPM(BotName, "---------------------------------------------------------")
elseif (strlower(firstWord) == strlower(QuestionsTrigg)) then
if (curUser.bOperator) then
curUser:SendPM(BotName, " Top "..lngMaxShowList.." Quest?es:")
curUser:SendPM(BotName, "---------------------------------------------------------")
local lngShowMax = lngMaxShowList
for index, value in guessArray do
if (lngShowMax == 0) then
break
end
arrTmp = tokenize(guessArray[index], strGSep)
strTmp = arrTmp[1]
curUser:SendPM(BotName, "["..index.."] "..strTmp)
lngShowMax = lngShowMax - 1
end
curUser:SendPM(BotName, "Numero Total de Quest?es: "..lngGuessmax)
curUser:SendPM(BotName, "---------------------------------------------------------")
curUser:SendPM(BotName, " ")
end
elseif (strlower(firstWord) == strlower(StartTrigg)) then
if (curUser.bOperator) then
if (lngWord == 0) then
playerArray[curUser.sName] = 1
SendToAll(BotName, strStart)
if (lngMode == 2) then
SendToPlayers(strStart)
end
StopQuiz(1)
else
SendToAll(BotName, "J? est? a decorrer um jogo,escreve "..JoinTrigg.." para te juntares a ele!")
end
else
curUser:SendPM(BotName, "S? operadores conseguem iniciar o TriviaBot.")
end
I will post on the next post the rest of it because it's too long
elseif (strlower(firstWord) == strlower(StopTrigg)) then
if (curUser.bOperator) then
HoldQuiz()
StopQuiz()
SendToAll(BotName, strStopp)
if (lngMode == 2) then
SendToPlayers(strStopp)
end
end
elseif (strlower(firstWord) == strlower(WordTrigg)) then
if ((lngWord ~= 0) and (playerArray[curUser.sName] == "Jogador")) then
SendToPlayers("Quest?o actual ?: "..strSolved.." ("..strlen(strSolved)..")")
end
elseif (strlower(firstWord) == strlower(QuizTrigg)) then
if ((lngWord ~= 0) and (playerArray[curUser.sName])) then
SendToPlayers("Quest?o ?: "..strQuestion)
end
elseif (strlower(firstWord) == strlower(ReloadTrigg)) then
if (curUser.bOperator) then
local _, _, secondWord = strfind(data, "%b<>%s+%S+%s+(%S+)")
if (secondWord) then
handle = openfile(secondWord, "r")
if (handle) then
closefile(handle)
strTrivFile = secondWord
end
end
HoldQuiz()
ReloadQuestions()
end
else
if ((lngWord ~= 0) and (boolPlaydata)) then
if (playerArray[curUser.sName]) then
local msg = strsub(data, (4 + strlen(curUser.sName)))
if (strlen(msg) >= strlen(strWord)) then
local boolDisc = nil
local strTurn = ""
if strlower(msg) == strlower(strWord) then
strTurn = strWord
boolDisc = 1
else
strTurn = strSolved
end
if (boolDisc) then
strSolved = strTurn
end
if (boolDisc) then
if (strlower(msg) ~= strlower(strWord)) then
SendToOthers(curUser, msg)
SendToPlayers("Resposta ?: "..strSolved)
end
end
if (strlower(msg) == strlower(strWord)) then
if (not (pointArray[curUser.sName])) then
pointArray[curUser.sName] = 0
end
if lngHinted == 3 then pointArray[curUser.sName] = pointArray[curUser.sName] + 1
elseif lngHinted == 2 then pointArray[curUser.sName] = pointArray[curUser.sName] + 2
elseif lngHinted == 1 then pointArray[curUser.sName] = pointArray[curUser.sName] + 3
else pointArray[curUser.sName] = pointArray[curUser.sName] + 4
end
SendToPlayers("Resposta foi: "..strSolved)
SendToPlayers("Respondida por: "..curUser.sName)
SendToPlayers("Proxima quest?o em "..(lngRestTime * 15).." segundos")
lngPassed = lngMaxTime
SendToPlayers(curUser.sName.."pontos: "..pointArray[curUser.sName])
HoldQuiz()
end
end
end
end
end
end
end
function HoldQuiz()
strWord = ""
strQuestion = ""
strSolved = ""
end
function StopQuiz(restart)
lngWord = 0
lngPassed = 0
strWord = ""
strQuestion = ""
strSolved = ""
StopTimer()
if (restart) then
StartQuiz()
end
end
function StartQuiz()
lngWord = random(1, lngGuessmax)
QWarray = tokenize(guessArray[lngWord], strGSep)
strQuestion = QWarray[1]
strWord = QWarray[2]
strSolved = ""
for x=1, strlen(strWord) do
if (strsub(strWord, x, x) == " ") then
strSolved = strSolved.." "
else
strSolved = strSolved..strCMask
end
end
lngPassed = 0
lngHinted = 0
lngHintTimeCount = lngHintTime
bcount = strlen(strWord) / 4
SetTimer(15000)
StartTimer()
SendToPlayers("Quest?o: "..strQuestion)
SendToPlayers("Resposta: "..strSolved.." ("..strlen(strSolved)..")")
end
function OnTimer()
lngPassed = lngPassed + 1
if (lngPassed == lngMaxTime) then
scount = scount + 1
if scount == countSaveTurns then
local handle = openfile(strTrivScoreFile, "w")
for index, value in pointArray do
write(handle,index..strGSep..value.."\r\n")
end
closefile(handle)
scount = 0
end
if (boolRevealAnswer) then
SendToPlayers("Resposta era '"..strWord.."' e ninguem respondeu!")
else
SendToPlayers("N?o foi dada nenhuma resposta")
end
SendToPlayers("Proxima quest?o "..(lngRestTime * 15).." segundos")
HoldQuiz()
elseif (lngPassed >= (lngRestTime + lngMaxTime)) then
StopQuiz(1)
elseif (lngPassed == lngHintTimeCount) then
if (boolAutoHint) then
for nn=1,bcount do
local lngRepChar = random(1, strlen(strWord))
while 1 do
if (boolHintAlwaysHitHidden) then
if (strsub(strSolved, lngRepChar, lngRepChar) == strCMask) then
break
end
else
break
end
lngRepChar = random(1, strlen(strWord))
end
local strTurn = ""
for x=1, strlen(strWord) do
if (x == lngRepChar) then
strTurn = strTurn..strsub(strWord, x, x)
else
strTurn = strTurn..strsub(strSolved, x, x)
end
end
strSolved = strTurn
end
lngHinted = lngHinted + 1
lngHintTimeCount = lngHintTimeCount + lngHintTime
SendToPlayers("Dica: "..strSolved)
end
end
end
function tokenize (inString,token)
_WORDS = {}
local matcher = "([^"..token.."]+)"
gsub(inString, matcher, function (w) tinsert(_WORDS,w) end)
return _WORDS
end
function ReloadQuestions()
guessArray = {}
lngGuessmax = 0
local boolRestart = nil
if (lngWord ~= 0) then
SendToPlayers("Reiniciando perguntas, jogo ir? ser reiniciado!")
StopQuiz()
boolRestart = 1
end
handle = openfile(strTrivFile, "r")
if (handle) then
local line = read(handle)
while line do
if ((line ~= "") and (strfind(line, strGSep, 1, plain))) then
tinsert(guessArray, line)
lngGuessmax = lngGuessmax + 1
end
line = read(handle)
end
closefile(handle)
end
if (lngGuessmax < 1) then
guessArray = {
" qual o melhor carro k ha?"..strGSep.."Honda CRX ",
"Quem criou este TriviaBot"..strGSep.."Leon a.k.a. Don Leon"
}
lngGuessmax = 2
end
if (boolRestart) then
SendToPlayers("Reiniciando perguntas, jogo ir? ser reiniciado! N?mero actual de perguntas: "..lngGuessmax)
StartQuiz()
end
strTrivFile = strDefTrivFile
end
function SaveScores(curUser)
local handle = openfile(strTrivScoreFile, "w")
for index, value in pointArray do
write(handle,index..strGSep..value.."\r\n")
end
curUser:SendData(BotName,"pontua??o gravada")
closefile(handle)
end
function LoadScores(curUser)
local handle = openfile(strTrivScoreFile, "r")
if (handle) then
local line = read(handle)
while line do
local arrTmp = tokenize(line, strGSep)
if ((arrTmp[1] ~= nil) and (arrTmp[2] ~= nil)) then
pointArray[arrTmp[1]] = tonumber(arrTmp[2])
end
line = read(handle)
end
curUser:SendData(BotName,"pontua??o carregada")
closefile(handle)
end
end
function SendToPlayers(msg)
if ((lngMode > 2) or (lngMode < 0)) then
lngMode = lngDefMode
end
if (lngMode == 0) then
SendToAll(BotName, msg)
elseif (lngMode == 1) then
for index, value in playerArray do
SendToNick(index, "<"..BotName.."> "..msg)
end
elseif (lngMode == 2) then
for index, value in playerArray do
SendPmToNick(index, BotName, msg)
end
end
end
function SendToOthers(msg, curUser)
if ((lngMode == 2) and (boolShowGuessesInPM)) then
for index, value in playerArray do
if (index ~= curUser.sName) then
SendPmToNick(index, BotName, "<"..curUser.sName.."> "..msg)
end
end
end
end
function showHelp(curUser)
curUser:SendData(" ")
curUser:SendData(" "..BotVersion.." comandos:")
curUser:SendData(" ")
if (lngMode == 0) then
curUser:SendData(" "..BotVersion.." definido para ser jogado no main chat")
elseif (lngMode == 1) then
curUser:SendData(" "..BotVersion.."definido para ser jogado no main chat mas visivel so por quem est? a jogar")
else
curUser:SendData(" "..BotVersion.." definido para ser jogado em pvt")
end
curUser:SendData(" "..HelpTrigg.." - Mostra esta ajuda")
curUser:SendData(" "..JoinTrigg.." - Junta-te a um jogo")
curUser:SendData(" "..PartTrigg.." - Sais dum jogo")
curUser:SendData(" "..WordTrigg.." - Mostra a palavra actual(resposta)")
curUser:SendData(" "..QuizTrigg.." - Mostra a pergunta actual")
curUser:SendData(" "..PointTrigg.." - Mostra s? os teus pontos")
curUser:SendData(" "..ScoreTrigg.." - Mostra os pontos de todos os jogadores")
curUser:SendData(" "..PlayerTrigg.." - Mostra todos os jogadores currentemente")
curUser:SendData(" "..HintTrigg.." - Mostra um caracter de ajuda na palavra")
curUser:SendData(" "..LoadTop.." - Mostra o top ten das pontua??es")
if (curUser.bOperator) then
curUser:SendData(" ")
curUser:SendData(" "..BotVersion.." Comandos de operadores:")
curUser:SendData(" "..StopTrigg.." - Para o TriviaBot")
curUser:SendData(" "..RestartTrigg.." - Reinicia o bot reiniciando tambem as pontua??es")
curUser:SendData(" "..QuestionsTrigg.." - Mostra todas as quest?es do jogo currentemente")
curUser:SendData(" "..ReloadTrigg.." () - Carrega as perguntas do ficheiro "..strDefTrivFile)
curUser:SendData(" "..ModeTrigg.." - Para alterar o modo de jogo (0-2)!")
curUser:SendData(" "..SaveTrigg.." - Grava a informa?ao dos resultados "..strTrivScoreFile)
curUser:SendData(" "..LoadTrigg.." - Carrega as informa?oes dos resultados do ficheiro "..strTrivScoreFile)
curUser:SendData(" "..StartTrigg.." - Inicia o TriviaBot e define-te como jogador automaticamente")
curUser:SendData(" ")
curUser:SendData(" "..BotVersion.." Modos:")
curUser:SendData(" ".."0 = O jogo ? jogado em main chat!")
curUser:SendData(" ".."1 = O jogo ? jogado em main chat mas so os jogadores veem as perguntas")
curUser:SendData(" ".."2 = O jogo ? jogado em pvt com os jogadores")
end
end
function TopTen(user)
local index = {n=0}
foreach(pointArray, function(nick, score) tinsert(%index, nick) end)
local func = function(a, b) return %pointArray[a] > %pointArray end
sort(index, func)
x = x or 10 if x > index.n then x = index.n end
local result = "\r\nTop "..x.." Pontua?ao do trivia neste hub..\r\n Posi??o\t Pontua??o\t\t Nick\t\r\n"
for i = 1, x do result = result.."\r\n "..i..".\t"..pointArray[index].."\t\t"..index end
SendToAll("Trivia", result)
end
And that's it! the bot is complete! gonna wait here for ya help who ever comes! :))))
I get the same problem as well; its a bug. Hopefully Don Leon will return to the forum and post a new version, which is supposed to be 2.2a, that will not cause this problem. Until then, either don't run trivia or use a very small questions file :-\
ahhhhhhhhhhh i see!tkhx for the hint and all the help broken brick :))))))))
uhm i've found other thing! the scripts crashes ptokax console when the users send to many answers to a question! is it possible to someone solve this problem?