2020-05-17 22:30:22 +02:00
|
|
|
<?php namespace MapGuesser\Controller;
|
|
|
|
|
2021-03-19 23:13:35 +01:00
|
|
|
use DateTime;
|
|
|
|
use Faker\Factory;
|
2020-06-09 00:56:00 +02:00
|
|
|
use MapGuesser\Interfaces\Request\IRequest;
|
2020-05-31 20:43:14 +02:00
|
|
|
use MapGuesser\Response\HtmlContent;
|
|
|
|
use MapGuesser\Response\JsonContent;
|
|
|
|
use MapGuesser\Interfaces\Response\IContent;
|
2021-03-19 23:13:35 +01:00
|
|
|
use MapGuesser\Interfaces\Response\IRedirect;
|
|
|
|
use MapGuesser\Multi\MultiConnector;
|
|
|
|
use MapGuesser\PersistentData\Model\MultiRoom;
|
|
|
|
use MapGuesser\PersistentData\PersistentDataManager;
|
2020-06-06 01:11:45 +02:00
|
|
|
use MapGuesser\Repository\MapRepository;
|
2021-03-19 23:13:35 +01:00
|
|
|
use MapGuesser\Repository\MultiRoomRepository;
|
|
|
|
use MapGuesser\Response\Redirect;
|
2020-05-17 22:30:22 +02:00
|
|
|
|
2020-05-31 20:43:14 +02:00
|
|
|
class GameController
|
2020-05-17 22:30:22 +02:00
|
|
|
{
|
2020-06-09 00:56:00 +02:00
|
|
|
private IRequest $request;
|
|
|
|
|
2021-03-19 23:13:35 +01:00
|
|
|
private PersistentDataManager $pdm;
|
|
|
|
|
|
|
|
private MultiConnector $multiConnector;
|
|
|
|
|
|
|
|
private MultiRoomRepository $multiRoomRepository;
|
|
|
|
|
2020-06-06 01:11:45 +02:00
|
|
|
private MapRepository $mapRepository;
|
|
|
|
|
2020-06-09 00:56:00 +02:00
|
|
|
public function __construct(IRequest $request)
|
2020-05-31 20:43:14 +02:00
|
|
|
{
|
2020-06-09 00:56:00 +02:00
|
|
|
$this->request = $request;
|
2021-03-19 23:13:35 +01:00
|
|
|
$this->pdm = new PersistentDataManager();
|
|
|
|
$this->multiConnector = new MultiConnector();
|
|
|
|
$this->multiRoomRepository = new MultiRoomRepository();
|
2020-06-06 01:11:45 +02:00
|
|
|
$this->mapRepository = new MapRepository();
|
2020-06-09 00:56:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getGame(): IContent
|
|
|
|
{
|
|
|
|
$mapId = (int) $this->request->query('mapId');
|
2020-07-05 00:58:03 +02:00
|
|
|
|
2021-03-19 23:13:35 +01:00
|
|
|
return new HtmlContent('game', ['mapId' => $mapId]);
|
2020-05-31 20:43:14 +02:00
|
|
|
}
|
2020-05-25 19:14:49 +02:00
|
|
|
|
2021-03-19 23:13:35 +01:00
|
|
|
public function getNewMultiGame(): IRedirect
|
2020-05-25 19:14:49 +02:00
|
|
|
{
|
2020-06-09 00:56:00 +02:00
|
|
|
$mapId = (int) $this->request->query('mapId');
|
2021-03-19 23:13:35 +01:00
|
|
|
$map = $this->mapRepository->getById($mapId);
|
|
|
|
$roomId = bin2hex(random_bytes(3));
|
|
|
|
$token = $this->getMultiToken($roomId);
|
|
|
|
|
|
|
|
$room = new MultiRoom();
|
|
|
|
$room->setRoomId($roomId);
|
|
|
|
$room->setStateArray([
|
|
|
|
'mapId' => $mapId,
|
|
|
|
'area' => $map->getArea(),
|
|
|
|
'rounds' => [],
|
|
|
|
'currentRound' => -1
|
|
|
|
]);
|
|
|
|
$room->setMembersArray(['owner' => $token, 'all' => []]);
|
|
|
|
$room->setUpdatedDate(new DateTime());
|
2020-07-05 00:58:03 +02:00
|
|
|
|
2021-03-19 23:13:35 +01:00
|
|
|
$this->pdm->saveToDb($room);
|
|
|
|
|
|
|
|
$this->multiConnector->sendMessage('create_room', ['roomId' => $roomId]);
|
|
|
|
|
|
|
|
return new Redirect(
|
|
|
|
\Container::$routeCollection
|
|
|
|
->getRoute('multiGame')
|
|
|
|
->generateLink(['roomId' => $roomId]),
|
|
|
|
IRedirect::TEMPORARY
|
|
|
|
);
|
2020-05-25 19:14:49 +02:00
|
|
|
}
|
|
|
|
|
2021-03-20 20:58:57 +01:00
|
|
|
public function getMultiGame(): IContent
|
2020-05-17 22:30:22 +02:00
|
|
|
{
|
2021-03-19 23:13:35 +01:00
|
|
|
$roomId = $this->request->query('roomId');
|
|
|
|
|
|
|
|
return new HtmlContent('game', ['roomId' => $roomId]);
|
|
|
|
}
|
|
|
|
|
2021-03-20 20:58:57 +01:00
|
|
|
public function prepareGame(): IContent
|
2021-03-19 23:13:35 +01:00
|
|
|
{
|
|
|
|
$mapId = (int) $this->request->query('mapId');
|
2020-06-06 01:11:45 +02:00
|
|
|
$map = $this->mapRepository->getById($mapId);
|
2020-06-09 00:56:00 +02:00
|
|
|
$session = $this->request->session();
|
|
|
|
|
2020-06-10 20:34:29 +02:00
|
|
|
if (!($state = $session->get('state')) || $state['mapId'] !== $mapId) {
|
2020-06-09 00:56:00 +02:00
|
|
|
$session->set('state', [
|
2020-05-31 20:43:14 +02:00
|
|
|
'mapId' => $mapId,
|
2020-06-19 18:52:07 +02:00
|
|
|
'area' => $map->getArea(),
|
2021-03-15 13:58:44 +01:00
|
|
|
'rounds' => [],
|
|
|
|
'currentRound' => -1
|
2020-06-09 00:56:00 +02:00
|
|
|
]);
|
2020-05-25 19:14:49 +02:00
|
|
|
}
|
2021-04-27 16:46:40 +02:00
|
|
|
// update the area of the map in the session in any case
|
|
|
|
else {
|
|
|
|
$state['area'] = $map->getArea();
|
|
|
|
$session->set('state', $state);
|
|
|
|
}
|
2020-05-17 23:35:10 +02:00
|
|
|
|
2021-03-19 23:13:35 +01:00
|
|
|
return new JsonContent([
|
|
|
|
'mapId' => $mapId,
|
|
|
|
'mapName' => $map->getName(),
|
|
|
|
'bounds' => $map->getBounds()->toArray()
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function prepareMultiGame(): IContent
|
|
|
|
{
|
|
|
|
$roomId = $this->request->query('roomId');
|
|
|
|
$userName = $this->request->post('userName');
|
|
|
|
if (empty($userName)) {
|
|
|
|
$faker = Factory::create();
|
|
|
|
$userName = $faker->userName;
|
|
|
|
}
|
|
|
|
|
|
|
|
$room = $this->multiRoomRepository->getByRoomId($roomId);
|
|
|
|
$state = $room->getStateArray();
|
|
|
|
$map = $this->mapRepository->getById($state['mapId']);
|
|
|
|
$token = $this->getMultiToken($roomId);
|
|
|
|
|
|
|
|
$members = $room->getMembersArray();
|
|
|
|
|
|
|
|
if (!in_array($token, $members['all'])) {
|
|
|
|
if ($state['currentRound'] >= 0) {
|
|
|
|
return new JsonContent(['error' => 'game_already_started']);
|
|
|
|
}
|
|
|
|
|
|
|
|
$members['all'][] = $token;
|
|
|
|
}
|
|
|
|
|
|
|
|
$room->setMembersArray($members);
|
|
|
|
$room->setUpdatedDate(new DateTime());
|
|
|
|
|
|
|
|
$this->pdm->saveToDb($room);
|
|
|
|
|
|
|
|
$this->multiConnector->sendMessage('join_room', [
|
|
|
|
'roomId' => $roomId,
|
|
|
|
'token' => $token,
|
|
|
|
'userName' => $userName
|
|
|
|
]);
|
|
|
|
|
|
|
|
return new JsonContent([
|
|
|
|
'roomId' => $roomId,
|
|
|
|
'token' => $token,
|
|
|
|
'owner' => $members['owner'] == $token,
|
|
|
|
'mapId' => $state['mapId'],
|
|
|
|
'mapName' => $map->getName(),
|
|
|
|
'bounds' => $map->getBounds()->toArray()
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2021-03-20 20:58:57 +01:00
|
|
|
private function getMultiToken(string $roomId): string
|
2021-03-19 23:13:35 +01:00
|
|
|
{
|
|
|
|
$session = $this->request->session();
|
|
|
|
|
|
|
|
if (!($multiState = $session->get('multiState')) || $multiState['roomId'] !== $roomId) {
|
|
|
|
$token = bin2hex(random_bytes(16));
|
|
|
|
|
|
|
|
$session->set('multiState', [
|
|
|
|
'roomId' => $roomId,
|
|
|
|
'token' => $token
|
|
|
|
]);
|
|
|
|
} else {
|
|
|
|
$token = $multiState['token'];
|
|
|
|
}
|
|
|
|
|
|
|
|
return $token;
|
2020-05-17 22:30:22 +02:00
|
|
|
}
|
|
|
|
}
|