-2

我有以下代码

  $timeNow = time();
    $timeExpire = $timeNow - 15;
    $con->query("UPDATE players SET Time='".$timeNow."', TimeExpire='".$timeExpire."' WHERE Username='". $_SESSION['Username']."'");
    $User = $con->query("SELECT * FROM players WHERE Username='".$_SESSION['Username']."'");
    $UserInfo = $User->fetch_object();

    if($UserInfo->Wood == "") {
    $con->query("UPDATE players SET Wood='0'");
    } elseif($UserInfo->Stone == "") {
    $con->query("UPDATE players SET Stone='0'");
    } elseif($UserInfo->Gold == "") {
    $con->query("UPDATE players SET Gold='0'");
    }
    if((time() - 15) >= $UserInfo->TimeExpire) {

    $getAmount = (time() - $UserInfo->TimeExpire);
    $con->query("UPDATE players SET Wood=Wood+$getAmount, Stone=Stone+$getAmount, Gold=Gold+$getAmount WHERE Username='". $_SESSION['Username']."'");


}

我有一些问题:

  • 我怎样才能让用户每 10 秒只能收到一次木头、石头和金子(否则他们只能刷新页面)

  • 我如何计算有多少到期?例如,如果他们注销 2 小时,然后重新登录,他们会得到与
    在线 2 小时一样的钱

4

2 回答 2

1

您不一定需要始终执行这些查询。只需在您的玩家表中创建一个列来记录他们上次收到资源的时间,如下所示:

ALTER TABLE players ADD LastTimeReceivedResources INT(5)

当用户加载页面时,您检查此字段以查看他们没有获得资源的时间:

$wood_per_10_seconds = 15;
$last_time_received_resources = ...; // Get from players table
$time_now = time();
$added_wood = floor(($time_now-$last_time_received_resources)/10) * $wood_per_10_seconds;

$con->query("UPDATE players SET Wood=Wood+$added_wood WHERE Username='".$_SESSION['Username']."'");

在此之后,您将当前时间保存到 LastTimeReceivedResources 字段中:

if ($added_wood>0) $con->query("UPDATE players SET LastTimeReceivedResources=$time_now WHERE Username='".$_SESSION['Username']."'");
于 2013-09-11T20:13:09.487 回答
0

保存上次刷新完成的时间。检索最后一次刷新,计算从那时起的秒数。

$date = new DateTime( '2013-09-10 18:07:13' );
$date2 = new DateTime( '2013-09-11 18:11:08' );

$diff = $date2->getTimestamp() - $date->getTimestamp();

虽然我确实认为一个好的旧全球 cron 将是要走的路,就个人而言。

于 2013-09-11T20:10:30.427 回答