Skip to content

Commit

Permalink
Allow players to choose game configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
marein committed Dec 7, 2024
1 parent 0f961ac commit 1742016
Show file tree
Hide file tree
Showing 26 changed files with 343 additions and 199 deletions.
8 changes: 8 additions & 0 deletions assets/css/app.css
Original file line number Diff line number Diff line change
Expand Up @@ -172,3 +172,11 @@ notification-list {
[data-bs-theme=dark] .gp-game__field {
border-color: var(--tblr-blue);
}

/** Fix specificity for checked buttons on hover */
.btn-check:checked + .btn:hover {
color: var(--tblr-btn-active-color);
background-color: var(--tblr-btn-active-bg);
border-color: var(--tblr-btn-active-border-color);
box-shadow: var(--tblr-btn-active-shadow)
}
63 changes: 0 additions & 63 deletions assets/js/ConnectFour/OpenButton.js

This file was deleted.

1 change: 0 additions & 1 deletion config/connect-four/importmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
'connect-four-running-games' => ['path' => 'js/ConnectFour/RunningGames.js'],
'connect-four-game-list' => ['path' => 'js/ConnectFour/GameList.js'],
'connect-four-game' => ['path' => 'js/ConnectFour/Game.js'],
'connect-four-open-button' => ['path' => 'js/ConnectFour/OpenButton.js'],
'connect-four-abort-button' => ['path' => 'js/ConnectFour/AbortButton.js'],
'connect-four-resign-button' => ['path' => 'js/ConnectFour/ResignButton.js']
];
2 changes: 1 addition & 1 deletion config/web-interface/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ framework:
csrf_protection: false # CSRF protection is handled by marein/symfony-standard-headers-csrf-bundle.

twig:
form_themes: ['bootstrap_5_layout.html.twig']
form_themes: ['@web-interface/layout/forms.html.twig']
paths: { '%kernel.project_dir%/src/WebInterface/Presentation/Http/View': web-interface }

security:
Expand Down
12 changes: 11 additions & 1 deletion config/web-interface/routing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ game:
methods: [GET]
controller: web-interface.page-controller::gameAction

challenge:
path: /challenge/{id}
methods: [GET]
controller: web-interface.page-controller::challengeAction

signup:
path: /signup
methods: [GET, POST]
Expand Down Expand Up @@ -73,7 +78,6 @@ open:
path: /api/connect-four/games/open
methods: [POST]
controller: web-interface.connect-four-controller::openAction
defaults: { _format: json }

abort:
path: /api/connect-four/games/{gameId}/abort
Expand All @@ -98,3 +102,9 @@ move:
methods: [POST]
controller: web-interface.connect-four-controller::moveAction
defaults: { _format: json }

challenge_abort:
path: /api/connect-four/challenges/{gameId}/abort
methods: [POST]
controller: web-interface.connect-four-controller::abortChallengeAction
defaults: { _format: json }
8 changes: 5 additions & 3 deletions config/web-interface/services/controller.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
services:
web-interface.page-controller:
class: Gaming\WebInterface\Presentation\Http\PageController
arguments: ['@twig', '@connect-four.query-bus', '@web-interface.security']
tags: ['controller.service_arguments']
arguments: ['@connect-four.query-bus', '@web-interface.security']
calls: [[setContainer, ['@Psr\Container\ContainerInterface']]]
tags: ['controller.service_arguments', 'container.service_subscriber']

web-interface.signup-controller:
class: Gaming\WebInterface\Presentation\Http\SignupController
Expand Down Expand Up @@ -34,4 +35,5 @@ services:
web-interface.connect-four-controller:
class: Gaming\WebInterface\Presentation\Http\ConnectFourController
arguments: ['@connect-four.command-bus', '@web-interface.security']
tags: ['controller.service_arguments']
calls: [[setContainer, ['@Psr\Container\ContainerInterface']]]
tags: ['controller.service_arguments', 'container.service_subscriber']
10 changes: 4 additions & 6 deletions src/ConnectFour/Application/Game/Command/OpenCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,10 @@
final class OpenCommand implements Request
{
public function __construct(
private readonly string $playerId
public readonly string $playerId,
public readonly int $width,
public readonly int $height,
public readonly int $stone
) {
}

public function playerId(): string
{
return $this->playerId;
}
}
11 changes: 9 additions & 2 deletions src/ConnectFour/Application/Game/Command/OpenHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@

namespace Gaming\ConnectFour\Application\Game\Command;

use Gaming\ConnectFour\Domain\Game\Board\Size;
use Gaming\ConnectFour\Domain\Game\Board\Stone;
use Gaming\ConnectFour\Domain\Game\Configuration;
use Gaming\ConnectFour\Domain\Game\Game;
use Gaming\ConnectFour\Domain\Game\Games;
use Gaming\ConnectFour\Domain\Game\WinningRule\WinningRules;

final class OpenHandler
{
Expand All @@ -21,8 +24,12 @@ public function __invoke(OpenCommand $command): string
{
$game = Game::open(
$this->games->nextIdentity(),
Configuration::common(),
$command->playerId()
new Configuration(
new Size($command->width, $command->height),
WinningRules::standard(),
Stone::tryFrom($command->stone)
),
$command->playerId
);

$this->games->add($game);
Expand Down
23 changes: 10 additions & 13 deletions src/ConnectFour/Domain/Game/Board/Size.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,29 @@

final class Size
{
private int $width;

private int $height;

/**
* @throws InvalidSizeException
*/
public function __construct(int $width, int $height)
{
public function __construct(
public readonly int $width,
public readonly int $height
) {
if ($width < 2 || $height < 2) {
throw new InvalidSizeException('Width and height must be greater then 1.');
}

if (($width * $height) % 2 !== 0) {
throw new InvalidSizeException('Product of width and height must be an even number.');
}

$this->height = $height;
$this->width = $width;
}

/**
* @deprecated Use property instead.
*/
public function width(): int
{
return $this->width;
}

/**
* @deprecated Use property instead.
*/
public function height(): int
{
return $this->height;
Expand Down
5 changes: 5 additions & 0 deletions src/ConnectFour/Domain/Game/Board/Stone.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,9 @@ enum Stone: int
case None = 0;
case Red = 1;
case Yellow = 2;

public static function random(): self
{
return self::from(rand(1, 2));
}
}
42 changes: 28 additions & 14 deletions src/ConnectFour/Domain/Game/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,28 @@
namespace Gaming\ConnectFour\Domain\Game;

use Gaming\ConnectFour\Domain\Game\Board\Size;
use Gaming\ConnectFour\Domain\Game\Board\Stone;
use Gaming\ConnectFour\Domain\Game\Exception\PlayersNotUniqueException;
use Gaming\ConnectFour\Domain\Game\WinningRule\WinningRules;

final class Configuration
{
private Size $size;

private WinningRules $winningRules;

private function __construct(Size $size, WinningRules $winningRules)
{
$this->size = $size;
$this->winningRules = $winningRules;
public function __construct(
private readonly Size $size,
private readonly WinningRules $winningRules,
private readonly ?Stone $preferredStone = null
) {
}

public static function common(): Configuration
{
return new self(
new Size(7, 6),
WinningRules::standard()
WinningRules::standard(),
Stone::Red // Should be null, but is Red for keeping unit tests green.
);
}

public static function custom(Size $size, WinningRules $winningRules): Configuration
{
return new self($size, $winningRules);
}

public function size(): Size
{
return $this->size;
Expand All @@ -41,4 +36,23 @@ public function winningRules(): WinningRules
{
return $this->winningRules;
}

/**
* @throws PlayersNotUniqueException
*/
public function createPlayers(string $playerId, string $joinedPlayerId): Players
{
$players = match ($this->preferredStone ?? Stone::random()) {
Stone::Red => [
new Player($playerId, Stone::Red),
new Player($joinedPlayerId, Stone::Yellow)
],
default => [
new Player($joinedPlayerId, Stone::Red),
new Player($playerId, Stone::Yellow)
]
};

return new Players(...$players);
}
}
14 changes: 5 additions & 9 deletions src/ConnectFour/Domain/Game/Event/GameAborted.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,17 @@

use Gaming\Common\Domain\DomainEvent;
use Gaming\ConnectFour\Domain\Game\GameId;
use Gaming\ConnectFour\Domain\Game\Player;

final class GameAborted implements DomainEvent
{
private string $gameId;

private string $abortedPlayerId;

private string $opponentPlayerId;

public function __construct(GameId $gameId, Player $abortedPlayer, ?Player $opponentPlayer = null)
{
public function __construct(
GameId $gameId,
private readonly string $abortedPlayerId,
private readonly string $opponentPlayerId = ''
) {
$this->gameId = $gameId->toString();
$this->abortedPlayerId = $abortedPlayer->id();
$this->opponentPlayerId = $opponentPlayer ? $opponentPlayer->id() : '';
}

public function aggregateId(): string
Expand Down
6 changes: 1 addition & 5 deletions src/ConnectFour/Domain/Game/Event/GameOpened.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use Gaming\Common\Domain\DomainEvent;
use Gaming\ConnectFour\Domain\Game\Board\Size;
use Gaming\ConnectFour\Domain\Game\GameId;
use Gaming\ConnectFour\Domain\Game\Player;

final class GameOpened implements DomainEvent
{
Expand All @@ -17,14 +16,11 @@ final class GameOpened implements DomainEvent

private int $height;

private string $playerId;

public function __construct(GameId $gameId, Size $size, Player $player)
public function __construct(GameId $gameId, Size $size, private readonly string $playerId)
{
$this->gameId = $gameId->toString();
$this->width = $size->width();
$this->height = $size->height();
$this->playerId = $player->id();
}

public function aggregateId(): string
Expand Down
14 changes: 5 additions & 9 deletions src/ConnectFour/Domain/Game/Event/PlayerJoined.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,17 @@

use Gaming\Common\Domain\DomainEvent;
use Gaming\ConnectFour\Domain\Game\GameId;
use Gaming\ConnectFour\Domain\Game\Player;

final class PlayerJoined implements DomainEvent
{
private string $gameId;

private string $joinedPlayerId;

private string $opponentPlayerId;

public function __construct(GameId $gameId, Player $joinedPlayer, Player $opponentPlayer)
{
public function __construct(
GameId $gameId,
private readonly string $joinedPlayerId,
private readonly string $opponentPlayerId
) {
$this->gameId = $gameId->toString();
$this->joinedPlayerId = $joinedPlayer->id();
$this->opponentPlayerId = $opponentPlayer->id();
}

public function aggregateId(): string
Expand Down
Loading

0 comments on commit 1742016

Please sign in to comment.