Skip to content

Commit

Permalink
#15 Moves to win validation is ready, now move to #14 - store in DB
Browse files Browse the repository at this point in the history
  • Loading branch information
janis-rullis committed Oct 18, 2020
1 parent b425ea9 commit a034269
Show file tree
Hide file tree
Showing 5 changed files with 289 additions and 269 deletions.
25 changes: 25 additions & 0 deletions symfony5/src/Controller/GameController.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,29 @@ public function setBoardDimensions(Request $request, GameCreatorService $gameCre
return $this->json(['errors' => [$e->getMessage()]], Response::HTTP_BAD_REQUEST);
}
}

/**
* Set game rules like how many moves are required to win.
*
* @Route("/game/{gameId}rules", name="setRules", methods={"PUT"})
* @SWG\Tag(name="1. game")
*
* @SWG\Parameter(name="body", in="body", required=true, @SWG\Schema(required={"moves_to_win"}, type="object", ref=@Model(type=Game::class, groups={"PUB"})))
* @SWG\Response(response=200, description="Saved.", @SWG\Schema(type="object", ref=@Model(type=Game::class, groups={"PUB"})))
* @SWG\Response(response=404, description="Not found.", @SWG\Schema(type="object", ref=@Model(type=Game::class, groups={"PUB"})))
*/
public function setRules(Request $request, GameCreatorService $gameCreatorService, int $gameId): JsonResponse
{
try {
$resp = $gameCreatorService->setRules($gameId, json_decode($request->getContent(), true));

return $this->json($resp, Response::HTTP_OK);
} catch (\Exception $e) {
if (method_exists($e, 'getErrors')) {
return $this->json(['errors' => $e->getErrors()], Response::HTTP_BAD_REQUEST);
}

return $this->json(['errors' => [$e->getMessage()]], Response::HTTP_BAD_REQUEST);
}
}
}
248 changes: 124 additions & 124 deletions symfony5/src/Entity/Game.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace App\Entity;

use App\Exception\GameValidatorException;
Expand All @@ -9,128 +11,126 @@

class Game
{

// #12 Limits.
// #12 TODO: Maybe replace this hard-code with reading from DB? In case if def can be changed.
const MIN_HEIGHT_WIDTH = 2;
const MAX_HEIGHT_WIDTH = 20;
// #12 Error messages.
const ERROR_HEIGHT_WIDTH_INVALID = '#12 Width and height must be an integer from 2 to 20.';
const ERROR_HEIGHT_WIDTH_INVALID_CODE = 100;
const ERROR_WIDTH_ALREADY_SET = '#12 Can not change the width for a game that has already started.';
const ERROR_WIDTH_ALREADY_SET_CODE = 101;
const ERROR_MOVE_CNT_TO_WIN_INVALID = '#15 Move count to win must be an integer not smaller than 2 and not bigger than the height or width.';
const ERROR_MOVE_CNT_TO_WIN_INVALID_CODE = 102;
// #12 Field names.
const WIDTH = 'width';
const HEIGHT = 'height';
const HEIGHT_WIDTH = 'height_width';
const MOVE_CNT_TO_WIN = 'move_cnt_to_win';

/**
* @ORM\Column(type="integer")
* @SWG\Property(property="width", type="integer", example=3)
* @Groups({"CREATE", "PUB", "ID_ERROR"})
*/
private int $width;

/**
* @ORM\Column(type="integer")
* @SWG\Property(property="height", type="integer", example=3)
* @Groups({"CREATE", "PUB", "ID_ERROR"})
*/
private int $height;

/**
* @ORM\Column(type="integer", nullable=true)
* @SWG\Property(property="move_cnt_to_win", type="integer", example=3)
* @Groups({"CREATE", "PUB", "ID_ERROR"})
*/
private ?int $moveCntToWin = null;

/**
* #12 Make sure that the width is correct.
*
* @return \self
*
* @throws GameValidatorException
*/
public function setWidth(int $width): self
{
if ($width < self::MIN_HEIGHT_WIDTH || $width > self::MAX_HEIGHT_WIDTH) {
throw new GameValidatorException([self::WIDTH => self::ERROR_HEIGHT_WIDTH_INVALID], self::ERROR_HEIGHT_WIDTH_INVALID_CODE);
}

$this->width = $width;

return $this;
}

public function getWidth(): ?int
{
return $this->width;
}

/**
* #12 Make sure that the range is correct.
*
* @return \self
*
* @throws GameValidatorException
*/
public function setHeight(int $height): self
{
if ($height < self::MIN_HEIGHT_WIDTH || $height > self::MAX_HEIGHT_WIDTH) {
throw new GameValidatorException([self::HEIGHT => self::ERROR_HEIGHT_WIDTH_INVALID], self::ERROR_HEIGHT_WIDTH_INVALID_CODE);
}

$this->height = $height;

return $this;
}

public function getHeight(): ?int
{
return $this->height;
}

/**
* #15 Set how many moves are required to win.
* Board dimensions are required to be set first.
*
* @return \self
*/
public function setMoveCntToWin(int $moveCntToWin): self
{
// #15 Move count to win must be no smaller than the min board dimensions or go outside the board.
if ($moveCntToWin < $this->getMinDimension() || $moveCntToWin > $this->getMaxDimension()) {
throw new GameValidatorException([self::MOVE_CNT_TO_WIN => self::ERROR_MOVE_CNT_TO_WIN_INVALID], self::ERROR_MOVE_CNT_TO_WIN_INVALID_CODE);
}

$this->moveCntToWin = $moveCntToWin;
return $this;
}

/**
* #15 Get the smallest dimension - width or height.
* @return int
*/
public function getMinDimension(): int
{
return $this->getHeight() >= $this->getWidth() ? $this->getWidth() : $this->getHeight();
}

/**
* #15 Get the biggest dimension - width or height.
* @return int
*/
public function getMaxDimension(): int
{
return $this->getHeight() >= $this->getWidth() ? $this->getHeight() : $this->getWidth();
}

public function getMoveCntToWin(): ?int
{
return $this->moveCntToWin;
}
// #12 Limits.
// #12 TODO: Maybe replace this hard-code with reading from DB? In case if def can be changed.
const MIN_HEIGHT_WIDTH = 2;
const MAX_HEIGHT_WIDTH = 20;
// #12 Error messages.
const ERROR_HEIGHT_WIDTH_INVALID = '#12 Width and height must be an integer from 2 to 20.';
const ERROR_HEIGHT_WIDTH_INVALID_CODE = 100;
const ERROR_WIDTH_ALREADY_SET = '#12 Can not change the width for a game that has already started.';
const ERROR_WIDTH_ALREADY_SET_CODE = 101;
const ERROR_MOVE_CNT_TO_WIN_INVALID = '#15 Move count to win must be an integer not smaller than 2 and not bigger than the height or width.';
const ERROR_MOVE_CNT_TO_WIN_INVALID_CODE = 102;
// #12 Field names.
const WIDTH = 'width';
const HEIGHT = 'height';
const HEIGHT_WIDTH = 'height_width';
const MOVE_CNT_TO_WIN = 'move_cnt_to_win';

/**
* @ORM\Column(type="integer")
* @SWG\Property(property="width", type="integer", example=3)
* @Groups({"CREATE", "PUB", "ID_ERROR"})
*/
private int $width;

/**
* @ORM\Column(type="integer")
* @SWG\Property(property="height", type="integer", example=3)
* @Groups({"CREATE", "PUB", "ID_ERROR"})
*/
private int $height;

/**
* @ORM\Column(type="integer", nullable=true)
* @SWG\Property(property="move_cnt_to_win", type="integer", example=3)
* @Groups({"CREATE", "PUB", "ID_ERROR"})
*/
private ?int $moveCntToWin = null;

/**
* #12 Make sure that the width is correct.
*
* @return \self
*
* @throws GameValidatorException
*/
public function setWidth(int $width): self
{
if ($width < self::MIN_HEIGHT_WIDTH || $width > self::MAX_HEIGHT_WIDTH) {
throw new GameValidatorException([self::WIDTH => self::ERROR_HEIGHT_WIDTH_INVALID], self::ERROR_HEIGHT_WIDTH_INVALID_CODE);
}

$this->width = $width;

return $this;
}

public function getWidth(): ?int
{
return $this->width;
}

/**
* #12 Make sure that the range is correct.
*
* @return \self
*
* @throws GameValidatorException
*/
public function setHeight(int $height): self
{
if ($height < self::MIN_HEIGHT_WIDTH || $height > self::MAX_HEIGHT_WIDTH) {
throw new GameValidatorException([self::HEIGHT => self::ERROR_HEIGHT_WIDTH_INVALID], self::ERROR_HEIGHT_WIDTH_INVALID_CODE);
}

$this->height = $height;

return $this;
}

public function getHeight(): ?int
{
return $this->height;
}

/**
* #15 Set how many moves are required to win.
* Board dimensions are required to be set first.
*
* @return \self
*/
public function setMoveCntToWin(int $moveCntToWin): self
{
// #15 Move count to win must be no smaller than the min board dimensions or go outside the board.
if ($moveCntToWin < $this->getMinDimension() || $moveCntToWin > $this->getMaxDimension()) {
throw new GameValidatorException([self::MOVE_CNT_TO_WIN => self::ERROR_MOVE_CNT_TO_WIN_INVALID], self::ERROR_MOVE_CNT_TO_WIN_INVALID_CODE);
}

$this->moveCntToWin = $moveCntToWin;

return $this;
}

/**
* #15 Get the smallest dimension - width or height.
*/
public function getMinDimension(): int
{
return $this->getHeight() >= $this->getWidth() ? $this->getWidth() : $this->getHeight();
}

/**
* #15 Get the biggest dimension - width or height.
*/
public function getMaxDimension(): int
{
return $this->getHeight() >= $this->getWidth() ? $this->getHeight() : $this->getWidth();
}

public function getMoveCntToWin(): ?int
{
return $this->moveCntToWin;
}
}
5 changes: 4 additions & 1 deletion symfony5/src/Service/GameCreatorService.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,11 @@ public function setBoardDimensions(?array $request): Game
*
* @throws \App\Exception\GameValidatorException
*/
public function setRules(Game $game, ?array $request): Game
public function setRules(int $gameId, ?array $request): Game
{
// #15 TODO: #14 Collect the game created previously so could read it's dimensions.
exit;

if (!isset($request[Game::MOVE_CNT_TO_WIN])) {
throw new \App\Exception\GameValidatorException([Game::MOVE_CNT_TO_WIN => Game::ERROR_MOVE_CNT_TO_WIN_INVALID], Game::ERROR_MOVE_CNT_TO_WIN_INVALID_CODE);
}
Expand Down
Loading

0 comments on commit a034269

Please sign in to comment.