Triviabot

Share your scripts and packages with other Mudlet users.
Post Reply
DrDetroit
Posts: 5
Joined: Sat Jul 24, 2010 3:37 pm

Triviabot

Post by DrDetroit »

Hi

Below is my implementation of a trivia bot. It was constructed for Age of War mud, which is based on CircleMud code.
I would like t thank all the folks on the mudlet and mudlet-help IRC channels for their willingness to help a newbie, with a special thanks to demonnic and vadi2!

The bot consists of four elements, an alias, a script, a trigger, and a text file called triviaquestions.txt. This file structure is a colon delimited file in the form Question:Answer and it is important to not have any extra lines at the end of the file. You will need to set the path to the file in the script. You will also need to adjust the trigger to respond to the correct prompt for your mud. The bot is currently set to ask a question every 15 seconds. You can adjust this in the script also. The bot will ask all the questions in the file, then tally the scores and announce the winner. The bot is case insensitive.

09/21/10 - I edited the bot to include the ability to handle ties.

Alias triviaboi
Pattern: triviaboi
Code: [show] | [select all] lua
triviaScores = {}
readQuestions()
askQuestion()
enableTrigger("triviabot")
Script name: triviabotscript
Code: [show] | [select all] lua
--a helper function to create tables from strings, written by Richard Warburton
function explode(div,str)
  if (div=='') then return false end
  local pos,arr = 0,{}
-- for each divider found
  for st,sp in function() return string.find(str,div,pos,true) end do
    table.insert(arr,string.sub(str,pos,st-1)) -- Attach chars left of current divide.
     pos = sp + 1 -- Jump past current divider
  end
  table.insert(arr,string.sub(str,pos)) -- Attach chars right of last divider
  return arr
end
--function to read the lines from a file and build a table. colon delimited, no need for question numbers. form is   question:answer
function readQuestions(file)
  triviaQuestions = {}
  if file == nil then file = "/full/path/to/triviaquestions.txt" end
    for line in io.lines(file) do
    table.insert(triviaQuestions, explode(":",line))
  end
  qNum = 1 --sets the table position for the first question
  qTotal = #triviaQuestions --sets the total number of questions to ask
end
--function to ask trivia question
function askQuestion()
    currentQuestion = triviaQuestions[qNum][1]
	 currentAnswer = string.lower(triviaQuestions[qNum][2])
   --currentAnswer = triviaQuestions[qNum][2]
  if qNum == 1 then
    send("qsay First Question")
  elseif qNum == qTotal then
    send("qsay Last Question") 
  else                
    send("qsay Next Question")
  end
  --wait 1 sec then ask the question
  tempTimer(1, function () send(string.format("qsay %s: %s", qNum, currentQuestion)) end)
  --wait 15 seconds after asking the question before giving up the ghost. We store the id of the tempTimer in the variable
  --answerTimer so we can kill it when someone gets it right
  answerTimer = tempTimer(15, function() send("qsay Nobody got it right!") ; qNum = qNum + 1; if qNum <= qTotal then askQuestion() else tallyScores() end end)
end
 
function tallyScores()
  disableTrigger("triviabot")
  triviaWinner = {}
  triviaWinnerScore = 0
  for player,score in pairs(triviaScores) do
    if score > triviaWinnerScore then
      triviaWinner = {}
      table.insert(triviaWinner, player)
      triviaWinnerScore = score
    elseif score == triviaWinnerScore then
      table.insert(triviaWinner, player)
    end
  end
  if #triviaWinner == 1 then
    send(string.format("qsay And the winner is: %s with a score of %s", triviaWinner[1], triviaWinnerScore))
  else
    send(string.format("qsay We have %s winners today, with a score of %s. They are: %s", #triviaWinner, triviaWinnerScore, table.concat(triviaWinner, ", ")))
  end
end
The trigger is automatically turned on and off by the alias and script.
Trigger name: triviabot
responds to prompt: ^\[Quest\] (\w+): (.*)$ perl regex
Code: [show] | [select all] lua
if currentAnswer == string.lower(matches[3]) then
  playerName = matches[2]
  killTimer(answerTimer)
  send(string.format("qsay %s is correct", playerName))
  if triviaScores[playerName] then
    triviaScores[playerName] = triviaScores[playerName] + 1
  else
    triviaScores[playerName] = 1
  end
  qNum = qNum + 1
  if qNum <= qTotal then askQuestion() else tallyScores() end 
end

Post Reply