I am currently writing a sports strategy management game (cricket) in PHP, with a MYSQL database, and I have come across one stumbling block - how do I update games where neither player is online?
Cricket is a game played between two players, and when they (or one of them) is online then everything is fine; but what if neither player is online? This occurs when championship games are played, and these games need to happen at certain times for game reasons.
At the moment I have a private web page that updates every 5 seconds, and each time it loads all games are updated; but then I have the problem that when my private web page stops (for example my computer crashes or my web browser plays up) the game stops updating!
Answer
As some of the comments mentioned, the issue is that you are trying to get the behaviour of a real-time simulation via an event-driven application.
Your options are basically these:
- Create fake events to ensure the web page is accessed when you need to perform these updates. This could be a cron job that polls a certain page. (However, if you have a system capable of running periodic cron jobs to access a web page, you should probably just have that system perform the update itself. See below.)
- Make the system perform 'just-in-time' updates. Games don't actually need to occur when nobody is looking at the page - they just need to have seemed to have occurred whenever someone next looks at the site. So, you have a routine, usually included on every page in your game, which checks for overdue updates and executes them immediately, before returning the rest of the page as usual. So if nobody accessed the site for a week, the next visitor would trigger off one week's worth of updates, and would see those events as expected. This is probably the easiest route and I expect it would work well for you.
- Have the database perform timed updates. MySQL offers events that can run stored procedures. This is ok, providing that you can write all your game logic in your database's stored routine syntax. Usually this is impractical.
- Use your own long-running process (ie. a daemon as mentioned in comments) to host your game instead of one that only runs for the duration of serving up a page. There are many languages and systems that can do this, almost all of them arguably better for programming games in than PHP, but usually not as quick to get started with. They also tend to require more expensive hosting. But since they run permanently in the background while waiting for incoming HTTP connections, it is usually easy to implement background jobs that perform timed updates of your choosing, whether someone has connected to the site or not.
No comments:
Post a Comment