Jump to content

Recommended Posts

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

 

 

-POC0bob

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

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

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.   Restore formatting

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

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.