PtokaX forum

Archive => Archived 5.0 boards => Finished Scripts => Topic started by: jiten on 27 April, 2005, 13:15:25

Title: OnHub Time Logger V.1.65
Post by: jiten on 27 April, 2005, 13:15:25
-- Converted and optimized to Lua 5 by jiten

--OnHub Time Logger V.1.65 by CHILL INC.
--thx to tezlo for the nice calculation from minutes to months, days, etc., and to Nagini, and to RabidWombat
--Create a "txt" Folder in the PtokaX scripts Folder, like this /scripts/txt/

--ChangeLog :
--5.12.03 Some Fixes.
--27.9.03 Added Serialisation.
--28.11.03 Rewriten Timer

tSetup = {
mSet = { -- Script Settings
-- Name this bot to what you want
bot = frmHub:GetHubBotName(),
--This number stands for the maximum of displayed users, set it to what U like
Max = 40,
--This number is for the timespan between each data saving in minutes.
Max1 = 1,
--This number stands for time in minutes, a user must be online till he is saved to the file.
Max2 = 1,
sStuff = {
HTFolder = "txt", -- folder to save the Hubbers File (do create it)
File1 = "UserHubTime.txt", -- Hubbers File
},
-- don't change this
var1 = 0,
SHT = 0,
HubUpTime = frmHub:GetUpTime() or 0,
--------------------
},
UserHubTime = {},
UserSessionTime = {},
}
--- time settings
sec = 1000
min = 60 * sec
-----------------
---------------------------------------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------------------------------------

Main = function()
local f = io.open(tSetup.mSet.sStuff.HTFolder.."/"..tSetup.mSet.sStuff.File1)
if f then
f:close();
dofile(tSetup.mSet.sStuff.HTFolder.."/"..tSetup.mSet.sStuff.File1)
end
SetTimer(1 * min)
StartTimer()
table.foreach(tSetup.UserHubTime, function(i,_)
if GetItemByName(i) then
tSetup.UserSessionTime[i] = 0
end
end)
TopOnliners = GetUserMaxTime()
UpdateTime = "Last Updated: "..os.date("%d/%m/%y at %H:%M:%S")
end

OnTimer = function()
tSetup.mSet.SHT = tSetup.mSet.SHT + 1
table.foreach(tSetup.UserSessionTime, function(i,_)
if GetItemByName(i) then
tSetup.UserSessionTime[i] = tSetup.UserSessionTime[i] + 1
end
end)
tSetup.mSet.var1 = tSetup.mSet.var1 + 1
if tSetup.mSet.var1 >= tSetup.mSet.Max1 then
table.foreach(tSetup.UserSessionTime, function (a,_)
tSetup.UserHubTime[a] = tSetup.UserHubTime[a] + tSetup.UserSessionTime[a]
tSetup.UserSessionTime[a] = 0
if not GetItemByName(a) then
tSetup.UserSessionTime[a] = nil
end
end)
tSetup.mSet.HubUpTime = tSetup.mSet.HubUpTime + tSetup.mSet.SHT
WriteFile(tSetup.UserHubTime, "tSetup.UserHubTime", tSetup.mSet.sStuff.File1)
tSetup.mSet.var1 = 0
tSetup.mSet.SHT = 0
TopOnliners = GetUserMaxTime()
UpdateTime = "Updated: "..os.date("%d/%m/%y at %H:%M:%S")
end
end

OnExit = function()
table.foreach(tSetup.UserSessionTime, function (a,_)
tSetup.UserHubTime[a] = tSetup.UserHubTime[a] + tSetup.UserSessionTime[a]
tSetup.UserSessionTime[a] = 0
if not GetItemByName(a) then
tSetup.UserSessionTime[a] = nil
end
end)
tSetup.mSet.HubUpTime = tSetup.mSet.HubUpTime + tSetup.mSet.SHT
WriteFile(tSetup.UserHubTime, "tSetup.UserHubTime", tSetup.mSet.sStuff.File1)
end

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

OpConnected = NewUserConnected

ChatArrival = function(curUser,data)
data = string.sub(data,1,-2)
local s,e,cmd = string.find ( data, "%b<>%s+[%!%?%+%#](%S+)" )
if cmd then
local tCmds = {
["myhubtime"] = function(curUser,data)
local tmp = tSetup.UserHubTime[curUser.sName] + tSetup.UserSessionTime[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))
if tSetup.mSet.HubUpTime == 0 then curUser:SendData(tSetup.mSet.bot, "You have been online : "..months.." months, "..days.." days, "..hours.." hours, "..minutes.." minutes ( "..tmp.." min ).") return 0 end
curUser:SendData(tSetup.mSet.bot, "You have been online : "..months.." months, "..days.." days, "..hours.." hours, "..minutes.." minutes ( "..tmp.." min ). That is "..math.floor(math.mod((tmp*(tSetup.mSet.HubUpTime/60)),60)).." % of the total HubUpTime.")
end,
["allhubtime"] = function(curUser,data)
local tmp = tSetup.mSet.HubUpTime + tSetup.mSet.SHT
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:SendPM(tSetup.mSet.bot, "Current Top "..tSetup.mSet.Max.." Hubbers ("..UpdateTime..")\r\n\r\n"..
"\tHub's Online Time : "..months.." months, "..days.." days, "..hours.." hours, "..minutes.." minutes ( "..tmp.." min ).\r\n"..
TopOnliners.."\r\n")
end,
["hubtime"] = function (curUser,data)
local tmp = tSetup.mSet.HubUpTime + tSetup.mSet.SHT
local months, days, hours, minutes = math.floor(tmp/518400, 12), math.floor(tmp/43200, 30), math.floor(math.mod(tmp/1440, 24)), math.floor(math.mod(tmp/60, 60))
curUser:SendData(tSetup.mSet.bot, "The hub has now been online : "..months.." months, "..days.." days, "..hours.." hours, "..minutes.." minutes ( "..math.floor(math.mod(tmp/60, 60)).." min ).")
end,

}
if tCmds[cmd] then
return tCmds[cmd](curUser,data), 1
end
end
end

ToArrival = ChatArrival
----------------------------------------------------------------------------------------------------------
WriteFile = function(table, tablename, file)
local handle = io.open(tSetup.mSet.sStuff.HTFolder.."/"..file, "w+")
handle:write(tablename.." = {\n" )
for key, value in table do
if value >= tSetup.mSet.Max2 then
handle:write("\t"..string.format("[%q]",key).." = "..value..",\n")
end
end
handle:write("}");
handle:write("\r\nHubUpTime = "..tSetup.mSet.HubUpTime)
handle:close()
end
----------------------------------------------------------------------------------------------------------
GetUserMaxTime = function()
local TCopy={}
for i,v in tSetup.UserHubTime do
table.insert( TCopy, { tonumber(v),i } )
end
table.sort( TCopy, function(a, b) return (a[1] > b[1]) end)
local msg,border = "\r\n",string.rep ("-", 180)
msg = msg.."\t"..border.."\r\n"
msg = msg.."\tNr.\t\tTotal Time\t\t\t\tStatus\t\tNick\r\n"
msg = msg.."\t"..border.."\r\n"
for i = 1,tSetup.mSet.Max do
if TCopy[i] then
local months, days, hours, minutes, o = 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)), "*Offline*"
if GetItemByName(TCopy[i][2]) then o = "*Online*"; end
local m = o
msg = msg.."\t"..i..".\t"..months.." Months, "..days.." Days, "..hours.." Hours, "..minutes.." Min ( "..TCopy[i][1].." min )\t\t"..m.."\t\t"..TCopy[i][2].."\r\n"
end
end
msg = msg.."\t"..border.."\r\n"
local TCopy={}
return msg
end
----------------------------------------------------------------------------------------------------------
 

[*edit*] small fixes

Cheers
Title: small error
Post by: XPMAN on 22 May, 2005, 18:29:23
Thanx for the script :)   But I noticed when the script is running and someone just enters the hub and tries to use the   myhubtime  feature i get this:

[12:24] Syntax ...test hub\0.3.3.0.b17.08.nt.dbg\scripts\hubtime.lua:221: attempt to perform arithmetic on field `?' (a nil value)
Title:
Post by: jiten on 22 May, 2005, 18:37:50
QuoteOriginally posted by XPMAN
Thanx for the script :)   But I noticed when the script is running and someone just enters the hub and tries to use the   myhubtime  feature i get this:

[12:24] Syntax ...test hub\0.3.3.0.b17.08.nt.dbg\scripts\hubtime.lua:221: attempt to perform arithmetic on field `?' (a nil value)
Can you copy that line (221st)? I can't find it here ;)

Best regards
Title: 221
Post by: XPMAN on 22 May, 2005, 21:40:13
221st

local tmp = tSetup.UserHubTime[curUser.sName] + tSetup.UserSessionTime[curUser.sName]

I guess somehow the error is coming from this.
Title:
Post by: jiten on 23 May, 2005, 16:44:47
Try this one:
-- Converted and optimized to Lua 5 by jiten
-- some bug fixing (thx to zoni)
-- Added: User Uptime sending on connect (requested by kash)
-- Added: !nicktime (requested by kash)

--OnHub Time Logger V.1.65 by CHILL INC.
--thx to tezlo for the nice calculation from minutes to months, days, etc., and to Nagini, and to RabidWombat

--ChangeLog :
--5.12.03 Some Fixes.
--27.9.03 Added Serialisation.
--28.11.03 Rewriten Timer

bot = frmHub:GetHubBotName() -- Name this bot to what you want
Max = 40 -- This number stands for the maximum of displayed users, set it to what U like
Max1 = 1 -- This number is for the timespan between each data saving in minutes.
Max2 = 1 -- This number stands for time in minutes, a user must be online till he is saved to the file.
HTFolder = "txt" -- folder to save the Hubbers File (do create it)
File1 = "UserHubTime.txt" -- Hubbers File
--- don't change this ----------------
var1 = 0
SHT = 0
HubUpTime = frmHub:GetUpTime()
UserHubTime = {}
UserSessionTime = {}
tDelay = {}
---------------------------------------

Main = function()
if loadfile(HTFolder.."/"..File1) then dofile(HTFolder.."/"..File1) else os.execute("mkdir "..HTFolder) end
SetTimer(1000) StartTimer()
table.foreach(UserHubTime, function(i,_)
if GetItemByName(i) then
UserSessionTime[i] = 0
end
end)
TopOnliners = GetUserMaxTime()
UpdateTime = "Last Updated: "..os.date("%d/%m/%y at %H:%M:%S")
end

OnTimer = function()
SHT = SHT + 1
table.foreach(UserSessionTime, function(i,_)
if GetItemByName(i) then
UserSessionTime[i] = UserSessionTime[i] + 1
end
end)
var1 = var1 + 1
if var1 >= Max1 then
table.foreach(UserSessionTime, function (a,_)
UserHubTime[a] = UserHubTime[a] + UserSessionTime[a]
UserSessionTime[a] = 0
if not GetItemByName(a) then
UserSessionTime[a] = nil
end
end)
HubUpTime = HubUpTime + SHT
WriteFile(UserHubTime, "UserHubTime", File1)
var1 = 0
SHT = 0
TopOnliners = GetUserMaxTime()
UpdateTime = "Updated: "..os.date("%d/%m/%y at %H:%M:%S")
end
for nick,v in tDelay do
tDelay[nick]["iTime"] = tDelay[nick]["iTime"] - 1
if tDelay[nick]["iTime"] == 0 then
if UserHubTime[nick.sName] then
local tmp = UserHubTime[nick.sName] + UserSessionTime[nick.sName]
local months, days, hours, minutes = math.floor(tmp/518400, 12), math.floor(tmp/43200, 30), math.floor(math.mod(tmp/1440, 24)), math.floor(math.mod(tmp/60, 60))
if HubUpTime == 0 then nick:SendData(bot, "You have been online : "..months.." months, "..days.." days, "..hours.." hours, "..minutes.." minutes ( "..math.floor(math.mod(tmp/60, 60)).." mins ).") return 0 end
nick:SendData(bot, "You have been online : "..months.." months, "..days.." days, "..hours.." hours, "..minutes.." minutes ( "..math.floor(math.mod(tmp/60, 60)).." mins ). That is "..string.format("%0.2f",(tmp/HubUpTime)*100).." % of the total HubUpTime.")
end
end
end
end

OnExit = function()
table.foreach(UserSessionTime, function (a,_)
UserHubTime[a] = UserHubTime[a] + UserSessionTime[a]
UserSessionTime[a] = 0
if not GetItemByName(a) then
UserSessionTime[a] = nil
end
end)
HubUpTime = HubUpTime + SHT
WriteFile(UserHubTime, "UserHubTime", File1)
end

NewUserConnected = function(curUser)
if UserHubTime[curUser.sName]==nil then
UserHubTime[curUser.sName]=0
end
UserSessionTime[curUser.sName]=0
tDelay[curUser] = {}
tDelay[curUser]["iTime"] = 1
end

OpConnected = NewUserConnected

ChatArrival = function(curUser,data)
local data = string.sub(data,1,-2)
local s,e,cmd = string.find ( data, "%b<>%s+[%!%?%+%#](%S+)" )
if cmd then
local tCmds = {
["myhubtime"] = function(curUser,data)
if UserHubTime[curUser.sName] then
local tmp = UserHubTime[curUser.sName] + UserSessionTime[curUser.sName]
local months, days, hours, minutes = math.floor(tmp/518400, 12), math.floor(tmp/43200, 30), math.floor(math.mod(tmp/1440, 24)), math.floor(math.mod(tmp/60, 60))
if HubUpTime == 0 then curUser:SendData(bot, "You have been online : "..months.." months, "..days.." days, "..hours.." hours, "..minutes.." minutes ( "..math.floor(math.mod(tmp/60, 60)).." mins ).") return 0 end
curUser:SendData(bot, "You have been online : "..months.." months, "..days.." days, "..hours.." hours, "..minutes.." minutes ( "..math.floor(math.mod(tmp/60, 60)).." mins ). That is "..string.format("%0.2f",(tmp/HubUpTime)*100).." % of the total HubUpTime.")
end
end,
["allhubtime"] = function(curUser,data)
local tmp = HubUpTime + SHT
local months, days, hours, minutes = math.floor(tmp/518400, 12), math.floor(tmp/43200, 30), math.floor(math.mod(tmp/1440, 24)), math.floor(math.mod(tmp/60, 60))
curUser:SendPM(bot, "Current Top "..Max.." Hubbers ("..UpdateTime..")\r\n\r\n"..
"\tHub's Online Time : "..months.." months, "..days.." days, "..hours.." hours, "..minutes.." minutes ( "..math.floor(math.mod(tmp/60, 60)).." mins ).\r\n"..
TopOnliners.."\r\n")
end,
["nicktime"] = function (curUser,data)
local s,e,nick = string.find(data, "%b<>%s+%S+%s+(%S+)")
if nick then
if UserHubTime[nick] then
local tmp = UserHubTime[nick] + UserSessionTime[nick]
local months, days, hours, minutes = math.floor(tmp/518400, 12), math.floor(tmp/43200, 30), math.floor(math.mod(tmp/1440, 24)), math.floor(math.mod(tmp/60, 60))
if HubUpTime == 0 then curUser:SendData(bot, nick.." has been online : "..months.." months, "..days.." days, "..hours.." hours, "..minutes.." minutes ( "..math.floor(math.mod(tmp/60, 60)).." mins ).") return 0 end
curUser:SendData(bot, nick.." has been online : "..months.." months, "..days.." days, "..hours.." hours, "..minutes.." minutes ( "..math.floor(math.mod(tmp/60, 60)).." mins ). That is "..string.format("%0.2f",(tmp/HubUpTime)*100).." % of the total HubUpTime.")
else
curUser:SendData(bot,"*** Error: "..nick.." isn't in the tophubbers list.")
end
else
curUser:SendData(bot,"*** Syntax Error: Type !nicktime ")
end
end,

["hubtime"] = function (curUser,data)
local tmp = HubUpTime + SHT
local months, days, hours, minutes = math.floor(tmp/518400, 12), math.floor(tmp/43200, 30), math.floor(math.mod(tmp/1440, 24)), math.floor(math.mod(tmp/60, 60))
curUser:SendData(bot, "The hub has now been online : "..months.." months, "..days.." days, "..hours.." hours, "..minutes.." minutes ( "..math.floor(math.mod(tmp/60, 60)).." mins ).")
end,

}
if tCmds[cmd] then
return tCmds[cmd](curUser,data), 1
end
end
end

ToArrival = ChatArrival
----------------------------------------------------------------------------------------------------------
WriteFile = function(table, tablename, file)
local handle = io.open(HTFolder.."/"..file, "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:write("\r\nHubUpTime = "..HubUpTime)
handle:close()
end
----------------------------------------------------------------------------------------------------------
GetUserMaxTime = function()
local TCopy={}
for i,v in UserHubTime do
table.insert( TCopy, { tonumber(v),i } )
end
table.sort( TCopy, function(a, b) return (a[1] > b[1]) end)
local msg,border = "\r\n",string.rep ("-", 180)
msg = msg.."\t"..border.."\r\n"
msg = msg.."\tNr.\t\tTotal Time\t\t\t\tStatus\t\tNick\r\n"
msg = msg.."\t"..border.."\r\n"
for i = 1,Max do
if TCopy[i] then
local months, days, hours, minutes, o = math.floor(TCopy[i][1]/518400, 12), math.floor(math.mod(TCopy[i][1]/43200, 30)), math.floor(math.mod(TCopy[i][1]/1440, 24)), math.floor(math.mod(TCopy[i][1]/60, 60)), "*Offline*"
if GetItemByName(TCopy[i][2]) then o = "*Online*"; end
local m = o
msg = msg.."\t"..i..".\t"..months.." Months, "..days.." Days, "..hours.." Hours, "..minutes.." Min ( "..math.floor(math.mod(TCopy[i][1]/60, 60)).." mins )\t\t"..m.."\t\t"..TCopy[i][2].."\r\n"
end
end
msg = msg.."\t"..border.."\r\n"
local TCopy={}
return msg
end
----------------------------------------------------------------------------------------------------------

Cheers
Title:
Post by: zoni on 23 May, 2005, 17:16:53
in both versions are error in +hubtime command.

+hubtime shows:
[18:15:05] The hub has now been online : 0 months, 0 days, 5 hours, 12 minutes ( 12 min ).

and +allhubtime
   Hub's Online Time : 0 months, 5 days, 12 hours, 4 minutes ( 7924 min ).

I dunno wot u tried to get with that one but this works fine:

Quote["hubtime"] =      function (curUser,data)

                     local tmp = tSetup.mSet.HubUpTime + tSetup.mSet.SHT

                     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(tSetup.mSet.bot, "The hub has now been online : "..months.." months, "..days.." days, "..hours.." hours, "..minutes.." minutes ( "..math.floor(math.mod(tmp/60, 60)).." min ).")

                  end,

Title:
Post by: jiten on 23 May, 2005, 17:29:59
Thanks for the report, zoni.
I'm going to check those two commands.

Cheers
Title:
Post by: jiten on 23 May, 2005, 19:00:59
Script two posts above this one updated with the debugged version (I hope).

Best regards
Title:
Post by: zoni on 23 May, 2005, 22:20:45
seems working smoothly

nice job jiten.  :]
Title:
Post by: jiten on 24 May, 2005, 08:55:38
Nice :]
Title:
Post by: kash? on 29 June, 2005, 21:17:57
Is it possible to display hubtime of the respective
user at the time of connecting hub after MOTD...

Regards...
Title:
Post by: jiten on 30 June, 2005, 14:19:04
QuoteOriginally posted by kash?
Is it possible to display hubtime of the respective
user at the time of connecting hub after MOTD...

Regards...
Do you mean sending their uptime on connect?
Title:
Post by: kash? on 30 June, 2005, 17:25:21
ya...when a user connects hub,
their hubtime should be shown after MOTD in main chat

Regards,
Title:
Post by: jiten on 30 June, 2005, 20:58:38
Last script updated with your request.

Regards
Title:
Post by: kash? on 30 June, 2005, 22:59:52
Thanx m8, keep it up
Title:
Post by: kash? on 04 July, 2005, 19:50:24
Is it possible to know hubtime of any user
For e.g. +hubtime jiten
and this will show jiten's hubtime

Regards...
Title:
Post by: jiten on 04 July, 2005, 22:04:09
QuoteOriginally posted by kash?
Is it possible to know hubtime of any user
For e.g. +hubtime jiten
and this will show jiten's hubtime

Regards...
Last script updated.

Cheers