danman24 Posted January 18, 2015 Share Posted January 18, 2015 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. Quote Link to comment Share on other sites More sharing options...
Coly010 Posted January 18, 2015 Share Posted January 18, 2015 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? Quote Link to comment Share on other sites More sharing options...
danman24 Posted January 18, 2015 Author Share Posted January 18, 2015 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 Quote Link to comment Share on other sites More sharing options...
KyleMassacre Posted January 18, 2015 Share Posted January 18, 2015 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 Quote Link to comment Share on other sites More sharing options...
Coly010 Posted January 18, 2015 Share Posted January 18, 2015 (edited) 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 January 19, 2015 by Coly010 Quote Link to comment Share on other sites More sharing options...
SRB Posted January 18, 2015 Share Posted January 18, 2015 3600*24 86400 then :P Quote Link to comment Share on other sites More sharing options...
Coly010 Posted January 18, 2015 Share Posted January 18, 2015 86400 then :P Yeah I typed that on my phone I was bothered to work it out, it would do the same job aha Quote Link to comment Share on other sites More sharing options...
SRB Posted January 19, 2015 Share Posted January 19, 2015 Yeah I typed that on my phone I was bothered to work it out, it would do the same job aha Guess I must be sad because I remember the second amounts up to a day. After a day and I have to calculate it :D Quote Link to comment Share on other sites More sharing options...
HauntedDawg Posted January 19, 2015 Share Posted January 19, 2015 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. Quote Link to comment Share on other sites More sharing options...
Damond Posted February 16, 2015 Share Posted February 16, 2015 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. Quote Link to comment Share on other sites More sharing options...
HauntedDawg Posted February 16, 2015 Share Posted February 16, 2015 Does it eventually trigger this line: $db->query("UPDATE `crons` SET `nextUpdate`={$times} WHERE `file`='{$file}'"); As that is what will depict the next cron time. Check and make sure nextUpdate is now() time + 60. Quote Link to comment Share on other sites More sharing options...
Damond Posted February 16, 2015 Share Posted February 16, 2015 (edited) 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 February 16, 2015 by Damond Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.