Reputation: 68
I recently started programming in PHP. For exercise I'd like to build my own PHP text strategy game. One technical aspect needs some thinking.
In my game every player must have the ability to gain resources, for example iron and grain. With these resources a player is able to recruit soldiers or build buildings. The resources are stored in a kind of warehouse.
The resources will be gain in a period of time. In example you gain 100 grain a hour and that grain is stored in the warehouse. This means that every 0.6 minute, 1 grain has to be added. I have been looking to cron jobs but that is not the best answer to my problem because is takes too much system resource. The problem is that I'd like to be able to update my resources in my warehouse every minute. Does anyone have an idea for my problem?
Upvotes: 0
Views: 1669
Reputation: 117427
Rather than trying to solve this by updating state, you could look at trying to solve it in a functional programming way. E.g. write a function that can calculate the amount of resources, given known conditions (Such as when production was started). Very simplified:
state based model:
class Gold {
public $amount = 0;
}
class GoldDigger {
public $amount_per_second = 1;
}
$player_gold = new Gold();
$player_worker = new GoldDigger();
while (true) {
$player_gold->amount += $player_worker->amount_per_second;
sleep(1);
echo "Player has {$player_gold->amount} gold pieces\n";
}
functional based model:
class Gold {
public $workers = array();
function getAmount() {
$total = 0;
foreach ($this->workers as $worker) {
$total += $worker->getAmount();
}
return $total;
}
}
class GoldDigger {
public $amount_per_second = 1;
public $started = 0;
function __construct() {
$this->started = time();
}
function getAmount() {
return $this->amount_per_second * (time() - $this->started);
}
}
$player_gold = new Gold();
$player_gold->workers[] = new GoldDigger();
while (true) {
sleep(1);
echo "Player has {$player_gold->getAmount()} gold pieces\n";
}
Both these examples are a bit contrived - you would likely store the data in a database or similar, which complicates matters a bit, but I hope it illustrates the difference between the two strategies.
Upvotes: 2
Reputation: 2989
You can update resources when user (or something else) request their amount. If you can calculate how much resources player will get in given time interval, then you can simply add them later. You only need to store the time of last update.
Example:
Player looked at warehouse at 01:00 and he found 50 grain there with current income 100 grain per hour. So he went to bed and next morning at 09:00 (8 hours later) he checks warehouse again. Now he can see 50 grain + 8 hours * 100 grain per hour = 850 grain.
But data on the server looked like this:
Upvotes: 1
Reputation: 7155
You can play with cache but updating something that frequently (when you have many many players), could cause a problem , instead of consuming resources on server , you can try to store the Timestamp of last time the value was requested and make a calculation of how many "Grain" the player has generated from now (LastAccessedTimestamp-NowTimestamp) = time that has passed (ex, 5min)
or you can let the client-side javascript show the player that warehouse has X Grains and update it on client-side only, once every 1 or 3min send a ajax request to sync things if there is no user activity, or else update every time the user sends an action or something.
Upvotes: 0
Reputation: 219804
You probably will need to use a daemon for this. Look into Gearman.
Upvotes: 1