Skip to content
This repository has been archived by the owner on Apr 18, 2019. It is now read-only.

[RFR] Setup the multiplayer game #15

Open
wants to merge 19 commits into
base: master
Choose a base branch
from
Open

[RFR] Setup the multiplayer game #15

wants to merge 19 commits into from

Conversation

Luwangel
Copy link
Contributor

@Luwangel Luwangel commented Nov 15, 2017

  • Create, join and play multiplayer games
  • See the list of open multiplayer games

@Luwangel
Copy link
Contributor Author

capture du 2017-11-15 14-31-48
capture du 2017-11-15 14-32-18
capture du 2017-11-15 14-33-15

@Luwangel Luwangel changed the title [WIP] Setup the multiplayer game [RFR] Setup the multiplayer game Nov 16, 2017

public function new(Request $request) {
$mode = $request->get('mode');
$apiResponse = $this->api->new(self::DEFAULT_SIZE, $mode && $mode == 'multi');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefer strict equality check ===

Copy link
Contributor

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'

$game = $context->getGame();
$player = $context->getPlayer();

if ($context->getIsPlayer() && $game->getWinner() == null) {
Copy link
Contributor

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;
Copy link
Contributor

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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

===

public function new(Request $request) {
$mode = $request->get('mode');
$apiResponse = $this->api->new(self::DEFAULT_SIZE, $mode && $mode == 'multi');
$this->playerRepository->save($apiResponse['player']);
Copy link
Contributor

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

public function games() {
$gameIds = $this->gameRepository->findOpenMultiplayerGames();

$response = new JsonResponse();
Copy link
Contributor

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]);

$this->gameRepository->save($apiResponse['game']);
}

$response = new JsonResponse();
Copy link
Contributor

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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

===

$this->playerRepository->save($apiResponse['player']);
$this->gameRepository->save($apiResponse['game']);

$response = new JsonResponse();
Copy link
Contributor

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([......]);

$game->setPlayer2($newPlayer);
$this->gameRepository->save($game);

$response = new JsonResponse();
Copy link
Contributor

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()) {
Copy link
Contributor

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()) {
Copy link
Contributor

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 {
Copy link
Contributor

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.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants