View Single Post
Old May 22nd, 2008, 4:39 PM   #2
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Posts: 1,827
Rep Power: 5 Sane will become famous soon enough
Re: PHP browser game resource question

In general, there are two ways to do this:
  1. Set a cron job on your server to run a php script every 60 minutes which adds resources to all players.

    Advantages:
    • Easy to code
    • Very extendable

    Disadvantages:
    • If your server goes down during the time the cron job should occur, everyone misses the update
    • Not portable between all servers
    • Requires some degree of maintenance
    • Server stress is less uniformly distributed (peaks every hour)


  2. In every location where a user could possibly see someone's gold (or resources, or whatever you need to update), call a common function which checks how many hours ago the last update for this player was. Then make a number of updates equal to the amount of hours passed.

    Advantages:
    • Better integrated with the rest of your code
    • Becomes very simple to code if you already have a common function that returns the amount of gold someone has (simply place the check inside the common function)
    • Portable between all servers
    • Updates will still occur even if the server goes down momentarily
    • Server stress is more uniformly distributed (no abnormal peaks)

    Disadvantages:
    • Often more difficult to code
    • Potentially buggy if not done correctly
    • Any bugs can lead to very bad exploits and even necessary database restorations

Picking your weapon will completely depend on the server you're running, your long term plans for the project, and the amount of time and effort you want to commit.
Sane is offline   Reply With Quote