Jump to content
MakeWebGames

Timestamp Crons help


danman24

Recommended Posts

ive recently implemented timestamp crons on my site. i have them working but the problem is that they do not update unless someone is on the site going from page to page or refreshing. ie using globals.php (where the function is). Is there a way i can have this automated somehow? (besides using actual crons) Any help would be appreciated. Thanks in advance.

Link to comment
Share on other sites

Yeah, that's because you need the page to load for the queries to execute. It's a drawback of them, although, if you count how long has passed since the last update say 10mins, then you run the 5min cron twice and the 1 min cron 10 times.

If there is no one on your site, does it really matter if the users aren't being update, as long as when someone does come on your site, everything is updated correctly?

Link to comment
Share on other sites

yeah its a shame. Hospital/Jail time require the 1 min cron. and users are not going to wait 30 mins watching paint dry. They leave and come back later when their time is up. Just wondering if anyone had a work around for this lol. Like an auto page refresh or a way to keep a player active to keep those quaries going

Link to comment
Share on other sites

Like I said, if someone is in jail for 20 mins, then if no-one is on for 30 mins, the person that was in jail comes back after 30mins, then you can have it that it takes 30mins away from everyone's jail time. If your not put off by a bit of work, then I have a workaround.

 

$time = time();
$q = $db->query("SELECT lastUpdate FROM crons WHERE file='crons/minute.php'");
$r = $q->fetch_row();
$lastUpdate = $r[0];
$diff = $time - $lastUpdate;
$updates = floor($diff / 60); // 300 for 5 min crons. 3600 for hour. 3600*24 for day
if ($updates > 0) {
  // run crons, multiplying default values by $updates
}

 

That workaround is quite handy especially when you use it along with LEAST(); for any values that have a maxvalue,

Eg

$db->query("UPDATE users SET energy=LEAST(energy+(10 * '$updates'), maxenergy)");
Edited by Coly010
Link to comment
Share on other sites

Maybe not the most efficient way but you can create a table that holds your last cron ran timestamps then subtract your current time by the last time it was ran and loop through it that many times. I have seen this done before but don't remember who or where

http://makewebgames.io/showthread.php/32642-any-version-Removal-of-1-minute-crons

I think (have not checked), but lower down or further in the thread shows the 5 minute and etc.. And its quite easy to add to it.

This does run 1 minute cron 10 times if the user has been inactive for 10 minutes.

Link to comment
Share on other sites

  • 4 weeks later...

I found this to be really interesting as I am developing on a local host and didn't REALLY want to go to all the effort of setting up cron jobs. We set it up several weeks ago and it has been working fine up to yesterday. Now I don't understand what has gone wrong.

 

<?php

$file 		= 'crons/minute.php';
$time		= time();
$q 			= $db->query("SELECT `nextUpdate` FROM `crons` WHERE `file`='{$file}' AND `nextUpdate` <= unix_timestamp()");
$r 			= $q->fetch_row();
$lastUpdate = $r[0];
$diff 		= $time - $lastUpdate;
$updates 	= floor($diff / 60); // 300 for 5 min crons. 3600 for hour. 3600*24 for day
if ($updates > 0) {

$db->query("UPDATE users SET hospital=hospital-(1*$updates) WHERE hospital > 0");
$db->query("UPDATE users SET jail=jail-(1*$updates) WHERE `jail` > 0");
$db->query("UPDATE users SET travel_time=travel_time-(1*$updates) WHERE user_level!=2 AND `travel_time` > 0");
$db->query("UPDATE users SET quest_time=quest_time-(1*$updates) WHERE quest_time > 0");

$db->query("UPDATE users SET quest_time=0 WHERE quest_time < 0");
$db->query("UPDATE users SET hospital=0 WHERE hospital < 0");
$db->query("UPDATE users SET jail=0 WHERE jail < 0");
$db->query("UPDATE users SET travel_time=0 WHERE travel_time < 0");

$times = time()+60;
$db->query("UPDATE `crons` SET `nextUpdate`={$times} WHERE `file`='{$file}'");
}
?>

 

No one is going to the hospital, or jail. I have gone into the DB and added times to these rows and as soon as you click in the game it is resetting it to 0.

Link to comment
Share on other sites

It is updating the timestamp by sixty second, but it is doing it with every click. I don't think it is supposed to do that.

You know what... I looked at this other version of doing the same thing.

http://makewebgames.io/showthread.php/32642-any-version-Removal-of-1-minute-crons

This works some much better.

Edited by Damond
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...