7

What I Am Trying To Achieve

In an on-line game, you can achieve awards and other energy based goals. One could require a total of 24,000 energy which is a time based one, whereas others only 25. Based on starting with 0 energy and no lost energy sitting there whilst the user sleeps or whatnot, I wish to calculate how long it would take to acquire the amount of energy needed.

--------------------------------------------------------------------
|   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   |
--------------------------------------------------------------------

The total energy a person can amount to in a 24 hour period is 2,470 via 720 natural energy, 150 from Booster 1, 600 from Booster 2 and 1,000 from Booster 3.

So you gain 5 natural energy every 10 minutes (600 seconds) and you can top up your energy with boosters which instantly adds the "Cooldown". So based on starting with 0 energy, you could jump to 1,000 energy. The "Wait" means you need to wait until the cooldown is over or not.


What I Have Done So Far

To calculate the days, I have done the following:

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

And for the remaining energy needed I have done the following:

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

Question

Once the $days have been calculated, it is a matter of starting of fresh again so if $remaining > 1000 (as the user can jump to this so it would just be N Days) how can I find the best remaining time?

NOTE: I am only looking for the total amount of seconds, no "prettiness".

mickmackusa
  • 43,625
  • 12
  • 83
  • 136
Tyler
  • 854
  • 1
  • 10
  • 26
  • 4
    ...you totally lost me on this explanation. 24,000 versus 25 ? That's a pretty big gap. – mickmackusa Jul 04 '17 at 02:02
  • @mickmackusa They would be energy inputs, so I would call the function and send one of these values for example. – Tyler Jul 04 '17 at 02:09
  • 1
    So you would call a function and send one of these values... where? With what effect? I guess you'll find the answer yourself as soon as you learn how to explain it (*Rubber duck debugging*). – shudder Jul 25 '17 at 21:48
  • why is that $remaining = $Energy_Needed - (floor($Energy_Needed / 2470) * 2470); ? – Michael GEDION Jul 27 '17 at 16:08
  • Booster 2 have 6 hours cooldown but no need to wait until cooldown is over, what purpose of this cooldown? Is that adding main cooldown? If the cooldown is for all the type of booster and natural, then 2470 is not maximum energy in 24h – Jun Rikson Jul 28 '17 at 06:44
  • If booster 2's cooldown reaches over 24 hours, you cannot take another one until it has – Tyler Jul 28 '17 at 22:34
  • _So based on starting with 0 energy, you could jump to 1,000 energy_. it can be 1005 if we consider Natural energy awarded at 0th min? – Karan Thakkar Aug 01 '17 at 12:40

2 Answers2

5

Try something like this. It was hard for me to understand the problem!

Actually this is a tested working example. I have made these assumptions:

1)Any player with the booster or not will acquire the natural energy

2)The Wait time will not affect THE BOOSTER2

Try this

<?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>

The player who played so far for 86400 seconds (24 hours) TO ACQUIRE 24000 ENERGY needs this amount of time in seconds:

OUTPUT :

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
    )

)
Michael GEDION
  • 879
  • 8
  • 16
  • Please give me some time to look over your answer. The initial outputs don't appear to seem correct for what I am looking for. I will look over your answer and maybe update my question for you to review. Thank you – Tyler Jul 28 '17 at 22:36
2

Why are you multiplying with 86400? That's the number of seconds, not days.

The remainder looks correct. I would however use the modulo (%) operator.

So, from the remainder we first subtract 1000, for your boosters. Then, if what is left is less than 180, i.e. the natural energy gained in 6 hours, you just divide it by 0.5 (5 energy/10 min) to get the number of minutes left.

If there is more than that, subtract 250 + 180 and add 6 h to the time needed. Then again compare it to 180, if larger repeat this step (multiple times if needed), otherwise see above.

EDIT: If the value is negative after subtracting 250+180, it means you reach the value using the booster. For your example with 24000 energy, this is the case, so it takes exactly 9 days and 12 hours.

----- I think the code below is correct, not tested -----

$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;
        }
    }
}
raahlb
  • 195
  • 2
  • 10