Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"doctrine/doctrine-migrations-bundle": "^3.3",
"doctrine/migrations": "^3.7",
"doctrine/orm": "^3.0",
"gaming-platform/api": "^1.8",
"gaming-platform/api": "^1.9",
"jms/serializer": "^3.17",
"marein/php-nchan-client": "^3.1",
"marein/symfony-lock-doctrine-migrations-bundle": "^1.0",
Expand Down
14 changes: 7 additions & 7 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion config/connect-four/services/game.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ services:

connect-four.open-game-store:
class: Gaming\ConnectFour\Port\Adapter\Persistence\Repository\PredisOpenGameStore
arguments: ['@connect-four.predis', 'open-games']
arguments: ['@connect-four.predis', 'open-games', '@connect-four.normalizer']

connect-four.running-games-store:
class: Gaming\ConnectFour\Port\Adapter\Persistence\Repository\PredisRunningGameStore
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ final class OpenGame
{
public function __construct(
public readonly string $gameId,
public readonly int $width,
public readonly int $height,
public readonly string $playerId
) {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ public function save(OpenGame $openGame): void;

public function remove(string $gameId): void;

public function all(): OpenGames;
public function all(int $limit): OpenGames;
}
10 changes: 4 additions & 6 deletions src/ConnectFour/Application/Game/Query/OpenGamesHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,13 @@

final class OpenGamesHandler
{
private OpenGameStore $openGameStore;

public function __construct(OpenGameStore $openGameStore)
{
$this->openGameStore = $openGameStore;
public function __construct(
private readonly OpenGameStore $openGameStore
) {
}

public function __invoke(OpenGamesQuery $query): OpenGames
{
return $this->openGameStore->all();
return $this->openGameStore->all($query->limit);
}
}
4 changes: 4 additions & 0 deletions src/ConnectFour/Application/Game/Query/OpenGamesQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,8 @@
*/
final class OpenGamesQuery implements Request
{
public function __construct(
public readonly int $limit
) {
}
}
2 changes: 1 addition & 1 deletion src/ConnectFour/Port/Adapter/Http/FragmentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function statisticsAction(): Response
public function openGamesAction(): Response
{
return $this->render('@connect-four/open-games.html.twig', [
'openGames' => $openGames = $this->queryBus->handle(new OpenGamesQuery()),
'openGames' => $openGames = $this->queryBus->handle(new OpenGamesQuery(100)),
'usernames' => $this->usernames->byIds(
array_map(
static fn(OpenGame $openGame) => $openGame->playerId,
Expand Down
32 changes: 32 additions & 0 deletions src/ConnectFour/Port/Adapter/Messaging/RpcMessageHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
use Gaming\ConnectFour\Application\Game\Query\Model\Game\Game;
use Gaming\ConnectFour\Application\Game\Query\Model\Game\Move;
use Gaming\ConnectFour\Application\Game\Query\Model\GamesByPlayer\State;
use Gaming\ConnectFour\Application\Game\Query\Model\OpenGames\OpenGame;
use Gaming\ConnectFour\Application\Game\Query\OpenGamesQuery;
use GamingPlatform\Api\ConnectFour\V1\ConnectFourV1;
use GamingPlatform\Api\ConnectFour\V1\Game as ProtoV1Game;
use GamingPlatform\Api\ConnectFour\V1\GetGamesByPlayer\State as ProtoV1State;
Expand All @@ -33,6 +35,7 @@ public function handle(Message $message, Context $context): void
ConnectFourV1::OpenGameType => $this->handleOpenGame($message, $context),
ConnectFourV1::JoinGameType => $this->handleJoinGame($message, $context),
ConnectFourV1::MakeMoveType => $this->handleMakeMove($message, $context),
ConnectFourV1::GetOpenGamesType => $this->handleGetOpenGames($message, $context),
ConnectFourV1::GetGamesByPlayerType => $this->handleGetGamesByPlayer($message, $context),
default => true
};
Expand Down Expand Up @@ -99,6 +102,35 @@ private function handleMakeMove(Message $message, Context $context): void
);
}

private function handleGetOpenGames(Message $message, Context $context): void
{
$request = ConnectFourV1::createGetOpenGames($message->body());

$response = $this->queryBus->handle(
new OpenGamesQuery(
$request->getLimit()
)
);

$context->reply(
new Message(
ConnectFourV1::GetOpenGamesResponseType,
ConnectFourV1::createGetOpenGamesResponse()
->setGames(
array_map(
static fn(OpenGame $game) => ConnectFourV1::createGetOpenGamesResponse_Game()
->setGameId($game->gameId)
->setWidth($game->width)
->setHeight($game->height)
->setPlayerId($game->playerId),
$response->games()
)
)
->serializeToString()
)
);
}

private function handleGetGamesByPlayer(Message $message, Context $context): void
{
$request = ConnectFourV1::createGetGamesByPlayer($message->body());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,17 @@ public function handle(DomainEvent $domainEvent): void
$content = $domainEvent->content;

match ($content::class) {
GameOpened::class => $this->saveGame($content->aggregateId(), $content->playerId()),
GameOpened::class => $this->openGameStore->save(
new OpenGame(
$content->aggregateId(),
$content->width(),
$content->height(),
$content->playerId()
)
),
GameAborted::class,
PlayerJoined::class => $this->removeGame($content->aggregateId()),
PlayerJoined::class => $this->openGameStore->remove($content->aggregateId()),
default => true
};
}

private function saveGame(string $gameId, string $playerId): void
{
$this->openGameStore->save(
new OpenGame($gameId, $playerId)
);
}

private function removeGame(string $gameId): void
{
$this->openGameStore->remove($gameId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Gaming\ConnectFour\Port\Adapter\Persistence\Repository;

use Gaming\Common\Normalizer\Normalizer;
use Gaming\ConnectFour\Application\Game\Query\Model\OpenGames\OpenGame;
use Gaming\ConnectFour\Application\Game\Query\Model\OpenGames\OpenGames;
use Gaming\ConnectFour\Application\Game\Query\Model\OpenGames\OpenGameStore;
Expand All @@ -16,14 +17,21 @@ final class PredisOpenGameStore implements OpenGameStore

public function __construct(
private readonly Client $predis,
private readonly string $storageKey
private readonly string $storageKey,
private readonly Normalizer $normalizer
) {
}

public function save(OpenGame $openGame): void
{
$this->predis->pipeline(function (ClientContextInterface $pipeline) use ($openGame): void {
$pipeline->set($this->storageKeyForGameInfo($openGame->gameId), $openGame->playerId);
$pipeline->set(
$this->storageKeyForGameInfo($openGame->gameId),
json_encode(
$this->normalizer->normalize($openGame, OpenGame::class),
JSON_THROW_ON_ERROR
)
);

$pipeline->zadd(
$this->storageKey,
Expand All @@ -41,17 +49,19 @@ public function remove(string $gameId): void
});
}

public function all(): OpenGames
public function all(int $limit): OpenGames
{
$gameIds = $this->predis->zrange($this->storageKey, 0, 10000);
$gameIds = $this->predis->zrange($this->storageKey, 0, $limit - 1);
if (count($gameIds) === 0) {
return new OpenGames([]);
}

return new OpenGames(
array_map(
static fn(string $gameId, string $playerId): OpenGame => new OpenGame($gameId, $playerId),
$gameIds,
fn(string $openGame): OpenGame => $this->normalizer->denormalize(
json_decode($openGame, true, flags: JSON_THROW_ON_ERROR),
OpenGame::class
),
$this->predis->mget(array_map($this->storageKeyForGameInfo(...), $gameIds))
)
);
Expand Down