views:

46

answers:

4

Im making a web game. It runs relatively in real time. Buildings produce a certain amount of money each hour. The amount of money varies from player to player based on what they own.

I need to make a php script that calculates how much money to add and then insert it into the DB every hour, this would have to be ran for every player.

Whats the best way to do this?

Thanks!

+4  A: 

Probably setting up a cron job on the server, if your host allows it.

Chris Sobolewski
A: 

You are looking for cron job, see:

What is a cronjob and how do I use it?

Sarfraz
A: 

The best way to do this I think is to store the last time the user was logged in and calculate the money earned when the user logs in for the next time. You can animate that via JS, if you want to (let a timer increase the money every hour, so that it correlates with the amount calculated by php).

If you really need the hourly update, google "cron job". Try "free cron job" to find free providers...

Martin
+1  A: 

The problem with using a cron job for something like this, is that it starts taking longer to calculate everyone's money once there's enough players to make the game interesting. The key is to find ways to break the calculation up.

One thing you can do is add a "last_calculation" timestamp to each user, and only do the calculation when something happens where you need to know an individuals money. When a player loads a page that displays how much money they have, or when someone gives them money, are two examples of actions where you would want to do the calculation.

Once you determine what these actions are in your case, you can have these actions check that timestamp, perform the calculation, and update that timestamp before doing whatever they were going to do in the first place.

Doing something like this, rather than using a cron job will let your application scale better. Rather than having a massive load spike every hour when this stuff needs to be calculated, every player might simply have a fraction of a second added to their request every once in awhile.

joebert
Yeah I like this idea best!
Which is just what I answered, too. ;-)
Martin