Honesty Rocks! truth rules.

How To Make A Value In The Database Raise Every Minute.

HOME      >>       Programming

Feelay

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


Eggie

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.php
write
lynx -dump http://yourgameurl.com/folder/revive.php?revive=insert_your_own_password_here > /dev/null
and set it to hourly(to do that script every hour)
did that help?i hope it did!
Eggie

Feelay

It helped a bit, Thanks. But, how do I do it without the cron jobs =?


pyost

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 ;)


Feelay

But how do I do that =/

Thats the thing I don't know =(


pyost

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.


Eggie

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


pyost

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


Eggie

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


Feelay

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 =?


pyost

Exactly ;) And after the calculation you use the same variables in an INSERT MySQL query.


TavoxPeru

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,


Feelay

ok.. I did as you said. now it shows 1 hp all the time =/


pyost

Then you didn't implement it properly ;)


Feelay

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


pyost

Try this:

<?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' WHERE user='$userfinal'"); //Uppdatera HP och TIME?>

It should work if you are dealing with the right fields in the database - the script will add 5 health for every two seconds that have passed.

If it still doesn't work, it would be a good idea to echo all the variables before and after altering them, so you can see what happens.

Feelay

it's stil not working.I did as you said :S echoed all the variables.. this is the result :SResource id #1712024997771202499760Resource id #1860124988030062494001202499777


pyost

Well, it was nice making such a stupid mistake... mysql_query doesn't return the exact value, but a resource, which you cannot use just like that. So try this code:

 

<?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$updatedR = mysql_query("SELECT hptime FROM characters WHERE user='$userfinal'"); //hämta förra tiden från databasen$updated = mysql_result($updatedR, 0, 'hptime');$currentTime =time(); //tiden just nu$difference = $currentTime - $updated;// HP$healthR = mysql_query("SELECT temphealth FROM characters WHERE user='$userfinal'");//hp, etc. kan du nog fixa från annat värde som du hämtat.$health = mysql_result($healthR, 0, 'temphealth');// 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' WHERE user='$userfinal'"); //Uppdatera HP och TIME?>

Feelay

this code is making me mad :S Still not working :S


pyost

What's happening now?