----------------
Code Part 2
----------------
--Do these commands whether they are in PM or main chat
--Help
if (lFW == strlower(HelpTrigg)) then
ShowHelp(curUser)
--Save Scores
elseif (lFW == strlower(SaveTrigg)) then
if (curUser.bOperator or (boolRaligIsOp and strlower(curUser.sName) == "ralig")) then
SaveScores(curUser)
end
--Load Scores
elseif (lFW == strlower(LoadTrigg)) then
if (curUser.bOperator or (boolRaligIsOp and strlower(curUser.sName) == "ralig")) then
LoadScores(curUser)
end
--Change Trivia File
elseif (lFW == strlower(ChangeTrigg)) then
if (curUser.bOperator or (boolRaligIsOp and strlower(curUser.sName) == "ralig")) then
local _, _, secondWord = strfind(data, "%b<>%s+%S+%s+(%S+)")
if (secondWord) then
ChangeTrivia(curUser,secondWord) --Typo fixed by AcE
else
SendDataToPlayer(curUser,"Usage: "..ChangeTrigg.."
", WayToSend)
SendDataToPlayer(curUser," Currently these filenames are accepted:", WayToSend)
for x=1,getn(arrKnownDataFiles) do
SendDataToPlayer(curUser," "..arrKnownDataFiles end
end
else
SendDataToPlayer(curUser,"Only an operator can use that command.", WayToSend)
end
--Change Mode
elseif (lFW == strlower(ModeTrigg)) then
if (curUser.bOperator or (boolRaligIsOp and strlower(curUser.sName) == "ralig")) then
local secondWord = tonumber(strsub(data,strfind(data,ModeTrigg,1,1)+strlen(ModeTrigg)+1))
if (secondWord) then
if ((secondWord <= 3) and (secondWord >= 0)) then
if (secondWord == 0) then
lngMode = lngDefMode
SendToAll(BotName,"Trivia changed to default mode.")
else
lngMode = secondWord
end
if (lngMode == 1) then
SendToAll(BotName,"Trivia changed to Mode 1: Played in main chat by all.")
elseif (lngMode == 2) then
SendToAll(BotName,"Trivia changed to Mode 2: Played in main chat by joining.")
elseif (lngMode == 3) then
SendToAll(BotName,"Trivia changed to Mode 3: Played in PM.")
end
else
SendDataToPlayer(curUser,"That mode is not a valid mode. Valid modes are: 1 2 3")
end
else
SendDataToPlayer(curUser,"That mode is not a valid mode. Valid modes are: 1 2 3")
end
end
--Show Scores
elseif (lFW == strlower(ScoreTrigg)) then
local secondWord = strsub(data,strfind(data,ScoreTrigg,1,1)+strlen(ScoreTrigg)+1)
local pn = nil
if (secondWord == "me") then
pn = curUser.sName
elseif (pn == "") then
pn = nil
else
pn = secondWord
end
ShowScores(curUser, pn)
--Show Player Stats
elseif (lFW == strlower(StatTrigg)) then
local secondWord = strsub(data,strfind(data,StatTrigg,1,1)+strlen(StatTrigg)+1)
local pn = nil
if (secondWord == "me") then
pn = curUser.sName
elseif (secondWord == "" or secondWord == nil) then
pn = curUser.sName
else
pn = secondWord
end
if (pn == curUser.sName) then
if (arrUserData[pn]) then
ShowPlayerStats(curUser,pn)
else
SendDataToPlayer(curUser,"you have 0 points!", WayToSend)
end
else
ShowPlayerStats(curUser,pn)
end
--Join Trivia
elseif (lFW == strlower(JoinTrigg)) then
if (lngMode ==1) then
SendDataToPlayer(curUser, "All players are currently joined to the trivia.", WayToSend)
else
if (not arrPlayers[curUser.sName]) then
arrPlayers[curUser.sName] = 1
SendToPlayers(curUser.sName.." has joined trivia.")
else
SendDataToPlayer(curUser, "You are already joined to the trivia.", WayToSend)
end
end
--Part Trivia
elseif (lFW == strlower(PartTrigg)) then
if (lngMode == 1) then
SendDataToPlayer(curUser, "All players are joined to trivia all the time, there is no need to leave.", WayToSend)
else
if (arrPlayers[curUser.sName]) then
arrPlayers[curUser.sName] = nil
SendToPlayers(curUser.sName.." has left the trivia game.")
SendDataToPlayer(curUser, "You have left the trivia game.", WayToSend)
else
SendDataToPlayer(curUser, "You have not joined the trivia, so it's kinda hard for you to leave it.", WayToSend)
end
end
--Start Trivia Round
elseif (lFW == strlower(StartTrigg)) then
if (boolRoundStarted) then
SendDataToPlayer(curUser,"There is already a round of trivia started. Use "..JoinTrigg.." to join or "..HelpTrigg.." for additional commands.", WayToSend)
else
if (not arrPlayers[curUser.sName]) then
arrPlayers[curUser.sName] = 1 --Changed start game
SendToPlayers(curUser.sName.." has joined trivia.")
end
local secondWord = tonumber(strsub(data,strfind(data,StartTrigg,1,1)+strlen(StartTrigg)+1))
if (secondWord) then
if (secondWord > lngMaxQuestions) then
lngNumQInRound = lngMaxQuestions
elseif (secondWord <= 0) then
lngNumQInRound = lngDefNumQuestions
else
lngNumQInRound = secondWord
end
end
StartRound(curUser)
end
--Stop Trivia Round
elseif (lFW == strlower(StopTrigg)) then
StopRound(curUser)
--Show Current Players
elseif (lFW == strlower(PlayerTrigg)) then
if (lngMode == 1) then
SendDataToPlayer(curUser,"The trivia is in a mode where all players are joined to the game.", WayToSend)
else
ShowPlayers(curUser)
end
end
--Only do these command if the mode and placement of the command are right
if (boolPlayData or ((lngMode == 3) and (boolMessageIsPM))) then
--Show Current Question
if (lFW == strlower(QuestionTrigg)) then
if (((lngMode == 2 or lngMode ==3) and (arrPlayers[curUser.sName])) or (lngMode == 1)) then
if (boolQuestionStarted) then
ShowQuestion()
end
end
--Show answers to previous question
elseif (lFW == strlower(AnswersTrigg)) then
if (((lngMode == 2 or lngMode ==3) and (arrPlayers[curUser.sName])) or (lngMode == 1)) then
if (boolRoundStarted and boolAllowAltAnswersToBeSeen) then
if (not boolQuestionStarted) then
SendDataToPlayer(curUser, "The answers to '"..arrQuestions[lngCurrQandA][1].."' are:")
for x=2, getn(arrQuestions[lngCurrQandA]) do
SendDataToPlayer(curUser, " " .. tostring(x-1) .. ".> " .. arrQuestions[lngCurrQandA] end
else
SendDataToPlayer(curUser,"You can't do that before the question is answered. That would be cheating!")
end
end
end
--Show Hint
elseif (lFW == strlower(HintTrigg)) then
if (((lngMode == 2 or lngMode == 3) and (arrPlayers[curUser.sName])) or (lngMode == 1)) then
if (boolQuestionStarted) then
ShowHint("Hint: ")
else
SendDataToPlayer(curUser, "There is no question to give a hint for!")
end
else
SendDataToPlayer(curUser, "You must join trivia in order to see a hint.")
end
--Skip Question
elseif (lFW == strlower(SkipTrigg)) then
if (((lngMode == 2 or lngMode == 3) and (arrPlayers[curUser.sName])) or (lngMode == 1)) then
if (boolQuestionStarted) then
if (Time_Counter >= lngSkipAllowTime/lngTimerSpeed) then
SkipQuestion()
else
SendToPlayers(lngSkipAllowTime/1000 .." seconds must elapse before a question can be skipped.")
end
else
SendToPlayers("There is currently no question to skip!")
end
end
--Check for answer
else
if (boolQuestionStarted and boolRoundStarted) then
if (((lngMode == 2 or lngMode == 3) and (arrPlayers[curUser.sName])) or (lngMode == 1)) then
local msg = strsub(data, (4 + strlen(curUser.sName)))
if (strlen(msg) >= DetermineAnsMinLen()) then --there is a possibility that the answer is in msg
local strIsAnswerCorrect = CheckForRightAnswer(msg)
if (strIsAnswerCorrect) then -- the correct answer was given by this player
DoTheStreakStuff(curUser.sName)
local arrScoreInfo = IncreaseScore(curUser.sName)
CheckForHigherRanking(curUser.sName)
SendToPlayers("Winner: "..curUser.sName.." Answer: "..strIsAnswerCorrect.." Time: "..arrScoreInfo[2].."s Points: "..arrScoreInfo[1])
SendToPlayers(curUser.sName.." Rank #: "..arrUserData[curUser.sName][3].." Score: "..arrUserData[curUser.sName][1].." Streak: "..lngStreakLen.." Wins: "..arrUserData[curUser.sName][2])
EndQuestion()
end
end
end
end
end -- if (boolPlayData)
end -- if (firstWord ~= nil)
end -- if (boolPlayData)
boolMessageIsPM = nil
end -- function
function OnTimer()
if (boolQuestionStarted) then
Time_Counter = Time_Counter + 1
--Question Time Out
if(lngMaxTime/lngTimerSpeed == Time_Counter) then
SendToPlayers("No one answered that question correctly.")
SendToPlayers("The answer was: "..arrQuestions[lngCurrQandA][2])
EndQuestion()
--AutoHint
elseif ((lngHintTime/lngTimerSpeed == Time_Counter) and boolAutoHint) then
ShowHint("Here's a hint: ")
end
--Skip timer
elseif (boolSkipInProgress) then
boolSkipInProgress = nil
NextQuestion()
--Should only occur in between questions
elseif (boolRoundStarted) then
NextQuestion()
end
end
--End functions called by hub
--Start functions called by script
function ShowQuestion()
local Q = ""
Question_Counter = Question_Counter + 1
if (arrQuestions[lngCurrQandA][1] == "Scramble") then
Q = "Unscramble: "..Scramble(arrQuestions[lngCurrQandA][2])
else
Q = AddPunctuation(arrQuestions[lngCurrQandA][1])
end
SendToPlayers
("["..
lngCurrQNum
.."/"..
lngNumQInRound.."]"..
Q)
--AutoSave stuff
if (lngAutoSaveCount) then
if (mod(Question_Counter,lngAutoSaveCount) == 0) then
SaveScores()
end
end
end
function AddPunctuation(strQ)
local tmp = strsub(strQ,strlen(strQ))
if (tmp ~= "." and tmp ~= "!" and tmp ~= "?") then
strQ = strQ.."?"
end
return strQ
end
--------------
Code Pat 3
--------------
function Scramble(sOrigAns)
--Split the string by words
local tmp = {}
local lngLastSpace = 1
local lngCurrSpace
local cnt = 1
local strAScrambled = ""
--Break the answer into words into the tmp array
lngCurrSpace = strfind(sOrigAns," ",lngLastSpace,1)
while (lngCurrSpace ~= nil) do
tmp[cnt] = strsub(sOrigAns,lngLastSpace,lngCurrSpace-1)
lngLastSpace = lngCurrSpace+1
lngCurrSpace = strfind(sOrigAns," ",lngLastSpace,1)
cnt = cnt + 1
end
tmp[cnt] = strsub(sOrigAns,lngLastSpace)
for x=1,cnt do
local tmp1 = tmp
local strWScrambled = ""
while (strlen(strWScrambled) < strlen(tmp1)) do
local i = random(1,strlen(tmp1))
local ch = strsub(tmp1,i,i)
if (ch ~= "*") then
strWScrambled = strWScrambled..ch
local tmp2 = strsub(tmp1,1,i-1).."*"..strsub(tmp1,i+1)
tmp1 = tmp2
end
end
strAScrambled = strAScrambled..strWScrambled.." "
end
return strlower(strsub(strAScrambled,1,strlen(strAScrambled)-1))
end
function EndQuestion()
if (lngCurrQNum+1 > lngNumQInRound) then
StopRound()
else
Rest()
end
end
function Rest()
StopTimer()
boolQuestionStarted = nil
-- lngCurrQandA = 0
strQEndTime = ""
strQBeginTime = ""
lngNumHintsGiven = 0
Time_Counter = 0
SetTimer(lngRestTime)
StartTimer()
end
function SaveStats()
local file_handle, err = openfile(strTrivStatsFile, "w")
local WayToSend = nil
if (boolMessageIsPM) then
WayToSend = "PM"
else
WayToSend = "MC"
end
if (file_handle) then
write(file_handle,Question_Counter.."\r\n")
write(file_handle,strLongestStreakName.."$"..lngLongestStreakNum.."\r\n")
closefile(file_handle)
else
SendToPlayers(BotName, "Error opening/writing stats file " ..strTrivStatsFile.. " - " ..err) --AcE
if (not arrPlayers[curUser.sName]) then
SendDataToPlayer(curUser, "Error opening/writing score file " ..strTrivStatsFile.. " - " ..err, WayToSend)
end
end
end
function LoadStats()
local file_handle, err = openfile(strTrivStatsFile, "r")
local WayToSend = nil
if (boolMessageIsPM) then
WayToSend = "PM"
else
WayToSend = "MC"
end
if (file_handle) then
local line = read(file_handle)
local cnt = 1
while (line) do
if (cnt == 1) then
Question_Counter = tonumber(line)
elseif (cnt == 2) then
local arrTmp = tokenize(line,"$")
strLongestStreakName = arrTmp[1]
lngLongestStreakNum = tonumber(arrTmp[2])
end
line = read(file_handle)
cnt = cnt + 1
end
closefile(file_handle)
else
if (curUser) then
SendToPlayers(BotName, "Error opening/reading score file " ..strTrivScoreFile.. " - " ..err) --AcE
if (not arrPlayers[curUser.sName]) then
SendDataToPlayer(curUser, "Error opening/reading score file " ..strTrivScoreFile.. " - " ..err, WayToSend)
end
end
end
end
function SaveScores(curUser)
--Error handling added by AcE
local file_handle,err = openfile(strTrivScoreFile, "w")
local WayToSend = nil
if (boolMessageIsPM) then
WayToSend = "PM"
else
WayToSend = "MC"
end
if (file_handle) then
for index, value in arrUserData do
write(file_handle,index..strSSep..value[1]..strSSep..value[2]..strSSep..value[4].."\r\n")
end
if (curUser) then
SendDataToPlayer(curUser,"Scores saved", WayToSend)
end
closefile(file_handle)
else
SendToPlayers(BotName, "Error opening/writing score file " ..strTrivScoreFile.. " - " ..err) --AcE
if (not arrPlayers[curUser.sName]) then
SendDataToPlayer(curUser, "Error opening/writing score file " ..strTrivScoreFile.. " - " ..err, WayToSend)
end
end
SaveStats()
end
function LoadScores(curUser)
--Error handling added by AcE
local file_handle, err = openfile(strTrivScoreFile, "r")
local WayToSend = nil
if (boolMessageIsPM) then
WayToSend = "PM"
else
WayToSend = "MC"
end
if (file_handle) then
arrUserData = {}
local line = read(file_handle)
while line do
local arrTmp = tokenize(line, strSSep)
local tmp = 0
if ((arrTmp[1] ~= nil) and (arrTmp[2] ~= nil) and (arrTmp[3] ~= nil)) then
if (not arrTmp[4]) then
tinsert(arrTmp,0)
end
arrUserData[arrTmp[1]] = {tonumber(arrTmp[2]),tonumber(arrTmp[3]),0,tonumber(arrTmp[4])}
end
line = read(file_handle)
end
if (curUser ~= nil) then
SendDataToPlayer(curUser,"Scores Loaded", WayToSend)
end
closefile(file_handle)
SortPlayerArray()
else
if (curUser) then
SendToPlayers(BotName, "Error opening/reading score file " ..strTrivScoreFile.. " - " ..err) --AcE
if (not arrPlayers[curUser.sName]) then
SendDataToPlayer(curUser, "Error opening/reading score file " ..strTrivScoreFile.. " - " ..err, WayToSend)
end
else
SendToAll(BotName, "Error opening/reading score file " ..strTrivScoreFile.. " - " ..err) --AcE
end
end
end
function LoadQuestions()
--Error handling added by AcE
local file_handle, err = openfile(strTrivFile, "r")
local WayToSend = nil
if (boolMessageIsPM) then
WayToSend = "PM"
else
WayToSend = "MC"
end
if (file_handle) then
arrQuestions = {}
local line = read(file_handle)
while line do
if ((line ~= "") and (strfind(line, strQSep, 1, plain))) then
local arrTmp = tokenize(line,strQSep)
local arrTmp2 = {}
for index, value in arrTmp do
arrTmp2[index] = value
end
tinsert(arrQuestions, arrTmp2)
end
line = read(file_handle)
end
closefile(file_handle)
else
if (curUser) then
SendToPlayers("Error opening/reading questions database " ..strTrivFile.. " - " ..err) --AcE
if (not arrPlayers[curUser.sName]) then
SendDataToPlayer(curUser, "Error opening/reading questions database " ..strTrivFile.. " - " ..err, WayToSend)
end
return 1
end
end
if (getn(arrQuestions) < 1) then
arrQuestions = {
{"Who needs to add more questions to the trivia? Type DJ Valhala to win.","DJ Valhala"},
{"Who brought you this TriviaBot?","assaf"}
}
if (curUser) then
SendToPlayers("Questions NOT loaded from "..strTrivFile..". Current # of questions: "..getn(arrQuestions))
end
else
SendToPlayers("Questions loaded from "..strTrivFile..". Current # of questions: "..getn(arrQuestions))
end
end
function tokenize (inString,token)
_WORDS = {}
local matcher = "([^"..token.."]+)"
gsub(inString, matcher, function (w) tinsert(_WORDS,w) end)
return _WORDS
end
function StartRound(curUser)
boolRoundStarted = 1
if ((lngMode == 1) or ((lngMode == 2 or lngMode == 3) and not boolSilentInMainChat)) then
SendToAll(BotName,strStart)
else
SendToPlayers(strStart)
end
lngCurrQNum = 0
NextQuestion()
end
function StopRound(curUser)
if (boolRoundStarted) then
local WayToSend = nil
if (boolMessageIsPM) then
WayToSend = "PM"
else
WayToSend = "MC"
end