You are here

private function RadioactivityProcessor::calculateEnergy in Radioactivity 4.0.x

Calculate energy value using decay profile data.

Parameters

float $energy: The current energy value.

int $timestamp: The last changed timestamp.

string $profile: The profile type.

int $halfLife: The half life time, in seconds.

Return value

float The calculated energy value.

1 call to RadioactivityProcessor::calculateEnergy()
RadioactivityProcessor::queueProcessDecay in src/RadioactivityProcessor.php
Queue processing of Radioactivity decays.

File

src/RadioactivityProcessor.php, line 365

Class

RadioactivityProcessor
Processes Radioactivity incidents and and energy decay.

Namespace

Drupal\radioactivity

Code

private function calculateEnergy($energy, $timestamp, $profile, $halfLife) {
  $result = $energy;
  $elapsed = $timestamp ? $this->requestTime - $timestamp : 0;
  switch ($profile) {
    case 'linear':
      $result = $energy > $elapsed ? $energy - $elapsed : 0;
      break;
    case 'decay':
      $result = $energy * pow(2, -$elapsed / $halfLife);
      break;
  }
  return $result;
}