Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Calculating Game Energy Consumption Timescale

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

like image 685
Tim Marshall Avatar asked Jul 04 '17 01:07

Tim Marshall


1 Answers

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
    )

)
like image 139
Michael GEDION Avatar answered Sep 20 '22 13:09

Michael GEDION