From 0cadd9af2424cd0db2ca8e34211a1e4a27dd0dd1 Mon Sep 17 00:00:00 2001 From: Nepster Date: Wed, 20 Oct 2021 16:37:48 +0300 Subject: [PATCH] add doc ShiftedNumber --- docs/guide/ShiftedNumber.md | 11 +++++-- src/ShiftedNumber.php | 44 +++++++++++++++++-------- tests/ShiftedNumberTest.php | 66 +++++++++++++++++++++++++++++++++++++ 3 files changed, 105 insertions(+), 16 deletions(-) create mode 100644 tests/ShiftedNumberTest.php diff --git a/docs/guide/ShiftedNumber.md b/docs/guide/ShiftedNumber.md index 557830d..d60fd30 100644 --- a/docs/guide/ShiftedNumber.md +++ b/docs/guide/ShiftedNumber.md @@ -1,11 +1,16 @@ Shifted number ============== ------------------- +Shifts the specified number using a hash. +**Example:** ----- - +```php +$number = ((new ShiftedNumber()) + ->setMin(0) + ->setMax(100)) + (42, 'my_hash'); +```
diff --git a/src/ShiftedNumber.php b/src/ShiftedNumber.php index 2cfad8d..9f6faa3 100644 --- a/src/ShiftedNumber.php +++ b/src/ShiftedNumber.php @@ -4,14 +4,17 @@ namespace Gambling\Tech; +use ErrorException; +use Exception; + /** * Base value shifting by hash */ class ShiftedNumber { - private float $min = 0; + private int $min = 0; - private float $max = 100; + private int $max = 100; /** * @param float $number @@ -22,14 +25,14 @@ public function __invoke(float $number, string $hash): int { $shiftValue = $this->getShiftValue($hash); - return $this->shift($number, $shiftValue); + return (int)$this->shift($number, $shiftValue); } /** - * @param float $min + * @param int $min * @return $this */ - public function setMin(float $min): self + public function setMin(int $min): self { $this->min = $min; @@ -37,10 +40,10 @@ public function setMin(float $min): self } /** - * @param float $max + * @param int $max * @return $this */ - public function setMax(float $max): self + public function setMax(int $max): self { $this->max = $max; @@ -50,25 +53,40 @@ public function setMax(float $max): self /** * @param float $number * @param int $shift - * @return int + * @return float */ - protected function shift(float $number, int $shift): int + protected function shift(float $number, int $shift): float { if ($shift > $this->max) { $shift %= ($this->max - $this->min + 1); } - return $number + $shift <= $this->max ? + $shift = $shift === 0 ? (int)(7 / 100 * $this->max) : $shift; + + $result = $number + $shift <= $this->max ? $number + $shift : $this->min + ($number + $shift - $this->max) - 1; + + if ($result < $this->min || $result > $this->max) { + $float = round(($this->min / ($this->max + 1)) + M_PI, 5); + $percent = (int)mb_substr((string)$float, -2, 2); + $result = ($this->min === 0 ? $this->max / 2 : $this->min) + ($percent / 100 * $this->min); + $result = $result > $this->max ? $this->max : $result; + } + + return $result; } /** - * @param string $clientHash + * @param string $hash * @return int */ - protected function getShiftValue(string $clientHash): int + protected function getShiftValue(string $hash): int { - return (int)hexdec(substr($clientHash, -5)); + if (preg_match("/^([a-f0-9])$/", $hash) === 0) { + $hash = sha1($hash); + } + + return (int)hexdec(mb_substr($hash, -5)); } } diff --git a/tests/ShiftedNumberTest.php b/tests/ShiftedNumberTest.php new file mode 100644 index 0000000..13e99dd --- /dev/null +++ b/tests/ShiftedNumberTest.php @@ -0,0 +1,66 @@ +setMin($min)->setMax(1000))($i, sha1(uniqid('', true))); + if ($min > $number) { echo $number . ' '; + $pinpoint = true; + } + } + + self::assertFalse($pinpoint); + } + + public function testMaxLimit(): void + { + $max = 1000; + $pinpoint = false; + + for ($i = 0; $i < 100; ++$i) { + $number = ((new ShiftedNumber()) + ->setMin(100)->setMax($max))($i, sha1(uniqid('', true))); + if ($number > $max) { + $pinpoint = true; + } + } + + self::assertFalse($pinpoint); + } + + public function testShiftedNumberByRandomString(): void + { + $number = (new ShiftedNumber())(7, 'my_random_string'); + + self::assertNotEquals(7, $number); + } +}