Jump to content
xisto Community
Network1405241550

Want To Update Every Hour

Recommended Posts

hi guys, im in the process of coding a game, and i have abit of a problemi would like a script known as "h_hourly_maint.php" to go off every 30 mins, now i know il need a cron job for thiswhat i want it to do is grab all the users from my DB where mining is set to 2, then i want it to look in the "mining_rate" table of these users.each users mining rate will be differant, so what i want it to do is:-get all users where mining=2-sort through the users and add "mining_rate" to total minedall the users rates will be differant, i just aint sure how to write a script to do this, can anyone help?

Share this post


Link to post
Share on other sites

hi guys, im in the process of coding a game, and i have abit of a problem
i would like a script known as "h_hourly_maint.php" to go off every 30 mins, now i know il need a cron job for this
what i want it to do is grab all the users from my DB where mining is set to 2, then i want it to look in the "mining_rate" table of these users.

each users mining rate will be differant, so what i want it to do is:

-get all users where mining=2
-sort through the users and add "mining_rate" to total mined
all the users rates will be differant, i just aint sure how to write a script to do this, can anyone help?

if i understand correctly what you want to do, try this (The easy way ;)):

<?php// assuming that your connection to the DB is set$sql="SELECT * from users where users.mining=2"; //get users with mining set to 2$rs=mysql_query($sql) or die('Query Fail');while ($row = mysql_fetch_array($rs) ) { //loop your users   $id_user=(int) $row["id_user"];  $sql1="SELECT mining_rate from mining_rate where mining_rate.id_user=$id_user";  $rs1=mysql_query($sql1) or die('Query Fail');  $row1=mysql_fetch_array($rs1);  $mining_rate=(int) $row1["mining_rate"];  mysql_query("UPDATE users set total_mined=total_mined+$mining_rate);}?>
I dont test this code and i use my own variables and field names, sure you must change them, also, i don't know your field types, so, i assume that for the user id and for the mining rate and total mined fields they are integers fields.

Finally, maybe there is another way to do this ;).

Best regards,

Share this post


Link to post
Share on other sites

Anytime I consider using a CRON job, I always try and figure out a way to accomplish the same result without having to rely on such a server intensive method. For example, most of the time the data only needs to be updated if someone wants to view it...

I keep a time file that simply has a unix timestamp from time() and that is considered the last update time. When someone requests a page that contains data that might need to be updated, the script compare the current time to the last update time. If the difference between the two is greater than the refresh rate, the data is updated and then outputted. Otherwise, if the difference is les than the refresh rate, the existing data is used.

Kind of like this:

<?php$time_current = time();$time_updates_last = file_get_contents('time_data.txt');  // gets the contents from a file that only has a single value in it$refresh = 1800; // 60 seconds / 1 minutes * 30 minutes = 1800 seconds$data_age = $time_current - $time_updated_last;if($data_age >= $refresh){ // past refresh time// Do whatever data refreshing you need to here and then write a new vale to the 'time_data.txt' file// Then outout the data to the user.}else{// Just output the data to the user.}?>

However, if this is a deal where you are doig more than simply updating data for output, then you have more work to do.

For example, imagine that you have a ining rate and your user is able to mine 4 units per hour and you want to update this every 30 minutes. If you used the system above, then the users mined units would only increase by 2 when the refresh rate was reached and someone loaded the page... That means if nobody loaded the page for 4 hours, then that user would only increase his mined units by 2 for the 4 hours...

This would be a problem wouldn't it?

So you need to make sure that the script then accounts for the number of time that the data should have been refreshed.
For example, here is a quick example to determin the number of times the update should have taken place.

<?php$time_current = time();$time_updates_last = file_get_contents('time_data.txt');  // gets the contents from a file that only has a single value in it$refresh = 1800; // 60 seconds / 1 minutes * 30 minutes = 1800 seconds$data_age = $time_current - $time_updated_last;if($data_age >= $refresh){ // past refresh time$update_count = $data_age / $refresh;  // This give you the number of times in decimal form that the update should have happened.}else{// Just output the data to the user.}?>
I suggest that you round this number "DOWN" to the nearest interger 3.7654 becomes 3 etc...

Do what ever update you need on the data and use the update count to determin the number of times to multiply that or perform a loop.

When you update the stored timestamp in 'time_data.txt' don't use the $time_current as that will throw off your interval by whatever the fraction was that you removed earlier. Instead, use ($refresh * $update_count) + $time_updates_last;

This will make sure that you only update every 30 minutes not 24, 29, 20, 10, 18 minutes.

I mentioned a loop...
If your values build on each other like compounding interest, then you have to use a loop like so:

Increase the user's gold by 25% each cycle:
for($x = 1; $x <= $update_count; $x++){$gold = $gold * 1.25;}
So if $gold equaled 10 and the update count was 3, then the following would result:
$gold = 19.53125;
10 * 1.25 = 12.5
12.5 * 1.25 = 15.625
15.625 * 1.25 = 19.53125

The same result can be found without a loop like so:
$gold = $gold * (1.25^$update_count);

Now this would only affect the server when someone loads a page otherwise, the server is happy to ignore the script. This saves the server from performing calulations when it isn't really needed.

Your web host will realy appreciate you efforts with this...

vujsa

Share this post


Link to post
Share on other sites

"then i want it to look in the "mining_rate" table of these users."

Don't you mean mining_rate cell?

Cause then it'd be as simply as this:

UPDATE user total_mined=total_mined+mining_rate WHERE mining=2

If that's not the case we'd need more specific data about the mining_rate table.

Share this post


Link to post
Share on other sites

Anytime I consider using a CRON job, I always try and figure out a way to accomplish the same result without having to rely on such a server intensive method. For example, most of the time the data only needs to be updated if someone wants to view it...
I keep a time file that simply has a unix timestamp from time() and that is considered the last update time. When someone requests a page that contains data that might need to be updated, the script compare the current time to the last update time. If the difference between the two is greater than the refresh rate, the data is updated and then outputted. Otherwise, if the difference is les than the refresh rate, the existing data is used.

Kind of like this:

<?php$time_current = time();$time_updates_last = file_get_contents('time_data.txt');  // gets the contents from a file that only has a single value in it$refresh = 1800; // 60 seconds / 1 minutes * 30 minutes = 1800 seconds$data_age = $time_current - $time_updated_last;if($data_age >= $refresh){ // past refresh time// Do whatever data refreshing you need to here and then write a new vale to the 'time_data.txt' file// Then outout the data to the user.}else{// Just output the data to the user.}?>

However, if this is a deal where you are doig more than simply updating data for output, then you have more work to do.

For example, imagine that you have a ining rate and your user is able to mine 4 units per hour and you want to update this every 30 minutes. If you used the system above, then the users mined units would only increase by 2 when the refresh rate was reached and someone loaded the page... That means if nobody loaded the page for 4 hours, then that user would only increase his mined units by 2 for the 4 hours...

This would be a problem wouldn't it?

So you need to make sure that the script then accounts for the number of time that the data should have been refreshed.
For example, here is a quick example to determin the number of times the update should have taken place.

<?php$time_current = time();$time_updates_last = file_get_contents('time_data.txt');  // gets the contents from a file that only has a single value in it$refresh = 1800; // 60 seconds / 1 minutes * 30 minutes = 1800 seconds$data_age = $time_current - $time_updated_last;if($data_age >= $refresh){ // past refresh time$update_count = $data_age / $refresh;  // This give you the number of times in decimal form that the update should have happened.}else{// Just output the data to the user.}?>
I suggest that you round this number "DOWN" to the nearest interger 3.7654 becomes 3 etc...

Do what ever update you need on the data and use the update count to determin the number of times to multiply that or perform a loop.

When you update the stored timestamp in 'time_data.txt' don't use the $time_current as that will throw off your interval by whatever the fraction was that you removed earlier. Instead, use ($refresh * $update_count) + $time_updates_last;

This will make sure that you only update every 30 minutes not 24, 29, 20, 10, 18 minutes.

I mentioned a loop...
If your values build on each other like compounding interest, then you have to use a loop like so:

Increase the user's gold by 25% each cycle:
for($x = 1; $x <= $update_count; $x++){$gold = $gold * 1.25;}
So if $gold equaled 10 and the update count was 3, then the following would result:
$gold = 19.53125;
10 * 1.25 = 12.5
12.5 * 1.25 = 15.625
15.625 * 1.25 = 19.53125

The same result can be found without a loop like so:
$gold = $gold * (1.25^$update_count);

Now this would only affect the server when someone loads a page otherwise, the server is happy to ignore the script. This saves the server from performing calulations when it isn't really needed.

Your web host will realy appreciate you efforts with this...

vujsa
Hey vujsa, thanks for this information, you don't know how much time i will save with this nice piece of code.

Best regards,

Share this post


Link to post
Share on other sites

Very good information Vusja.Although a cron job is fast, more simple and convenient.Building up a system that goes on a need only basis suits web applications very nicely.Data is not required to be updated until someone actually requests it (there is no point in doing so earlier).For example, a high score table for users could work this way.You still might want to rig it up, not to do it too often, but the flipside is that if no one accesses it in a longer time, you save further resources in the process.

Share this post


Link to post
Share on other sites

I originally came up with the idea when working on a dynamic signature image. Since the image is generated on the fly, it uses a lot of resources and that use of resources is why many websites don't provide them. If when you generate the image, you save a copy in an image folder; you can use the saved image for a while instead of generating a new one. This is useful if the information in the dynamic signature image isn't updated in real time and the signature is likely to be displayed frequently. Saving a cached copy like this really saves server memory and CPU usage but requires a more complex script and more disk space.This type of on demand data processing could probably be used for about 90% of the current cron processing used these days. The problem is that most people don't consider that User A's activity can trigger a global event. Most developers tend to separate the user from the group so they simply don't see the relationship between the user and the group.The down side is that the user that triggers the global update will likely notice a delay in the loading of the page they requested and if the action is too large, the user could be timed out and the process may terminated before it finishes. So it is important to allow enough time for the operations and try and keep the operations as small as possible.One way to keep the processes small is to use a cascading method. For example, User A initiates the process the sets a value that will be checked the next time a page is requested. User B requests a page which triggers the second step of the process after the script checks for the value set by the first step. Then if required, the second step could set a value which would trigger the third step when then next page request comes. This could continue indefinitely. Alternating processes could also be used. If X amount of time has passed, perform action A unless Y amount of time has passed then perform action B instead...The options and uses are limitless and I urge everyone to experiment with this method.vujsa

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.