include('TBQuestions.lua')
local TB = {}
TB.QTimeLimit = 25
function TB.Init(ply, txt, toall)
if string.lower(txt) == "/tb start" then
if !ply:IsAdmin() then return end
TB.Running = true
TB.QuestCount = 1
TB.MaxQuest = 20
TB.CurQuestNum = 0
TB.CurQuest = ""
TB.CurAns = ""
TB.PrevQuests = {}
for k,v in pairs(player.GetAll()) do
v:ChatPrint("[TriviaBot]: Trivia Bot initialized! First question coming up in 10 seconds!")
v:ChatPrint("[TriviaBot]: Get ready!")
v.CorrectAns = 0
end
timer.Create("TriviaInitTimer", 10, 1, TB.Generate)
end
end
hook.Add("PlayerSay", "StartTriviaBot", TB.Init)
function TB.Generate()
if !TB.Running then return end
local rand = math.random(1, #TB_QUESTIONS)
if table.HasValue(TB.PrevQuests, rand) then
rand = math.random(1, #TB_QUESTIONS)
end
TB.CurQuestNum = rand
TB.CurQuest = TB_QUESTIONS[rand]
TB.CurAns = TB_ANSWER1[rand]
for k,v in pairs(player.GetAll()) do
v:ChatPrint("[TriviaBot]: Question " .. TB.QuestCount)
v:ChatPrint("[TriviaBot]: " .. TB.CurQuest)
end
timer.Create("TBQuestionTimer", TB.QTimeLimit, 1, TB.NextQuestion)
end
function TB.Advance()
if !TB.Running then return end
TB.QuestCount = TB.QuestCount + 1
if TB.QuestCount > TB.MaxQuest then
TB.End()
return
end
timer.Simple(5, function()
for k,v in pairs(player.GetAll()) do
v:ChatPrint("[TriviaBot]: Next question coming up in 5 seconds.")
end
timer.Create("TBAdvanceTimer", 5, 1, TB.Generate)
end
)
end
function TB.NextQuestion()
if !TB.Running then return end
for k,v in pairs(player.GetAll()) do
v:ChatPrint("[TriviaBot]: No correct answer was given.")
v:ChatPrint("[TriviaBot]: The correct answer is: " .. TB.CurAns)
end
TB.Advance()
end
function TB.CorrectAns(ply, txt)
if !TB.Running then return end
if ((string.lower(txt) == string.lower(TB_ANSWERS1[TB.CurQuestNum])) or
(TB_ANSWERS2[TB.CurQuestNum] and string.lower(txt) == string.lower(TB_ANSWERS2[TB.CurQuestNum])) or
(TB_ANSWERS3[TB.CurQuestNum] and string.lower(txt) == string.lower(TB_ANSWERS3[TB.CurQuestNum])) or
(TB_ANSWERS4[TB.CurQuestNum] and string.lower(txt) == string.lower(TB_ANSWERS4[TB.CurQuestNum]))
) then
ply.CorrectAns = ply.CorrectAns + 1
timer.Remove("TBQuestionTimer")
for k,v in pairs(player.GetAll()) do
v:ChatPrint("[TriviaBot]: Correct answer given by " .. ply .. "!")
v:ChatPrint("[TriviaBot]: The correct answer was: " .. TB.CurAns)
end
TB.Advance()
end
end
hook.Add("PlayerSay", "CorrectTBAnswer", TB.CorrectAns)
function TB.End()
for k,v in pairs(player.GetAll()) do
v:ChatPrint("[TriviaBot]: Game Over.")
v:ChatPrint("[TriviaBot]: Final Scores:")
v:ChatPrint("[TriviaBot]: " .. v .. ": " .. v.CorrectAns)
end
TB.Running = false
end
hook.Add("PlayerSay", "EndTBGame", function(ply, txt) if ply:IsAdmin() then if string.lower(txt) == "/tb end" then TB.End end end end)