AuthorMessage
CrazyGuy
n00b
Posts: 32

This is my first real, lua script for HeXHub.
Works on HeXHub 3.10 or higher and requires lua plugin by Meka][Meka which can be downloaded at the following link:
Lua Plugin
here the link to actual Trivia script
HeX_Trivia_101
Lemmy know how it works.
Any bugs/suggestion can be added to this post  :wink:
CrazyGuy
n00b
Posts: 32

Updated script for compatibility with Lua Plugin 5.02b by Meka][Meka
which can be downloaded HERE
Code:

--[[
Trivia Bot 1.01
Written in LUA 5.1.1 by CrazyGuy
Questions by Nillan™
for HeXHub 3.10 and higher @ http://sf.net/projects/hexhub
requires Lua-Plugin 1.501 by Meka][Meka @ http://www.meka-meka.com/forum/viewtopic.php?p=1940#1940
Updated to v1.02 (October 14th 2006)
>CHANGED: requires Lua-Plugin 1.502b or higher
Updated to v1.03 (October 15th 2006)
>FIXED: small bug in inproper answer casing
>FIXED: small bug in script recognizing correct answer after timer passed
>CHANGED: if the hint matches the answer, it will no longer be given
Updated to v1.04 (October 16th 2006)
>ADDED: profile protection to score resets
>ADDED: commands now all start with !
>CHANGED: Re-wrote script to optimized table-layout
>ADDED: Score-sheet saving/loading
Updated to v1.05 (October 17th 2006)
>FIXED: problem with loading scores
]]--
fScores = "./scripts/trivscores.tbl"
sBotname = "<-Trivia->"
sCmdStart ="!trivstart"
sCmdStop = "!trivstop"
sCmdHelp = "!trivhelp"
sCmdScores = "!trivscores"
sReset = "!trivclear"
sAnswer = ""
sHint = ""
iSteps = 0
tQuestions = {}
tAnswers = {}
tScores = {}
--
--!!!!!!!!!!!!!!!!
--here's a table which allow you to set which profiles can reset scores.
--If you use different profiles than the standard ones, please make sure you update this table
--!!!!!!!!!!!!!!!!
--
tReset = {
   ["Owner"] = 1,
   ["Admin"] = 1,
   ["SOP"] = 1,
   ["OP"] = 1,
   ["KVip"] = 0,
   ["Vip"] = 0,
   ["Reg"] = 0,
   ["Default"] = 0,
   ["OP-begger"] = 0,
   ["VIP-FAGS"] = 0,
   ["LAME-SPAMMER"] = 0,
   ["Banned"] = 0,
   ["Delete"] = 0,
}
tChat = {
   [sCmdStart] = function(_user)
      SendToAll(sBotname, _user.sName.." has started Trivia")
      tmrTrivia:Start();
   end,
   [sCmdStop] = function(_user)
      SendToAll(sBotname, "Trivia was stopped by ".._user.sName)
      tmrTrivia:Stop();
      iSteps = 0
   end,
   [sCmdHelp] = function(_user)
      SendChatTo(_user.iUserID,sBotname,"Here are your available Trivia commands:\r\n\r\n"
         ..sCmdStart.."\t\t-Starts Trivia\r\n"
         ..sCmdStop.."\t\t-Stops Trivia\r\n"
         ..sCmdHelp.."\t\t-Shows this help\r\n"
         ..sCmdScores.."\t-Shows the score-sheet\r\n"
         ..sReset.."\t\t-Clears the score-sheet\r\n\r\n")
   end,
   [sCmdScores] = function(_user)
      table.sort(tScores)
      local sScores = "Current score-sheet:\r\n\r\n\tNickname\t\tScore\r\n\r\n"
      for k in pairs(tScores) do
         sScores = sScores.."\t"..k.."\t\t\t"..tScores[k].."\r\n"
      end
      SendChatTo(_user.iUserID, sBotname, sScores)
   end,
   [sReset] = function(_user)
      if tReset[GetProfileName(_user.iProfile)] == 1 then
         tScores = {}
         SafeList(tScores, "tScores", fScores)
         SendToAll(sBotname, _user.sName.." has reset the scores")
      else
         SendChatTo(_user.iUserID, sBotname, "You're not allowed to use this command")
      end
   end,
}
tPoints = {
   [1] = 10,
   [2] = 8,
   [3] = 6,
   [4] = 4,
   [5] = 2,
}
tSteps = {
   [0] = function()
      local iQ = math.random(1,table.getn(tQuestions))
      SendToAll(sBotname, tQuestions[iQ].."?")
      sAnswer = tAnswers[iQ]
      iSteps = 1
   end,
   [1] = function()
      sHint = sAnswer
      sHint = string.gsub(sHint, "%S", &quot")
      if sHint ~= sAnswer then SendToAll(sBotname, sHint) end
      iSteps = 2
   end,
   [2] = function()
      local iMark = math.ceil(string.len(sAnswer) / 4)
      for k = 1, iMark do
         local iPos = math.random(1,string.len(sAnswer))
         local iChar = string.sub(sAnswer, iPos, iPos)
         sHint = string.sub(sHint,1,iPos-1)..iChar..string.sub(sHint,iPos+1)
      end
      if sHint ~= sAnswer then SendToAll(sBotname, sHint) end
      if string.len(sAnswer) == 1 then
         iSteps = 5
      else
         iSteps = 3
      end
   end,
   [3] = function()
      local iMark = math.ceil(string.len(sAnswer) / 4)
      for k = 1, iMark do
         local iPos = math.random(1,string.len(sAnswer))
         local iChar = string.sub(sAnswer, iPos, iPos)
         sHint = string.sub(sHint,1,iPos-1)..iChar..string.sub(sHint,iPos+1)
      end
      if sHint ~= sAnswer then SendToAll(sBotname, sHint) end
      if string.len(sAnswer) == 2 then
         iSteps = 5
      else
         iSteps = 4
      end
   end,
   [4] = function()
      local iMark = math.ceil(string.len(sAnswer) / 4)
      for k = 1, iMark do
         local iPos = math.random(1,string.len(sAnswer))
         local iChar = string.sub(sAnswer, iPos, iPos)
         sHint = string.sub(sHint,1,iPos-1)..iChar..string.sub(sHint,iPos+1)
      end
      if sHint ~= sAnswer then SendToAll(sBotname, sHint) end
      iSteps = 5
   end,
   [5] = function()
      SendToAll(sBotname, 'Nobody Answered correctly\r\n\tThe correct answer was: '..sAnswer)
      sAnswer = ""
      iSteps = 0
   end,
}
Main = function()
   local fHandleS, sErrorS = io.open(fScores)
   if fHandleS ~= nil then
      fHandleS:close()
      dofile('./scripts/trivscores.tbl')
   end
   local ID = Hub.RegBot(sBotname)
   SendToAll(sBotname, 'Starting Trivia...')
   SendToAll(sBotname, 'Loading questions...')
   local fHandle,sError = io.open("scripts\\trivQ.dat","r")
   if fHandle ~= nil then
      for line in io.lines("scripts\\trivQ.dat") do
          table.insert(tQuestions, line)
      end
      fHandle:close()
      SendToAll(sBotname, 'Loading answers...')
      local fHandleA,sError = io.open("scripts\\trivA.dat","r")
      if fHandleA ~= nil then
         for line in io.lines("scripts\\trivA.dat") do
             table.insert(tAnswers, line)
         end
         fHandleA:close()
         tmrTrivia = NewTimer('TimerCallBack')
         tmrTrivia:Set(15000);
         tmrTrivia:Stop();
         SendToAll(sBotname, 'Total number of questions: '..table.getn(tQuestions))
         local iScores = 0
         for k in pairs(tScores) do iScores = iScores + 1 end
         if iScores ~= 0 then
            SendToAll(sBotname, 'Score-sheet loaded successfully')
         else
            SendToAll(sBotname, 'Unable to load scores. Starting fresh')
         end
         SendToAll(sBotname, 'Trivia has started. Let the games begin !! (or type !trivhelp to figure out how)')
      else
         SendToAll(sBotname, tostring(sError))
         SendToAll(sBotname, 'Failed to start Trivia properly. Please reinstall the script and its files')
         Hub.UnregBot(ID)
      end
   else
      SendToAll(sBotname, tostring(sError))
      SendToAll(sBotname, 'Failed to start Trivia properly. Please reinstall the script and its files')
      Hub.UnregBot(ID)
   end
end
ChatArrival = function(_user,data)
   if string.find(data, "%>%s") ~= nil then _,_,data = string.find(data, "%>%s(.+)") end
   if string.lower(data) == string.lower(sAnswer) then
      if tScores[_user.sName] == nil then
         tScores[_user.sName] = tPoints[iSteps]
      else
         tScores[_user.sName] = tScores[_user.sName] + tPoints[iSteps]
      end
      SendToAll(sBotname, 'Correct '.._user.sName..".\r\n\t You scored "..tPoints[iSteps].." points with that answer.\r\n\tThis brings your total score to: "..tScores[_user.sName])
      SafeList(tScores, "tScores", fScores)
      iSteps = 0
      sAnswer = ""
   else
      if tChat[data] ~= nil then
         tChat[data](_user)
         return 1
      else
         return 0
      end
   end
end
TimerCallBack = function()
   tSteps[iSteps]()
end
Serialize = function(tTable, sTableName, sTab)
   sTab = sTab or "";
   sTmp = ""
   sTmp = sTmp..sTab..sTableName.." = {\n"
   for key, value in pairs(tTable) do
      local sKey = (type(key) == "string") and string.format("[%q]",key) or string.format("[%d]",key);
      if(type(value) == "table") then
         sTmp = sTmp..Serialize(value, sKey, sTab.."\t");
      else
         local sValue = (type(value) == "string") and string.format("%q",value) or tostring(value);
         sTmp = sTmp..sTab.."\t"..sKey.." = "..sValue
      end
      sTmp = sTmp..",\n"
   end
   sTmp = sTmp..sTab.."}"
   return sTmp
end
SafeList = function(tList, sList, fList)   
   local hList = io.open(fList, "w+")
   hList:write(Serialize(tList, sList))
   hList:flush()
   hList:close()
end

 :!: You will still need the 2 data files with questions and answers that are in the rar in the top post  :!:
  :!: post edited Oct 17th. Updated to version 1.05
AJ
Clone
Posts: 7

where do i extract the files ??????
Lord_Zero
Ametuar
Posts: 122

Quoted from AJ
where do i extract the files ??????

This question doesn't belong here. See http://www.meka-meka.com/forum/viewtopic.php?t=476
. But before asking there read what others have said. Maybe your question is already answered.