Skip to content

Commit

Permalink
add game components
Browse files Browse the repository at this point in the history
  • Loading branch information
nepster-web committed Dec 6, 2021
1 parent 584f810 commit 9255a69
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 27 deletions.
26 changes: 10 additions & 16 deletions src/Game/FairRNG.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,28 +22,22 @@ public function __construct(SeedPairGenerator $seedPairGenerator)

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

$luckyNumber = $this->generateLuckyNumber($seedPair, $nonce);
$seedPair = SeedPair::increment($seedPair);

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

/**
* @param SeedPair $seedPair
* @param int $nonce
* @return float
*/
protected function generateLuckyNumber(SeedPair $seedPair, int $nonce): float
{
return (new RngTillHundred())($seedPair->getServerSeed(), $seedPair->getClientSeed(), $nonce);
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;
}
}
23 changes: 22 additions & 1 deletion src/Game/SeedPair.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,33 @@
class SeedPair
{
private string $serverSeed;

private string $nextServerSeed;

private string $clientSeed;

private int $nonce;

public function __construct(
string $serverSeed,
string $nextServerSeed,
string $clientSeed
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
Expand All @@ -34,4 +50,9 @@ public function getClientSeed(): string
{
return $this->clientSeed;
}

public function getNonce(): int
{
return $this->nonce;
}
}
23 changes: 15 additions & 8 deletions src/Game/SeedPairGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

namespace Gambling\Tech\Game;

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

class SeedPairGenerator
{
Expand All @@ -26,7 +26,7 @@ public function __construct(StoreInterface $store)
* @throws GamblingTechException
* @throws InvalidArgumentException
*/
public function generation(?string $clientSeed = null, ?SeedPair $seedPair = null): SeedPair
public function generate(?string $clientSeed = null, ?SeedPair $seedPair = null): SeedPair
{
$serverSeed = $seedPair !== null ? $seedPair->getNextServerSeed() : hash('sha256', Random::getString(32));

Expand All @@ -38,7 +38,9 @@ public function generation(?string $clientSeed = null, ?SeedPair $seedPair = nul
Random::getString(16, '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ');
}

return $this->createSeedPair($serverSeed, $nextServerSeed, $clientSeed);
$nonce = 1; // each new generation resets nonce

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

/**
Expand All @@ -49,13 +51,13 @@ public function generation(?string $clientSeed = null, ?SeedPair $seedPair = nul
* @throws GamblingTechException
* @throws InvalidArgumentException
*/
public function generationIfNeeded(object $condition): SeedPair
public function getCurrentSeedPairOrGenerate(object $condition): SeedPair
{
if ($seed = $this->getCurrentSeedPair($condition)) {
return $seed;
}

return $this->generation();
return $this->generate();
}

/**
Expand All @@ -73,10 +75,15 @@ public function getCurrentSeedPair(object $condition): ?SeedPair
* @param string $serverSeed
* @param string $nextServerSeed
* @param string $clientSeed
* @param int $nonce
* @return SeedPair
*/
protected function createSeedPair(string $serverSeed, string $nextServerSeed, string $clientSeed): SeedPair
{
return new SeedPair($serverSeed, $nextServerSeed, $clientSeed);
protected function createSeedPair(
string $serverSeed,
string $nextServerSeed,
string $clientSeed,
int $nonce
): SeedPair {
return new SeedPair($serverSeed, $nextServerSeed, $clientSeed, $nonce);
}
}
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 9255a69

Please sign in to comment.