-
Notifications
You must be signed in to change notification settings - Fork 0
[RFR] Setup the multiplayer game #15
base: master
Are you sure you want to change the base?
Conversation
Luwangel
commented
Nov 15, 2017
•
edited
Loading
edited
- Create, join and play multiplayer games
- See the list of open multiplayer games
[RFR] Expose an api to play the 15 puzzle game
app/src/Controller/ApiController.php
Outdated
|
||
public function new(Request $request) { | ||
$mode = $request->get('mode'); | ||
$apiResponse = $this->api->new(self::DEFAULT_SIZE, $mode && $mode == 'multi'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Prefer strict equality check ===
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
$mode is not used elsewhere. You can directly do $request->get('mode') === 'multi'
app/src/Controller/ApiController.php
Outdated
$game = $context->getGame(); | ||
$player = $context->getPlayer(); | ||
|
||
if ($context->getIsPlayer() && $game->getWinner() == null) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same for ===
public static function isPlayer(Request $request, Game $game, string $token) : bool { | ||
$player1 = $game->getPlayer1(); | ||
$player2 = $game->getPlayer2(); | ||
return $player1->getToken() == $token || !$game->isFull() && $player2->getToken() == $token; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same for === (checks type too)
} | ||
|
||
public static function getPlayer(Request $request, Game $game, string $token) { | ||
if ($game->getIsMultiplayer() && $game->getPlayer2() && $game->getPlayer2()->getToken() == $token) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
===
app/src/Controller/ApiController.php
Outdated
public function new(Request $request) { | ||
$mode = $request->get('mode'); | ||
$apiResponse = $this->api->new(self::DEFAULT_SIZE, $mode && $mode == 'multi'); | ||
$this->playerRepository->save($apiResponse['player']); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As already said at presentation, you use our repositories in a "active record" way.
You don't benefit from Doctrine ORM DataMapper that let you save all our "persisted" entities in the same time.
You do:
->persist(player)
->flush() => Save to database
->persist(game)
->flush() => Save to database
You must preferably do:
->persist(player)
->persist(game)
->flush() => Save to database in only one transaction
app/src/Controller/ApiController.php
Outdated
public function games() { | ||
$gameIds = $this->gameRepository->findOpenMultiplayerGames(); | ||
|
||
$response = new JsonResponse(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can put data in JsonResponse constructor => 1 line instead of 5
return new JsonResponse(['game_ids' => $gameIds]);
app/src/Controller/ApiController.php
Outdated
$this->gameRepository->save($apiResponse['game']); | ||
} | ||
|
||
$response = new JsonResponse(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can put data in JsonResponse constructor
return new JsonResponse([......]);
public function move(GameContext $context, int $tile) { | ||
$game = $context->getGame(); | ||
$player = $context->getPlayer(); | ||
if ($context->getIsPlayer() && $game->getWinner() == null) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
===
app/src/Controller/ApiController.php
Outdated
$this->playerRepository->save($apiResponse['player']); | ||
$this->gameRepository->save($apiResponse['game']); | ||
|
||
$response = new JsonResponse(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can put data in JsonResponse constructor
return new JsonResponse([......]);
app/src/Controller/ApiController.php
Outdated
$game->setPlayer2($newPlayer); | ||
$this->gameRepository->save($game); | ||
|
||
$response = new JsonResponse(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can put data in JsonResponse constructor
return new JsonResponse([......]);
$currentPlayer = $context->getPlayer(); | ||
$player = $currentPlayer ?: $game->getPlayer1(); | ||
|
||
if ($game->getIsMultiplayer()) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can simplify this part of code, only otherPlayer changes
$game = $context->getGame(); | ||
$currentPlayer = $context->getPlayer(); | ||
$player = $currentPlayer ?: $game->getPlayer1(); | ||
if ($game->getIsMultiplayer()) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can simplify that part of code, only otherPlayer changes
@@ -4,15 +4,21 @@ | |||
|
|||
use Symfony\Bundle\FrameworkBundle\Controller\Controller; | |||
use Symfony\Component\HttpFoundation\Response; | |||
use App\Repository\GameRepository; | |||
|
|||
class MainController extends Controller { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As you extends Symfony Controller (https://api.symfony.com/2.7/Symfony/Bundle/FrameworkBundle/Controller/Controller.html)
You can benefit from ControllerTrait wich is used by it (https://github.com/symfony/symfony/blob/master/src/Symfony/Bundle/FrameworkBundle/Controller/ControllerTrait.php)
You can do the following.
- return $this->renderView(...) instead of return new Response($this->twig->render...)
- return $this->json(...) instead of return new JsonResponse($this->serializer->serialize....)
- ...
When you extends Controller, it's useless to inject services into the constructor because the Symfony Controller already has all the container (which is heavy).
So either you extends controller (and use Trait or $this->container->...), or you don't extends it an inject services in your constructor.