:Scripting:Utilities:Central:
 

News:

29 December 2022 - PtokaX 0.5.3.0 (20th anniversary edition) released...
11 April 2017 - PtokaX 0.5.2.2 released...
8 April 2015 Anti child and anti pedo pr0n scripts are not allowed anymore on this board!
28 September 2015 - PtokaX 0.5.2.1 for Windows 10 IoT released...
3 September 2015 - PtokaX 0.5.2.1 released...
16 August 2015 - PtokaX 0.5.2.0 released...
1 August 2015 - Crowdfunding for ADC protocol support in PtokaX ended. Clearly nobody want ADC support...
30 June 2015 - PtokaX 0.5.1.0 released...
30 April 2015 Crowdfunding for ADC protocol support in PtokaX
26 April 2015 New support hub!
20 February 2015 - PtokaX 0.5.0.3 released...
13 April 2014 - PtokaX 0.5.0.2 released...
23 March 2014 - PtokaX testing version 0.5.0.1 build 454 is available.
04 March 2014 - PtokaX.org sites were temporary down because of DDOS attacks and issues with hosting service provider.

Main Menu

:Scripting:Utilities:Central:

Started by Herodes, 11 July, 2004, 00:19:43

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Herodes

Hey ppl ...
I just want to see how many Utility snippets we can gather before reapeting ourselves ...:D

By saying that I mean :
Code snippets that can be used as they are to return some specific result ....

Lets kick off with some basic ones :
------------------------------------------------------ -? System ?- ------------------------------------------------------
--- // --- Getting data for the truely Global Variables ... 
------------------------------------------------------------------------------------------------------------------------------
function GetTime()			--- The Time 

seconds = date("%S")
minutes = date("%M")
hours = date("%H")
time = hours..":"..minutes..":"..seconds

return time

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

------------------------------------------------------------------------------------------------------------------------------
function GetDate()			--- The Date

day = date("%d")
month = date("%m")
year = date("%y")
date = day.."-"..month.."/"..year

return date

end
------------------------------------------------------------------------------------------------------------------------------
Some user-based stuff goes here
------------------------------------------------------ - User Object - ------------------------------------------------------
--- // --- User Info standard .. :D			(these go into DataArrival ..)
------------------------------------------------------------------------------------------------------------------------------
function FindMode(user)	---- Find info from tag ( DC++ )

s,e,mode = strfind(user.sMyInfoString,  ",M:(%S)")
modestr = "Mode : "..mode

return modestr

end
------------------------------------------------------------------------------------------------------------------------------
function FindHubs(user)	---- Find info from tag ( DC++ )

s,e,guest,regged,opped = strfind(user.sMyInfoString,  ",H:(%d+)/(%d+)/(%d+)")
gueststr = guest.." as normal user"
regstr = regged.."as registered user" 
opstr = opped.." as operator"
allstr = "Hubs : "..guest.." ( normal ), "..regged.." ( registered ), "..opped..." ( operator )"

guestnum = tonumber(guest)
regnum = tonumber(regged) 
opnum = tonumber(opped)
allnum = tonumber(guest) + tonumber(regged) + tonumber(opped)

return gueststr, regstr, opstr, allstr, guestnum, regnum, opnum, allnum

end
------------------------------------------------------------------------------------------------------------------------------
function FindSlots(user)	---- Find info from tag ( DC++ )

s,e,slots = strfind(user.sMyInfoString,  ",S:(%d+)")
slotstr = "Slots :\t"..slots
slotnum = tonumber(slots)
return slotstr, slotnum

end
------------------------------------------------------------------------------------------------------------------------------
Carrying on with an external file ...
------------------------------------------------------ - File Operation - ------------------------------------------------------
--- // ---  This is so info can be saved in a file ( retrieval method has to be posted as well )
------------------------------------------------------------------------------------------------------------------------------
function Serialize(tTable, sTableName, hFile, sTab) 	--- Famous Utility Function

assert(tTable, "tTable equals nil");
assert(sTableName, "sTableName equals nil");
assert(hFile, "hFile equals nil");
assert(type(tTable) == "table", "tTable must be a table!");
assert(type(sTableName) == "string", "sTableName must be a string!");
sTab = sTab or "";
write(hFile, sTab..sTableName.." = {\n" );

for key, value in tTable do
	local 	sKey = ( type(key) == "string" ) and format("[%q]",key) or format("[%d]",key);
	if ( type(value) == "table" ) then
		Serialize(value, sKey, hFile, sTab.."\t");
	else	local 	sValue = (type(value) == "string") and format("%q",value) or tostring(value);
		write(hFile, sTab.."\t"..sKey.." = "..sValue);
	end
	write(hFile, ",\n");
end

write(hFile, sTab.."}");

end
-----------------------------------------------------------------------------------
function SaveFile(table , tablename, file)

local 	hFile = openfile(file, "w");
Serialize(table, tablename, hFile);
closefile(hFile);

end
-----------------------------------------------------------------------------------
function LoadFile(file)

assert(readfrom(file),file.." is not found.")
dostring(read("*all"))
readfrom()

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

Pls try to keep the functions as indepented as possible ...
This aims to save time from ppl staring their own scripts and ppl trying to understand
how you do stuff in the awsome Ptx scirpting platform.

Feel free and share all ..

kepp

--// Get amount of Online users by Profile Name
function Online(strProfile)
   tblFolks = GetUsersByProfile(strProfile)
   Count = 0
   for i=0,getn(tblFolks) do
      if GetItemByName(tblFolks[i]) then
         Count = Count + 1
      end
   end
   return Count
end
Guarding    

Herodes

thanks for the input kepp,

I think I can use this straight away :)

NotRabidWombat

#3
"--- Famous Utility Function"
LOL!

On that note: here is a more sophisticated version of serialization:
http://www.dekorte.com/Software/Lua/LuaPickle/Download/Pickle_preLua5.0/Pickle.lua

Binary Search
function BinarySearch(tTable, SearchVar)
	assert(tTable and SearchVar);

	local iBot, iMid, iTop = 1, nil, getn(tTable);
	local CompVal = nil;

	if(getn(tTable) == 0) then return -1, 1; end

	while(iBot <= iTop) do
		iMid = floor((iTop + iBot) / 2);

		CompVal = tTable[iMid];

		if(CompVal == nil) then return -1, -1; end

		if(SearchVar < CompVal) then
			iTop = iMid - 1;
		elseif(SearchVar > CompVal) then
			iBot = iMid + 1;
		else
			return iMid, -1;
		end
	end

	if(SearchVar > CompVal) then 
		iMid = iMid + 1;
	end

	return -1, iMid;
end
Element Count for Zero Based Array
Usefull for GetUsersByProfile(...)
function zgetn(tTable)
   return (tTable and (getn(tTable) + (tTable[0] and 1 or 0))) or 0;
end

This is an excellent idea. However, I feel it will grow out of control as the thread grows in size. Perhaps someone should host these snippets on a site, offering a name, a simple description, and a link to the snippet. Personally, I wouldn't expect any credit to be given since most of these functions have been writen before.

*EDIT*
"Pls try to keep the functions as indepented as possible ..."
These functions should probably not modify globals then. Remember, you can return more than one variable.
*/EDIT*

-NotRabidWombat


I like childish behavior. Maybe this post will be deleted next.

plop

--
#4
QuoteOriginally posted by NotRabidWombat
This is an excellent idea. However, I feel it will grow out of control as the thread grows in size. Perhaps someone should host these snippets on a site, offering a name, a simple description, and a link to the snippet. Personally, I wouldn't expect any credit to be given since most of these functions have been writen before.

-NotRabidWombat
sounds like a nice new section for my website.
i'll bombart herodes 2 moderator of that section.

plop
http://www.plop.nl lua scripts/howto\'s.
http://www.thegoldenangel.net
http://www.vikingshub.com
http://www.lua.org

>>----> he who fights hatred with hatred, drives the spreading of hatred <----<<

Herodes

...
#5
Thanks for putting ur pieces up boys ...

Now this one returns any given text file ...

function ReadTextFile(file)
local message = "\r\n"
readfrom(file, "r")
while 1 do
local 	line = read()
	if ( line == nil ) then break
	else 	message = message.."\t"..line.."\r\n"
	end
end
readfrom()	
return message
end

Optimus

function Readtextfile(file)
	local text = ""
	local line
	local handle = openfile(file, "r") 
	if (handle) then
		local line = read(handle)
		while line do
			if line then
				text = text..line.."\r\n"
			end
			line = read(handle)
		end
		closefile(handle)
	else
		text = text.."Error file "..file.." not found!\r\n"
	end
	return text
end

And here is a other one

Optimus

--// Get ProfileName
function tProfileName(who)
	local tmp = GetProfileName(who.iProfile) or "User"
	return tmp
end

This is a nice and small one

Herodes

Hey Opti nice keystrokes ... :D

--- I was thinking that a zipped file would be nice to have ....
but this utility collection has to grow more ...

I am sure some ppl in this forum have some debugger snippets ... so I'd like to see something like this too ...

Keep em coming !!

NotRabidWombat

This exert is explained here: http://lua-users.org/wiki/OptimisationCodingTips
function fast_assert(condition, ...)
    if not condition then
        if getn(arg) > 0 then
            assert(condition, call(format, arg))
        else
            assert(condition)
        end
    end
end

Aother flavor.
function Readtextfile(file)
   local text;
   local handle = readfrom(file);
   if (handle) then
      text = gsub( read(handle, "*a") , "\n", "\r\n" ) or
          "Empty File.\r\n";
      closefile(handle)
   else
      text = "Error file "..file.." not found!\r\n"
   end
   return text
end

-- Return: fractional part of number
function frac(num)
	return num - floor(num);
end

-- Convert Gregorian Date to Julian Date
function JulianDate(DAY, MONTH, YEAR, HOUR, MINUTE, SECOND) -- HOUR is 24hr format
	local jy, ja, jm;

	assert(YEAR ~= 0);
	assert(YEAR ~= 1582 or MONTH ~= 10 or DAY < 4 or DAY > 15);
	--The dates 5 through 14 October, 1582, do not exist in the Gregorian system!");

	if(YEAR < 0 ) then
		YEAR = YEAR + 1;
	end

	if( MONTH > 2) then 
		jy = YEAR;
		jm = MONTH + 1;
	else
		jy = YEAR - 1;
		jm = MONTH + 13;
	end

	local intgr = floor( floor(365.25*jy) + floor(30.6001*jm) + DAY + 1720995 );

	--check for switch to Gregorian calendar
	local gregcal = 15 + 31*( 10 + 12*1582 );
	if(DAY + 31*(MONTH + 12*YEAR) >= gregcal ) then
		ja = floor(0.01*jy);
		intgr = intgr + 2 - ja + floor(0.25*ja);
	end

	--correct for half-day offset

	local dayfrac = HOUR / 24 - 0.5;
	if( dayfrac < 0.0 ) then
		dayfrac = dayfrac + 1.0;
		intgr = intgr - 1;
	end

	--now set the fraction of a day
	local frac = dayfrac + (MINUTE + SECOND/60.0)/60.0/24.0;

	--round to nearest second
	local jd0 = (intgr + frac)*100000;
	local  jd  = floor(jd0);
	if( jd0 - jd > 0.5 ) then jd = jd + 1 end
	return jd/100000;
end

-- Convert Julian Date to Gregorian Date
-- return: day, month, year, hour, minute, second
function GregorianDate(jDate)
	local	j1, j2, j3, j4, j5;

	--
	-- get the date from the Julian day number
	--
	local intgr   = floor(jd);
	local frac    = jd - intgr;
	local gregjd  = 2299161;

	if( intgr >= gregjd ) then				--Gregorian calendar correction
		local tmp = floor( ( (intgr - 1867216) - 0.25 ) / 36524.25 );
		j1 = intgr + 1 + tmp - floor(0.25*tmp);
	else
		j1 = intgr;
	end

	--correction for half day offset
	local dayfrac = frac + 0.5;
	if( dayfrac >= 1.0 ) then
		dayfrac = dayfrax - 1.0;
		j1 = j1 + 1;
	end

	j2 = j1 + 1524;
	j3 = floor( 6680.0 + ( (j2 - 2439870) - 122.1 )/365.25 );
	j4 = floor(j3*365.25);
	j5 = floor( (j2 - j4)/30.6001 );

	local d = floor(j2 - j4 - floor(j5*30.6001));
	local m = floor(j5 - 1);
	if( m > 12 ) then m = m - 12 end

	local y = floor(j3 - 4715);

	if( m > 2 )	then y = y - 1; end
	if( y <= 0 ) 	then y = y - 1; end

	--
	-- get time of day from day fraction
	--
	local hr  = floor(dayfrac * 24.0);
	local mn  = floor((dayfrac*24.0 - hr)*60.0);
		 f  = ((dayfrac*24.0 - hr)*60.0 - mn)*60.0;
	local sc  = floor(f);
		 f = f - sc;
	if( f > 0.5 ) then sc = sc + 1; end

	return d,m,y,hr,mn,sc;
end

-NotRabidWombat


I like childish behavior. Maybe this post will be deleted next.

VidFamne

Here's another Julian number function. Gives you minute-number. ;)
function Jmn() --(Modified Julian "minute" number. This restricts the algorithm to 1900 Mar 01 until 2100 Feb 28) 
  local D = tonumber(date("%d")) 
  local H = tonumber(date("%H")) 
  local minutE = tonumber(date("%M")) 
  local Y = tonumber(date("%Y")) 
  local M = tonumber(date("%m")) 
    if M <= 2 then 
      M = M + 12 
      Y=Y-1 
    end 
        mn = 1440*(floor(Y*365,25) + floor((M+1)*30,6) + D -428) + H*60 + minutE 
        return mn 
end

kepp

Here's another i worked on to Show bots to specific  profiles

function Main()
   SendToAll(UnregBot("Unregister_My_Old_Bot"))
end

function NewUserConnected(curUser)
   -- Show bots depending on what User Profiles are
   if curUser.iProfile == 0 or curUser.iProfile == 1 then
      curUser:SendData(RegBot("Test"))
   end
end

OpConnected = NewUserConnected


--////////////////////////////////////////////////////////////////////////////
--// 
--////////////////////////////////////////////////////////////////////////////
function RegBot(...)
   strBots = "";
   for i,v in arg do
      if not strfind(v,getn(arg)) then
         strBots = strBots..v.."$$";
      end
   end
   if strBots then return "$OpList "..strBots.."|" end
end

function UnregBot(...)
   strBots = "";
   for i,v in arg do
      if not strfind(v,getn(arg)) then
         strBots = strBots.."$Quit "..v.."|";
      end
   end
   if strBots then return strBots; end
end
Guarding    

Herodes

Back to contribution mode after a dreadful afternoon dealing diplomatics
Here is a nice one ... I don't know about its efficiency(optimal mem usage), though it gets the job done ...

--- // --- Transforming bytes into KB, MB, GB, TB, PT and Returning the ideal (highest possible) Unit
function BytesPostFix(size)
local tUnits = {}
tUnits[1] = tonumber(size)
if ((tonumber(tUnits[1]))  and (tonumber(tUnits[1]) >= 1024)) then
	tinsert(tUnits, 2, ((tonumber(tUnits[1]))/1024))
else	return tonumber(tUnits[1])
end
if ((tonumber(tUnits[2])) and (tonumber(tUnits[2]) >= 1024)) then
	tinsert(tUnits, 3, ((tonumber(tUnits[1]))/(1024^2)))
end
if ((tonumber(tUnits[3])) and (tonumber(tUnits[3]) >= 1024)) then
	tinsert(tUnits, 4, ((tonumber(tUnits[1]))/(1024^3)))
end
if ((tonumber(tUnits[4]) and (tonumber(tUnits[4]) >= 1024)) then
	tinsert(tUnits, 5, ((tonumber(tUnits[1]))/(1024^4)))
end
if ((tonumber(tUnits[5]) and (tonumber(tUnits[5]) >= 1024)) then
	tinsert(tUnits, 6, ((tonumber(tUnits[1]))/(1024^5)))
end

local rdSizeStr = format("%0.2f", tUnits[getn(tUnits)])

if (getn(tUnits) == 1) then 
	rdSizeStr = rdSizeStr.." B"
elseif (getn(tUnits) == 2) then 
	rdSizeStr = rdSizeStr.." KB"
elseif getn(tUnits) == 3 then 
	rdSizeStr = rdSizeStr.." MB"
elseif getn(tUnits) == 4 then 
	rdSizeStr = rdSizeStr.." GB"
elseif getn(tUnits) == 5 then 
	rdSizeStr = rdSizeStr.." TB"
elseif getn(tUnits) == 6 then 
	rdSizeStr = rdSizeStr.." PB"
end
tUnits = nil
return rdSizeStr
end

kepp

Nice, This does pretty much the same thing though

function GetByteUnit(intSize)
   intSize = tonumber(intSize);
   strPut = "";
   if intSize >= 1073741824 then
      strPut = format("%0.2f",intSize / 1024 / 1024 / 1024).." GB"
   elseif intSize >= 1048576 then
      strPut = format("%0.2f",intSize / 1024 / 1024).." MB"
   elseif intSize >= 1024 then
      strPut = format("%0.2f",intSize / 1024).." Kb"
   else
      strPut = intSize.." Bytes"
   end
   return strPut or "0 Bytes";
end
Guarding    

NotRabidWombat

#14
How small can we go?!
Just add or remove to the end of tUnits depending on your needs.
function GetByteUnit(intSize)
	local tUnits = { "Bytes", "KB", "MB", "GB", "TB" }
	intSize = tonumber(intSize);
	local sUnits;
	for index = 1, getn(tUnits) do
		if(intSize < 1024) then
			sUnits = tUnits[index];
			break;
		else
			intSize = intSize / 1024;
		end
	end
	return format("%0.2f %s",intSize, sUnits);
end

-NotRabidWombat


I like childish behavior. Maybe this post will be deleted next.

kepp

Ok, Nice, but consider it LUA 4!! lol
Or maybe you did make it by purpose for lua5 ?
Nice :)
Guarding    

NotRabidWombat

Oh duh! Did my testing in Lua 5 :-)
Just forgot to convert.

Edited

-NotRabidWombat


I like childish behavior. Maybe this post will be deleted next.

Herodes

Another way to prove the results of colloaborative work would be for me to grab this b>mb>gb>tb snippet  and use it rightaway... :D

Thats what I am gonna do lol

--- thanks for this one to kepp and NotRambitWombat

Typhoon

also a small but very usefull one ...

doGetProfile =  GetProfileName(user.iProfile)

usage :

      "..doGetProfile.."

-Typhoon?



Herodes

Say u wanna view a table for debugging purposes, huh?
try the following script ....

tTable = { ["theFirstvalue"] = {
					["level"] = "AAA1",
					["nrvalues"] = 33300000,
					["goodarray"] = { "array A1", "array A2", "array A3", 123 },
					},
		["theSecond"] = {
					["2level"] = "AAA2",
					["2nrvalues"] = 3,
					["2goodarray"] = { "array B1", "array B2", "array B3", 456},
					},
		};
function DataArrival(user,data)
	if strsub(data, 1, 1) == "<" then
		local s, e, cmd = strfind(data, "%b<>%s+(%S+)")
		SendToAll(cmd)
		if strsub(cmd, 1 ,3) == "!do" then 
			ShowTable(tTable)
		end
	end
end
	
function ShowTable(table)
	if count == nil then
		count = 1
	else 
		count = count + 1
	end

	for index, val in table do 
		if type(val) == "table" then
				SendToAll("--------> Table/Array STARTS here ...")
				ShowTable(val)
				SendToAll("<-------- Table/Array ENDS here ...")
		elseif type(val) == "string" or type(val) == "number" then
				if count ~= nil then
					SendToAll(strrep("- ", count*2)..">  "..index.."  >?<  "..val.."    it is a "..type(val))
				else 
					SendToAll( ">  "..index.."  >?<  "..val.."   it is a "..type(val))
				end
		end
	end

end


the function u need is this one ...
function ShowTable(table)
	if count == nil then
		count = 1
	else 
		count = count + 1
	end

	for index, val in table do 
		if type(val) == "table" then
				SendToAll("--------> Table/Array STARTS here ...")
				ShowTable(val)
				SendToAll("<-------- Table/Array ENDS here ...")
		elseif type(val) == "string" or type(val) == "number" then
				if count ~= nil then
					SendToAll(strrep("- ", count*2)..">  "..index.."  >?<  "..val.."    it is a "..type(val))
				else 
					SendToAll( ">  "..index.."  >?<  "..val.."   it is a "..type(val))
				end
		end
	end

end

Herodes

and some funny snippet over here ...
function LineStr(str)
local out = ""
local c = nil
	for i = 1, strlen(str) do
		if c == nil then 
			c = 1 
		else c = c +1 
		end
		out = out..gsub(strsub(str, c, c), "(.)",  "( "..strsub(str, c, c).." )", 1)
	end
	str = out
	out = nil
	return str
end
(G)(e)(e)(!)( )(I)( )(w)(o)(n)(d)(e)(r)( )(w)(h)(a)(t)( )(t)(h)(i)(s)( )(o)(n)(e)( )(d)(o)(e)(s)

Herodes

#21
This one gives an nice clean time string ....
in the form of  " 2 days 3 hours 34 minutes 40 seconds"
it will not show any reduntants ... ex: 0 days.. or 0 minutes ...
[*note*] The initial value ' time ' is in seconds and not miliseconds ....

function DoTimeUnits(time)
	local tTimes = {}
	local time = time * 1000
	if ( time >= 86400000 ) then
	repeat 
		if tTimes[4] then
			tTimes[4] = tTimes[4] + 1
		else tTimes[4] = 1
		end
		time = time - 86400000 
	until time < 86400000
	end

	if ( time >= 3600000 ) then
	repeat 
		if tTimes[3] then
			tTimes[3] = tTimes[3] + 1
		else tTimes[3] = 1
		end
		time = time - 3600000 
	until time < 3600000
	end

	if ( time >= 60000 ) then
	repeat
		if tTimes[2] then
			tTimes[2] = tTimes[2] + 1
		else tTimes[2] = 1
		end
		time = time - 60000
	until time < 60000
	end
	
	if ( time >= 1000 ) then
	repeat 
		if tTimes[1] then
			tTimes[1] = tTimes[1] + 1
		else tTimes[1] = 1
		end
		time = time - 1000
	until time < 1000
	end
local msg = ""
local tTimeUns = { "seconds", "minutes", "hours", "days"}
for i,v in tTimes do 
	msg = v.." "..tTimeUns[i].." "..msg
end
return msg
end
for the time action above I suspect there is a nicer-shorter way to do it ... I tried something but it needs more work so thats why I post this one ... :)
Any input on this will be mostly appreciated ...


and this one is used so that user can be handled in an appropriate way ... :)
--- // --- Performing action and also informing the user about it ... It is handled from the ' TheAction ' variable ....
function Act(user)
	local tActions = {
		[1] = function(user) user:SendPM(wsBot, "You will now be disconnected") user:Disconnect() end,
		[2] = function(user) user:SendPM(wsBot, "You will now be redirected to "..frmHub:GetRedirectAddress()) user:SendData("$OpForceMove "..frmHub:GetRedirectAddress()) end,
		[3] = function(user) user:SendPM(wsBot, "You are kicked") user:Kick() end,
		[4] = function(user) user:SendPM(wsBot, "You are temporalily Banned") user:TempBan() end,
		[5] = function(user) user:SendPM(wsBot, "You are permenantly Banned") user:Ban() end,
		};
	for i,v in tActions do
		if i == TheAction then
			tUsers[user.sName] = nil
			return tActions[i](user)
		end
	end
	tActions = nil
end

Herodes

Can some1 help me with the DoTimeUnits function posted above ? I wanted to see if I could make it nicer but I cant really get anywhere ... I still think there has to be better way . :)

Herodes

#23
OooOleeeee!!!!!
I found that better way on the DoTimeUnits Function in an earlier post ;)
Here it is ...
function DoTimeUnits(time)
	local time = time*1000
	local msg = ""
	local tO = {
		[1] = { 86400000, 0, "days"},
		[2] = { 3600000, 0, "hours"},
		[3] = { 60000, 0, "minutes"},
		[4] = { 1000, 0, "seconds"},
		};
	for i , v in (tO) do
		if time >= tO[i][1] then
			repeat 
				tO[i][2] = tO[i][2] + 1
				time = time - tO[i][1]
			until time < tO[i][1]
		end
	end
	for i,v in tO do 
		if tO[i][2] ~= 0 then
			msg = msg.." "..tO[i][2].." "..tO[i][3]
		end
	end
	return msg
end

To the above function add the snippet found below and witness the perfect uptime string (...lol) with ' !uptime '...

function DataArrival(user, data)
	data = strsub(data,1,strlen(data)-1)
	local s,e,cmd = strfind( data, "%b<>%s+(%S*)" )
	if cmd == "!uptime" then
		SendToAll("UpTime", "The "..frmHub:GetHubName().." Hub has been running for"..DoTimeUnits(clock()) )
	end
end

Typhoon

--
#24
QuoteOriginally posted by Herodes
OooOleeeee!!!!!
I found that better way on the DoTimeUnits Function in an earlier post ;)
Here it is ...
function DoTimeUnits(time)
	local time = time*1000
	local msg = ""
	local tO = {
		[1] = { 86400000, 0, "days"},
		[2] = { 3600000, 0, "hours"},
		[3] = { 60000, 0, "minutes"},
		[4] = { 1000, 0, "seconds"},
		};
	for i , v in (tO) do
		if time >= tO[i][1] then
			repeat 
				tO[i][2] = tO[i][2] + 1
				time = time - tO[i][1]
			until time < tO[i][1]
		end
	end
	for i,v in tO do 
		if tO[i][2] ~= 0 then
			msg = msg.." "..tO[i][2].." "..tO[i][3]
		end
	end
	return msg
end


To the above function add the snippet found below and witness the perfect uptime string (...lol) with ' !uptime '...

function DataArrival(user, data)
	data = strsub(data,1,strlen(data)-1)
	local s,e,cmd = strfind( data, "%b<>%s+(%S*)" )
	if cmd == "!uptime" then
		SendToAll("UpTime", "The "..frmHub:GetHubName().." Hub has been running for"..DoTime(clock()) )
	end
end


Great function Herodes , but i bet this one is better than the one you use in the current post ;o)

SendToAll("UpTime", "The "..frmHub:GetHubName().." Hub has been running for"..DoTimeUnits(clock()) )


*** Typhoon?



SMF spam blocked by CleanTalk