<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use App\Entity\Game;
class KoehandelController extends AbstractController
{
/**
* @Route("/", methods={"GET"}, name="index")
*/
public function index() {
return $this->render('koehandel/index.html.twig');
}
/**
* Login a user by name.
*
* @Route("/login", methods={"POST"}, name="login")
*/
public function login(Request $request) {
$data = json_decode($request->getContent(), true);
$userName = isset($data['userName']) ? $data['userName'] : false;
$result = in_array(strtolower($userName), $this->users());
return $this->json([
'result' => $result,
'userName' => $userName
]);
}
/**
* Returns the gamestate.
*
* @Route("/gamestate", methods={"GET"}, name="gamestate")
*/
public function gamestate(Request $request) {
// We currently only support 1 game, with id 1
// Find its database record
$game = $this->getDoctrine()
->getRepository(Game::class)
->find(1);
// If there is no database record yet, create it
if (!$game) {
$game = new Game();
$game->setTurn('jantien');
$history = [];
array_push($history, ['user' => 'menno', 'move' => '-']);
array_push($history, ['user' => 'silvan', 'move' => '-']);
array_push($history, ['user' => 'jantien', 'move' => '-']);
$game->setHistory(json_encode($history));
$entityManager = $this->getDoctrine()->getManager();
$entityManager->persist($game);
$entityManager->flush();
}
// Return gamestate
$gamestate = [
'turn' => $game->getTurn(),
'history' => json_decode($game->getHistory(), true)
];
return $this->json([
'gamestate' => $gamestate
]);
}
/**
* A user submits a move.
*
* @Route("/move", methods={"POST"}, name="move")
*/
public function move(Request $request) {
try {
// Get input params
$data = json_decode($request->getContent(), true);
$userName = isset($data['userName']) ? strtolower($data['userName']) : false;
$move = isset($data['move']) ? $data['move'] : false;
// Validate input params
if (!$this->validUser($userName)) {
throw new \Exception('Invalid user');
}
if (!$this->validMove($move)) {
throw new \Exception('Invalid move');
}
// Get current game data from database
$game = $this->getDoctrine()
->getRepository(Game::class)
->find(1);
// Verify that it is this user's turn
if($game->getTurn() != $userName) {
throw new \Exception("Je bent niet aan de beurt.");
}
// Handle the move
// Remove the last entry from the game history and
// insert this move as the first entry
$history = json_decode($game->getHistory(), true);
array_pop($history);
array_unshift($history, ['user' => $userName, 'move' => $move]);
// Determine wich user has the next turn
$turn = $this->nextTurn($game->getTurn());
$game->setTurn($turn);
// Store game data to the database
$game->setHistory(json_encode($history));
$entityManager = $this->getDoctrine()->getManager();
$entityManager->persist($game);
$entityManager->flush();
// Return success
return $this->json([
'result' => true
]);
} catch (\Exception $e) {
// Oops, something is wrong.
return $this->json([
'result' => false,
'message' => $e->getMessage()
]);
}
}
/**
* Return the users.
*/
protected function users() {
return ['jantien', 'silvan', 'menno'];
}
/**
* Return the moves.
*/
protected function moves() {
return ['veilen', 'koehandel'];
}
/**
* Return whether a user is valid.
*/
protected function validUser($user) {
return in_array(strtolower($user), $this->users());
}
/**
* Return whether a move is valid.
*/
protected function validMove($move) {
return in_array($move, $this->moves());
}
/**
* Given a player whose turn it is, who is the next player?
*/
protected function nextTurn($turn) {
switch ($turn) {
case 'jantien':
return 'silvan';
break;
case 'silvan':
return 'menno';
break;
case 'menno':
return 'jantien';
break;
}
}
}