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;
|
2021-05-10 19:10:09 +02:00
|
|
|
use MapGuesser\Interfaces\Authorization\ISecured;
|
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;
|
2021-05-12 13:51:06 +02:00
|
|
|
use MapGuesser\PersistentData\Model\Challenge;
|
2021-03-19 23:13:35 +01:00
|
|
|
use MapGuesser\PersistentData\Model\MultiRoom;
|
2021-05-12 13:51:06 +02:00
|
|
|
use MapGuesser\PersistentData\Model\PlaceInChallenge;
|
|
|
|
use MapGuesser\PersistentData\Model\UserInChallenge;
|
2021-03-19 23:13:35 +01:00
|
|
|
use MapGuesser\PersistentData\PersistentDataManager;
|
2021-05-12 13:51:06 +02:00
|
|
|
use MapGuesser\Repository\ChallengeRepository;
|
2020-06-06 01:11:45 +02:00
|
|
|
use MapGuesser\Repository\MapRepository;
|
2021-03-19 23:13:35 +01:00
|
|
|
use MapGuesser\Repository\MultiRoomRepository;
|
2021-05-12 13:51:06 +02:00
|
|
|
use MapGuesser\Repository\PlaceRepository;
|
|
|
|
use MapGuesser\Repository\UserInChallengeRepository;
|
2021-03-19 23:13:35 +01:00
|
|
|
use MapGuesser\Response\Redirect;
|
2020-05-17 22:30:22 +02:00
|
|
|
|
2021-05-10 19:10:09 +02:00
|
|
|
class GameController implements ISecured
|
2020-05-17 22:30:22 +02:00
|
|
|
{
|
2021-05-12 13:51:06 +02:00
|
|
|
const NUMBER_OF_ROUNDS = 5;
|
|
|
|
|
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;
|
|
|
|
|
2021-05-12 13:51:06 +02:00
|
|
|
private PlaceRepository $placeRepository;
|
|
|
|
|
|
|
|
private ChallengeRepository $challengeRepository;
|
|
|
|
|
|
|
|
private UserInChallengeRepository $userInChallengeRepository;
|
|
|
|
|
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();
|
2021-05-12 13:51:06 +02:00
|
|
|
$this->placeRepository = new PlaceRepository();
|
|
|
|
$this->challengeRepository = new ChallengeRepository();
|
|
|
|
$this->userInChallengeRepository = new UserInChallengeRepository();
|
2020-06-09 00:56:00 +02:00
|
|
|
}
|
|
|
|
|
2021-05-10 19:10:09 +02:00
|
|
|
public function authorize(): bool
|
|
|
|
{
|
|
|
|
return !empty($_ENV['ENABLE_GAME_FOR_GUESTS']) || $this->request->user() !== null;
|
|
|
|
}
|
|
|
|
|
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-05-12 13:51:06 +02:00
|
|
|
public function getChallenge(): IContent
|
2021-05-18 19:55:05 +02:00
|
|
|
{
|
2021-05-12 13:51:06 +02:00
|
|
|
$challengeToken = $this->request->query('challengeToken');
|
|
|
|
|
|
|
|
return new HtmlContent('game', ['challengeToken' => $challengeToken]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function createNewChallenge(): IContent
|
|
|
|
{
|
|
|
|
// create Challenge
|
|
|
|
$challengeToken = rand();
|
2021-05-20 08:31:52 +02:00
|
|
|
while ($this->challengeRepository->getByToken($challengeToken)) {
|
|
|
|
// if a challenge with the same token already exists
|
|
|
|
$challengeToken = rand();
|
|
|
|
}
|
2021-05-12 13:51:06 +02:00
|
|
|
|
|
|
|
$challenge = new Challenge();
|
|
|
|
$challenge->setToken($challengeToken);
|
|
|
|
$challenge->setCreatedDate(new DateTime());
|
|
|
|
|
2021-05-19 17:55:04 +02:00
|
|
|
if ($this->request->post('timerEnabled') !== null && $this->request->post('timeLimit') !== null) {
|
2021-05-16 13:14:00 +02:00
|
|
|
$challenge->setTimeLimit($this->request->post('timeLimit'));
|
2021-05-12 13:51:06 +02:00
|
|
|
}
|
2021-05-19 17:55:04 +02:00
|
|
|
if ($this->request->post('timeLimitType') !== null) {
|
|
|
|
$challenge->setTimeLimitType($this->request->post('timeLimitType'));
|
|
|
|
}
|
|
|
|
if ($this->request->post('noMove') !== null) {
|
2021-05-12 13:51:06 +02:00
|
|
|
$challenge->setNoMove(true);
|
|
|
|
}
|
2021-05-19 17:55:04 +02:00
|
|
|
if ($this->request->post('noPan') !== null) {
|
2021-05-12 13:51:06 +02:00
|
|
|
$challenge->setNoPan(true);
|
|
|
|
}
|
2021-05-19 17:55:04 +02:00
|
|
|
if ($this->request->post('noZoom') !== null) {
|
2021-05-12 13:51:06 +02:00
|
|
|
$challenge->setNoZoom(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->pdm->saveToDb($challenge);
|
|
|
|
|
|
|
|
// save owner/creator
|
|
|
|
|
|
|
|
$session = $this->request->session();
|
|
|
|
$userId = $session->get('userId');
|
|
|
|
|
|
|
|
$userInChallenge = new UserInChallenge();
|
|
|
|
$userInChallenge->setUserId($userId);
|
|
|
|
$userInChallenge->setChallenge($challenge);
|
2021-05-16 13:14:00 +02:00
|
|
|
$userInChallenge->setTimeLeft($challenge->getTimeLimit());
|
2021-05-12 13:51:06 +02:00
|
|
|
$userInChallenge->setIsOwner(true);
|
|
|
|
|
|
|
|
$this->pdm->saveToDb($userInChallenge);
|
|
|
|
|
|
|
|
// select places
|
|
|
|
|
|
|
|
$mapId = (int) $this->request->post('mapId');
|
2021-05-15 11:32:41 +02:00
|
|
|
// $map = $this->mapRepository->getById($mapId);
|
2021-05-12 13:51:06 +02:00
|
|
|
|
|
|
|
$places = $this->placeRepository->getRandomNPlaces($mapId, static::NUMBER_OF_ROUNDS, $userId);
|
|
|
|
|
2021-05-16 13:14:00 +02:00
|
|
|
$round = 0;
|
2021-05-12 13:51:06 +02:00
|
|
|
foreach ($places as $place) {
|
|
|
|
$placeInChallenge = new PlaceInChallenge();
|
|
|
|
$placeInChallenge->setPlace($place);
|
|
|
|
$placeInChallenge->setChallenge($challenge);
|
2021-05-16 13:14:00 +02:00
|
|
|
$placeInChallenge->setRound($round++);
|
2021-05-12 13:51:06 +02:00
|
|
|
$this->pdm->saveToDb($placeInChallenge);
|
|
|
|
}
|
|
|
|
|
|
|
|
return new JsonContent(['challengeToken' => dechex($challengeToken)]);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
]);
|
2021-04-27 17:54:47 +02:00
|
|
|
} else { // update the area of the map in the session in any case
|
2021-04-27 16:46:40 +02:00
|
|
|
$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);
|
2021-05-08 18:03:17 +02:00
|
|
|
|
2021-05-22 21:17:49 +02:00
|
|
|
if (!isset($room)) {
|
2021-05-08 18:03:17 +02:00
|
|
|
return new JsonContent(['error' => 'game_not_found']);
|
|
|
|
}
|
|
|
|
|
2021-03-19 23:13:35 +01:00
|
|
|
$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-05-12 13:51:06 +02:00
|
|
|
public function prepareChallenge(): IContent
|
|
|
|
{
|
|
|
|
$challengeToken_str = $this->request->query('challengeToken');
|
|
|
|
$session = $this->request->session();
|
|
|
|
$userId = $session->get('userId');
|
2021-05-18 19:55:05 +02:00
|
|
|
|
|
|
|
if (!isset($userId))
|
|
|
|
{
|
|
|
|
return new JsonContent(['error' => 'anonymous_user']);
|
|
|
|
}
|
|
|
|
|
2021-05-12 13:51:06 +02:00
|
|
|
$challenge = $this->challengeRepository->getByTokenStr($challengeToken_str);
|
|
|
|
|
2021-05-18 19:55:05 +02:00
|
|
|
if (!isset($challenge))
|
|
|
|
{
|
|
|
|
return new JsonContent(['error' => 'game_not_found']);
|
|
|
|
}
|
|
|
|
|
2021-05-22 21:17:49 +02:00
|
|
|
if (!$this->userInChallengeRepository->isUserParticipatingInChallenge($userId, $challenge)) {
|
2021-05-15 11:32:41 +02:00
|
|
|
// new player is joining
|
2021-05-12 13:51:06 +02:00
|
|
|
$userInChallenge = new UserInChallenge();
|
|
|
|
$userInChallenge->setUserId($userId);
|
2021-05-12 21:01:26 +02:00
|
|
|
$userInChallenge->setChallenge($challenge);
|
2021-05-16 13:14:00 +02:00
|
|
|
$userInChallenge->setTimeLeft($challenge->getTimeLimit());
|
2021-05-12 21:01:26 +02:00
|
|
|
$this->pdm->saveToDb($userInChallenge);
|
2021-05-12 13:51:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$map = $this->mapRepository->getByChallenge($challenge);
|
|
|
|
|
|
|
|
return new JsonContent([
|
|
|
|
'mapId' => $map->getId(),
|
|
|
|
'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
|
|
|
}
|
|
|
|
}
|