Online game - resources through time

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
Mafarazzo
Forum Newbie
Posts: 1
Joined: Mon Sep 29, 2008 3:26 pm

Online game - resources through time

Post by Mafarazzo »

Hi, I am working on an online php text-based game. In this game, you can build/upgrade a resource gatherer (for example, a gold mine) to get more resources per hour. Let's say:

Level 1: 1000 gold per hour
Level 2: 2000 gold per hour
And so on.

I'm storing on the database the time when the last upgrade was finished, and how much gold the player had back then. That way, if he wants to know how much gold he has right now, I only have to do a little math (gold_he_had * time_passed * gold_per_hour).

The problem: If an upgrade finishes while the player is offline, I won't be able to calculate how much gold he has since the equation is no longer linear (i.e. the gold rate changed sometime we were not watching).

The solution could be on of those:

- Work on non-linear calculations (seems a little complex)
- Keep a cronjob running to check for finished upgrades and update the gathering rates on the fly (more server-resource consuming)
- Ask for user input to start gathering resources faster after an upgrade completes (that way the player couldn't have the benefit of an offline upgrade if he doesn't show up by the time the upgrade finishes)

I would like to hear more ideas for solutions, or any feedback about this part of my game. The application runs on PHP5 + MySQL. Thanks in advance.
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Online game - resources through time

Post by josh »

you could express your gold calculation formula as an algorithm ( set of procedural commands ) that gets run at read time, and store a timestamp in the DB of when the gold was last calculated, subtract that timestamp from the current time when you load your data, and then write it back also updating the timestamp
Post Reply