Page 1 of 1
recharge
Posted: Wed Jan 12, 2011 5:31 am
by pretender5552
I want to make it so stats refill after a set time how do I do this?
Re: recharge
Posted: Wed Jan 12, 2011 7:29 am
by Jackolantern
Since PHP has no built-in mechanism of keeping time and responding without user interaction, you have to get creative. The traditional way is to set a "cron job", which is not a feature of PHP, but rather, a feature of Linux (the OS that your game will be hosted on). You set up a scheduled job in the OS and you point the job to the PHP script you want run and you set how often you want it to run. Halls has a tutorial about them
here. If cron jobs aren't an option for you,
here is an alternative that sets up a table in the database to manage triggers to handle the scheduled script.
Re: recharge
Posted: Wed Jan 12, 2011 8:47 am
by pretender5552
Will this keep the timer the same on all pages? and I am not really sure how to apply this to the health, and energy bars
Re: recharge
Posted: Wed Jan 12, 2011 9:17 am
by Chris
Imagine cron as someone who refreshes a certain script in a browser for you at a certain time you tell them to. That's basically what it is.
You can tell cron to visit this script every minute, hour, day, month, 64 minutes, 78988 minutes, 9982398471987 years or whatever as long as it's relative to apparently what is earths time (which I think is bullshit) time=time which we can calculate relative to earth. Okay but that's beside the point.
It's crontab you need.
What you need to do is make a script to update everyones stats, by whatever you want. Then cron will visit this script whenever you want.
Re: recharge
Posted: Sun Jan 16, 2011 2:10 pm
by pretender5552
Cron doesn't quite do what I am looking for I want the timer to refill there stats starting up as soon as they use one of the stats and I want to have an active timer showing them how long it will be to refill their stat.
Re: recharge
Posted: Sun Jan 16, 2011 6:59 pm
by Jackolantern
You can add the refill to the script so that when they use the stat it is refilled. Since that requires a player action, you can add it to that script. For the other part, you can just add Javascript timers to show how long it will be until the cron runs, and then use a cron to actually refill it.
Re: recharge
Posted: Sun Jan 16, 2011 7:22 pm
by PaxBritannia
Does anybody here remember out 'database cron' discussion? Apparently not

(edit: Apparently yes?)
Here's how to do it:
Insert a new column into the database called 'recharge'. When they use one of the stats, store the current time into the database. This is the time they last got recharged. So now, compare that time plus the recharge interval to the current time. If it has already past the needed time, then refill the stats. Else display a 'seconds till recharge' message.
Code: Select all
$time = $user3['recharge']; // or whatever database you store your users
$rechargeinterval = 60*60*24 // this makes them recharge every 24 hours
$rechargetime = $time + $rechargeinterval
$currenttime = time();
if ($currenttime>=$rechargetime){
while ($currenttime>=$rechargetime){
do something
$updatetime = $time + $rechargeinterval; // then update db
}
}else{
$timeremaining = $rechargetime - $currenttime;
echo $timeremaining.' seconds till recharge';
}
This script is good for automated operations: the while loop there makes sure that if more than 1 of the update time intervals have passed, it will continue to run. For example, to gain 50 action points every hour.
If we want the script to only do it once and then reset the timer, this one is better suited. I'm getting the impression that this is the one you want. That is, using the above example, if someone waits a long time after the points become available, this time will not be recorded and the timer will only restart when the user redeems it. In other words, this will not account for 'lost time'.
Code: Select all
$time = $user3['recharge']; // or whatever database you store your users
$rechargeinterval = 60*60*24 // this makes them recharge every 24 hours
$rechargetime = $time + $rechargeinterval
$currenttime = time();
if ($currenttime>=$rechargetime){
do something
$updatetime = time(); // then update db
}else{
$timeremaining = $rechargetime - $currenttime;
echo $timeremaining.' seconds till recharge';
}
I'm not sure which one you want, so feel free to use any or both.
pax.
Re: recharge
Posted: Sun Jan 16, 2011 8:55 pm
by pretender5552
Thanks pax just a few quick question, if you want it to refil every three minutes or a variable based on class how would you put that, also can you get the time remaining to appear in minutes?
Re: recharge
Posted: Sun Jan 16, 2011 9:18 pm
by PaxBritannia
Here it is.
Code: Select all
$time = $user3['recharge']; // or whatever database you store your users
$class= $user3['class']; // or whatever database you store your users
$rechargeinterval = 60*3; // this makes them recharge every 3 minutes
$rechargetime = $time + $rechargeinterval;
$currenttime = time();
if ($currenttime>=$rechargetime){
while ($currenttime>=$rechargetime){
if($class=='classa'){
//do this
}else if ($class=='classb'){
//do this
}else if ($class=='classc'){
//do this
}else{
die ('error: class does not exist');
}
$updatetime = $time + $rechargeinterval; // then update db
}
}
$timeremainings = $rechargetime - $currenttime;
$timeremainingm = floor($timeremainings/60);
$timeremainingsm = $timeremainings%60;
echo "$timeremainingm m $timeremainingsm s till recharge";
I don't know what you classes are, nor how many you have, so you'll have to add that in yourself. I've also made the time display in minutes and seconds because without the seconds, it wouldn't really be informative. Feel free to remove it.
Note that the page will give the time remaining when the page was served, meaning it won't count down on the user's loaded page. You'll need to use Javascript to do that.
Glad to help.
pax.
Re: recharge
Posted: Sun Jan 16, 2011 9:19 pm
by Jackolantern
Pax, that is the same technique used in the tutorial I linked in the 2nd post

It is called the "on-view" technique.