-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from nepster-web/components
Components
- Loading branch information
Showing
7 changed files
with
236 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,9 +4,6 @@ | |
|
||
namespace Gambling\Tech; | ||
|
||
use ErrorException; | ||
use Exception; | ||
|
||
/** | ||
* Base value shifting by hash | ||
*/ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters