PtokaX forum

Archive => Archived 4.0 boards => Finished Lua 4 scripts => Topic started by: plop on 10 May, 2004, 22:13:46

Title: alcoholic top 10
Post by: plop on 10 May, 2004, 22:13:46
simple useless script i made.
it counts how much ppl say the word bier and can show a top 10.
it's in dutch, so translate it yourself if needed.
Alcohol = {}
Bot = "alcoholic"
timer = date("%H")

function CountBier(user, data)
   data = strlower(data)
   _, bier = gsub(data, "bier", "bier")
   if bier then
      if Alcohol[user.sName] then
         Alcohol[user.sName] = Alcohol[user.sName] + bier
      else
         Alcohol[user.sName] = bier
      end
   end
end

function ShowBier()
   local Tmp = {}
   local line
   local c,d = 0,0
   for a,b in Alcohol do
      if Tmp[b] then
         Tmp[b]=(Tmp[b].."/"..a)
      else
         Tmp[b]=a
      end
      if b > c then
         c = b
      end
   end
   if c ~= 0 then
      for i=c,1, -1 do
         if line == nil then
            line = "\r\n\r\n---------------------------------------------------------------\r\n"
            line = line.."   De alcoholist top 10\r\n"
            line = line.."---------------------------------------------------------------\r\n"
         end
         if Tmp[i] then
            line = line.."   "..i.." biertjes\t"..Tmp[i]..".\r\n"
            d = d + 1
            if d == 10 then
               break
            end
         end
      end
      line = line.."---------------------------------------------------------------\r\n"
   else
      line = "no stats yet"
   end
   Tmp = nil
   return line
end

function Save()
   writeto("alcoholist.lst")
   for a,b in Alcohol do
      write(a.."$"..b.."\r\n")
   end
   writeto()
end

function Load()
   if readfrom("alcoholist.lst") then
      readfrom("alcoholist.lst")
      while 1 do
         local kicks = read()
         if kicks ==  nil then
            break
         end
         s,e,name,amount = strfind(kicks, "(.+)$(.+)")
         Alcohol[name]=tonumber(amount)
      end
      readfrom()
   end
end

function DataArrival(user, data)
   if timer ~= date("%H") then
      Save()
   end  
   if(strsub(data, 1, 4) == "$To:") then
      data=strsub(data,1,strlen(data)-1)
      CountBier(user, data)
   elseif( strsub(data, 1, 1) == "<" ) then
      data=strsub(data,1,strlen(data)-1)
      local s,e,cmd = strfind(data,"%b<>%s+(%S+)")
      if cmd == "!bier" then
         user:SendData(Bot, ShowBier().."|")
         return 1
      else
         CountBier(user, data)
      end
   end
end

function Main()
   Load()
end
plop
Title:
Post by: bastya_elvtars on 12 May, 2004, 11:40:25
Not that bad one... psychologists would surely love it, because this can be part of a DC survey  :D