PtokaX forum

Archive => Archived 5.0 boards => Finished Scripts => Topic started by: UwV on 05 March, 2005, 14:37:04

Title: OnHub Time Logger V.1.63_LUA5
Post by: UwV on 05 March, 2005, 14:37:04
Code (lua) Select

--OnHub Time Logger V.1.63_LUA5
--27.9.03 by c h i l l e r
--thx to tezlo for the nice calculation from math.minutes to months, days, etc., and to Nagini, and to RabidWombat
--ChangeLog :
--27.9.03 Added Serialisation.
-- 03-04-05 converted to LUA5 for PtokaX by UwV

-- You will need to create " UserHubTime.txt "  in "txt"  Folder in the PtokaX scripts Folder,
-- ,.. like this ->  your_Ptokaxfolder/scripts/txt/UserHubTime.txt  <-
---------------------------------------------------------------------------------------------------------------------------------------------------

-- settings

bot="-=Rimmer=-"        -- Name this bot to what you want
cmd1 = "!myhubtime"    -- Name the commands to what you like
cmd2 = "!allhubtime"
cmd3 = "!hubtime"
Max = 100  --This number stands for the maximum of displayed users for cmd2  , set it to what U like
Max1 = 60  --This number is for the timespan between each data saving in minutes.
Max2 = 23  --This number stands for time in minutes, a user must be online till he is saved to the file.

---------------------------------------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------------------------------------

Sec = 1000
Min = 60 * Sec
var1 = 0
UserHubTime ={
        ["HubUpTime"] = 0,
        }
File1 = "UserHubTime.txt"
dofile("txt/"..File1)
---------------------------------------------------------------------------------------------------------------------------------------------------

function Main()
SetTimer(1 * Min)
StartTimer()
end
---------------------------------------------------------------------------------------------------------------------------------------------------

function OnTimer()
for i,v in UserHubTime do
if GetItemByName(i) or i=="HubUpTime" then
v=v+1
UserHubTime[i]=v
end
end
var1 = var1 + 1
if var1 == Max1 then
WriteFile(UserHubTime, "UserHubTime", File1)
var1 = 0
end
end
---------------------------------------------------------------------------------------------------------------------------------------------------

function NewUserConnected(curUser)
if UserHubTime[curUser.sName]==nil then
UserHubTime[curUser.sName]=0
end
end
OpConnected = NewUserConnected
---------------------------------------------------------------------------------------------------------------------------------------------------

function ChatArrival(curUser,data)
                local _,_,cmd = string.find( data, "%b<>%s+(%S+)|")
if (cmd and string.lower(cmd) == string.lower(cmd1) and UserHubTime[curUser.sName] ) then
local tmp = UserHubTime[curUser.sName]
local months, days, hours, minutes = math.floor(tmp/43200), math.floor(math.mod(tmp/1440, 30)), math.floor(math.mod(tmp/60, 24)), math.floor(math.mod(tmp/1, 60))
curUser:SendData(bot, "You have been online : "..months.." months, "..days.." days, "..hours.." hours, and "..minutes.." minutes ( "..tmp.." min ). That is "..string.format("%.2f",tmp/UserHubTime["HubUpTime"]*100).." % of the total HubUpTime.")
return 1

elseif (cmd and string.lower(cmd) == string.lower(cmd2)) then
curUser:SendData(bot, "   -= The Top "..Max.." Antennea =-\r\n"..
GetUserMaxTime().."\r\n")
return 1

elseif (cmd and string.lower(cmd) == string.lower(cmd3)) then
local tmp = UserHubTime["HubUpTime"]
local months, days, hours, minutes = math.floor(tmp/43200), math.floor(math.mod(tmp/1440, 30)), math.floor(math.mod(tmp/60, 24)), math.floor(math.mod(tmp/1, 60))
curUser:SendData(bot, "The hub has now been online : "..months.." months, "..days.." days, "..hours.." hours, and "..minutes.." minutes ( "..tmp.." min ).")
return 1

end
end
----------------------------------------------------------------------------------------------------------

function WriteFile(table, tablename, filename)
local handle = io.open("txt/"..filename, "w+")
handle:write(tablename.." = {\n" )
for key, value in table do
if value >= Max2 then
handle:write("\t"..string.format("[%q]",key).." = "..value..",\n")
end
end
handle:write("}");
  handle:close()
end
----------------------------------------------------------------------------------------------------------

function GetUserMaxTime()
local TCopy={}
for i,v in UserHubTime do
if i~="HubUpTime" then
table.insert( TCopy, { tonumber(v),i } )
end
end
table.sort( TCopy, function(a, b) return (a[1] > b[1]) end)
local msg ="\r\n"
for i = 1,Max do
if TCopy[i] then
local months, days, hours, minutes = math.floor(TCopy[i][1]/43200), math.floor(math.mod(TCopy[i][1]/1440, 30)), math.floor(math.mod(TCopy[i][1]/60, 24)), math.floor(math.mod(TCopy[i][1]/1, 60))
msg = msg.."\t\t# "..i.."  -  "..TCopy[i][2].."\t::\t"..months.." Months, "..days.." Days, "..hours.." Hours, And "..minutes.." Minutes ( "..TCopy[i][1].." min ).\r\n"
end
end
local TCopy={}
return msg
end


thanks to this board :-)