Private
Public Access
1
0

chg: usr: add share button to the overlay when the game ends #4

This commit is contained in:
2026-04-14 19:37:42 +02:00
parent d515f42cfd
commit af67ec3931
6 changed files with 188 additions and 68 deletions

View File

@@ -20,7 +20,9 @@ use Doctrine\ORM\EntityManagerInterface;
use Exception;
use JsonException;
use Psr\Log\LoggerInterface;
use Random\RandomException;
use RuntimeException;
use Symfony\Component\Uid\Uuid;
/**
* Class RpcManager
@@ -34,9 +36,9 @@ use RuntimeException;
*/
class RpcManager implements RpcManagerInterface
{
private const ROWS = 16;
private const COLS = 16;
private const MINES = 51;
private const int ROWS = 16;
private const int COLS = 16;
private const int MINES = 51;
public function __construct(
private readonly EntityManagerInterface $entityManager,
@@ -99,6 +101,7 @@ class RpcManager implements RpcManagerInterface
$this->entityManager->persist($grid);
$playedGame->setGameAssoc($gameAssoc);
$playedGame->setUuid(Uuid::fromString($gameAssoc));
$playedGame->setGrid($grid);
$playedGame->setCreated(new DateTime());
$playedGame->setUpdated(new DateTime());
@@ -117,25 +120,32 @@ class RpcManager implements RpcManagerInterface
*/
private function generateGrid(): array
{
// Build flat set: 51 mines ('m') + remaining water ('w')
/** Build flat set: 51 mines ('m') + remaining water ('w') */
$set = array_merge(
array_fill(0, self::MINES, 'm'),
array_fill(0, self::ROWS * self::COLS - self::MINES, 'w'),
);
// Fisher-Yates shuffle
/**
* Fisher-Yates shuffle
* @see https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle
*/
for ($i = count($set) - 1; $i > 0; $i--) {
$j = random_int(0, $i);
try {
$j = random_int(0, $i);
} catch (RandomException $e) {
throw new RuntimeException('Failed to generate random index: ' . $e->getMessage());
}
[$set[$i], $set[$j]] = [$set[$j], $set[$i]];
}
// Reshape to 2-D
/** Reshape to 2-D */
$grid = [];
for ($r = 0; $r < self::ROWS; $r++) {
$grid[$r] = array_slice($set, $r * self::COLS, self::COLS);
}
// Replace 'w' with adjacent-mine count
/** Replace 'w' with adjacent-mine count */
$dirs = [[-1, -1], [-1, 0], [-1, 1], [0, -1], [0, 1], [1, -1], [1, 0], [1, 1]];
for ($r = 0; $r < self::ROWS; $r++) {
for ($c = 0; $c < self::COLS; $c++) {