A way back I had a hub with a friend and he made a TRIVIA script, very fast and popular too. Now I have on my hub the last version of TRIVIA using my set of almost 5000 questions. The problem is that it is not working as fast as I want for my users. They are complainyng about the lag. I tried to reduce as maximum the size of the packages and the cache, but I still have problem.
Than I remembered of this script that i present you today. It is not compatible with LUA5 and it doesn't work with the latest PtokaX I'm using.
Can someoane translate it to LUA5 and check it out if it worth something as practical use?
---Made originally by Anthos, as I recall
--Bot name
Bot = "TriviaBot"
started = 0
answered = 1
half=0
questions={"TV: Actrita din Masca","Cum se numeste muntele din Japonia considerat sfant"} --- sample questions
myarray={"Cameron Diaz","Fuji-Yama"} --- sample answears
numar=1
gotHint=0
gotHint2=0
forceStopped=0
usersThatStarted = { "!", "!", "!" }
howManyStarted = 0
--// This function is fired at the serving start
function Main()
frmHub:RegBot(Bot)
SetTimer(15000)
numar = random(4118)
end
--// This function is fired when a new data arrives
function DataArrival(curUser, data)
s, e, msg = strfind(data, "%b<> (![%w ]*)")
if curUser.bOperator then
if msg == "!disable" then
if started==1 then
StopTimer()
half=0
answered = 1
started = 0
howManyStarted=0
usersThatStarted[1]="!"
usersThatStarted[2]="!"
usersThatStarted[3]="!"
end
forceStopped = 1
SendToAll(Bot, "*** Trivia DISABLED (can be enabled by an OP only)...")
end
if msg == "!start" then
forceStopped = 0
if started==1 then
SendToAll(Bot, "*** Trivia already started!!!")
return
else
SendToAll(Bot, "*** Trivia Starts in 15 seconds...")
half=0
StopTimer()
StartTimer()
started = 1
end
end
if msg == "!stop" then
if started==1 then
SendToAll(Bot, "*** Trivia Stopped...")
StopTimer()
half=0
answered = 1
started = 0
howManyStarted=0
usersThatStarted[1]="!"
usersThatStarted[2]="!"
usersThatStarted[3]="!"
end
end
--// return
end
s, e, msg = strfind(data, "%b<> (![%w ]*)")
if msg == "!top" then
fd = openfile("_top.txt","r")
pos = 0
SendPmToNick(curUser.sName, Bot, "TOP 20:")
repeat
nume = read(fd)
if nume == nil then break end
pos=pos+1
scor_t = read(fd)
scor = tonumber(scor_t)
if (pos<=20) then
SendPmToNick(curUser.sName, Bot, pos..". "..nume.." - "..scor)
else
break
end
until (1==2)
closefile(fd)
end
if msg == "!rank" then
fd = openfile("_top.txt","r")
pos = 0
repeat
nume = read(fd)
if nume == nil then break end
pos=pos+1
scor_t = read(fd)
scor = tonumber(scor_t)
if (nume == curUser.sName) then
SendPmToNick(curUser.sName, Bot, "You are on place "..pos.." with "..scor.." points!")
pos = -1
break;
end
until (1==2)
if (pos ~= -1) then SendPmToNick(curUser.sName, Bot, "You are not in the database!") end
closefile(fd)
end
if msg == "!start" and not curUser.bOperator then
if forceStopped == 1 then
SendToAll(Bot, "*** Trivia has been disabled for now!!!")
return
end
if started==1 then
SendToAll(Bot, "*** Trivia already started!!!")
return
end
if usersThatStarted[1]==curUser.sName then
return
end
if usersThatStarted[2]==curUser.sName then
return
end
if usersThatStarted[3]==curUser.sName then
return
end
howManyStarted=howManyStarted+1
usersThatStarted[howManyStarted]=curUser.sName
if howManyStarted == 3 then
SendToAll(Bot, "*** Trivia Starts in 15 seconds...")
half=0
StopTimer()
StartTimer()
started = 1
else
SendToAll(Bot, "* Need "..3-howManyStarted.." more !start command(s) to start trivia")
end
end
if started == 0 then
return
end
if answered == 1 then
return
end
s, e, msg = strfind(data, "%b<> (![%w ]*)")
if msg == "!hint" then
if gotHint==0 then
hint = strsub(myarray[numar],1,1)
gotHint2=1
else
hint = strsub(myarray[numar],1,2)
end
SendToAll(Bot, hint.."* (length: "..strlen(myarray[numar])..")")
return
end
s, e, msg = strfind(data, "%b<> ([%w ]*)")
scor = 0
scor2= 0
gasit = 0
if strlower(myarray[numar]) == strlower( msg ) then
half=0
answered=1
fHandle = openfile("_top.txt","r")
if (fHandle == nil) then
fHandle = openfile("_top.txt","w")
write(fHandle,curUser.sName.."\n")
write(fHandle,"1\n")
SendToAll(Bot, "* "..curUser.sName.." you got the right answer <"..strlower(msg)..">. Congratulations! You have 1 right answer")
closefile(fHandle)
else
repeat
nume = read(fHandle)
if (nume == nil) then break end
scor_text = read(fHandle)
scor = tonumber(scor_text)
if nume == curUser.sName then
gasit = 1
scor=scor+1
break;
end
until (1==2)
if (gasit == 0) then
SendToAll(Bot, "* "..curUser.sName.." you got the right answer <"..strlower(msg)..">. Congratulations! You have 1 right answer")
scor = -1
end
seek(fHandle,"set")
fd = openfile("top.tmp","w")
repeat
nume = read(fHandle)
if (nume == nil) then break end
scor_text = read(fHandle)
scor2 = tonumber(scor_text)
if (scor2>scor) then
if (nume ~= curUser.sName) then
write(fd,nume.."\n")
write(fd,scor2)
write(fd,"\n")
end
else
write(fd,curUser.sName.."\n")
write(fd,scor)
write(fd,"\n")
SendToAll(Bot, "* "..curUser.sName.." you got the right answer <"..strlower(msg)..">. Congratulations! You have "..scor.." right answers")
scor = -1
if (nume ~= curUser.sName) then
write(fd,nume.."\n")
write(fd,scor2)
write(fd,"\n")
end
end
until (1==2)
if gasit == 0 then
write(fd,curUser.sName.."\n")
write(fd,"1\n")
end
closefile(fHandle)
closefile(fd)
remove("_top.txt")
rename("top.tmp","_top.txt")
end
end
end
--// This function is fired when a new user finishes the login
function NewUserConnected(curUser)
curUser:SendData("<"..Bot.."> Hi "..curUser.sName..". Have fun!")
end
function OnTimer()
if half==1 then
half=0
SendToAll(Bot, "*** You have 15 seconds left!")
if gotHint2==0 then
hint = strsub(myarray[numar],1,1)
gotHint=1
else
hint = strsub(myarray[numar],1,2)
end
gotHint=gotHint+1
SendToAll(Bot, "*** Here's a hint for you: "..hint.."* (length: "..strlen(myarray[numar])..")")
return
end
if answered==0 then
SendToAll(Bot, "*** The answer was: "..myarray[numar])
end
oldnumar = numar
numar = random(4118)
if numar==oldnumar then
numar=oldnumar+1
end
SendToAll(Bot, "*** Question no."..numar..": "..questions[numar])
SendToAll(Bot, "*** You have 30 seconds to answer!")
answered=0
gotHint=0
gotHint2=0
half=1
end