include('TBQuestions.lua')
local TB = {}
TB.QTimeLimit = 25
TB.MaxQuest = 20
function TB.Init(ply, txt)
if string.lower(txt) == "/tb start" then
if !ply:IsAdmin() then return end
TB.Running = true
TB.QuestCount = 1
TB.CurQuestNum = 0
TB.CurQuest = ""
TB.CurAns = ""
TB.PrevQuests = {}
for k,v in pairs(player.GetAll()) do
v:ChatPrint("[TriviaBot]: Trivia Bot initialized! " .. TB.QTimeLimit .. " Seconds for each question, " .. TB.MaxQuest .. " Question round!" )
v:ChatPrint("[TriviaBot]: First question coming up in 10 seconds!")
v:ChatPrint("[TriviaBot]: Get ready!")
v.CorrectAns = 0
end
timer.Create("TBInitTimer", 10, 1, TB.Generate)
end
end
hook.Add("PlayerSay", "StartTriviaBot", TB.Init)
function TB.Settings(ply, cmd, args)
if !ply:IsAdmin() then return end
if TB.Running then
Msg("[TiviaBot]: Cannot change settings while game is in progress!")
return
end
if string.lower(args[1]) == "time" then
TB.QTimeLimit = tonumber(args[2])
Msg("[TriviaBot]: Question time limit set to " .. args[2] .. ".\n")
end
if string.lower(args[1]) == "mquest" then
TB.MaxQuest = tonumber(args[2])
Msg("[TriviaBot]: Max questions set to " .. args[2] .. ".\n")
end
end
concommand.Add("TBSettings", TB.Settings)
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_ANSWERS1[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.Create("TBNextQuestionTimer", 5, 1, 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.Skip(ply, txt)
if !TB.Running then return end
if !ply:IsAdmin() then return end
if string.lower(txt) == "/tb skip" then
for k,v in pairs(player.GetAll()) do
v:ChatPrint("[TriviaBot]: Question skipped.")
v:ChatPrint("[TriviaBot]: The correct answer is: " .. TB.CurAns)
end
TB.Advance()
end
end
hook.Add("PlayerSay", "TBSkipQuestion", TB.Skip)
function TB.CorrectAns(ply, txt)
if !TB.Running then return end
if ((string.lower(txt) == string.lower(tostring(TB_ANSWERS1[TB.CurQuestNum]))) or
(TB_ANSWERS2[TB.CurQuestNum] and string.lower(txt) == string.lower(tostring(TB_ANSWERS2[TB.CurQuestNum]))) or
(TB_ANSWERS3[TB.CurQuestNum] and string.lower(txt) == string.lower(tostring(TB_ANSWERS3[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 " .. v:Nick .. "!")
v:ChatPrint("[TriviaBot]: The correct answer was: " .. TB_ANSWERS1[TB.CurQuestNum] .. ".")
end
TB.Advance()
end
end
hook.Add("PlayerSay", "CorrectTBAnswer", TB.CorrectAns)
function TB.End()
timer.Remove("TBQuestionTimer")
timer.Remove("TBAdvanceTimer")
timer.Remove("TBInitTimer")
for k, v in pairs(player.GetAll()) do
v:ChatPrint("[TriviaBot]: Game Over.")
v:ChatPrint("[TriviaBot]: Final Scores:")
for j, n in pairs(player.GetAll()) do
v:ChatPrint("[TriviaBot]: " .. n:Nick() .. ": " .. n.CorrectAns)
end
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)