在PHP游戏中每分钟更新数据库
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?
我最近开始用PHP编程。 为了锻炼,我想建立自己的PHP文本策略游戏。 一个技术方面需要一些思考。 p>
在我的游戏中,每个玩家都必须有能力获取资源,例如铁和谷物。 利用这些资源,玩家可以招募士兵或建造建筑物。 资源存储在一种仓库中。 p>
资源将在一段时间内获得。 例如,您每小时获得100粒粮食,粮食储存在仓库中。 这意味着每0.6分钟,必须添加1粒。 我一直在寻找cron的工作,但这不是我问题的最佳答案,因为它需要太多的系统资源。 问题是我希望能够每分钟更新我仓库中的资源。 有没有人对我的问题有所了解? p> div>
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
";
}
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
";
}
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.
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)
5min * 60 secs = 300secs passed
300secs / 0.6sec = 500 Grains in 5 minutes
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.
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:
- At 01:01: "50 grain, last update at 01:00"
- At 05:00: "50 grain, last update at 01:00"
- At 08:59: "50 grain, last update at 01:00"
- At 09:01: "850 grain, last update at 09:00"