コード例 #1
0
 /**
  * @param bool|float|\Granam\Scalar\ScalarInterface|int|string $value
  * @throws Exceptions\InvalidLevelRank
  * @throws \Doctrineum\Scalar\Exceptions\UnexpectedValueToEnum
  */
 public function __construct($value)
 {
     parent::__construct($value);
     if ($this->getValue() < 0) {
         throw new Exceptions\InvalidLevelRank('Level can not be lesser than 0, got ' . ValueDescriber::describe($value));
     }
 }
コード例 #2
0
 /**
  * @param string $weaponlikeCode
  * @param string $valueName
  * @return float|int|string
  * @throws \DrdPlus\Tables\Armaments\Exceptions\UnknownMeleeWeapon
  */
 private function getValueOf($weaponlikeCode, $valueName)
 {
     try {
         return $this->getValue([$weaponlikeCode], $valueName);
     } catch (RequiredRowNotFound $exception) {
         throw new UnknownMeleeWeapon('Unknown weapon code ' . ValueDescriber::describe($weaponlikeCode));
     }
 }
コード例 #3
0
 /**
  * @param int $hoursPerDay
  * @return int
  * @throws \DrdPlus\Tables\Measurements\Time\Exceptions\UnexpectedHoursPerDayForTimeBonusAdjustment
  * @throws \DrdPlus\Tables\Partials\Exceptions\RequiredColumnNotFound
  * @throws \Granam\Integer\Tools\Exceptions\WrongParameterType
  * @throws \Granam\Integer\Tools\Exceptions\ValueLostOnCast
  */
 private function getBonusAdjustmentForHoursPerDay($hoursPerDay)
 {
     try {
         return $this->getValue([ToInteger::toInteger($hoursPerDay)], self::ADJUSTMENT_HEADER);
     } catch (RequiredRowNotFound $requiredRowDataNotFound) {
         throw new Exceptions\UnexpectedHoursPerDayForTimeBonusAdjustment('Expected 1 to 24 hours of activity per day, got ' . ValueDescriber::describe($hoursPerDay) . ' From what universe you came from?');
     }
 }
コード例 #4
0
 /**
  * @param array $arguments
  * @return string
  */
 private function extractArgumentsDescription(array $arguments)
 {
     $descriptions = [];
     foreach ($arguments as $argument) {
         $descriptions[] = ValueDescriber::describe($argument);
     }
     return implode(',', $descriptions);
 }
コード例 #5
0
ファイル: ToFloat.php プロジェクト: jaroslavtyc/granam-float
 /**
  * @param mixed $value
  * @param bool $strict = true allows only explicit values, not null and empty string
  * @param bool $paranoid = false Throws exception if some value is lost on cast due to rounding on cast
  * @return int
  * @throws \Granam\Float\Tools\Exceptions\NegativeFloatCanNotBePositive
  * @throws \Granam\Float\Tools\Exceptions\WrongParameterType
  * @throws \Granam\Float\Tools\Exceptions\ValueLostOnCast
  */
 public static function toNegativeFloat($value, $strict = true, $paranoid = false)
 {
     $floatValue = static::toFloat($value, $strict, $paranoid);
     if ($floatValue > 0) {
         throw new Exceptions\NegativeFloatCanNotBePositive('Expected zero or lesser number, got ' . ValueDescriber::describe($value));
     }
     return $floatValue;
 }
コード例 #6
0
 /**
  * @param $value
  * @return float|int
  * @throws \Granam\Number\Tools\Exceptions\NegativeNumberCanNotBePositive
  * @throws \Granam\Number\Tools\Exceptions\WrongParameterType
  */
 public static function toNegativeNumber($value)
 {
     $positive = static::toNumber($value);
     if ($positive > 0) {
         throw new Exceptions\NegativeNumberCanNotBePositive('Expected positive number, got ' . ValueDescriber::describe($value));
     }
     return $positive;
 }
コード例 #7
0
 /**
  * @param string $armorCode
  * @param $valueName
  * @return bool|float|int|string
  * @throws \DrdPlus\Tables\Armaments\Exceptions\UnknownArmor
  */
 protected function getValueFor($armorCode, $valueName)
 {
     try {
         return $this->getValue([$armorCode], $valueName);
     } catch (\DrdPlus\Tables\Partials\Exceptions\RequiredRowNotFound $exception) {
         throw new UnknownArmor('Unknown armor code ' . ValueDescriber::describe($armorCode));
     }
 }
コード例 #8
0
ファイル: Time.php プロジェクト: jaroslavtyc/drd-plus-tables
 /**
  * @param string $unit
  * @return Time
  * @throws \DrdPlus\Tables\Measurements\Time\Exceptions\CanNotConvertTimeToUnit
  */
 public function getIn($unit)
 {
     $inDifferentUnit = $this->findIn($unit);
     if ($inDifferentUnit !== null) {
         return $inDifferentUnit;
     }
     throw new Exceptions\CanNotConvertTimeToUnit('Can not convert ' . $this->getValue() . ' ' . $this->getUnit() . '(s)' . ' into ' . ValueDescriber::describe($unit));
 }
コード例 #9
0
 /**
  * @param string $activityCode
  * @return int
  * @throws \DrdPlus\Tables\Body\Healing\Exceptions\UnknownCodeOfHealingInfluence
  * @throws \DrdPlus\Tables\Partials\Exceptions\RequiredColumnNotFound
  */
 public function getHealingBonusByActivity($activityCode)
 {
     try {
         /** @noinspection ExceptionsAnnotatingAndHandlingInspection */
         return $this->getValue([$activityCode], 'bonus');
     } catch (RequiredRowNotFound $requiredRowDataNotFound) {
         throw new Exceptions\UnknownCodeOfHealingInfluence('Unknown influence on healing code ' . ValueDescriber::describe($activityCode));
     }
 }
コード例 #10
0
 /**
  * @param int|IntegerInterface $skillRank
  * @param string $parameterName
  * @return int
  * @throws \DrdPlus\Tables\Armaments\Partials\Exceptions\UnexpectedSkillRank
  * @throws \Granam\Integer\Tools\Exceptions\WrongParameterType
  * @throws \Granam\Integer\Tools\Exceptions\ValueLostOnCast
  * @throws \DrdPlus\Tables\Partials\Exceptions\RequiredColumnNotFound
  */
 protected function getValueForSkillRank($skillRank, $parameterName)
 {
     try {
         /** @noinspection ExceptionsAnnotatingAndHandlingInspection */
         return $this->getValue([ToInteger::toInteger($skillRank)], $parameterName);
     } catch (RequiredRowNotFound $requiredRowNotFound) {
         throw new Exceptions\UnexpectedSkillRank('Expected skill rank from 0 to 3, got ' . ValueDescriber::describe($skillRank));
     }
 }
コード例 #11
0
 /**
  * @param string|StringInterface|ProjectileCode $projectileCode
  * @param string $valueName
  * @return float|int|string|bool
  * @throws \DrdPlus\Tables\Armaments\Exceptions\UnknownProjectile
  */
 protected function getValueOf($projectileCode, $valueName)
 {
     try {
         /** @noinspection ExceptionsAnnotatingAndHandlingInspection */
         return $this->getValue([$projectileCode], $valueName);
     } catch (RequiredRowNotFound $exception) {
         throw new UnknownProjectile('Unknown projectile code ' . ValueDescriber::describe((string) $projectileCode));
     }
 }
コード例 #12
0
 /**
  * @param string $shieldCode
  * @param string $valueName
  * @return int|float|string|bool
  * @throws \DrdPlus\Tables\Armaments\Exceptions\UnknownShield
  */
 private function getValueOf($shieldCode, $valueName)
 {
     try {
         /** @noinspection ExceptionsAnnotatingAndHandlingInspection */
         return $this->getValue([$shieldCode], $valueName);
     } catch (RequiredRowNotFound $exception) {
         throw new UnknownShield('Unknown shield code ' . ValueDescriber::describe($shieldCode));
     }
 }
コード例 #13
0
 /**
  * @param string|StringInterface|RangedWeaponCode $rangedWeaponCode
  * @param string $valueName
  * @return float|int|string|bool
  * @throws \DrdPlus\Tables\Armaments\Exceptions\UnknownRangedWeapon
  */
 protected function getValueOf($rangedWeaponCode, $valueName)
 {
     try {
         /** @noinspection ExceptionsAnnotatingAndHandlingInspection */
         return $this->getValue([$rangedWeaponCode], $valueName);
     } catch (RequiredRowNotFound $exception) {
         throw new UnknownRangedWeapon('Unknown ranged armament code ' . ValueDescriber::describe($rangedWeaponCode));
     }
 }
コード例 #14
0
 /**
  * @param string $code
  * @return string|ExceptionalityFate
  * @throws \DrdPlus\Exceptionalities\Fates\Exceptions\ClassNotFoundByCode
  */
 protected static function determineClassByCode($code)
 {
     $class = __NAMESPACE__ . '\\' . implode(array_map(function ($part) {
         return ucfirst($part);
     }, preg_split('~_~', $code, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE)));
     if (!class_exists($class)) {
         throw new Exceptions\ClassNotFoundByCode('Did not found a class by code ' . ValueDescriber::describe($code) . ', were searching for ' . $class);
     }
     return $class;
 }
コード例 #15
0
 /**
  * @param ProfessionCode $professionCode
  * @return Profession
  * @throws Exceptions\ProfessionNotFound
  */
 public static function getItByCode(ProfessionCode $professionCode)
 {
     $baseClassName = implode(array_map('ucfirst', explode('_', $professionCode->getValue())));
     /** @var Profession $className */
     $className = __NAMESPACE__ . '\\' . $baseClassName;
     if (!class_exists($className)) {
         throw new Exceptions\ProfessionNotFound('No profession found by code ' . ValueDescriber::describe($professionCode));
     }
     return $className::getIt();
 }
コード例 #16
0
 /**
  * Prefixes code value by its class for later identification on restoring from database.
  *
  * @param Code $code
  * @param AbstractPlatform $platform
  * @return null|string
  * @throws \Doctrineum\Scalar\Exceptions\UnexpectedValueToDatabaseValue
  */
 public function convertToDatabaseValue($code, AbstractPlatform $platform)
 {
     if ($code === null) {
         return null;
     }
     if (!is_object($code) || !is_a($code, Code::class)) {
         throw new UnexpectedValueToDatabaseValue('Expected NULL or instance of ' . Code::class . ', got ' . ValueDescriber::describe($code));
     }
     return get_class($code) . '::' . $code->getValue();
 }
コード例 #17
0
 /**
  * @param mixed $value
  * @throws \DrdPlus\Tables\Equipment\Riding\Exceptions\UnexpectedDefianceOfWildPercents
  */
 public function __construct($value)
 {
     try {
         parent::__construct($value);
     } catch (UnexpectedPercents $unexpectedPercents) {
         throw new Exceptions\UnexpectedDefianceOfWildPercents($unexpectedPercents->getMessage());
     }
     if ($this->getValue() > 100) {
         throw new Exceptions\UnexpectedDefianceOfWildPercents('Expected percents of defiance for wild animal to be from zero to one hundred, got ' . ValueDescriber::describe($value));
     }
 }
コード例 #18
0
 /**
  * @param mixed $value
  * @throws \DrdPlus\Tables\Environments\Exceptions\UnexpectedDifficultyPercents
  */
 public function __construct($value)
 {
     try {
         parent::__construct($value);
     } catch (UnexpectedPercents $unexpectedPercents) {
         throw new Exceptions\UnexpectedDifficultyPercents($unexpectedPercents->getMessage());
     }
     if ($this->getValue() > 100) {
         throw new Exceptions\UnexpectedDifficultyPercents('Percents can be from zero to one hundred, got ' . ValueDescriber::describe($value));
     }
 }
コード例 #19
0
 /**
  * @param $terrainCode
  * @return array|\int[]
  * @throws \DrdPlus\Tables\Environments\Exceptions\UnknownTerrainCode
  * @throws \DrdPlus\Tables\Environments\Exceptions\InvalidTerrainCodeFormat
  */
 public function getSpeedMalusValuesRangeForTerrain($terrainCode)
 {
     try {
         /** @noinspection ExceptionsAnnotatingAndHandlingInspection */
         return $this->getRow([$terrainCode]);
     } catch (RequiredRowNotFound $requiredRowDataNotFound) {
         throw new Exceptions\UnknownTerrainCode('Unknown terrain code ' . ValueDescriber::describe($terrainCode));
     } catch (WrongParameterType $wrongParameterType) {
         throw new Exceptions\InvalidTerrainCodeFormat('Unexpected format of a terrain code: ' . ValueDescriber::describe($terrainCode));
     }
 }
コード例 #20
0
 /**
  * @param string $conditionsCode
  * @param HealingConditionsPercents $healingConditionsPercents
  * @return int
  * @throws \DrdPlus\Tables\Body\Healing\Exceptions\UnknownCodeOfHealingInfluence
  * @throws \DrdPlus\Tables\Body\Healing\Exceptions\UnexpectedHealingConditionsPercents
  */
 public function getHealingBonusByConditions($conditionsCode, HealingConditionsPercents $healingConditionsPercents)
 {
     try {
         /** @noinspection ExceptionsAnnotatingAndHandlingInspection */
         return $this->getBonusBy($conditionsCode, $healingConditionsPercents);
     } catch (RequiredRowNotFound $requiredRowDataNotFound) {
         throw new Exceptions\UnknownCodeOfHealingInfluence('Unknown influence on healing code ' . ValueDescriber::describe($conditionsCode));
     } catch (UnexpectedPercents $unexpectedPercents) {
         throw new Exceptions\UnexpectedHealingConditionsPercents($unexpectedPercents->getMessage());
     }
 }
コード例 #21
0
 /**
  * @param mixed $value
  * @throws \DrdPlus\Tables\Partials\Exceptions\UnexpectedPercents
  */
 public function __construct($value)
 {
     try {
         $value = ToInteger::toInteger($value);
     } catch (ToIntegerException $toIntegerException) {
         throw new Exceptions\UnexpectedPercents('Invalid percent value ' . $toIntegerException->getMessage());
     }
     if ($value < 0) {
         throw new Exceptions\UnexpectedPercents('Percents can be from zero to one hundred, got ' . ValueDescriber::describe($value));
     }
     $this->value = $value;
 }
コード例 #22
0
 public function getFate($fateCode)
 {
     switch (ToString::toString($fateCode)) {
         case FateOfGoodRear::FATE_OF_GOOD_REAR:
             return $this->getFateOfGoodRear();
         case FateOfCombination::FATE_OF_COMBINATION:
             return $this->getFateOfCombination();
         case FateOfExceptionalProperties::FATE_OF_EXCEPTIONAL_PROPERTIES:
             return $this->getFateOfExceptionalProperties();
         default:
             throw new Exceptions\UnknownExceptionalityFate('Unknown exceptionality fate code ' . ValueDescriber::describe($fateCode));
     }
 }
コード例 #23
0
 /**
  * @param $value
  * @param $fromUnit
  * @param $toUnit
  * @return float
  * @throws UnknownUnit
  * @throws \Granam\Float\Tools\Exceptions\WrongParameterType
  * @throws \Granam\Float\Tools\Exceptions\ValueLostOnCast
  */
 private function getValueInDifferentUnit($value, $fromUnit, $toUnit)
 {
     if ($fromUnit === $toUnit) {
         return ToFloat::toFloat($value);
     }
     if ($fromUnit === self::M && $toUnit === self::KM) {
         return $value / 1000;
     }
     if ($fromUnit === self::KM && $toUnit === self::M) {
         return $value * 1000;
     }
     throw new UnknownUnit('Unknown one or both from ' . ValueDescriber::describe($fromUnit) . ' to ' . ValueDescriber::describe($toUnit) . ' unit(s)');
 }
コード例 #24
0
 /**
  * @param Integer1To6 $roll
  * @return int
  * @throws \DrdPlus\Exceptionalities\Fates\Exceptions\UnexpectedRoll
  */
 public function getPrimaryPropertyBonusOnFortune(Integer1To6 $roll)
 {
     switch ($roll->getValue()) {
         case 1:
         case 2:
         case 3:
             return 0;
         case 4:
         case 5:
         case 6:
             return 1;
         default:
             throw new Exceptions\UnexpectedRoll('Unexpected roll value ' . ValueDescriber::describe($roll->getValue()));
     }
 }
コード例 #25
0
 /**
  * @param string $code
  * @param Percents $percents
  * @return int
  * @throws \DrdPlus\Tables\Partials\Exceptions\UnexpectedPercents
  * @throws \Granam\Scalar\Tools\Exceptions\WrongParameterType
  * @throws \DrdPlus\Tables\Partials\Exceptions\RequiredRowNotFound
  */
 protected function getBonusBy($code, Percents $percents)
 {
     if ($percents->getValue() > 100 && !$this->canBeMore($code)) {
         throw new Exceptions\UnexpectedPercents('For ' . ValueDescriber::describe($code) . " can be percents of addition only between zero and one hundred, got {$percents}");
     }
     $bonusFrom = $this->getBonusBorder($code, self::BONUS_FROM);
     $bonusTo = $this->getBonusBorder($code, self::BONUS_TO);
     if ($bonusFrom < $bonusTo) {
         // has to swap start and end
         list($bonusFrom, $bonusTo) = [$bonusTo, $bonusFrom];
     }
     $difference = $bonusTo - $bonusFrom;
     $addition = $difference * $percents->getRate();
     $totalBonus = $bonusFrom + $addition;
     return SumAndRound::round($totalBonus);
 }
 /**
  * @param string $professionCode
  * @return string[]
  * @throws \LogicException
  */
 protected function getPrimaryProperties($professionCode)
 {
     switch ($professionCode) {
         case ProfessionCode::FIGHTER:
             return [Strength::STRENGTH, Agility::AGILITY];
         case ProfessionCode::THIEF:
             return [Agility::AGILITY, Knack::KNACK];
         case ProfessionCode::RANGER:
             return [Strength::STRENGTH, Knack::KNACK];
         case ProfessionCode::WIZARD:
             return [Will::WILL, Intelligence::INTELLIGENCE];
         case ProfessionCode::THEURGIST:
             return [Intelligence::INTELLIGENCE, Charisma::CHARISMA];
         case ProfessionCode::PRIEST:
             return [Will::WILL, Charisma::CHARISMA];
     }
     throw new \LogicException('Unknown profession code ' . ValueDescriber::describe($professionCode));
 }
コード例 #27
0
 /**
  * @param $propertyValue
  * @param string|StringInterface $propertyCode
  * @return Agility|Charisma|Intelligence|Knack|Strength|Will
  * @throws \DrdPlus\Properties\Base\Exceptions\UnknownBasePropertyCode
  * @throws \Granam\Scalar\Tools\Exceptions\WrongParameterType
  */
 public function createProperty($propertyValue, $propertyCode)
 {
     switch (ToString::toString($propertyCode)) {
         case Strength::STRENGTH:
             return $this->createStrength($propertyValue);
         case Agility::AGILITY:
             return $this->createAgility($propertyValue);
         case Knack::KNACK:
             return $this->createKnack($propertyValue);
         case Will::WILL:
             return $this->createWill($propertyValue);
         case Intelligence::INTELLIGENCE:
             return $this->createIntelligence($propertyValue);
         case Charisma::CHARISMA:
             return $this->createCharisma($propertyValue);
         default:
             throw new Exceptions\UnknownBasePropertyCode('Unknown code of a base property ' . ValueDescriber::describe($propertyCode));
     }
 }
コード例 #28
0
 /**
  * @param ProfessionCode $professionCode
  * @param BaseProperties $baseProperties
  * @param Height $height
  * @return int
  * @throws \DrdPlus\Properties\Combat\Exceptions\UnknownProfession
  */
 private function calculateValue(ProfessionCode $professionCode, BaseProperties $baseProperties, Height $height)
 {
     $modifierByHeight = SumAndRound::ceiledThird($height->getValue()) - 2;
     switch ($professionCode->getValue()) {
         case ProfessionCode::FIGHTER:
             return $baseProperties->getAgility()->getValue() + $modifierByHeight;
         case ProfessionCode::THIEF:
         case ProfessionCode::RANGER:
             // same as a thief
             return SumAndRound::average($baseProperties->getAgility()->getValue(), $baseProperties->getKnack()->getValue()) + $modifierByHeight;
         case ProfessionCode::WIZARD:
         case ProfessionCode::THEURGIST:
             // same as a wizard
             return SumAndRound::average($baseProperties->getAgility()->getValue(), $baseProperties->getIntelligence()->getValue()) + $modifierByHeight;
         case ProfessionCode::PRIEST:
             return SumAndRound::average($baseProperties->getAgility()->getValue(), $baseProperties->getCharisma()->getValue()) + $modifierByHeight;
         default:
             throw new Exceptions\UnknownProfession('Unknown profession of code ' . ValueDescriber::describe($professionCode->getValue()));
     }
 }
コード例 #29
0
 /**
  * @test
  * @dataProvider provideSomeArguments
  * @param $justSomeArgument
  * @param $justAnotherArgument
  */
 public function I_can_subtract_value($justSomeArgument, $justAnotherArgument)
 {
     /** @var AbstractIntegerProperty $propertyClass */
     $propertyClass = self::getSutClass();
     /** @var AbstractIntegerProperty $property */
     $property = $propertyClass::getIt(123);
     $expectedChangeBy = ['name' => 'i can subtract value', 'with' => implode(',', [ValueDescriber::describe($justSomeArgument), ValueDescriber::describe($justAnotherArgument)])];
     $expectedPropertyHistory = [['changeBy' => $expectedChangeBy, 'result' => $property->getValue()]];
     self::assertEquals($expectedPropertyHistory, $property->getHistory());
     $lesser = $property->sub(456);
     self::assertSame(123, $property->getValue());
     self::assertNotEquals($property, $lesser);
     self::assertSame(-333, $lesser->getValue());
     $expectedLesserHistoryChange = ['changeBy' => $expectedChangeBy, 'result' => $lesser->getValue()];
     $expectedLesserHistory = $expectedPropertyHistory;
     $expectedLesserHistory[] = $expectedLesserHistoryChange;
     self::assertEquals($expectedLesserHistory, $lesser->getHistory());
     $zero = $lesser->sub($lesser);
     $expectedLesserHistoryChange = ['changeBy' => $expectedChangeBy, 'result' => $zero->getValue()];
     $expectedZeroHistory = $expectedLesserHistory;
     $expectedZeroHistory[] = $expectedLesserHistoryChange;
     self::assertEquals($expectedZeroHistory, $zero->getHistory());
     self::assertSame(0, $zero->getValue());
 }
コード例 #30
0
ファイル: Race.php プロジェクト: jaroslavtyc/drd-plus-races
 /**
  * @param string $basePropertyCode
  * @param GenderCode $genderCode
  * @param Tables $tables
  * @return int
  * @throws \DrdPlus\Races\Exceptions\UnknownBasePropertyCode
  * @throws \DrdPlus\Races\Exceptions\UnknownPropertyCode
  */
 public function getBaseProperty($basePropertyCode, GenderCode $genderCode, Tables $tables)
 {
     if (in_array($basePropertyCode, PropertyCode::getBasePropertyPossibleValues(), true)) {
         return $this->getProperty($basePropertyCode, $genderCode, $tables);
     }
     throw new Exceptions\UnknownBasePropertyCode('Unknown base property ' . ValueDescriber::describe($basePropertyCode));
 }