Skip to content

Commit 7c3a484

Browse files
authored
Expose GetOpenGames via messaging (#310)
Expose GetOpenGames via messaging and update the query model with all required bot data #122.
1 parent db4c99a commit 7c3a484

File tree

11 files changed

+78
-37
lines changed

11 files changed

+78
-37
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
"doctrine/doctrine-migrations-bundle": "^3.3",
3838
"doctrine/migrations": "^3.7",
3939
"doctrine/orm": "^3.0",
40-
"gaming-platform/api": "^1.8",
40+
"gaming-platform/api": "^1.9",
4141
"jms/serializer": "^3.17",
4242
"marein/php-nchan-client": "^3.1",
4343
"marein/symfony-lock-doctrine-migrations-bundle": "^1.0",

composer.lock

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config/connect-four/services/game.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ services:
1717

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

2222
connect-four.running-games-store:
2323
class: Gaming\ConnectFour\Port\Adapter\Persistence\Repository\PredisRunningGameStore

src/ConnectFour/Application/Game/Query/Model/OpenGames/OpenGame.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ final class OpenGame
88
{
99
public function __construct(
1010
public readonly string $gameId,
11+
public readonly int $width,
12+
public readonly int $height,
1113
public readonly string $playerId
1214
) {
1315
}

src/ConnectFour/Application/Game/Query/Model/OpenGames/OpenGameStore.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@ public function save(OpenGame $openGame): void;
1010

1111
public function remove(string $gameId): void;
1212

13-
public function all(): OpenGames;
13+
public function all(int $limit): OpenGames;
1414
}

src/ConnectFour/Application/Game/Query/OpenGamesHandler.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,13 @@
99

1010
final class OpenGamesHandler
1111
{
12-
private OpenGameStore $openGameStore;
13-
14-
public function __construct(OpenGameStore $openGameStore)
15-
{
16-
$this->openGameStore = $openGameStore;
12+
public function __construct(
13+
private readonly OpenGameStore $openGameStore
14+
) {
1715
}
1816

1917
public function __invoke(OpenGamesQuery $query): OpenGames
2018
{
21-
return $this->openGameStore->all();
19+
return $this->openGameStore->all($query->limit);
2220
}
2321
}

src/ConnectFour/Application/Game/Query/OpenGamesQuery.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,8 @@
1212
*/
1313
final class OpenGamesQuery implements Request
1414
{
15+
public function __construct(
16+
public readonly int $limit
17+
) {
18+
}
1519
}

src/ConnectFour/Port/Adapter/Http/FragmentController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public function statisticsAction(): Response
3636
public function openGamesAction(): Response
3737
{
3838
return $this->render('@connect-four/open-games.html.twig', [
39-
'openGames' => $openGames = $this->queryBus->handle(new OpenGamesQuery()),
39+
'openGames' => $openGames = $this->queryBus->handle(new OpenGamesQuery(100)),
4040
'usernames' => $this->usernames->byIds(
4141
array_map(
4242
static fn(OpenGame $openGame) => $openGame->playerId,

src/ConnectFour/Port/Adapter/Messaging/RpcMessageHandler.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
use Gaming\ConnectFour\Application\Game\Query\Model\Game\Game;
1616
use Gaming\ConnectFour\Application\Game\Query\Model\Game\Move;
1717
use Gaming\ConnectFour\Application\Game\Query\Model\GamesByPlayer\State;
18+
use Gaming\ConnectFour\Application\Game\Query\Model\OpenGames\OpenGame;
19+
use Gaming\ConnectFour\Application\Game\Query\OpenGamesQuery;
1820
use GamingPlatform\Api\ConnectFour\V1\ConnectFourV1;
1921
use GamingPlatform\Api\ConnectFour\V1\Game as ProtoV1Game;
2022
use GamingPlatform\Api\ConnectFour\V1\GetGamesByPlayer\State as ProtoV1State;
@@ -33,6 +35,7 @@ public function handle(Message $message, Context $context): void
3335
ConnectFourV1::OpenGameType => $this->handleOpenGame($message, $context),
3436
ConnectFourV1::JoinGameType => $this->handleJoinGame($message, $context),
3537
ConnectFourV1::MakeMoveType => $this->handleMakeMove($message, $context),
38+
ConnectFourV1::GetOpenGamesType => $this->handleGetOpenGames($message, $context),
3639
ConnectFourV1::GetGamesByPlayerType => $this->handleGetGamesByPlayer($message, $context),
3740
default => true
3841
};
@@ -99,6 +102,35 @@ private function handleMakeMove(Message $message, Context $context): void
99102
);
100103
}
101104

105+
private function handleGetOpenGames(Message $message, Context $context): void
106+
{
107+
$request = ConnectFourV1::createGetOpenGames($message->body());
108+
109+
$response = $this->queryBus->handle(
110+
new OpenGamesQuery(
111+
$request->getLimit()
112+
)
113+
);
114+
115+
$context->reply(
116+
new Message(
117+
ConnectFourV1::GetOpenGamesResponseType,
118+
ConnectFourV1::createGetOpenGamesResponse()
119+
->setGames(
120+
array_map(
121+
static fn(OpenGame $game) => ConnectFourV1::createGetOpenGamesResponse_Game()
122+
->setGameId($game->gameId)
123+
->setWidth($game->width)
124+
->setHeight($game->height)
125+
->setPlayerId($game->playerId),
126+
$response->games()
127+
)
128+
)
129+
->serializeToString()
130+
)
131+
);
132+
}
133+
102134
private function handleGetGamesByPlayer(Message $message, Context $context): void
103135
{
104136
$request = ConnectFourV1::createGetGamesByPlayer($message->body());

src/ConnectFour/Port/Adapter/Persistence/Projection/OpenGamesProjection.php

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,22 +27,17 @@ public function handle(DomainEvent $domainEvent): void
2727
$content = $domainEvent->content;
2828

2929
match ($content::class) {
30-
GameOpened::class => $this->saveGame($content->aggregateId(), $content->playerId()),
30+
GameOpened::class => $this->openGameStore->save(
31+
new OpenGame(
32+
$content->aggregateId(),
33+
$content->width(),
34+
$content->height(),
35+
$content->playerId()
36+
)
37+
),
3138
GameAborted::class,
32-
PlayerJoined::class => $this->removeGame($content->aggregateId()),
39+
PlayerJoined::class => $this->openGameStore->remove($content->aggregateId()),
3340
default => true
3441
};
3542
}
36-
37-
private function saveGame(string $gameId, string $playerId): void
38-
{
39-
$this->openGameStore->save(
40-
new OpenGame($gameId, $playerId)
41-
);
42-
}
43-
44-
private function removeGame(string $gameId): void
45-
{
46-
$this->openGameStore->remove($gameId);
47-
}
4843
}

0 commit comments

Comments
 (0)