public function evaluate($scope = []) { for ($i = 0; $i < $this->count; $i++) { $this->results[] = rollDie($this->sides); } $this->value = array_reduce($this->results, function ($sum, $result) { return $sum + $result; }, 0); return $this; }
/** * Re-roll a die when result below the first input parameter * * @param $minVal float * @param $expr Roll * @param $scope Scope * * @return int * @throws \TypeError */ public function rerollBelow($minVal, $expr, $scope) { $roll = $expr->evaluate($scope); if ($expr->type !== 'roll') { throw new \TypeError('Non-roll passed to "rerollBelow()": ' . $expr); } $minVal = $minVal->evaluate($scope); if (!is_numeric($minVal->value)) { throw new \TypeError('Non-number passed to "rerollBelow()": ' . $minVal->value); } $results = array_reduce($roll->results, function ($results, $result) use($minVal, $roll) { if ($result <= $minVal->value) { // We reroll, and keep $results[] = rollDie($roll->sides); } else { $results[] = $result; } return $results; }, []); return array_reduce($results, function ($sum, $result) { return $sum + $result; }, 0); }