-
Autor: Gooden
-
Servidor testado: TFS 0.4 provável dar no 3.6
-
Base: Mock The Bear - Advanced quiz system com logs!
Sistema:
Sistema de votações com a oportunidade de ter mais que 1 Opção personalizada.
talkactions.xml
<talkaction words="/quiz" event="script" access="3" value="vote.lua" /> <talkaction words="/quizinstall" event="script" access="3" value="vote.lua" /> <talkaction words="!vote" event="script" value="vote.lua" />
vote.lua
---Script: 'Super Quiz System 1.0' By: Gooden
---Based: 'Advanced quiz system com logs!' By: Mock the bear
--Informations
--/quiz - Start a Quiz - Example: /quiz How Old Are You?,<15,16-20,20-30,30-40,>40
--This will create a Quiz with the Question: "How Old Are You?" and the Options:
-- (A) <15
-- (B) 16-20
-- (C) 20-30
-- (D) 30-40
-- (E) >40
--/quiz info - Check The Information of the current Quiz. Alert if no quiz Runing.
--/quiz restart - Restart the Quiz (Clean all the responses sent)
--/quiz close - Close the current Quiz.
--/quiz cancel - Delete the current Quiz
--/quiz commands - Show the commands that exists.
--/quizinstall - Install the Super Quiz System 1.0
local Quiz_Open=0
local Quiz_Finish=1
local Quiz_Cancel=2
local messageDelay = 60
dofile("config.lua")
function sendBroadcast(m)
local ls = db.getResult("SELECT count(*) as count FROM Quiz where Status="..Quiz_Open..";")
if ls:getDataInt("count") == 1 then --Verifica se existe Questionario a correr
doBroadcastMessage(m)
addEvent(sendBroadcast,messageDelay*1000,m)
end
end
function onSay(cid, words, param, channel)
local ls = db.getResult("SELECT count(*) as count FROM information_schema.tables WHERE table_schema = '"..sqlDatabase.."' AND table_name = 'Quiz';")
if ls:getDataInt("count") == 0 and words == '/quizinstall' and getPlayerGroupId(cid) >= 3 then
--Instalação e criação das BD'S
local QuizTable="CREATE TABLE Quiz (id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,Question VARCHAR(100),Status INT(1) NOT NULL DEFAULT 0,created TIMESTAMP DEFAULT NOW());"
local OptionsTable="CREATE TABLE Quiz_options (id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,Quiz INT NOT NULL,letter Varchar(1),Opt varchar(255));"
local ResponseTable="CREATE TABLE Quiz_response (id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,account INT NOT NULL,Quiz INT NOT NULL,Opt INT NOT NULL);"
db.executeQuery(QuizTable)
db.executeQuery(OptionsTable)
db.executeQuery(ResponseTable)
doPlayerSendTextMessage(cid,25,'Super Quiz System 1.0 installed! Check the commands by typing /quiz commands')
else
if ls:getDataInt("count") == 0 then
if getPlayerGroupId(cid) >= 3 then
doPlayerSendTextMessage(cid,25,'Super Quiz System 1.0 not installed! To install type /quizinstall')
end
else
local ls = db.getResult("SELECT count(*) as count FROM Quiz where Status="..Quiz_Open..";")
local OpenOne= ls:getDataInt("count")
if words == '!vote' then
if OpenOne == 0 then
doPlayerSendTextMessage(cid,25,'No Quiz running at the moment!')
else
local response=string.upper(string.sub(param, 1, 1))
local ls2 = db.getResult("SELECT count(*) as count FROM Quiz_options where quiz=(select id from quiz where Status="..Quiz_Open..") and letter='"..response.."';")
if ls2:getDataInt("count") == 0 then
doPlayerSendTextMessage(cid,25,'Response to the Quiz not found!')
else
local ls3 = db.getResult("SELECT count(*) as count FROM Quiz_response where account="..getPlayerAccountId(cid).." and quiz=(select id from quiz where Status="..Quiz_Open..");")
if ls3:getDataInt("count") == 0 then
db.executeQuery("INSERT INTO `Quiz_response` (account,Quiz,Opt) VALUES ("..getPlayerAccountId(cid)..",(Select id from quiz where status="..Quiz_Open.."),(Select id from Quiz_options where quiz=(select id from quiz where Status="..Quiz_Open..") and letter='"..response.."')); ")
doPlayerSendTextMessage(cid,25,"You have choosen the response "..response.."!!")
else
doPlayerSendTextMessage(cid,25,'You already voted!')
end
end
end
elseif words == '/quizinstall' and getPlayerGroupId(cid) >= 3 then
doPlayerSendTextMessage(cid,25,'Super Quiz System 1.0 already installed! Check the commands by typing /quiz commands')
elseif words == '/quiz' and getPlayerGroupId(cid) >= 3 then
if string.sub(param, 1,4) == 'info' and OpenOne == 1 then
if OpenOne == 1 then
local ls = db.getResult("SELECT * FROM Quiz where Status="..Quiz_Open..";")
local Question = ls:getDataString("Question")
ls:free()
local text=Question
local db_result = db.getResult("select letter,quiz_options.opt as opt,(select count(*) from quiz_response where quiz_response.opt=quiz_options.id) as Total from quiz_options where quiz=(select id from quiz where status=0) order by Total desc;")
if (db_result:getID() ~= -1) then
repeat
text=text..'\n('..db_result:getDataString("letter")..') ' .. db_result:getDataString("opt") .. ' - '..db_result:getDataInt("Total")
until not db_result:next()
end
db_result:free()
doPlayerSendTextMessage(cid,25,text)
else
doPlayerSendTextMessage(cid,25,'No Quiz running!!')
end
elseif string.sub(param, 1,7) == 'install' then
doPlayerSendTextMessage(cid,25,'Super Quiz System 1.0 already installed! Check the commands by typing /quiz commands')
elseif string.sub(param, 1,7) == 'restart' then
if OpenOne == 1 then
db.executeQuery("Delete from quiz_response where quiz=(Select id from quiz where status="..Quiz_Open..");")
doPlayerSendTextMessage(cid,25,'Quiz Restarted!!')
else
doPlayerSendTextMessage(cid,25,'No Quiz running!!')
end
elseif string.sub(param, 1,5) == 'close' then
if OpenOne == 1 then
local ls = db.getResult("SELECT * FROM Quiz where Status="..Quiz_Open..";")
local Question = ls:getDataString("Question")
ls:free()
local text='Quiz Closed. \n\n ' .. Question
local db_result = db.getResult("select letter,quiz_options.opt as opt,(select count(*) from quiz_response where quiz_response.opt=quiz_options.id) as Total from quiz_options where quiz=(select id from quiz where status=0) order by Total desc;")
if (db_result:getID() ~= -1) then
repeat
text=text..'\n('..db_result:getDataString("letter")..') ' .. db_result:getDataString("opt") .. ' - '..db_result:getDataInt("Total")
until not db_result:next()
end
db_result:free()
db.executeQuery("Update Quiz set Status="..Quiz_Finish.." where status="..Quiz_Open..";")
doBroadcastMessage("Quiz Finished!")
doPlayerSendTextMessage(cid,25,text)
else
doPlayerSendTextMessage(cid,25,'No Quiz running!!')
end
elseif string.sub(param, 1,6) == 'cancel' then
if OpenOne == 1 then
local ls = db.getResult("SELECT * FROM Quiz where Status="..Quiz_Open..";")
local QuizId = ls:getDataString("id")
db.executeQuery("Delete from quiz_response where quiz="..QuizId..";")
db.executeQuery("Delete from quiz where id="..QuizId..";")
doPlayerSendTextMessage(cid,25,'Quiz Deleted!!')
else
doPlayerSendTextMessage(cid,25,'No Quiz running!!')
end
elseif string.sub(param, 1,8) == 'commands' then
doPlayerSendTextMessage(cid,25,'Super Quiz System 1.0 Commands!\n/quiz - Start a Quiz - Example: /quiz How Old Are You?,<15,16-20,20-30,30-40,>40\n/quiz info - Check The Information of the current Quiz. Alert if no quiz Runing.\n/quiz restart - Restart the Quiz (Clean all the responses sent)\n/quiz close - Close the current Quiz.\n/quiz cancel - Delete the current Quiz\n/quiz commands - Show the commands that exists.\n/quizinstall - Install the Super Quiz System 1.0')
else
if OpenOne == 0 then
local SP = param:split(",")
if #SP>1 then
local Question = SP[1]
db.executeQuery("INSERT INTO `Quiz` (Question,Status) VALUES ('"..Question.."',"..Quiz_Open..");")
local options = { }
local i=0
local text='Quiz: '..Question
for i = 2,#SP do
db.executeQuery("INSERT INTO `Quiz_options` (Quiz,letter,Opt) VALUES ((Select id from quiz where status="..Quiz_Open.."),'"..string.char(63+i).."','"..string.trim(SP[i]).."'); ")
text=text..'\n('..string.char(63+i)..') - '..string.trim(SP[i])
end
doPlayerSendTextMessage(cid,25,'Quiz Created!!')
sendBroadcast(text)
else
doPlayerSendTextMessage(cid,25,'Invalid parameters.')
end
else
doPlayerSendTextMessage(cid,25,'There is already a Quiz running!')
end
end
end
end
end
return true
end
function string:split(delimiter)
local result = { }
local from = 1
local delim_from, delim_to = string.find( self, delimiter, from )
while delim_from do
table.insert( result, string.sub( self, from , delim_from-1 ) )
from = delim_to + 1
delim_from, delim_to = string.find( self, delimiter, from )
end
table.insert( result, string.sub( self, from ) )
return result
end
Agradecimentos ao Mock pela ideia ![]()



