pyost 0 Report post Posted February 8, 2008 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. Share this post Link to post Share on other sites
Feelay 0 Report post Posted February 8, 2008 (edited) it's stil not working.I did as you said :S echoed all the variables.. this is the result :SResource id #1712024997771202499760Resource id #1860124988030062494001202499777 Edited February 8, 2008 by Feelay (see edit history) Share this post Link to post Share on other sites
pyost 0 Report post Posted February 8, 2008 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?> Share this post Link to post Share on other sites
Feelay 0 Report post Posted February 8, 2008 this code is making me mad :S Still not working :S Share this post Link to post Share on other sites
pyost 0 Report post Posted February 8, 2008 What's happening now? Share this post Link to post Share on other sites
Feelay 0 Report post Posted February 8, 2008 still showing 1 hp =/ Share this post Link to post Share on other sites
pyost 0 Report post Posted February 8, 2008 Let's echo the variables once more... Share this post Link to post Share on other sites
Feelay 0 Report post Posted February 8, 2008 1202504970027501202504970this is what i get Share this post Link to post Share on other sites
pyost 0 Report post Posted February 8, 2008 Which number is what variable? Share this post Link to post Share on other sites
Feelay 0 Report post Posted February 8, 2008 Resource id #16 $updatedR1202505413 $updated0 $differenceResource id #17 $healthR1 $health0 $addHealth1202505413 $updated Share this post Link to post Share on other sites
pyost 0 Report post Posted February 8, 2008 I am getting really confused... Sorry, but I can't help you anymore tonight, as I have some things to do. I'll see to this problem tomorrow Share this post Link to post Share on other sites
vujsa 0 Report post Posted February 9, 2008 Well, let's try and get this figured out now! I think a simpler approach is in order. Instead of looking at each user individually, lets look at the whole system! I'll start by explaining what I have in mind: Load the system and get the current time.Check the database and get the last update time (global)Calculate the difference between the current time and the last update time.$time_difference = $current_time - $last_update_time We then convert the number of seconds that have passed to the number of decimal hours that have passed$hours_passed = $time_difference / 60 / 60 We then calculate the number of whole hours that have passed.$full_hours_passed = floor($hours_passed) Round down to the nearest whole hour Determine the number of seconds left over.$remaining_seconds = $time_difference % (60 * 60) We have to add the remaining seconds to the new update time if there is an update.if($full_hours_passed > 0) {$new_update_time = $current_time + $remaining_seconds; } We then calculate the number of HP to increase by based on the number of hours passed and the rate of increase$hp_increase = $full_hours_passed * 5 Finally, we increase each user's HP (Requires MySQL 5.0+)This is kind of tricky because we have to be sure that we don't go over 100 if that is the highest possible HP value mysql_query("UPDATE user SET hp = CASE WHEN hp + $hp_increase <= 100 THEN hp + $hp_increase ELSE 100 WHERE hp < 100") Now, this will update every user at one time and won't place too high of a load on the server. Don't forget to update the last update time in the database! Don't forget that we only update the database if at least one full hour has passed. Here is what I had in mind: $current_time = time(); // get the current server time in second$last_update_time = mysql_result(mysql_query("SELECT update_time FROM update_table"), 0); // This could be anywhere even in a separate text file!$time_difference = $current_time - $last_update_time;$hours_passed = $time_difference / 60 / 60; // 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 % (60 * 60); // number of sec left when time difference is divided by 3600 sec --> 7 % 3 = 1 and 6 % 3 = 0if($full_hours_passed > 0){ $new_update_time = $current_time + $remaining_seconds; $hp_increase = $full_hours_passed * 5; mysql_query("UPDATE user SET hp = CASE WHEN hp + $hp_increase <= 100 THEN hp + $hp_increase ELSE 100 WHERE hp < 100"); mysql_query("UPDATE update_table SET update_time = $new_update_time");} The reason that we have to add the remaining seconds back to the current time is basically because we didn't use them and as a result, if we left them out then you would probably update more than 24 times each day! Now, we could modify this script to update by 1 every 20 minutes if needed. One last important note: I don't know how you intend to connect to and use your database. Adjust your PHP to match whatever technique you will use to read information from the database results. I hope this helps. vujsa Share this post Link to post Share on other sites
Feelay 0 Report post Posted February 9, 2008 (edited) I hope it does :S I have to wait 1 hour. is there any way to make the time shorter (every 1 min or something). when i tryed, the hp value showed 1 all the time, so I changed it back.btw, i changed the 100 in the mysql_query to maxhp, because i have a table named that, and now all users have 280 hp =/ how can i solve that =/ =?the users maxhp depends on the users level, but all users have the same hp as the highest level.. Edited February 9, 2008 by Feelay (see edit history) Share this post Link to post Share on other sites
vujsa 0 Report post Posted February 10, 2008 I hope it does :S I have to wait 1 hour. is there any way to make the time shorter (every 1 min or something). when i tryed, the hp value showed 1 all the time, so I changed it back. btw, i changed the 100 in the mysql_query to maxhp, because i have a table named that, and now all users have 280 hp =/ how can i solve that =/ =? the users maxhp depends on the users level, but all users have the same hp as the highest level.. Is that a table or a field named maxhp? If it is a table, then we have some additional work to do on the system. Otherwise, you should be okay. Now, for shortening the time, consider the following: 7200 / 60 / 60 = 2 Likewise: 7200 / ( 60 *60 ) = 2 Now, we know that 3600 second equals 1 hour so lets create a variable for that! $frequency = 3600; Might as well make a variable for the number of HP to increase by each interval: $increment = 5; Now, we modify the previous code to include that! $frequency = 3600;$increment = 5;$current_time = time(); // get the current server time in second$last_update_time = mysql_result(mysql_query("SELECT update_time FROM update_table"), 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 = 0if($full_hours_passed > 0){ $new_update_time = $current_time + $remaining_seconds; $hp_increase = $full_hours_passed * $increment; mysql_query("UPDATE user SET hp = CASE WHEN hp + $hp_increase <= 100 THEN hp + $hp_increase ELSE 100 WHERE hp < 100"); mysql_query("UPDATE update_table SET update_time = $new_update_time");}Note that I replaced / (60 * 60) and / 60 / 60 with / $frequency.Additionally, the value of 5 for the increment value was change to the variable $increment. These changes make it easier to change how often and how much the HP in increased for the users. See, this way we could do the following to increase the value by 1 point every 12 minutes instead on 5 points every 60 minutes: $frequency = 3600 / 5; // 720 seconds = 12 minutes$increment = 5 / 5; // 1 Or even better, we could add one more variable as the multiplier!7200 / ( 60 *60 ) = 2 Now, we know that 3600 second equals 1 hour so lets create a variable for that! $frequency = 3600; Might as well make a variable for the number of HP to increase by each interval: $increment = 5; Now, we modify the previous code to include that! $frequency = 3600;$increment = 5;$current_time = time(); // get the current server time in second$last_update_time = mysql_result(mysql_query("SELECT update_time FROM update_table"), 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 = 0if($full_hours_passed > 0){ $new_update_time = $current_time + $remaining_seconds; $hp_increase = $full_hours_passed * $increment; mysql_query("UPDATE user SET hp = CASE WHEN hp + $hp_increase <= 100 THEN hp + $hp_increase ELSE 100 WHERE hp < 100"); mysql_query("UPDATE update_table SET update_time = $new_update_time");}Note that I replaced / (60 * 60) and / 60 / 60 with / $frequency.Additionally, the value of 5 for the increment value was change to the variable $increment. These changes make it easier to change how often and how much the HP in increased for the users. See, this way we could do the following to increase the value by 1 point every 12 minutes instead on 5 points every 60 minutes: $multiplier = 1 / 5; // One fifth of an hour is 12 minutes$frequency = 3600 * $multiplier; // 720 seconds = 12 minutes$increment = 5 * $multiplier; // 1 So the final code would look like this: $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 update_time FROM update_table"), 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 = 0if($full_hours_passed > 0){ $new_update_time = $current_time + $remaining_seconds; $hp_increase = $full_hours_passed * $increment; mysql_query("UPDATE user SET hp = CASE WHEN hp + $hp_increase <= 100 THEN hp + $hp_increase ELSE 100 WHERE hp < 100"); mysql_query("UPDATE update_table SET update_time = $new_update_time");}So, now you only need to modify the first 3 variables to change the frequency and amount of HP that is increased. However, if you have a field in the 'user' table named maxhp for EACH player, then you should change every instance of the number 100 in the query to maxhp like this: mysql_query("UPDATE user SET hp = CASE WHEN hp + $hp_increase <= maxhp THEN hp + $hp_increase ELSE maxhp WHERE hp < maxhp"); Now as I hinted previously, if you are using some other method for storing each users maximum hp value, then I'll need some database information from you to know how to write the correct MySQL query for this operation. I must warn against trying to update the system every 1 minute as this will overtax the server and probably cause all of us a lot of trouble with our hosting accounts. It would be okay for testing purposes but in a live environment, it will probably crash the server. I hope this gets you on your way to finishing your project. vujsa Share this post Link to post Share on other sites
Feelay 0 Report post Posted February 10, 2008 hmm =/ Vujsa. Nothing happened =S I've bee waiting for 12 minutes, but nothing changed. Share this post Link to post Share on other sites