Jump to content

Running a Script at 5 Minute Intervals


publius

Recommended Posts

Hey guys. I am working on a browser/text based [mm]orpg. It is a mafia-style game, and you get certain things every 5 minutes (more energy, hp, etc). Well I am currently using a script that checks the database to check how many seconds ago the script was last run, and if it was more then 5 minutes ago, then it will run, and if it has been like 10 minutes instead of 5, it will run twice, if it has been 15 minutes, it will run 3 times, etc. It will then take the time over 5 minutes that it has been e.g. if it has been exactly 6 minutes, there will be 1 minute left over time, and it will subtract 60 seconds from the current timestamp, so that next time around, it will think it ran at a 5 minute interval.

 

Well, it slowly starts to get off the correct intervals, like it started out doing it at every 5 minute, 0 second interval, like 4:05, 4:10, 4:15, etc., and now it runs at 4:23 and 5 seconds, etc.

 

I have been told it is best to use cron scripts but I don't have access to those on my server, and I was wondering if there was a better way to do this that you guys knew of. If I didn't make it very clear what it is I am asking, let me know what you are confused about and I will try to clarify myself.

 

Thanks in advance.

Link to comment
https://forums.phpfreaks.com/topic/47686-running-a-script-at-5-minute-intervals/
Share on other sites

Well unless I am mistaken, it seems that your concept is valid and all that is holding you back is some mathematical mistake. Maybe posting some code will help us out.

 

CRON would be the exact same thing but you wouldn't be forced to calculate all the time behind it.

 

I think what you have is definitely usable, if I am not mistaken.

Sure thing, and thanks for trying to help!

 

//Updates
$updates_sql = mysql_query("SELECT * FROM `updates` WHERE `name` = '5min'");
while ($line = mysql_fetch_array($updates_sql, MYSQL_ASSOC)) {
$update = $line['lastdone'];
}
$timesinceupdate = time() - $update;
if ($timesinceupdate>=300) {
$num_updates = floor($timesinceupdate / 300);
$result = mysql_query("SELECT * FROM `grpgusers`");
while($line = mysql_fetch_assoc($result)) {
	$updates_user = new User($line['id']);
	if ($updates_user->rmdays > 0) {
		$multiplier = 2;
	} else {
		$multiplier = 1;
	}
	$username = $updates_user->username;
	$newawake = $updates_user->awake + (5 * $num_updates) * $multiplier;
	$newawake = ($newawake > $updates_user->maxawake) ? $updates_user->maxawake : $newawake;
	$newhp = $updates_user->hp + (10 * $num_updates) * $multiplier;
	$newhp = ($newhp > $updates_user->maxhp) ? $updates_user->maxhp : $newhp;
	$newenergy = $updates_user->energy + (2 * $num_updates) * $multiplier;
	$newenergy = ($newenergy > $updates_user->maxenergy) ? $updates_user->maxenergy : $newenergy;
	$newnerve = $updates_user->nerve + (2 * $num_updates) * $multiplier;
	$newnerve = ($newnerve > $updates_user->maxnerve) ? $updates_user->maxnerve : $newnerve;
	$result2 = mysql_query("UPDATE `grpgusers` SET `awake` = '".$newawake."', `energy` = '".$newenergy."', `nerve` = '".$newnerve."', `hp` = '".$newhp."' WHERE `username` = '".$username."'");
}
//update the timer and db
$thetime = time();
$result2 = mysql_query("UPDATE `updates` SET `lastdone` = '".$thetime."' WHERE `name` = '5min'");
$leftovertime = $timesinceupdate - (floor($timesinceupdate / 300) * 300);
if ($leftovertime>0) {
	$newupdate =  time() - $leftovertime;
	$setleftovertime = mysql_query("UPDATE `updates` SET `lastdone` = '".$newupdate."' WHERE `name` = '5min'");
}
}

 

It has to select which update to do because there are multiple different ones. There is one ran every 5 minutes, one ran every 1 Minute, and one ran every 24 hours.

 

As far as CRON goes, is there some sort of free CRON hosting or something (I am pretty much ignorant about how CRON works) where it would like call a script on my server for me at certain intervals. Because if not, that might be an interesting project, but that's getting off on a tangent :P

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.