PHP计算游戏能耗时间尺度

时间:2017-07-04 01:41:02

标签: php formula seconds

我正在努力实现

在在线游戏中,您可以实现奖励和其他能源目标。一个人可能需要总共24,000个能量,这是一个基于时间的能量,而其他人只有25个。基于0能量开始并且在用户睡觉或其他什么时没有失去的能量,我想计算需要多长时间才能获得所需的能量。

--------------------------------------------------------------------
|   Energy      |   Cooldown   |  Additional  |   Limit   |  wait  |
|               |              |    Energy    |           |        |
--------------------------------------------------------------------
|   Natural     |   10 minutes |     5        |   N/A     |    Y   |
|   Booster 1   |   24 hours   |     150      |   1 p/24h |    Y   |
|   Booster 2   |   6 hours    |     150      |   4 p/24h |    N   |
|   Booster 3   |   6 hours    |     250      |   4 p/24h |    Y   |
--------------------------------------------------------------------

一个人在24小时内所能达到的总能量是2,470个,通过720个自然能量,150个来自Booster 1,600个来自Booster 2,1,000个来自Booster 3。

所以你每10分钟(600秒)就能获得5点自然能量,你可以通过助推器补充能量,立即增加“冷却时间”。所以基于从0能量开始,你可以跳到1000能量。 “等待”意味着你需要等到冷却时间结束或不结束。

我到目前为止做了什么

为计算天数,我做了以下工作:

$days = floor($Energy_Needed / 2470) * 86400;

对于剩余的能量,我已经完成了以下工作:

$remaining = $Energy_Needed - (floor($Energy_Needed / 2470) * 2470);

问题

计算出$days之后,再次开始新的问题是if $remaining > 1000(因为用户可以跳到此,所以它只是N天)我怎样才能找到最好的剩余时间?

注意:我只是在寻找总秒数,没有“漂亮”。

2 个答案:

答案 0 :(得分:5)

尝试这样的事情。我很难理解这个问题!

实际上这是一个经过测试的工作示例。 我做了这些假设:

1)任何有助推器的球员都会获得自然能量

2)等待时间不会影响THE BOOSTER2

试试这个

<?php

/*-------------------------------------*/
// Natural Energy
/*-------------------------------------*/

//the natural energy is 720 in 24 hours (86400s)
// Energy formula E(t) = const * t since they are proportional

 $naturalEnergy = function ($time){
 $energyNatural = number_format((720/86400)*$time);
 return $energyNatural;
};

//time needed for to obtain naturally $energyAcquired

 $timeNatural = function ($energyAcquired){
  // Maths : Reversed the previous equation
 $timeNeeded = number_format( $energyAcquired / (720 / 86400) );
 return $timeNeeded;
};

/*-------------------------------------*/
// Booster One
/*-------------------------------------*/

//The booster will help to acquire 150 Energy in 24 hours (86400s)
//and will also naturally gain 720 energy per 24hour

 $booster1Energy = function ($time){
  //booster1 + natural
 $energyBooster1 =number_format(( (150/86400) + (720/86400) )*$time) ; 
 return $energyBooster1;
};

 $timeBooster1 = function ($energyAcquired){
  //Reversed the previous equation
 $timeNeeded = number_format(( $energyAcquired / ( (150/86400) + (720/86400) ) ));
 return $timeNeeded;
};

/*-------------------------------------*/
// Booster two
/*-------------------------------------*/

//The booster 2 will help to acquire 600 Energy in 24 hours (86400s)
//and will also naturally gain 720 energy per 24 hour

 $booster2Energy = function ($time){
  //booster2 + natural
 $energyBooster = number_format(( (600/86400) + (720/86400) )*$time); 
return $energyBooster;
};

 $timeBooster2 = function ($energyAcquired){
  //Reversed the previous equation
 $timeNeeded = number_format(( $energyAcquired / ( (600/86400) + (720/86400) ) ));
 return $timeNeeded;
};

/*-------------------------------------*/
// Booster three
/*-------------------------------------*/

//The booster 3 will help to acquire 1000 Energy in 24 hours (86400s)
//and will also naturally gain 720 energy per hour

  $booster3Energy = function ($time){
  //booster3 + natural
  $energyBooster = number_format(( (1000/86400) + (720/86400) )*$time); 
 return $energyBooster;
 };

  $timeBooster3 = function ($energyAcquired){
  //Reversed the previous equation
  $timeNeeded = number_format(( $energyAcquired / ( (1000/86400) + (720/86400) ) ));
 return $timeNeeded;
 };

/*-------------------------------------*/
// Booster all inlcuded
/*-------------------------------------*/

//Everything will help to acquire 2470 Energy in 24 hours (86400s)
//and will also naturally gain 720 energy per hour

  $allBoosterEnergy = function($time){
   //booster1 + booster2 + booster3 + natural
  $energyBooster = number_format( (2470/86400)* $time ); 
 return $energyBooster;
 };

  $timeAll = function($energyAcquired){
   //Reversed the previous equation
  $timeNeeded = number_format(( $energyAcquired / (2470/8600) ));
 return $timeNeeded;
 };

// introduced elapsed time so far in the game 

function bestTimeRemaining( $energyToAttain, $elapsedTimeSoFar, $naturalEnergy, $booster1Energy, $booster2Energy,$booster3Energy, $allBoosterEnergy, $timeNatural, $timeBooster1, $timeBooster2, $timeBooster3,$timeAll ){

//Remaining energy = EnergyToAttain - Energy obtained so far
//The Energy obtained so far can be calculated with $elapsedTimeSoFar as follow
//----------------------------------------------------------------------------------------------------
//if played with natural energy from the beginning
   $remainingEnergy['usedNatural'] = $energyToAttain - 
   $naturalEnergy($elapsedTimeSoFar);

//if played with booster1 from the beginning*/
   $remainingEnergy['usedBooster1'] = $energyToAttain - $booster1Energy($elapsedTimeSoFar);

//if played with booster2 from the beginning
   $remainingEnergy['usedBooster2'] = $energyToAttain - $booster2Energy($elapsedTimeSoFar);

//if played with booster3 from 
   $remainingEnergy['usedBooster3'] = $energyToAttain - $booster3Energy($elapsedTimeSoFar);

//if played with all boosters from the beginning, the remaining energy will be
   $remainingEnergy['usedAllBoosters'] = $energyToAttain - $allBoosterEnergy( $elapsedTimeSoFar );

//----------------------------------------------------------------------------------------------------
//After the elapsedTime , the player knows the remaining energy so he can decide to use a booster
//---------------------------------------------------------------------------------------------------

//time calculation using the remaining energy

//Remaining time for the user who used natural enrgy with option to finish with natural, booster1, booster2, booster3

   $remainingTime['usedNaturalSoFar']['FinishWithNatural'] = $timeNatural($remainingEnergy['usedNatural']);
   $remainingTime['usedNaturalSoFar']['FinishWithBooster1'] = $timeBooster1($remainingEnergy['usedNatural']);
   $remainingTime['usedNaturalSoFar']['FinishWithBooster2'] = $timeBooster2($remainingEnergy['usedNatural']);
   $remainingTime['usedNaturalSoFar']['FinishWithBooster3'] = $timeBooster3($remainingEnergy['usedNatural']);
   $remainingTime['usedNaturalSoFar']['FinishWithAllBooster'] = $timeAll($remainingEnergy['usedNatural']);

//Remaining time for the user who used Booster1 with option to finish with natural, booster1, booster2, booster3
   $remainingTime['usedBooster1SoFar']['FinishWithNatural'] = $timeNatural($remainingEnergy['usedBooster1']);
   $remainingTime['usedBooster1SoFar']['FinishWithBooster1'] = $timeBooster1($remainingEnergy['usedBooster1']);
   $remainingTime['usedBooster1SoFar']['FinishWithBooster2'] = $timeBooster2($remainingEnergy['usedBooster1']);
   $remainingTime['usedBooster1SoFar']['FinishWithBooster3'] = $timeBooster3($remainingEnergy['usedBooster1']);
   $remainingTime['usedBooster1SoFar']['FinishWithAllBooster'] = $timeAll($remainingEnergy['usedBooster1']);

//Remaining time for the user who used Booster2 with option to finish with natural, booster1, booster2, booster3
   $remainingTime['usedBooster2SoFar']['FinishWithNatural'] = $timeNatural($remainingEnergy['usedBooster2']);
   $remainingTime['usedBooster2SoFar']['FinishWithBooster1'] = $timeBooster1($remainingEnergy['usedBooster2']);
   $remainingTime['usedBooster2SoFar']['FinishWithBooster2'] = $timeBooster2($remainingEnergy['usedBooster2']);
   $remainingTime['usedBooster2SoFar']['FinishWithBooster3'] = $timeBooster3($remainingEnergy['usedBooster2']);
   $remainingTime['usedBooster2SoFar']['FinishWithAllBooster'] = $timeAll($remainingEnergy['usedBooster2']);

//Remaining time for the user who used Booster3 with option to finish with natural, booster1, booster2, booster3
   $remainingTime['usedBooster3SoFar']['FinishWithNatural'] = $timeNatural($remainingEnergy['usedBooster3']);
   $remainingTime['usedBooster3SoFar']['FinishWithBooster1'] = $timeBooster1($remainingEnergy['usedBooster3']);
   $remainingTime['usedBooster3SoFar']['FinishWithBooster2'] = $timeBooster2($remainingEnergy['usedBooster3']);
   $remainingTime['usedBooster3SoFar']['FinishWithBooster3'] = $timeBooster3($remainingEnergy['usedBooster3']);
   $remainingTime['usedBooster3SoFar']['FinishWithAllBooster'] = $timeAll($remainingEnergy['usedBooster3']);

//Remaining time for the user who used all the Booster with option to finish with natural, booster1, booster2, booster3
   $remainingTime['usedAllBoosterSoFar']['FinishWithNatural'] = $timeNatural($remainingEnergy['usedAllBoosters']);
   $remainingTime['usedAllBoosterSoFar']['FinishWithBooster1'] = $timeBooster1($remainingEnergy['usedAllBoosters']);
   $remainingTime['usedAllBoosterSoFar']['FinishWithBooster2'] = $timeBooster2($remainingEnergy['usedAllBoosters']);
   $remainingTime['usedAllBoosterSoFar']['FinishWithBooster3'] = $timeBooster3($remainingEnergy['usedAllBoosters']);
   $remainingTime['usedAllBoosterSoFar']['FinishWithAllBooster'] = $timeAll($remainingEnergy['usedAllBoosters']);

  return $remainingTime;
 }
?>


<?php 
//EXAMPLE TIME REMAINING TO AQCUIRE 24000 ENERGY FOR AN ELAPSED TIME OF 86400 S (24HOURS)
$test = bestTimeRemaining(24000, 86400, $naturalEnergy, 
$booster1Energy, $booster2Energy,$booster3Energy, $allBoosterEnergy, 
$timeNatural, $timeBooster1, $timeBooster2, $timeBooster3,$timeAll  ) 
?>


The player who played so far for 86400 seconds (24 hours):</br>:
<pre>
    <?php print_r($test ) ?>
</pre>

到目前为止玩了86400秒(24小时)以获得24000能量的玩家需要在几秒钟内完成这段时间:

输出:

Array
(
 [usedNaturalSoFar] => Array
    (
        [FinishWithNatural] => 2,793,600
        [FinishWithBooster1] => 2,311,945
        [FinishWithBooster2] => 1,523,782
        [FinishWithBooster3] => 1,169,414
        [FinishWithAllBooster] => 81,056
    )

[usedBooster1SoFar] => Array
    (
        [FinishWithNatural] => 2,775,600
        [FinishWithBooster1] => 2,297,048
        [FinishWithBooster2] => 1,513,964
        [FinishWithBooster3] => 1,161,879
        [FinishWithAllBooster] => 80,534
    )

[usedBooster2SoFar] => Array
    (
        [FinishWithNatural] => 2,879,880
        [FinishWithBooster1] => 2,383,349
        [FinishWithBooster2] => 1,570,844
        [FinishWithBooster3] => 1,205,531
        [FinishWithAllBooster] => 83,559
    )

[usedBooster3SoFar] => Array
    (
        [FinishWithNatural] => 2,879,880
        [FinishWithBooster1] => 2,383,349
        [FinishWithBooster2] => 1,570,844
        [FinishWithBooster3] => 1,205,531
        [FinishWithAllBooster] => 83,559
    )

[usedAllBoosterSoFar] => Array
    (
        [FinishWithNatural] => 2,879,760
        [FinishWithBooster1] => 2,383,250
        [FinishWithBooster2] => 1,570,778
        [FinishWithBooster3] => 1,205,481
        [FinishWithAllBooster] => 83,556
    )

)

答案 1 :(得分:2)

为什么你乘以86400?这是秒数,而不是几天。

其余的看起来是正确的。但是我会使用modulo(%)运算符。

所以,从剩下的我们首先减去1000,为你的助推器。然后,如果剩下的是小于180,即在6小时内获得的自然能量,你只需将其除以0.5(5能量/ 10分钟)以得到剩余的分钟数。

如果有更多,减去250 + 180并在所需时间增加6小时。然后再将它与180进行比较,如果更大则重复此步骤(如果需要则多次),否则请参见上文。

编辑:如果减去250 + 180后该值为负,则表示您使用助推器达到该值。对于24000能量的例子,情况确实如此,因此需要9天12小时。

-----我认为下面的代码是正确的,没有经过测试-----

$BOOSTER3_ENERGY = 250;

$NAT_EN_6H = 180;

$days = floor($Energy_Needed / 2470);

$remaining = $Energy_Needed % 2470;

$seconds = 0;

if ($remaining > 1000)
{
    $remaining -= 1000;
    while ($remaining > 0)
    {
        if ($remaining <= $NAT_EN_6H)
        {
            $seconds += (ceil($remaining / 5)) * 10 * 60;
            break;
        }
        else
        {
            $seconds += 6 * 60 * 60;
            $remaining -= $NAT_EN_6H + $BOOSTER3_ENERGY;
        }
    }
}