/** * @param Distance $distance * @return int */ public function getAttackNumberModifierByDistance(Distance $distance) { $distanceInMeters = $distance->getMeters(); $orderedByDistanceDesc = $this->getOrderedByDistanceAsc(); $attackNumberModifierCandidate = null; foreach ($orderedByDistanceDesc as $distanceInMetersFrom => $row) { /** @noinspection ExceptionsAnnotatingAndHandlingInspection */ if ($distanceInMeters >= ToFloat::toPositiveFloat($distanceInMetersFrom)) { $attackNumberModifierCandidate = $row[self::RANGED_ATTACK_NUMBER_MODIFIER]; } } return $attackNumberModifierCandidate; }
/** * @param Distance $distance * @return int * @throws Exceptions\DistanceOutOfKnownValues */ public function getAttackNumberModifierByDistance(Distance $distance) { $distanceInMeters = $distance->getMeters(); $orderedByDistanceDesc = $this->getOrderedByDistanceAsc(); foreach ($orderedByDistanceDesc as $distanceInMetersTo => $row) { /** @noinspection ExceptionsAnnotatingAndHandlingInspection */ if ($distanceInMeters <= ToFloat::toPositiveFloat($distanceInMetersTo)) { // including return $row[self::RANGED_ATTACK_NUMBER_MODIFIER]; } } throw new Exceptions\DistanceOutOfKnownValues("Given distance {$distance} is so far so we do not have values for it"); }
/** * Values may be already ordered from file, but have to be sure. * * @return array|\string[][] */ protected function getOrderedByDistanceAsc() { $values = $this->getIndexedValues(); uksort($values, function ($oneDistanceInMeters, $anotherDistanceInMeters) { /** @noinspection ExceptionsAnnotatingAndHandlingInspection */ $oneDistanceInMeters = ToFloat::toPositiveFloat($oneDistanceInMeters); $anotherDistanceInMeters = ToFloat::toPositiveFloat($anotherDistanceInMeters); if ($oneDistanceInMeters < $anotherDistanceInMeters) { return -1; // lowest first } if ($oneDistanceInMeters > $anotherDistanceInMeters) { return 1; } return 0; }); return $values; }
/** * @param mixed $value * @param bool $strict = true allows only explicit values, not null and empty string * @param bool $paranoid Throws exception if some value is lost on cast because of rounding * @throws \Granam\Float\Tools\Exceptions\PositiveFloatCanNotBeNegative * @throws \Granam\Float\Tools\Exceptions\WrongParameterType * @throws \Granam\Float\Tools\Exceptions\ValueLostOnCast */ public function __construct($value, $strict = true, $paranoid = false) { /** @noinspection ExceptionsAnnotatingAndHandlingInspection */ parent::__construct(ToFloat::toPositiveFloat($value, $strict, $paranoid)); }