Jump to content
MakeWebGames

Energy Regeneration PHP Question


Fireal

Recommended Posts

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.

Link to comment
Share on other sites

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.

  • Like 2
Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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 by Dayo
  • Like 4
Link to comment
Share on other sites

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.   Paste as plain text instead

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