phpmysqlonline-gamelive-update

How to live update browser game attributes like the 4 resources in Travian game?


I would like to make a web-based game which is Travian-like (or Ikariam-like). The game will be in PHP & MySQL-based. I wonder how can I achieve the live updating of game attributes.

For frontend, I can achieve by using AJAX calls (fetch the latest values from database), or even fake update of values (not communicated with server).

For backend, is this done by a PHP cron job (which runs every few seconds)? If so, can anyone provide me some sample codes?

by the way, I know it would be a trouble if I use IIS + FastCGI.

=== Version Information ===

PHP : 5.2.3

IIS : 6.0 with FastCGI

OS : Windows Server 2003 Standard R2


Solution

  • The correct answer depends on your exact needs.

    Does everyone always get resources at the same rate? If so, a simple solution is to track how long their user has existed, calculate the amount of resources based on the rate they're getting, and subtract the number of resources they've spent in total. That's going to be a bit of a problem if the rate can ever change, though, so if you use this solution, you're pretty much stuck with the rate you pick unless you rewrite the handling entirely (for example to the one below).

    If it varies how quickly people can get resources, you'll need to update the data periodically. A cronjob/scheduled task would work well to make sure everyone is updated, but in some situations, it might be better to simply measure how long it's been since you've updated each user's resources, and then update them on every page load they make while logged in by multiplying the time they've been away by the rate at which they gain resources - that way, you avoid updating until you actually need the new value.