PHP游戏中每分钟更新数据库

PHP游戏中每分钟更新数据库,php,Php,我最近开始用PHP编程。作为练习,我想构建我自己的PHP文本策略游戏。一个技术方面需要一些思考 在我的游戏中,每个玩家都必须有能力获得资源,例如铁和谷物。有了这些资源,玩家可以招募士兵或建造建筑。这些资源存储在一种仓库中 资源将在一段时间内获得。例如,您每小时获得100粒谷物,这些谷物存储在仓库中。这意味着每0.6分钟必须添加1粒。我一直在寻找cron作业,但这并不是解决我问题的最佳答案,因为它占用了太多的系统资源。问题是我希望能够每分钟更新仓库中的资源。有人对我的问题有什么想法吗?您可能需要为

我最近开始用PHP编程。作为练习,我想构建我自己的PHP文本策略游戏。一个技术方面需要一些思考

在我的游戏中,每个玩家都必须有能力获得资源,例如铁和谷物。有了这些资源,玩家可以招募士兵或建造建筑。这些资源存储在一种仓库中


资源将在一段时间内获得。例如,您每小时获得100粒谷物,这些谷物存储在仓库中。这意味着每0.6分钟必须添加1粒。我一直在寻找cron作业,但这并不是解决我问题的最佳答案,因为它占用了太多的系统资源。问题是我希望能够每分钟更新仓库中的资源。有人对我的问题有什么想法吗?

您可能需要为此使用守护进程。查看。

您可能需要为此使用守护进程。查看。

您可以使用缓存,但频繁更新(当您有许多玩家时)可能会导致问题,您可以尝试存储缓存,而不是消耗服务器上的资源 最后一次请求值的时间戳,并计算玩家从现在起生成的“谷物”数量 (LastAccessedTimestamp NowTimestamp)=经过的时间(例如,5分钟)

5分钟*60秒=通过300秒 300秒/0.6秒=5分钟内500粒
或者,您可以让客户端javascript向播放器显示warehouse有X个Grains,并仅在客户端进行更新,如果没有用户活动,则每1或3分钟发送一次ajax请求以同步内容,或者每次用户发送操作或其他内容时进行更新。

您可以使用缓存,但可以频繁更新内容(当您有许多玩家时)可能会导致问题,您可以尝试存储 最后一次请求值的时间戳,并计算玩家从现在起生成的“谷物”数量 (LastAccessedTimestamp NowTimestamp)=经过的时间(例如,5分钟)

5分钟*60秒=通过300秒 300秒/0.6秒=5分钟内500粒
或者,您可以让客户端javascript向播放器显示warehouse有X个Grains,并仅在客户端进行更新,如果没有用户活动,则每1或3分钟发送一次ajax请求以同步内容,或者每次用户发送操作或其他内容时进行更新。

您可以在用户(或其他内容)时更新资源请求他们的数量。如果你能计算出玩家在给定的时间间隔内将获得多少资源,那么你可以稍后简单地添加它们。你只需要存储上次更新的时间

示例:

玩家在01:00查看了仓库,他在那里发现了50粒粮食,当前收入为每小时100粒。所以他上床睡觉,第二天早上09:00(8小时后)他再次检查了仓库。现在他可以看到50粒粮食+8小时*100粒粮食每小时=850粒粮食

但服务器上的数据如下所示:

  • 01:01时:“50粒,上次更新时间为01:00”
  • 05:00时:“50粒,上次更新时间为01:00”
  • 08:59:“50粒,上次更新时间为01:00”
  • 09:01:“850粒,最后更新时间09:00”

当用户(或其他人)请求资源数量时,您可以更新资源。如果您可以计算玩家在给定时间间隔内将获得多少资源,那么您可以稍后简单地添加资源。您只需要存储上次更新的时间

示例:

玩家在01:00查看了仓库,他在那里发现了50粒粮食,当前收入为每小时100粒。所以他上床睡觉,第二天早上09:00(8小时后)他再次检查了仓库。现在他可以看到50粒粮食+8小时*100粒粮食每小时=850粒粮食

但服务器上的数据如下所示:

  • 01:01时:“50粒,上次更新时间为01:00”
  • 05:00时:“50粒,上次更新时间为01:00”
  • 08:59:“50粒,上次更新时间为01:00”
  • 09:01:“850粒,最后更新时间09:00”

与其尝试通过更新状态来解决此问题,不如尝试通过函数编程的方式来解决。例如,在已知条件下(如生产开始时),编写一个可以计算资源量的函数。非常简单:

基于状态的模型:

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";
}
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";
}
基于功能的模型:

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";
}
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";
}

这两个例子都有点做作——您可能会将数据存储在数据库或类似的数据库中,这会使问题变得有点复杂,但我希望它能说明这两种策略之间的区别。

与其试图通过更新状态来解决这一问题,不如尝试用函数编程的方式来解决。例如,编写一个有趣的可以在已知条件下(如生产开始时)计算资源量的操作。非常简化:

基于状态的模型:

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";
}
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";
}
基于功能的模型:

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";
}
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";
}

这两个例子都有点做作——你可能会将数据存储在数据库或类似的数据库中,这会使事情变得有点复杂,但我希望它能说明这两种策略之间的区别。

如果我有成千上万的用户,这是一个好的解决方案吗?你可以将其与《特拉维亚》或《三轮车大战》之类的游戏进行比较。我以前的雇主用过它o发送数千条短信。我确信如果它能做到这一点,它可以大量完成其他类似的任务。Travian和大多数类似的游戏都不会实时更新资源。从游戏机制中可以看出,除非你或其他人查看它,否则什么都不会发生。特别是Travian是唯一一个有预定事件的哑状态机当用户动作发生时会重播。如果我有数千名用户,这是一个好的解决方案吗?你可以将其与Travian或Tribalwars等游戏进行比较。我以前的雇主使用它发送数千条短信。我确信如果它能做到这一点,它可以大量执行其他类似任务。Travian和mos