Page 1 of 1
Keeping track of time with cron jobs?
Posted: Sun Jul 20, 2008 8:24 pm
by JoeVector
Hi there. I'm currently trying to make a browser and turn-based game with PHP. I would like to make the server check for player actions every 30 minutes (so that would be the duration of a single turn) but the only way I can think of to keep track of time that way is opening a page open in the server that refreshes for all eternity. And that is probably not the most practical way to tackle the issue. I was told that by using a cron job I could execute actions every certain period of time, but the problem is I don't know what to tell the cron application to do once the time is reached.
I'd really appreciate it if someone would tell me how to make the cron job somehow execute PHP code or write to a MySQL table or whether there is an alternative method to achieve so.
Thanks.

Re: Keeping track of time with cron jobs?
Posted: Sun Jul 20, 2008 8:42 pm
by califdon
JoeVector wrote:Hi there. I'm currently trying to make a browser and turn-based game with PHP. I would like to make the server check for player actions every 30 minutes (so that would be the duration of a single turn) but the only way I can think of to keep track of time that way is opening a page open in the server that refreshes for all eternity. And that is probably not the most practical way to tackle the issue. I was told that by using a cron job I could execute actions every certain period of time, but the problem is I don't know what to tell the cron application to do once the time is reached.
I'd really appreciate it if someone would tell me how to make the cron job somehow execute PHP code or write to a MySQL table or whether there is an alternative method to achieve so.
Thanks.

What is it that you would write to a MySQL table?? The real question is What are you trying to do? Do you want to kick a user out of the game after 30 minutes? That's easy: just set a session variable with the current time plus 30 minutes when the user logs in. Then each time the user takes some action, check to see if his session has expired. Of course, this assumes that the user will be interacting with the server as part of the game. The point is, you don't need to constantly check, you only need to check when the user is about to take some action. If that's not what you're trying to do, you'll have to explain more.
Re: Keeping track of time with cron jobs?
Posted: Sun Jul 20, 2008 9:54 pm
by JoeVector
Hmm, I want to do something a bit more complex. When a turn passes, the server would check all the actions players have taken and modify stats and other variables. For instance, every turn, the HP of a wounded player would be raised by one point as they regenerate, so that would need to be recorded in the respective table. If they attacked someone, the combat script would compare their stats and give a result for the fight. Or if they decided to build something that takes 3 turns to complete, I would need to subtract one turn to indicate their progress.
I hope that's more clear.

Re: Keeping track of time with cron jobs?
Posted: Sun Jul 20, 2008 10:08 pm
by califdon
You'll probably have to get some advice from someone who has programmed real-time games, which I haven't. The general problem is that HTTP is inherently stateless, that is, the server receives a request and sends the response and no longer maintains any status. That's why you have to use session variables and/or data stored in a database. So it requires workarounds to maintain the illusion of a real-time system. The thing that would concern me is that there's no way to know if a player is even still participating until they take some action. Maybe somebody with experience in this can provide some help.