2020-05-25 19:46:31 +02:00
|
|
|
<?php namespace MapGuesser\Controller;
|
|
|
|
|
2021-03-19 23:14:01 +01:00
|
|
|
use DateTime;
|
2023-04-16 17:14:27 +02:00
|
|
|
use SokoWeb\Interfaces\Authentication\IAuthenticationRequired;
|
2020-05-25 19:46:31 +02:00
|
|
|
use MapGuesser\Util\Geo\Position;
|
2023-04-07 20:28:14 +02:00
|
|
|
use SokoWeb\Response\JsonContent;
|
|
|
|
use SokoWeb\Interfaces\Response\IContent;
|
2021-03-19 23:14:01 +01:00
|
|
|
use MapGuesser\Multi\MultiConnector;
|
2021-05-19 13:06:46 +02:00
|
|
|
use MapGuesser\PersistentData\Model\Challenge;
|
2021-05-12 21:01:26 +02:00
|
|
|
use MapGuesser\PersistentData\Model\Guess;
|
2021-05-19 13:06:46 +02:00
|
|
|
use MapGuesser\PersistentData\Model\Map;
|
|
|
|
use MapGuesser\PersistentData\Model\Place;
|
|
|
|
use MapGuesser\PersistentData\Model\PlaceInChallenge;
|
|
|
|
use MapGuesser\PersistentData\Model\User;
|
2021-05-05 19:19:38 +02:00
|
|
|
use MapGuesser\PersistentData\Model\UserPlayedPlace;
|
2021-05-12 21:01:26 +02:00
|
|
|
use MapGuesser\Repository\GuessRepository;
|
2021-03-19 23:14:01 +01:00
|
|
|
use MapGuesser\Repository\MultiRoomRepository;
|
2021-05-12 21:01:26 +02:00
|
|
|
use MapGuesser\Repository\PlaceInChallengeRepository;
|
2020-06-02 01:16:59 +02:00
|
|
|
use MapGuesser\Repository\PlaceRepository;
|
2021-05-12 15:34:15 +02:00
|
|
|
use MapGuesser\Repository\UserInChallengeRepository;
|
2021-05-05 19:19:38 +02:00
|
|
|
use MapGuesser\Repository\UserPlayedPlaceRepository;
|
2020-05-25 19:46:31 +02:00
|
|
|
|
2023-04-16 17:14:27 +02:00
|
|
|
class GameFlowController implements IAuthenticationRequired
|
2020-05-25 19:46:31 +02:00
|
|
|
{
|
|
|
|
const NUMBER_OF_ROUNDS = 5;
|
|
|
|
const MAX_SCORE = 1000;
|
|
|
|
|
2021-03-19 23:14:01 +01:00
|
|
|
private MultiConnector $multiConnector;
|
|
|
|
|
|
|
|
private MultiRoomRepository $multiRoomRepository;
|
|
|
|
|
2020-06-02 01:16:59 +02:00
|
|
|
private PlaceRepository $placeRepository;
|
|
|
|
|
2021-05-05 19:19:38 +02:00
|
|
|
private UserPlayedPlaceRepository $userPlayedPlaceRepository;
|
|
|
|
|
2021-05-12 15:34:15 +02:00
|
|
|
private UserInChallengeRepository $userInChallengeRepository;
|
|
|
|
|
2021-05-12 21:01:26 +02:00
|
|
|
private PlaceInChallengeRepository $placeInChallengeRepository;
|
|
|
|
|
|
|
|
private GuessRepository $guessRepository;
|
|
|
|
|
2023-04-20 00:19:54 +02:00
|
|
|
public function __construct()
|
2020-06-02 01:16:59 +02:00
|
|
|
{
|
2021-03-19 23:14:01 +01:00
|
|
|
$this->multiConnector = new MultiConnector();
|
|
|
|
$this->multiRoomRepository = new MultiRoomRepository();
|
2020-06-02 01:16:59 +02:00
|
|
|
$this->placeRepository = new PlaceRepository();
|
2021-05-05 19:19:38 +02:00
|
|
|
$this->userPlayedPlaceRepository = new UserPlayedPlaceRepository();
|
2021-05-12 15:34:15 +02:00
|
|
|
$this->userInChallengeRepository = new UserInChallengeRepository();
|
2021-05-12 21:01:26 +02:00
|
|
|
$this->placeInChallengeRepository = new PlaceInChallengeRepository();
|
|
|
|
$this->guessRepository = new GuessRepository();
|
2020-06-02 01:16:59 +02:00
|
|
|
}
|
|
|
|
|
2023-04-16 17:14:27 +02:00
|
|
|
public function isAuthenticationRequired(): bool
|
2021-05-10 19:10:09 +02:00
|
|
|
{
|
2023-04-16 17:14:27 +02:00
|
|
|
return empty($_ENV['ENABLE_GAME_FOR_GUESTS']);
|
2021-05-10 19:10:09 +02:00
|
|
|
}
|
|
|
|
|
2021-03-19 23:14:01 +01:00
|
|
|
public function initialData(): IContent
|
2020-05-30 15:33:28 +02:00
|
|
|
{
|
2023-04-20 00:19:54 +02:00
|
|
|
$mapId = (int) \Container::$request->query('mapId');
|
|
|
|
$session = \Container::$request->session();
|
2020-05-25 19:46:31 +02:00
|
|
|
|
2020-06-09 00:56:00 +02:00
|
|
|
if (!($state = $session->get('state')) || $state['mapId'] !== $mapId) {
|
2020-07-05 00:58:03 +02:00
|
|
|
return new JsonContent(['error' => 'no_session_found']);
|
2020-05-25 19:46:31 +02:00
|
|
|
}
|
|
|
|
|
2021-03-15 13:58:44 +01:00
|
|
|
if (!isset($state['currentRound']) || $state['currentRound'] == -1 || $state['currentRound'] >= static::NUMBER_OF_ROUNDS) {
|
2021-05-06 17:12:18 +02:00
|
|
|
$this->startNewGame($state, $mapId);
|
2021-03-19 23:14:01 +01:00
|
|
|
$session->set('state', $state);
|
2021-03-15 13:58:44 +01:00
|
|
|
}
|
2020-06-20 00:03:49 +02:00
|
|
|
|
2021-03-15 13:58:44 +01:00
|
|
|
$response = [];
|
2020-05-25 19:46:31 +02:00
|
|
|
|
2021-03-15 13:58:44 +01:00
|
|
|
$last = $state['rounds'][$state['currentRound']];
|
|
|
|
$response['place'] = [
|
|
|
|
'panoId' => $last['panoId'],
|
|
|
|
'pov' => $last['pov']->toArray()
|
|
|
|
];
|
|
|
|
|
|
|
|
$response['history'] = [];
|
|
|
|
for ($i = 0; $i < $state['currentRound']; ++$i) {
|
|
|
|
$round = $state['rounds'][$i];
|
|
|
|
$response['history'][] = [
|
|
|
|
'position' => $round['position']->toArray(),
|
2021-04-03 13:38:16 +02:00
|
|
|
'result' => [
|
|
|
|
'guessPosition' => $round['guessPosition']->toArray(),
|
|
|
|
'distance' => $round['distance'],
|
|
|
|
'score' => $round['score']
|
|
|
|
]
|
2020-05-25 19:46:31 +02:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2021-03-15 13:58:44 +01:00
|
|
|
return new JsonContent($response);
|
2020-05-31 20:43:14 +02:00
|
|
|
}
|
|
|
|
|
2021-03-19 23:14:01 +01:00
|
|
|
public function multiInitialData(): IContent
|
|
|
|
{
|
2023-04-20 00:19:54 +02:00
|
|
|
$roomId = \Container::$request->query('roomId');
|
|
|
|
$session = \Container::$request->session();
|
2021-03-19 23:14:01 +01:00
|
|
|
|
|
|
|
if (!($multiState = $session->get('multiState')) || $multiState['roomId'] !== $roomId) {
|
|
|
|
return new JsonContent(['error' => 'no_session_found']);
|
|
|
|
}
|
|
|
|
|
|
|
|
$room = $this->multiRoomRepository->getByRoomId($roomId);
|
|
|
|
$state = $room->getStateArray();
|
|
|
|
$members = $room->getMembersArray();
|
|
|
|
|
|
|
|
if ($members['owner'] !== $multiState['token']) {
|
|
|
|
return new JsonContent(['error' => 'not_owner_of_room']);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($state['currentRound'] == -1 || $state['currentRound'] >= static::NUMBER_OF_ROUNDS - 1) {
|
|
|
|
$this->startNewGame($state, $state['mapId']);
|
|
|
|
$room->setStateArray($state);
|
|
|
|
$room->setUpdatedDate(new DateTime());
|
2023-04-20 00:19:54 +02:00
|
|
|
\Container::$persistentDataManager->saveToDb($room);
|
2021-03-19 23:14:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$places = [];
|
|
|
|
foreach ($state['rounds'] as $round) {
|
|
|
|
$places[] = [
|
|
|
|
'position' => $round['position']->toArray(),
|
|
|
|
'panoId' => $round['panoId'],
|
|
|
|
'pov' => $round['pov']->toArray()
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->multiConnector->sendMessage('start_game', ['roomId' => $roomId, 'places' => $places]);
|
|
|
|
|
|
|
|
return new JsonContent(['ok' => true]);
|
|
|
|
}
|
|
|
|
|
2021-05-17 21:29:55 +02:00
|
|
|
private function prepareChallengeResponse(int $userId, Challenge $challenge, int $currentRound, bool $withHistory = false): array
|
2021-05-12 15:34:15 +02:00
|
|
|
{
|
2021-05-17 21:29:55 +02:00
|
|
|
$currentPlace = $this->placeRepository->getByRoundInChallenge($challenge, $currentRound);
|
2023-04-07 21:10:32 +02:00
|
|
|
|
2021-05-21 15:41:17 +02:00
|
|
|
// if the last round was played ($currentPlace == null) or history is explicitly requested (for initializing)
|
2021-05-17 21:29:55 +02:00
|
|
|
if (!isset($currentPlace) || $withHistory) {
|
2021-05-20 20:55:44 +02:00
|
|
|
|
2023-04-30 21:01:15 +02:00
|
|
|
$withRelations = ['user', 'place_in_challange', 'place'];
|
2023-04-07 21:10:32 +02:00
|
|
|
foreach ($this->guessRepository->getAllInChallenge($challenge, $withRelations) as $guess) {
|
2021-05-17 21:29:55 +02:00
|
|
|
$round = $guess->getPlaceInChallenge()->getRound();
|
2023-04-07 21:10:32 +02:00
|
|
|
|
2021-05-17 21:29:55 +02:00
|
|
|
if ($guess->getUser()->getId() === $userId) {
|
2023-04-07 21:10:32 +02:00
|
|
|
$response['history'][$round]['position'] =
|
2021-05-17 21:29:55 +02:00
|
|
|
$guess->getPlaceInChallenge()->getPlace()->getPosition()->toArray();
|
|
|
|
$response['history'][$round]['result'] = [
|
|
|
|
'guessPosition' => $guess->getPosition()->toArray(),
|
|
|
|
'distance' => $guess->getDistance(),
|
|
|
|
'score' => $guess->getScore()
|
|
|
|
];
|
|
|
|
} else {
|
|
|
|
$response['history'][$round]['allResults'][] = [
|
|
|
|
'userName' => $guess->getUser()->getDisplayName(),
|
2021-05-13 19:48:25 +02:00
|
|
|
'guessPosition' => $guess->getPosition()->toArray(),
|
|
|
|
'distance' => $guess->getDistance(),
|
|
|
|
'score' => $guess->getScore()
|
2021-05-17 21:29:55 +02:00
|
|
|
];
|
|
|
|
}
|
2021-05-13 19:48:25 +02:00
|
|
|
}
|
2021-05-20 20:55:44 +02:00
|
|
|
|
|
|
|
// setting default values for rounds without guesses (because of timeout)
|
|
|
|
for ($i = 0; $i < $currentRound; ++$i) {
|
|
|
|
if (!isset($response['history'][$i]) || !isset($response['history'][$i]['result'])) {
|
|
|
|
$response['history'][$i]['result'] = [
|
|
|
|
'guessPosition' => null,
|
|
|
|
'distance' => null,
|
|
|
|
'score' => 0
|
|
|
|
];
|
2023-04-07 21:10:32 +02:00
|
|
|
|
|
|
|
$response['history'][$i]['position'] =
|
2021-05-20 20:55:44 +02:00
|
|
|
$this->placeRepository->getByRoundInChallenge($challenge, $i)->getPosition()->toArray();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$response['history']['length'] = $currentRound;
|
2021-05-12 21:01:26 +02:00
|
|
|
}
|
2021-05-16 20:46:53 +02:00
|
|
|
|
2021-05-17 21:29:55 +02:00
|
|
|
if (!isset($currentPlace)) { // game finished
|
2021-05-12 21:01:26 +02:00
|
|
|
$response['finished'] = true;
|
|
|
|
} else { // continue game
|
|
|
|
$response['place'] = [
|
|
|
|
'panoId' => $currentPlace->getPanoIdCached(),
|
2021-05-17 21:29:55 +02:00
|
|
|
'pov' => $currentPlace->getPov()->toArray()
|
2021-05-12 21:01:26 +02:00
|
|
|
];
|
2021-05-17 21:29:55 +02:00
|
|
|
|
|
|
|
$prevRound = $currentRound - 1;
|
|
|
|
if ($prevRound >= 0) {
|
2023-04-30 21:01:15 +02:00
|
|
|
foreach ($this->guessRepository->getAllInChallengeByRound($prevRound, $challenge, ['user']) as $guess) {
|
2021-05-17 21:29:55 +02:00
|
|
|
if ($guess->getUser()->getId() != $userId) {
|
|
|
|
$response['allResults'][] = [
|
|
|
|
'userName' => $guess->getUser()->getDisplayName(),
|
|
|
|
'guessPosition' => $guess->getPosition()->toArray(),
|
|
|
|
'distance' => $guess->getDistance(),
|
|
|
|
'score' => $guess->getScore()
|
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-05-12 21:01:26 +02:00
|
|
|
}
|
2021-05-12 15:34:15 +02:00
|
|
|
|
2021-05-22 20:54:58 +02:00
|
|
|
$response['restrictions'] = [
|
|
|
|
'timeLimit' => $challenge->getTimeLimit() * 1000,
|
|
|
|
'timeLimitType' => $challenge->getTimeLimitType(),
|
|
|
|
'noMove' => $challenge->getNoMove(),
|
|
|
|
'noPan' => $challenge->getNoPan(),
|
|
|
|
'noZoom' => $challenge->getNoZoom()
|
|
|
|
];
|
|
|
|
|
2021-05-16 20:46:53 +02:00
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function challengeInitialData(): IContent
|
|
|
|
{
|
2023-04-20 00:19:54 +02:00
|
|
|
$session = \Container::$request->session();
|
2021-05-16 20:46:53 +02:00
|
|
|
$userId = $session->get('userId');
|
2023-04-20 00:19:54 +02:00
|
|
|
$challengeToken_str = \Container::$request->query('challengeToken');
|
2023-04-30 21:01:15 +02:00
|
|
|
$userInChallenge = $this->userInChallengeRepository->getByUserIdAndToken($userId, $challengeToken_str, ['challenge']);
|
2021-05-16 20:46:53 +02:00
|
|
|
|
|
|
|
if (!isset($userInChallenge)) {
|
|
|
|
return new JsonContent(['error' => 'game_not_found']);
|
|
|
|
}
|
|
|
|
|
|
|
|
$challenge = $userInChallenge->getChallenge();
|
|
|
|
$currentRound = $userInChallenge->getCurrentRound();
|
|
|
|
|
2021-05-17 21:29:55 +02:00
|
|
|
$response = $this->prepareChallengeResponse($userId, $challenge, $currentRound, true);
|
2021-05-16 20:46:53 +02:00
|
|
|
|
2021-05-21 15:41:17 +02:00
|
|
|
if ($challenge->getTimeLimitType() === 'game' && $challenge->getTimeLimit() !== null && $userInChallenge->getCurrentRound() > 0) {
|
2021-05-21 11:30:30 +02:00
|
|
|
$timeLimit = max(10, $userInChallenge->getTimeLeft());
|
|
|
|
$response['restrictions']['timeLimit'] = $timeLimit * 1000;
|
2023-04-07 21:10:32 +02:00
|
|
|
}
|
2021-05-21 11:30:30 +02:00
|
|
|
|
2021-05-12 15:34:15 +02:00
|
|
|
return new JsonContent($response);
|
|
|
|
}
|
|
|
|
|
2021-03-19 23:14:01 +01:00
|
|
|
public function guess(): IContent
|
2020-05-31 20:43:14 +02:00
|
|
|
{
|
2023-04-20 00:19:54 +02:00
|
|
|
$mapId = (int) \Container::$request->query('mapId');
|
|
|
|
$session = \Container::$request->session();
|
2020-05-31 20:43:14 +02:00
|
|
|
|
2020-06-09 00:56:00 +02:00
|
|
|
if (!($state = $session->get('state')) || $state['mapId'] !== $mapId) {
|
2020-07-05 00:58:03 +02:00
|
|
|
return new JsonContent(['error' => 'no_session_found']);
|
2020-05-31 20:43:14 +02:00
|
|
|
}
|
|
|
|
|
2021-03-15 13:58:44 +01:00
|
|
|
$last = $state['rounds'][$state['currentRound']];
|
2023-04-20 00:19:54 +02:00
|
|
|
$guessPosition = new Position((float) \Container::$request->post('lat'), (float) \Container::$request->post('lng'));
|
2021-05-12 21:01:26 +02:00
|
|
|
$result = $this->evaluateGuess($last['position'], $guessPosition, $state['area']);
|
2020-05-31 20:43:14 +02:00
|
|
|
|
2020-06-09 00:56:00 +02:00
|
|
|
$last['guessPosition'] = $guessPosition;
|
2021-03-19 23:14:01 +01:00
|
|
|
$last['distance'] = $result['distance'];
|
|
|
|
$last['score'] = $result['score'];
|
2020-05-31 20:43:14 +02:00
|
|
|
|
2021-03-15 12:28:06 +01:00
|
|
|
$response = [
|
2021-04-03 13:38:16 +02:00
|
|
|
'position' => $last['position']->toArray(),
|
|
|
|
'result' => $result
|
2021-03-15 12:28:06 +01:00
|
|
|
];
|
2020-05-31 20:43:14 +02:00
|
|
|
|
2021-03-19 23:14:01 +01:00
|
|
|
$state['rounds'][$state['currentRound']] = $last;
|
|
|
|
$state['currentRound'] += 1;
|
2021-03-15 13:58:44 +01:00
|
|
|
if ($state['currentRound'] < static::NUMBER_OF_ROUNDS) {
|
|
|
|
$next = $state['rounds'][$state['currentRound']];
|
2020-06-20 00:03:49 +02:00
|
|
|
|
2021-03-15 13:58:44 +01:00
|
|
|
$response['place'] = [
|
|
|
|
'panoId' => $next['panoId'],
|
|
|
|
'pov' => $next['pov']->toArray()
|
2021-03-15 12:28:06 +01:00
|
|
|
];
|
2020-05-31 20:43:14 +02:00
|
|
|
}
|
|
|
|
|
2020-06-20 00:03:49 +02:00
|
|
|
$session->set('state', $state);
|
|
|
|
|
2021-05-12 21:01:26 +02:00
|
|
|
$this->saveVisit($last['placeId']);
|
2021-05-06 10:05:23 +02:00
|
|
|
|
2021-05-06 10:09:28 +02:00
|
|
|
return new JsonContent($response);
|
2021-05-06 10:05:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// save the selected place for the round in UserPlayedPlace
|
2021-05-12 21:01:26 +02:00
|
|
|
private function saveVisit($placeId): void
|
2021-05-06 10:05:23 +02:00
|
|
|
{
|
2023-04-20 00:19:54 +02:00
|
|
|
$session = \Container::$request->session();
|
2021-05-05 19:19:38 +02:00
|
|
|
$userId = $session->get('userId');
|
2021-05-06 17:12:18 +02:00
|
|
|
|
2021-05-22 21:17:49 +02:00
|
|
|
if (isset($userId)) {
|
2021-05-05 19:19:38 +02:00
|
|
|
$userPlayedPlace = $this->userPlayedPlaceRepository->getByUserIdAndPlaceId($userId, $placeId);
|
2021-05-22 21:17:49 +02:00
|
|
|
if (!$userPlayedPlace) {
|
2021-05-05 19:19:38 +02:00
|
|
|
$userPlayedPlace = new UserPlayedPlace();
|
|
|
|
$userPlayedPlace->setUserId($userId);
|
|
|
|
$userPlayedPlace->setPlaceId($placeId);
|
|
|
|
} else {
|
|
|
|
$userPlayedPlace->incrementOccurrences();
|
|
|
|
}
|
|
|
|
$userPlayedPlace->setLastTimeDate(new DateTime());
|
2023-04-20 00:19:54 +02:00
|
|
|
\Container::$persistentDataManager->saveToDb($userPlayedPlace);
|
2021-05-05 19:19:38 +02:00
|
|
|
}
|
2020-05-25 19:46:31 +02:00
|
|
|
}
|
|
|
|
|
2021-03-19 23:14:01 +01:00
|
|
|
public function multiGuess(): IContent
|
|
|
|
{
|
2023-04-20 00:19:54 +02:00
|
|
|
$roomId = \Container::$request->query('roomId');
|
|
|
|
$session = \Container::$request->session();
|
2021-03-19 23:14:01 +01:00
|
|
|
|
|
|
|
if (!($multiState = $session->get('multiState')) || $multiState['roomId'] !== $roomId) {
|
|
|
|
return new JsonContent(['error' => 'no_session_found']);
|
|
|
|
}
|
|
|
|
|
|
|
|
$room = $this->multiRoomRepository->getByRoomId($roomId);
|
|
|
|
$state = $room->getStateArray();
|
|
|
|
|
|
|
|
$last = $state['rounds'][$state['currentRound']];
|
2023-04-20 00:19:54 +02:00
|
|
|
$guessPosition = new Position((float) \Container::$request->post('lat'), (float) \Container::$request->post('lng'));
|
2021-05-12 21:01:26 +02:00
|
|
|
$result = $this->evaluateGuess($last['position'], $guessPosition, $state['area']);
|
2021-03-19 23:14:01 +01:00
|
|
|
|
2021-04-04 23:18:26 +02:00
|
|
|
$responseFromMulti = $this->multiConnector->sendMessage('guess', [
|
2021-03-19 23:14:01 +01:00
|
|
|
'roomId' => $roomId,
|
|
|
|
'token' => $multiState['token'],
|
2021-04-03 13:38:16 +02:00
|
|
|
'guessPosition' => $guessPosition->toArray(),
|
2021-03-19 23:14:01 +01:00
|
|
|
'distance' => $result['distance'],
|
|
|
|
'score' => $result['score']
|
|
|
|
]);
|
|
|
|
|
2021-04-04 23:18:26 +02:00
|
|
|
if (isset($responseFromMulti['error'])) {
|
|
|
|
return new JsonContent(['error' => $responseFromMulti['error']]);
|
|
|
|
}
|
|
|
|
|
2021-03-19 23:14:01 +01:00
|
|
|
$response = [
|
2021-04-03 13:38:16 +02:00
|
|
|
'position' => $last['position']->toArray(),
|
|
|
|
'result' => $result,
|
2021-04-04 23:18:26 +02:00
|
|
|
'allResults' => $responseFromMulti['allResults']
|
2021-03-19 23:14:01 +01:00
|
|
|
];
|
|
|
|
|
|
|
|
return new JsonContent($response);
|
|
|
|
}
|
|
|
|
|
2021-05-12 21:01:26 +02:00
|
|
|
public function challengeGuess(): IContent
|
|
|
|
{
|
2023-04-20 00:19:54 +02:00
|
|
|
$session = \Container::$request->session();
|
2021-05-12 21:01:26 +02:00
|
|
|
$userId = $session->get('userId');
|
2023-04-20 00:19:54 +02:00
|
|
|
$challengeToken_str = \Container::$request->query('challengeToken');
|
2023-04-30 21:01:15 +02:00
|
|
|
$userInChallenge = $this->userInChallengeRepository->getByUserIdAndToken($userId, $challengeToken_str, ['challenge']);
|
2021-05-12 21:01:26 +02:00
|
|
|
|
2021-05-15 11:32:41 +02:00
|
|
|
if (!isset($userInChallenge)) {
|
2021-05-12 21:01:26 +02:00
|
|
|
return new JsonContent(['error' => 'game_not_found']);
|
|
|
|
}
|
|
|
|
|
2021-05-15 11:32:41 +02:00
|
|
|
$challenge = $userInChallenge->getChallenge();
|
2021-05-16 13:14:00 +02:00
|
|
|
$currentRound = $userInChallenge->getCurrentRound();
|
2023-04-30 21:01:15 +02:00
|
|
|
$currentPlaceInChallenge = $this->placeInChallengeRepository->getByRoundInChallenge($currentRound, $challenge, ['place', 'map']);
|
2021-05-16 20:46:53 +02:00
|
|
|
$currentPlace = $currentPlaceInChallenge->getPlace();
|
|
|
|
$map = $currentPlace->getMap();
|
|
|
|
|
2021-05-20 20:55:44 +02:00
|
|
|
// creating response
|
|
|
|
$nextRound = $currentRound + 1;
|
|
|
|
$response = $this->prepareChallengeResponse($userId, $challenge, $nextRound);
|
|
|
|
$response['position'] = $currentPlace->getPosition()->toArray();
|
2021-05-13 13:06:24 +02:00
|
|
|
|
2023-04-20 00:19:54 +02:00
|
|
|
if (\Container::$request->post('lat') && \Container::$request->post('lng')) {
|
|
|
|
$guessPosition = new Position((float) \Container::$request->post('lat'), (float) \Container::$request->post('lng'));
|
2021-05-20 20:55:44 +02:00
|
|
|
$result = $this->evaluateGuess($currentPlace->getPosition(), $guessPosition, $map->getArea());
|
2021-05-12 21:01:26 +02:00
|
|
|
|
2021-05-20 20:55:44 +02:00
|
|
|
// save guess
|
|
|
|
$guess = new Guess();
|
|
|
|
$guess->setUserId($userId);
|
|
|
|
$guess->setPlaceInChallenge($currentPlaceInChallenge);
|
|
|
|
$guess->setPosition($guessPosition);
|
|
|
|
$guess->setDistance($result['distance']);
|
|
|
|
$guess->setScore($result['score']);
|
2023-04-20 00:19:54 +02:00
|
|
|
\Container::$persistentDataManager->saveToDb($guess);
|
2021-05-12 21:01:26 +02:00
|
|
|
|
2021-05-20 20:55:44 +02:00
|
|
|
$response['result'] = $result;
|
2021-05-12 21:01:26 +02:00
|
|
|
|
2021-05-20 20:55:44 +02:00
|
|
|
} else {
|
|
|
|
// user didn't manage to guess in the round in the given timeframe
|
|
|
|
$response['result'] = ['distance' => null, 'score' => 0];
|
|
|
|
}
|
2021-05-12 21:01:26 +02:00
|
|
|
|
2021-05-20 21:32:01 +02:00
|
|
|
// save user relevant state of challenge
|
2021-05-16 13:14:00 +02:00
|
|
|
$userInChallenge->setCurrentRound($nextRound);
|
2023-04-20 00:19:54 +02:00
|
|
|
$timeLeft = \Container::$request->post('timeLeft');
|
2021-05-21 14:45:40 +02:00
|
|
|
if (isset($timeLeft)) {
|
|
|
|
$userInChallenge->setTimeLeft(intval($timeLeft));
|
2021-05-20 21:32:01 +02:00
|
|
|
}
|
2023-04-20 00:19:54 +02:00
|
|
|
\Container::$persistentDataManager->saveToDb($userInChallenge);
|
2021-05-12 21:01:26 +02:00
|
|
|
|
2021-05-21 14:45:40 +02:00
|
|
|
if ($challenge->getTimeLimitType() === 'game' && isset($timeLeft)) {
|
|
|
|
$timeLimit = max(10, intval($timeLeft));
|
2021-05-20 21:32:01 +02:00
|
|
|
$response['restrictions']['timeLimit'] = $timeLimit * 1000;
|
|
|
|
}
|
|
|
|
|
2021-05-22 21:17:49 +02:00
|
|
|
if (isset($response['history'][$currentRound]['allResults'])) {
|
2021-05-16 20:46:53 +02:00
|
|
|
$response['allResults'] = $response['history'][$currentRound]['allResults'];
|
2021-05-13 19:48:25 +02:00
|
|
|
}
|
2021-05-13 13:06:24 +02:00
|
|
|
|
2021-05-12 21:01:26 +02:00
|
|
|
$this->saveVisit($currentPlace->getId());
|
|
|
|
|
|
|
|
return new JsonContent($response);
|
|
|
|
}
|
|
|
|
|
2021-03-19 23:14:01 +01:00
|
|
|
public function multiNextRound(): IContent
|
|
|
|
{
|
2023-04-20 00:19:54 +02:00
|
|
|
$roomId = \Container::$request->query('roomId');
|
|
|
|
$session = \Container::$request->session();
|
2021-03-19 23:14:01 +01:00
|
|
|
|
|
|
|
if (!($multiState = $session->get('multiState')) || $multiState['roomId'] !== $roomId) {
|
|
|
|
return new JsonContent(['error' => 'no_session_found']);
|
|
|
|
}
|
|
|
|
|
|
|
|
$room = $this->multiRoomRepository->getByRoomId($roomId);
|
|
|
|
$state = $room->getStateArray();
|
|
|
|
$members = $room->getMembersArray();
|
|
|
|
|
|
|
|
if ($members['owner'] !== $multiState['token']) {
|
|
|
|
return new JsonContent(['error' => 'not_owner_of_room']);
|
|
|
|
}
|
|
|
|
|
|
|
|
$state['currentRound'] += 1;
|
|
|
|
if ($state['currentRound'] < static::NUMBER_OF_ROUNDS) {
|
|
|
|
$this->multiConnector->sendMessage('next_round', ['roomId' => $roomId, 'currentRound' => $state['currentRound']]);
|
|
|
|
}
|
|
|
|
|
|
|
|
$room->setStateArray($state);
|
|
|
|
$room->setUpdatedDate(new DateTime());
|
2023-04-20 00:19:54 +02:00
|
|
|
\Container::$persistentDataManager->saveToDb($room);
|
2021-03-19 23:14:01 +01:00
|
|
|
|
|
|
|
return new JsonContent(['ok' => true]);
|
|
|
|
}
|
|
|
|
|
2021-05-12 21:01:26 +02:00
|
|
|
private function evaluateGuess(Position $realPosition, Position $guessPosition, float $area)
|
2021-03-19 23:14:01 +01:00
|
|
|
{
|
|
|
|
$distance = $this->calculateDistance($realPosition, $guessPosition);
|
|
|
|
$score = $this->calculateScore($distance, $area);
|
|
|
|
|
|
|
|
return ['distance' => $distance, 'score' => $score];
|
|
|
|
}
|
|
|
|
|
2021-05-06 17:12:18 +02:00
|
|
|
private function startNewGame(array &$state, int $mapId): void
|
2020-06-20 00:03:49 +02:00
|
|
|
{
|
2023-04-20 00:19:54 +02:00
|
|
|
$session = \Container::$request->session();
|
2021-05-06 20:25:48 +02:00
|
|
|
$userId = $session->get('userId');
|
2021-05-06 17:12:18 +02:00
|
|
|
|
2021-05-05 19:19:38 +02:00
|
|
|
$places = $this->placeRepository->getRandomNPlaces($mapId, static::NUMBER_OF_ROUNDS, $userId);
|
2021-03-15 13:58:44 +01:00
|
|
|
|
|
|
|
$state['rounds'] = [];
|
|
|
|
$state['currentRound'] = 0;
|
|
|
|
|
|
|
|
foreach ($places as $place) {
|
|
|
|
$state['rounds'][] = [
|
|
|
|
'placeId' => $place->getId(),
|
|
|
|
'position' => $place->getPosition(),
|
|
|
|
'panoId' => $place->getPanoIdCached(),
|
|
|
|
'pov' => $place->getPov()
|
|
|
|
];
|
|
|
|
}
|
2020-06-20 00:03:49 +02:00
|
|
|
}
|
|
|
|
|
2020-05-25 19:46:31 +02:00
|
|
|
private function calculateDistance(Position $realPosition, Position $guessPosition): float
|
|
|
|
{
|
|
|
|
return $realPosition->calculateDistanceTo($guessPosition);
|
|
|
|
}
|
|
|
|
|
2020-05-28 21:05:55 +02:00
|
|
|
private function calculateScore(float $distance, float $area): int
|
2020-05-25 19:46:31 +02:00
|
|
|
{
|
2020-06-12 21:55:04 +02:00
|
|
|
$goodness = 1.0 - ($distance / (sqrt($area) * 1000));
|
2020-05-25 19:46:31 +02:00
|
|
|
|
2020-05-28 21:05:55 +02:00
|
|
|
return (int) round(pow(static::MAX_SCORE, $goodness));
|
2020-05-25 19:46:31 +02:00
|
|
|
}
|
|
|
|
}
|