Standalone LUA Timer
 

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

Standalone LUA Timer

Started by c h i l l a, 28 November, 2003, 16:39:27

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

c h i l l a

If been fumbling a while and just couldn't get an idea for an standalone LUA timer...  maybe someone else has.. and just for the info  while date()  or while clock()  sucks...

plop

so far i couldn't find any info on this.
but there are way's 2 solve this in a different way.
how about a standalone vbs program wich launches a lua script on a timer (lua.exe scriptname).
now on this i got a tiny problem, i don't know vbs.  lol
with this it would be posible 2 run operations behind the scenes.
clean up users/database and just export as list so the scripts running on ptokax only have 2 import that 2 proces again, this way those things won't lag/halt the bot as only the real needed things are done by ptokax.
so if some1 is able 2 make this it would @least make me very happy but i gues also lots of other scripters.

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 <----<<

Guibs

Hi there,,

I've searched for a long time, about the way to use the 'date()' function, trying to convert a number in a date format,... & I haven't found it,... :s

This piece of code can convert a number in a date,... usefull for the timeban, by example, or other things,... :)

('Number' is in minutes, and the code sorts the date, from the day date, until the 'Number',...)
-----
function ConvertTime(number)
	local y2 = floor(number/(60*24*30*12))
	local mm2 = floor((number-(y2*60*24*30*12))/(60*24*30))
	local d2 = floor((number-(y2*60*24*30*12)-(mm2*1440*30))/1440)
	local h2 = floor((number-(y2*60*24*30*12)-(mm2*1440*30)-(d2*1440))/60)
	local m2 = number-(y2*60*24*30*12)-(mm2*1440*30)-(d2*1440)-(h2*60)

	y1 = date("%Y")
	mm1 = date("%m")
	d1 = date("%d")
	h1 = date("%H")
	m1 = date("%M")

	y2 = y2 + y1

	mm2 = mm2 + mm1

	d2 = d2 + d1

	h2 = h2 + h1

	m2 = m2 + m1
	if m2 >= 60 then
		m2 = m2-60
		h2 = h2 + 1
	end

	if h2 >= 23 then
		h2 = h2-24
		d2 = d2 + 1
	end

	if d2 > 28 and mm2 == 2 then
		d2 = d2-28
		mm2 = mm2 + 1

	elseif d2 > 30 and (mm2 == 4 or mm2 == 6 or mm2 == 9 or mm2 == 11) then
		d2 = d2-30
		mm2 = mm2 + 1

	elseif d2 > 31 and (mm2 == 1 or mm2 == 3 or mm2 == 5 or mm2 == 7 or mm2 == 8 or mm2 == 10 or mm2 == 12) then
		d2 = d2-31
		mm2 = mm2 + 1
	end

	if mm2 > 12 then
		mm2 = mm2-12
		y2 = y2 + 1

		if mm2 == 2 and d2 > 28 then
			d2 = d2-28
			mm2 = mm2 + 1
		elseif (mm2 == 4 or mm2 == 6 or mm2 == 9 or mm2 == 11) and d2 > 30 then
			d2 = d2-30
			mm2 = mm2 + 1
		elseif (mm2 == 1 or mm2 == 3 or mm2 == 5 or mm2 == 7 or mm2 == 8 or mm2 == 10 or mm2 == 12) and d2 > 31 then
			d2 = d2-31
			mm2 = mm2 + 1
		end
	end

	if mm2 < 10 then
		mm2 = "0"..mm2
	end

	if d2 < 10 then
		d2 = "0"..d2
	end

	if h2 < 10 then
		h2 = "0"..h2
	end

	if m2 < 10 then
		m2 = "0"..m2
	end

	SendToOps("*Time*", "In "..number.." minutes, it will be the: "..d2.."."..mm2.."."..y2.." & hour: "..h2..":"..m2..":00")
end
-----
If you have a more simple syntax to use,... I would be glad to know it,... :)

l8tr,, ;)
-- Please,... don\'t ask help in Pm,...Forums are made for that, to help everyone & my Inbox pm will be safe,... Thks,,  :))  --
CB forum     /     CB Home page

plop

made by tezlo.
---------------------------------------------------------------------  julian day
function jdate(d, m, y)
	local a, b, c = 0, 0, 0
	if m <= 2 then
		y = y - 1
		m = m + 12
	end

	if (y*10000 + m*100 + d) >= 15821015 then
		a = floor(y/100)
		b = 2 - a + floor(a/4)
	end

	if y <= 0 then c = 0.75 end
	return floor(365.25*y - c) + floor(30.6001*(m+1) + d + 1720994 + b)
end

--------------------------------------------------------------------- cedian day
function cdate(j)
	local a, b, c, d, e
	local alpha, day, month, year

	local z = j+1
	if z < 2299161 then a = z
	else alpha = floor((z-1867216.25)/36524.25) a = z + 1 + alpha - floor(alpha/4)
	end

	b = a + 1524
	c = floor((b-122.1)/365.25)
	d = floor(365.25*c)
	e = floor((b-d)/30.6001)

	day = b - d - floor(30.6001*e)
	if e < 13.5 then month = e - 1
	else month = e - 13 end
	if month > 2.5 then year = c - 4716
	else year = c - 4715 end
	return day, month, year
end
i'm halfway on a standalone timer.
this works as a background process.
starting it is no problem, it's stopping it when ptokax quits/scripts restart/stop.

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 <----<<

NotRabidWombat

#4
I'm not quite sure why you would want a Lua Standalone Timer or how you would pull it off (Lua is not an event driven language). The way OnTimer() happens right now is PtokaX creates a Windows event that it recieves and then calls the OnTimer function. It would be very probable to improve upon this and have a timer event and the string name of the function you want to call. So now you would have:
SetTimer(1000, "OnTimer2");
StartTimer("OnTimer2");
etc
But this would have to change on the PtokaX end.

As for calculating the difference between two dates, there is a nice standard called Julian date that is used by astronomers. I wrote a tutorial on it on the previous lua forum but that doesn't really do us a lot of good ;-)

Here is the example script I used in the tutorial. Maybe an Mod will take it to the next level (I'm not sure if users have privs to make tutorials).
-- Displays Hub Uptime in Days, Hours, Mins
-- More of an example on how to use the Julian Date
-- function
-- Author: RabidWombat 

-- Globals --

HubStartTime = "6/17/2003 10:25:59 AM "
-- Store the hub start time from error.log. This occurs in Main()

-- Events --

function Main()
	-- Open error.log in PtokaX directory
	local hFile, sFinal = openfile("..\\error.log", "r"), nil 

	if (hFile) then 
		local line = read(hFile) 
		-- Find the last time the hub started
		-- The string format must be like:
		-- 6/17/2003 10:25:59 AM - Serving started
		while line do
			if(strsub(line, -15) == "Serving started") then 
				s, e, sFinal = strfind(line, "([^-]+).+") 
			end 
			line = read(hFile) 
		end 
		closefile(hFile)

		HubStartTime = sFinal	-- comment this line if you don't want time from error.log
	end
end

function NewUserConnected(user) 
	local dy, hr, mn = GetUpTime()
	user:SendData("test","Uptime: "..dy.." days, "..hr.." hours, "..mn.." minutes\r\n")
end 

-- Functions --

-- Gets the hubs uptime 
-- return: dys, hrs, mns 
function GetUpTime() 
	-- Get the Julian date for the hub start time and now
	local StartTime = JulianDate(SplitTimeString(HubStartTime)); 
	local EndTime = JulianDate(SplitTimeString(date("%m/%d/%Y %I:%M:%S %p "))); 

	-- Subtract two Julian Dates
	local diff = EndTime - StartTime;

	-- Days, Hours, Minutes 
	return floor(diff), floor(frac(diff) * 24), floor(frac(frac(diff)*24)*60);
end 

-- Split a specific Time string into its components 
-- Format: "M/D/Y HR:MN:SC x" x is AM or PM 
-- return: D,M,Y,HR,MN,SC --- HR is in 24hr format 
function SplitTimeString(TimeString) -- 
	local s,e,M,D,Y,HR,MN,SC,x = strfind(TimeString, "([^/]+).([^/]+).(%S+)%s([^:]+).([^:]+).(%S+)%s(%S+)"); 

	M = tonumber(M) 
	D = tonumber(D) 
	Y = tonumber(Y) 
	HR = tonumber(HR) 
	MN = tonumber(MN) 
	SC = tonumber(SC) 

	assert(HR <= 12); 
	assert(MN < 60); 
	assert(SC < 60); 

	if(x == "PM") then 
		HR = HR + 12; 
	end 

	return D,M,Y,HR,MN,SC 
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

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

----------------- END -------------------------
As you can see. Two Julian dates can simply be subtracted to determine the number of days. The fraction part is the difference in hours, mins, secs, etc. If you google Julian Date you can find lots of information on it.

Heres the function to convert from Julian date back to the Gregorian calender (I never needed this though :-) )
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

Oh! A big side note. I ran into problems with this example script and other users because the PtokaX error log uses the computers standard form of output rather than its own. So if your error log does not look something like "6/17/2003 10:25:59 AM " when it comes to displaying dates, you will have to either fudge the code in the main or comment out the part that gets the hub start time from the error log. This will result in a comparison of the current time and "6/17/2003 10:25:59 AM " which is sufficient for the example.

-NotRabidWombat


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

tezlo

yep this is old math and by no means mine.. i just rewrote it from pascal or somewhat
but Rabid's does seconds too :)

plop

that 1 from wombat is definatly cool but chilla allready gave me something like this.
but from the looks that version of wombat is more precise.
gotta take a close look @ that 1 for sure.

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 <----<<

kepp

What is a stand alone timer for?
Im this " " close to launch a lua file in the scripting manual in ptokax on a timer: start and stop timer.
but for now i can only open the file from that folder
and read from it.

A good way to do so, if i undersatnd "Stand alone" right.
that would be to make several "xx.lua" files available for configuration, so it launches into the script folder....
and when you stop the timer in the "Software" you will kill
that/those lua files.....

I don't know Visual Basic so well, I play with it from time to time....
but i just found a good manual for it..
:D
Guarding    

SMF spam blocked by CleanTalk