PHP计算游戏能耗时间尺度

Tim*_*all 7 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   |
--------------------------------------------------------------------
Run Code Online (Sandbox Code Playgroud)

一个人在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;
Run Code Online (Sandbox Code Playgroud)

为了剩余的能量,我做了以下事情:

$remaining = $Energy_Needed - (floor($Energy_Needed / 2470) * 2470);
Run Code Online (Sandbox Code Playgroud)

一旦$days计算出来,就应该再次开始新鲜事了if $remaining > 1000(因为用户可以跳到这里,所以它只是N天)我怎样才能找到最佳的剩余时间?

注意:我只寻找总秒数,没有"漂亮".

Mic*_*ION 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>
Run Code Online (Sandbox Code Playgroud)

到目前为止玩了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
    )

)
Run Code Online (Sandbox Code Playgroud)