POC0bob Posted January 29, 2013 Share Posted January 29, 2013 So, this is a bit of a complex thing.. I think. What I need is for an action to happen every 30 min. The action is adding one 'turn' to the user's turns. But this needs to happen even if the user is away, like if the person is away for 2 hours, he/she will have gained 4 turns in that time, but it needs to show how much time is left in the turn (the number doesn't have to update live, just every time the page is refreshed) I have an sql database if that will help..I was thinking of having it so it put a time stamp on the database of the last turn they where at, and then when it reaches 30 min do the query then reset the time stamp, and do that for every 30 min interval all at once when the user signs on.. But I do not know how to do math on the time and stuff. I hope I explained this well enough. I couldn't find anything online about this, unless I missed it somewhere. And just putting me in the right direction (telling me how to figure out if its been more then 30 min since the last time stamp, and if so how many times has it been 30 min, then leaving the remainder) and I should be able to figure it out from there. I wish I had some code to show you, any work that I have done so far on it, but I just got so lost on how to do it, my code got so messed up I just removed it all and tried to start over and here I am, I know how annoying it is from someone to just ask for code, which is why I am not really, Just wondering of someone here knows how to do this. THANKS -POC0bob Quote Link to comment https://forums.phpfreaks.com/topic/273770-action-every-30-min-even-if-user-offline/ Share on other sites More sharing options...
kicken Posted January 29, 2013 Share Posted January 29, 2013 You would setup a Cron Job in order to run a script on a schedule such as every 30 minutes. Based on your description though I don't think this is really what you need/want. Store a timestamp of when their turns last updated. Then you can calculate the # of minutes between the last update and now to determine how many turns need to be granted to them as well as how much time is left on the clock. As a rough example (assuming Mysql & PDO): $db = new PDO('mysql:...'); //connect to db //Assuming $userId is their sytem ID $minutesSinceUpdate = $db->query('SELECT TIMESTAMPDIFF(MINUTE, lastUpdate, NOW()) as minutes FROM users WHERE userid='.$userId)->fetchColumn(0); $numTurns = floor($minutesSinceUpdate/30); $minutesLeft = $minutesSinceUpdate%30; //Update the lastUpdate time to now, adjusted for # minutes remaining $db->exec('UPDATE users SET numTurns = numTurns + '.$numTurns.', lastUpdate = NOW() - INTERVAL '.$minutesLeft.' MINUTES WHERE userId='.$userId); On second though, you might even be able to wrap it up into a single update: UPDATE users SET numTurns = numTurns + FLOOR(TIMESTAMPDIFF(MINUTE, lastUpdate, NOW()) / 30) , lastUpdate = NOW() - INTERVAL TIMESTAMPDIFF(MINUTE, lastUpdate, NOW()) % 30 MINUTE WHERE userId=12345 Quote Link to comment https://forums.phpfreaks.com/topic/273770-action-every-30-min-even-if-user-offline/#findComment-1408878 Share on other sites More sharing options...
BagoZonde Posted January 29, 2013 Share Posted January 29, 2013 (edited) I was using that technique with timestamp in one of my game project few years ago as cron was unavailable so this post bring a smile on my face :]. Gentlemen, I think that inventing some basic algorithms is a grand joy :] so sufficient answer could be "To check how many turns passed, use timestamp to count difference and divide by 30 minutes". No doubt kicken's answer is the best delivered answer on a plate :]. Edited January 29, 2013 by BagoZonde Quote Link to comment https://forums.phpfreaks.com/topic/273770-action-every-30-min-even-if-user-offline/#findComment-1408919 Share on other sites More sharing options...
Barand Posted January 29, 2013 Share Posted January 29, 2013 You may find my response to a similar recent question of interest http://forums.phpfreaks.com/topic/273650-adding-numbers-to-a-database-over-time/?do=findComment&comment=1408365 Instead of game start you would use time of last turn Number of turns = minutes/30 time remaining = 30 - minutes%30 Quote Link to comment https://forums.phpfreaks.com/topic/273770-action-every-30-min-even-if-user-offline/#findComment-1408946 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.