Feelay 0 Report post Posted February 6, 2008 Hi.I am trying to figure out how to make a value in the database raise every minute.Lets say, I want the HP to raise every minute.The max HP is 100. I want it to raise 5 HP/ Hour. And the player don't have to be online. Anyone who know how I can do it =?Thanks for any help.//Feelay Share this post Link to post Share on other sites
Eggie 0 Report post Posted February 6, 2008 (edited) you can either raise 0.08 hp per minute or you can raise 5 HP per hor which is better if you ask me... <?php$cpass=insert_your_own_password_here;$revive=$_GET["revive"];if ($revive == $cpass) { mysql_query("update players set hp=hp+5"); print "Revive Complete"; exit;}?>now go to cron jobs in cpanel...let's say that the above code is in your www/ folder named revive.phpwritelynx -dump http://yourgameurl.com/folder/revive.php?revive=insert_your_own_password_here > /dev/nulland set it to hourly(to do that script every hour)did that help?i hope it did!Eggie Edited February 6, 2008 by Eggie (see edit history) Share this post Link to post Share on other sites
Feelay 0 Report post Posted February 6, 2008 It helped a bit, Thanks. But, how do I do it without the cron jobs =? Share this post Link to post Share on other sites
pyost 0 Report post Posted February 6, 2008 Don't update it all the time, only when the value is required. You would just need to remember the last time the value is updated, calculate how many minutes have passed, and then set the new value accordingly. Not only will you put less load on the server, but you will also avoid using cron jobs Share this post Link to post Share on other sites
Feelay 0 Report post Posted February 6, 2008 But how do I do that =/ Thats the thing I don't know =( Share this post Link to post Share on other sites
pyost 0 Report post Posted February 6, 2008 First of all, you need to take into consideration only those scripts that display/access a player's health. When such script is ran, it should retrieve the data from the database, check whether the player's health should be updated, and act accordingly. This is how I would do it. Let's suppose you have already retrieved $health and $updated. The first variable contain the current health, and the second when the health was last updated (UNIX timestamp). $currentTime = time(); // we need this in order to calculate how much time has passed$difference = $currentTime - $updated;$addHealth = floor($difference / 3600) // we need to know how many hours have passed - we get an round value (though it is a "float" number) by using floor()$addHealth = (int) $addHealth * 5 // this is how much health will be added, if you add 5HP per hour - I'm not sure whether (int) is necessary, but better safe than sory$health += $addHealth;if ($health > 100) { $health = 100; } // we don't want the health to go over 100!$updated = time(); // we also need to update the alteration time. Now you would just need to enter the new value of $health and $updated into the database, without the need to read them again in this script. I have not tested this code, so it might not work properly. Share this post Link to post Share on other sites
Eggie 0 Report post Posted February 6, 2008 why don't you want to work with cron jobs...it's not that hard...you have the script i wrote for you and you just need to adjust it and it will be over...i always like helping people with something i know and that's what i ask from other people...to help me if i have problems Share this post Link to post Share on other sites
pyost 0 Report post Posted February 6, 2008 Cron jobs can be useful at times, but here they would be too much. You would have a script that runs every sixty seconds and updates all the players. Sure, for only several players it is OK, but just imagine updating several thousands of players - it would definitely put immense load on the server (unnecessary load, that is). Share this post Link to post Share on other sites
Eggie 0 Report post Posted February 6, 2008 (edited) oh...i didn't know it's that much....but i only learned to do it with cron jobs so i posted that script before...i'm a newbie in scripting so i really don't know about that...(i'm currently getting my register script to work and helping bhupinunder with his game)@pyost:you have msn???if you do pls send me PM with it..i need help asap Edited February 6, 2008 by Eggie (see edit history) Share this post Link to post Share on other sites
Feelay 0 Report post Posted February 7, 2008 kill me if you want.. but I still don't understand xDYou mean that the "$Health" is the mysql_query for the current health and that the "$updated" is for the "last time the health was updated" colum = Both is mysql_queries =? Share this post Link to post Share on other sites
pyost 0 Report post Posted February 7, 2008 Exactly And after the calculation you use the same variables in an INSERT MySQL query. Share this post Link to post Share on other sites
TavoxPeru 0 Report post Posted February 8, 2008 I agree with pyost, avoid cron jobs, even them are simple, they may overload the server.BTW, the code he posts not only will work with this situation, i think it would be implemented for other cases with minimum modifications.Best regards, Share this post Link to post Share on other sites
Feelay 0 Report post Posted February 8, 2008 ok.. I did as you said. now it shows 1 hp all the time =/ Share this post Link to post Share on other sites
pyost 0 Report post Posted February 8, 2008 Then you didn't implement it properly Share this post Link to post Share on other sites
Feelay 0 Report post Posted February 8, 2008 what should it look like then =?this is how it looks atm =/ <?phpsession_start();include "database.php";include "hptime.php";include "level.php";$nuser=mysql_real_escape_string($_SESSION['user']);$auser=mysql_real_escape_string($_SESSION['admin']);if($nuser){$userfinal=$nuser;}elseif($auser){$userfinal=$auser;}#################################################################// TID$updated = mysql_query("SELECT hptime FROM characters WHERE user='userfinal'"); //hämta fÜrra tiden frün databasen$currentTime =time(); //tiden just nu$difference = $currentTime - $updated;// HP$health = mysql_query("SELECT temphealth FROM characters WHERE user='$userfinal'");//hp, etc. kan du nog fixa frün annat värde som du hämtat.// HEAL$addHealth = floor($difference / 2); // we need to know how many hours have passed - we get an round value (though it is a "float" number) by using floor()$addHealth = (int) $addHealth * 5; // this is how much health will be added, if you add 5HP per hour - I'm not sure whether (int) is necessary, but better safe than sory$health += $addHealth;if ($health > $user['maxhp']) { $health = $user['maxhp']; } // we don't want the health to go over max hp!$updated = time(); // we also need to update the alteration time.mysql_query("UPDATE characters SET temphealth='$health' AND hptime='$updated'"); //Uppdatera HP och TIME?> Share this post Link to post Share on other sites