Jump to content
xisto Community
Feelay

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

Recommended Posts

Okay, now that you have given me some variable data I realize what I did wrong. Remember when I explained that we want to add the remaining seconds back to the timestamp so that they would be included the next time the script ran; well, I forgot that to add time you have to subtract from the current time. So to fix that, you have to change your $new_update_time = to this:

$new_update_time = $current_time - $remaining_seconds;

Okay, I changed the code to fix that problem and eliminate the multiplier since we really don't need it if you are going to be controlling both increment and frequency.

Now to start, $frequency must be calculated in seconds. 60 seconds = 1 minute, 120 seconds = 2 minutes, and 3600 seconds = 1 hour. You can either put the numeric value here or you can use a mathematical expression like so $frequency = 60 * 60 for 1 hour or $frequency = 30 * 60 for 30 minutes.

Now as explained before the current time is a measurement of the number of seconds since 01/01/1970 and can be found using the time() fumction.

$last_update_time is the value that the script stores in the database when an update has been made.

The difference is however many seconds have passed between now and then.

Now to get the hours passed or in your case the time units passed such as minutes passed, we have to divide the number of seconds by the frequency (number of seconds in your unit). So if 120 seconds have passed and your frequency is 60, then your hours passed equals 2 because 120 / 60 = 2!

Now we need to calculate the number of full units that have passed because we don't want to get lost in fractions! So we take the number of hours passed and round it down to the nearest whole number. We use the floor() function for this.

We then need to figure out what the ramaining seconds are from our full hours. We use modulus to figure the seconds left over. this is just the remainder from division like so: 5 % 2 = 1 because 2 goes into 5 2 times (2 * 2 = 4) with a remainder of 1 (5 - 4 = 1), additionally, 5 % 3 = 2 because 3 goes into 5 1 time (3 * 1 = 3) with a remainder of 2 (5 - 3 = 2). I hope that is clear enough.

I'll explain more later, here is the new code:
<?php$frequency = 60;$increment = 1;$current_time = time();$last_update_time = mysql_result(mysql_query("SELECT `value` FROM `configuration` WHERE `name` = 'last_update'"), 0);$time_difference = $current_time - $last_update_time;$hours_passed = $time_difference / $frequency;$full_hours_passed = floor($hours_passed);$remaining_seconds = $time_difference % $frequency;if($full_hours_passed > 0){	 $new_update_time = $current_time - $remaining_seconds;	 $hp_increase = $full_hours_passed * $increment;mysql_query("UPDATE `characters` SET `temphealth` = CASE WHEN `temphealth` + $hp_increase <= `maxhp` THEN `temphealth` + $hp_increase ELSE `maxhp` END WHERE `temphealth` > `maxhp`");mysql_query("UPDATE `configuration` SET `value` = $new_update_time WHERE `name` = 'last_update'");}?>

vujsa

Share this post


Link to post
Share on other sites

vujsa. the value is still not updating ?

the $full_hours_passed sometimes reaches 0, and then the $hp_increase and the $new_update_time reaches nothing (not even 0).
look:

1203101853 current_time1203101821 last_update_time32 time_difference 0.533333333333 hours_passed0 full_hours_passed32 remaining_secondsnew_update_timehp_increase60 frequency10 increment

And this:

1203105671 current_time1203105541 last_update_time130 time_difference 2.16666666667 hours_passed2 full_hours_passed10 remaining_seconds1203105661 new_update_time20 hp_increase60 frequency10 increment

but this is a lot later..
2 full_hours_passed = 20 hp_increase.
Edited by Feelay (see edit history)

Share this post


Link to post
Share on other sites

You just want to update a value every minute you may need to just check with the database (as vujsa said before). This is quite simple to achieve. I use this method often here is a basic blueprint of what is going to occur:
-Step 1: only call when asked (so if selected for attack script or profile). I suggest that you make it into A: a function or B: just make it a require friendly page. Easy in easy out est. est.
-Such as you put in the $username=$name; --> Require --> $Current_health=$currenthealth.
-Step 2: Select for database where username=$username and extract two entries HP and TimeUD (time Updated).
-Step 3: Time() - TimeUD = TimeB (Time between).
-Step 4: TimeB*Rate (should be a number you could however put in an equation for health)=HPA (HP Added).
-Step 5: HP+HPA=NHP(New HP).
-Step 6: Simple if If(NHP>100){NHP=100;}
-Step 7: Update Database with Time() (from above). Update Database with NHP.
-Step 8: Finally just set $currenthealth and finish.

With that said you 'should' be able to figure it out. Basically we are NOT updating the database each minute but rather each time we need to. Then we are simply getting the value. Just change a few things to decrease health over time (stabbed and loosing blood, slowly dieing over time est.).

Here is my Untested Example (Change it accordingly):

<?php//In: $username//Out: $CH//Only use once each load//Require with require(); See Manual.$row = mysql_fetch_array(mysql_query("SELECT * FROM replace_w_db WHERE UserN='$username'") or die(mysql_error()));$currentHP=$row['HP'];$LUT=$row['LastUpdatedTime'];$currentTime=Time();$TD=$currentTime-$LUT;$HPD=$TD*0.1;//0.1 HP gain per secound.$CH=$currentHP+$HPD;//Add HP (change to - if subtract).//Update DB$result = mysql_query("UPDATE replace_w_db SET HP='$CH' WHERE UserN='$username'") or die(mysql_error());  $result = mysql_query("UPDATE replace_w_db SET LastUpdatedTime='$currentTime' WHERE UserN='$username'") or die(mysql_error());  //Finish off.?>


It should work (hopefully). If not, you know how to PM.
Good Luck,
Sparkx


EDIT: Spelling + Grammar Error fixed.
Oh ya and if you want every minute just divide $TD by 60 ex: $TD=$TD/60; (insert just after:$TD=...;)
Edited by sparkx (see edit history)

Share this post


Link to post
Share on other sites

*Sigh* I thought you would be able to add in bits of code here and there to make it custom but apparently you A: Didn't read my post's content or B: Simply didn't understand it. I (being a lazy programmer) forgot Step 6 in my example but I listed it above in the content but just not in the example. Anyway after $CH=...; you should add if($CH>280){$CH=280;} now if you have subtract simply add (after the same check) if($CH<0){$CH=0;} This will limit the $CH var to either 280 or 0 you could use both but I that is only if your mysql database stored a negative variable before and when you added the time the variable was still negative (or the value before was greater then 280 if you are subtracting). Hopefully you checked that on your attack script otherwise you will have some problems.Thanks,Sparkx

Share this post


Link to post
Share on other sites

I understand how theese stuff works now ;) (the time() function and stuff).But is it possible to make it update without a user being online =? Or must the script be loaded by a user, to run?

Edited by Feelay (see edit history)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.