Jump to content

Something is wrong with this time script =/


feelay

Recommended Posts

Hello, Again.

I am having a little problem with a time script. It is using the time() function, but I don't know how to use that function (I didn't create this script. a guy made it for me). The problem is, that the value is not updating in the database(it should update every 12 minutes, or every minute,depending on what i write in a place..) I guess that the fault is in the code somewhere.. but it is not in the queries.. I've tested them.. I can't find anything, but as I said, i don't know how this function works.. so can anyone please try to help me figure out whats wrong in this code:

 

<?php
$multiplier = 1 / 5;
$frequency = 3600 * $multiplier;
$increment = 5 * $multiplier;

$current_time = time();  // get the current server time in second
$last_update_time = mysql_result(mysql_query("SELECT hptime FROM characters"), 0);  //  This could be anywhere even in a separate text file!
$time_difference = $current_time - $last_update_time;
$hours_passed = $time_difference / $frequency; //  convert sec to min then to hours --> 3600 second / 60 = 60 minutes / 60 = 1 hour
$full_hours_passed = floor($hours_passed); // round the number of decimal hours down to the nearest whole hour
$remaining_seconds = $time_difference % $frequency; // number of sec left when time difference is divided by 3600 sec --> 7 % 3 = 1 and 6 % 3 = 0
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 characters SET hptime = $new_update_time");
}
?>

 

Link to comment
Share on other sites

I don't like cronjobs.. There must be another way..

 

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).
  Quote from another forum..
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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

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

Important Information

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.