Fireal Posted January 6, 2020 Share Posted January 6, 2020 Hey guys, I'm trying to figure out the best way to handle how energy restores for my game. At first I was just going to run a cron every 10 minutes adding 10 energy points to all users. However, that didn't seem right because certain users perform energy depleting actions at different times, and I would want the regeneration to be 10 minutes from their last action. With that said, how would you handle this? Add 10 to a user's energy 10 minutes after they performed their last action. Only thing I can think of is starting to use timestamps in the user tables. By the way I am using PHP/MySQL. Quote Link to comment Share on other sites More sharing options...
Dave Posted January 6, 2020 Share Posted January 6, 2020 I think you've hit the nail on the head, I'd store a timestamp of the last energy depleting action in their user table entry, then either have a minute cron (which I dislike) or do a check on page load to adjust their energy accordingly. As the user is the only one consuming the energy, and the only one who can see if, doing the calculations prior to rendering their energy bar would be sufficient. You'd obviously need to take into account how many minutes has passed and multiple accordingly until they hit the max. 2 Quote Link to comment Share on other sites More sharing options...
Fireal Posted January 6, 2020 Author Share Posted January 6, 2020 Thanks for the response Dave. I was thinking along the same lines but wasn't sure if it was the most ideal approach now these days. Appreciate it! ? 1 Quote Link to comment Share on other sites More sharing options...
Dave Posted January 6, 2020 Share Posted January 6, 2020 49 minutes ago, Fireal said: Thanks for the response Dave. I was thinking along the same lines but wasn't sure if it was the most ideal approach now these days. Appreciate it! ? If it makes sense and it works why not! 1 Quote Link to comment Share on other sites More sharing options...
Fireal Posted January 7, 2020 Author Share Posted January 7, 2020 On 1/5/2020 at 9:06 PM, Dave said: I think you've hit the nail on the head, I'd store a timestamp of the last energy depleting action in their user table entry, then either have a minute cron (which I dislike) or do a check on page load to adjust their energy accordingly. As the user is the only one consuming the energy, and the only one who can see if, doing the calculations prior to rendering their energy bar would be sufficient. You'd obviously need to take into account how many minutes has passed and multiple accordingly until they hit the max. Dave, quick question about what you posted. You said you would do a check on page load and adjust accordingly, but how would you go about say adjusting energy for those that haven't logged in a few days. When it comes with handling time and determining how much time has passed and putting it in a way to give energy points is making me rack my brain pretty hard. The issue seems to be getting a difference in total minutes. Perhaps, I'm going about it wrong. What's your input? Quote Link to comment Share on other sites More sharing options...
Dayo Posted January 7, 2020 Share Posted January 7, 2020 (edited) You can have a look at the logic i used for the GLv2 workout module. $secondsBetweenGain = 60; $timer = "statRegen"; $last = $user->getTimer($timer); /* Get the users last action */ $seconds = (time() - $last); $energyPerMinute = 1; $times = floor($seconds / $secondsBetweenGain) * $energyPerMinute; /* calculate how energy you need to add */ if ($times) { $newEnergy = $times + $user->info->US_energy; $maxEnergy = $user->info->US_maxEnergy; if ($newEnergy > $maxEnergy) $newEnergy = $maxEnergy; $newWill = $times + $user->info->US_will; $maxWill = $user->info->US_maxWill; if ($newWill > $maxWill) $newWill = $maxWill; $user->set("US_energy", $newEnergy); $user->set("US_will", $newWill); $user->updateTimer($timer, $last + ($times * $secondsBetweenGain)); /* Set the last action time */ $page->addToTemplate("energy", $newEnergy); $page->addToTemplate("will", $newWill); } this code does not rely on crons Edited January 7, 2020 by Dayo 4 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.