Guest jeremyhowell Posted December 1, 2009 Share Posted December 1, 2009 I need help building a timer for regenerating energy/stamina/health in a Facebook app like Mafia Wars.The max value of energy etc is stored in a database for each user registered, and currently I am using a timestamp to regenerate the energy every minute by 1, but I feel it is a lot less than accurate, and I also want a live timer (using Javascript) that displays a countdown to the next top up. I have the following script, which adds a new timestamp when the user logs in so he is not credited with extra points, but I dont think it is working right, like if the user logs in 30 seconds before the next minute is up, I have no idea what to do to make sure the user doesnt lose those 30 seconds. I have tried subtracting the rounded remainder from the timestamp, but I am not sure whether this is helping? and I really want a solution to displaying the countdown timer when the user is logged in. <?php$con = mysql_connect("localhost","dbusername","");mysql_select_db("users",$con);$result = mysql_query("SELECT * FROM usersWHERE facebookid='100000372257424'");while($row = mysql_fetch_array($result)) { $maxenergy = $row['maxenergy']; $energy = $row['curenergy']; $curtime = mktime(date("g"),date("i"),date("s"),date("m"),date("d"),date("Y")); $timereg = $row['timereg']; if ($energy < $maxenergy) { $dif = (($curtime-$timereg)/60); $earned = (int)($dif/1); if ($earned>0) { $remainder = (round($dif-($earned*1),2)*100); echo $remainder . "<br>"; echo $dif . " minutes passed. You have earned " . (int)($dif/1); $curtime = ($curtime-$remainder); $energy = ($energy+$earned); if ($energy > $maxenergy) { $energy = $maxenergy; } mysql_query("UPDATE users SET timereg = $curtime WHERE facebookid = '100000372257424'"); mysql_query("UPDATE users SET curenergy = $energy WHERE facebookid = '100000372257424'"); $addtoen = $earned; } } } ?> Link to comment Share on other sites More sharing options...
justsomeguy Posted December 3, 2009 Share Posted December 3, 2009 Instead of doing this:$curtime = mktime(date("g"),date("i"),date("s"),date("m"),date("d"),date("Y"));you can do this:$curtime = time();You're also dividing or multiplying by 1 in a few places, I'm not sure why you think that's necessary. For the remainder, I think you should be multiplying by 60 instead of 100.To display a timer on the page you need to use Javascript. If you do a search for Javascript timers you should find quite a few examples. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.