Skip to content

Commit

Permalink
Merge pull request #1 from nepster-web/components
Browse files Browse the repository at this point in the history
Components
  • Loading branch information
nepster-web authored Dec 6, 2021
2 parents 2cd9555 + 9255a69 commit f234a77
Show file tree
Hide file tree
Showing 7 changed files with 236 additions and 5 deletions.
43 changes: 43 additions & 0 deletions src/Game/FairRNG.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

declare(strict_types=1);

namespace Gambling\Tech\Game;

use Gambling\Tech\Dice\RngTillHundred;
use Gambling\Tech\Exception\GamblingTechException;
use Gambling\Tech\Exception\InvalidArgumentException;

class FairRNG
{
private SeedPairGenerator $seedPairGenerator;

/**
* @param SeedPairGenerator $seedPairGenerator
*/
public function __construct(SeedPairGenerator $seedPairGenerator)
{
$this->seedPairGenerator = $seedPairGenerator;
}

/**
* @param object $condition
* @return LuckyNumber
* @throws GamblingTechException
* @throws InvalidArgumentException
*/
public function __invoke(object $condition): LuckyNumber
{
$seedPair = $this->seedPairGenerator->getCurrentSeedPairOrGenerate($condition);

$seedPair = SeedPair::increment($seedPair);

$luckyNumber = (new RngTillHundred())(
$seedPair->getServerSeed(),
$seedPair->getClientSeed(),
$seedPair->getNonce()
);

return new LuckyNumber($luckyNumber, $seedPair);
}
}
33 changes: 33 additions & 0 deletions src/Game/LuckyNumber.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace Gambling\Tech\Game;

class LuckyNumber
{
private float $value;

private SeedPair $seedPair;

public function __construct(float $value, SeedPair $seedPair)
{
$this->value = $value;
$this->seedPair = $seedPair;
}

public function getValue(): float
{
return $this->value;
}

public function getSeedPair(): SeedPair
{
return $this->seedPair;
}

public function __toString(): string
{
return (string)$this->value;
}
}
58 changes: 58 additions & 0 deletions src/Game/SeedPair.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

declare(strict_types=1);

namespace Gambling\Tech\Game;

class SeedPair
{
private string $serverSeed;

private string $nextServerSeed;

private string $clientSeed;

private int $nonce;

public function __construct(
string $serverSeed,
string $nextServerSeed,
string $clientSeed,
int $nonce
) {
$this->serverSeed = $serverSeed;
$this->nextServerSeed = $nextServerSeed;
$this->clientSeed = $clientSeed;
$this->nonce = $nonce;
}

public static function increment(self $seedPair): self
{
return new self(
$seedPair->getServerSeed(),
$seedPair->getNextServerSeed(),
$seedPair->getClientSeed(),
$seedPair->getNonce() + 1
);
}

public function getServerSeed(): string
{
return $this->serverSeed;
}

public function getNextServerSeed(): string
{
return $this->nextServerSeed;
}

public function getClientSeed(): string
{
return $this->clientSeed;
}

public function getNonce(): int
{
return $this->nonce;
}
}
89 changes: 89 additions & 0 deletions src/Game/SeedPairGenerator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php

declare(strict_types=1);

namespace Gambling\Tech\Game;

use Gambling\Tech\Random;
use Gambling\Tech\Exception\GamblingTechException;
use Gambling\Tech\Exception\InvalidArgumentException;

class SeedPairGenerator
{
private StoreInterface $store;

public function __construct(StoreInterface $store)
{
$this->store = $store;
}

/**
* Generation new random seed pair
*
* @param string|null $clientSeed
* @param SeedPair|null $seedPair
* @return SeedPair
* @throws GamblingTechException
* @throws InvalidArgumentException
*/
public function generate(?string $clientSeed = null, ?SeedPair $seedPair = null): SeedPair
{
$serverSeed = $seedPair !== null ? $seedPair->getNextServerSeed() : hash('sha256', Random::getString(32));

$nextServerSeed = hash('sha256', Random::getString(32));

if ($clientSeed === null) {
$clientSeed = $seedPair ?
$seedPair->getClientSeed() :
Random::getString(16, '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ');
}

$nonce = 1; // each new generation resets nonce

return $this->createSeedPair($serverSeed, $nextServerSeed, $clientSeed, $nonce);
}

/**
* Get current seed pair or generation new random seed pair
*
* @param object $condition
* @return SeedPair
* @throws GamblingTechException
* @throws InvalidArgumentException
*/
public function getCurrentSeedPairOrGenerate(object $condition): SeedPair
{
if ($seed = $this->getCurrentSeedPair($condition)) {
return $seed;
}

return $this->generate();
}

/**
* Get the current active seed by an arbitrary condition
*
* @param object $condition
* @return SeedPair|null
*/
public function getCurrentSeedPair(object $condition): ?SeedPair
{
return $this->store->getCurrentSeedPair($condition);
}

/**
* @param string $serverSeed
* @param string $nextServerSeed
* @param string $clientSeed
* @param int $nonce
* @return SeedPair
*/
protected function createSeedPair(
string $serverSeed,
string $nextServerSeed,
string $clientSeed,
int $nonce
): SeedPair {
return new SeedPair($serverSeed, $nextServerSeed, $clientSeed, $nonce);
}
}
10 changes: 10 additions & 0 deletions src/Game/StoreInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace Gambling\Tech\Game;

interface StoreInterface
{
public function getCurrentSeedPair(object $condition): ?SeedPair;
}
3 changes: 0 additions & 3 deletions src/ShiftedNumber.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@

namespace Gambling\Tech;

use ErrorException;
use Exception;

/**
* Base value shifting by hash
*/
Expand Down
5 changes: 3 additions & 2 deletions tests/ShiftedNumberTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

namespace Gambling\TechTest;

use Gambling\Tech\ShiftedNumber;
use PHPUnit\Framework\TestCase;
use Gambling\Tech\ShiftedNumber;

class ShiftedNumberTest extends TestCase
{
Expand Down Expand Up @@ -33,7 +33,8 @@ public function testMinLimit(): void
for ($i = 0; $i < 100; ++$i) {
$number = ((new ShiftedNumber())
->setMin($min)->setMax(1000))($i, sha1(uniqid('', true)));
if ($min > $number) { echo $number . ' ';
if ($min > $number) {
echo $number . ' ';
$pinpoint = true;
}
}
Expand Down

0 comments on commit f234a77

Please sign in to comment.