Compare commits

..

No commits in common. "3b98570f6d7c57d0c1c412b607cbd32eae66829e" and "45ddb7f56a727d28ae0897f048337478e26e0b8f" have entirely different histories.

20 changed files with 327 additions and 408 deletions

View File

@ -2,7 +2,6 @@ CREATE TABLE `challenges` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`token` int(10) unsigned NOT NULL,
`time_limit` int(10) unsigned,
`time_limit_type` enum('game', 'round') NOT NULL DEFAULT 'game',
`no_move` tinyint(1) NOT NULL DEFAULT 0,
`no_pan` tinyint(1) NOT NULL DEFAULT 0,
`no_zoom` tinyint(1) NOT NULL DEFAULT 0,
@ -49,6 +48,6 @@ CREATE TABLE `guesses` (
PRIMARY KEY (`id`),
KEY `user_id` (`user_id`),
KEY `place_in_challenge_id` (`place_in_challenge_id`),
CONSTRAINT `guesses_user_id` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`),
CONSTRAINT `guesses_user_id` FOREIGN KEY (`user_id`) REFERENCES `places` (`id`),
CONSTRAINT `guesses_place_in_challenge_id` FOREIGN KEY (`place_in_challenge_id`) REFERENCES `place_in_challenge` (`id`)
) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4;

View File

@ -15,17 +15,6 @@
right: 0;
background-color: #000000;
opacity: 0.5;
z-index: 4;
}
#panningBlockerCover {
display: none;
position: absolute;
top: 0;
left: 0;
bottom: 0;
right: 0;
opacity: 0;
z-index: 3;
}
@ -33,7 +22,7 @@
position: absolute;
bottom: 30px;
right: 20px;
z-index: 3;
z-index: 2;
}
#guess.result {
@ -164,10 +153,6 @@
z-index: 2;
}
#goToStart {
display: none;
}
@media screen and (max-width: 599px) {
#mapName {
display: none;

View File

@ -77,7 +77,6 @@ div.mapItem>div.buttonContainer {
#restrictions {
margin-top: 1em;
margin-bottom: 1em;
font-family: 'Roboto', sans-serif;
}
@ -85,7 +84,7 @@ div.mapItem>div.buttonContainer {
font-weight: 500;
}
#restrictions input {
#restrictions input[type=checkbox] {
height: auto;
margin: 0.5em;
}
@ -95,10 +94,6 @@ div.mapItem>div.buttonContainer {
margin-left: 2em;
}
#timeLimitType {
margin-left: 2em;
}
@media screen and (min-width: 1504px) {
#mapContainer {
grid-template-columns: auto auto auto auto;

View File

@ -1,6 +1,6 @@
'use strict';
const GameType = Object.freeze({ 'SINGLE': 0, 'MULTI': 1, 'CHALLENGE': 2 });
const GameType = Object.freeze({'SINGLE': 0, 'MULTI': 1, 'CHALLENGE': 2});
(function () {
var Game = {
@ -215,8 +215,8 @@ const GameType = Object.freeze({ 'SINGLE': 0, 'MULTI': 1, 'CHALLENGE': 2 });
}
},
getGameIdentifier: function () {
switch (Game.type) {
getGameIdentifier: function() {
switch(Game.type) {
case GameType.SINGLE:
return '/game/' + mapId;
case GameType.MULTI:
@ -295,120 +295,72 @@ const GameType = Object.freeze({ 'SINGLE': 0, 'MULTI': 1, 'CHALLENGE': 2 });
return;
}
Game.loadHistory(this.response.history);
Game.history = this.response.history;
if (this.response.history !== undefined) {
for (var i = 0; i < this.response.history.length; ++i) {
var round = this.response.history[i];
if (round.result) {
Game.rounds.push({ position: round.position, guessPosition: round.result.guessPosition, realMarker: null, guessMarkers: [] });
Game.addPositionToResultMap(true);
if (round.result.guessPosition) {
Game.addGuessPositionToResultMap(round.result.guessPosition, null, true);
}
Game.scoreSum += round.result.score;
if (round.allResults !== undefined) {
for (var j = 0; j < round.allResults.length; ++j) {
var result = round.allResults[j];
if (result.guessPosition) {
Game.addGuessPositionToResultMap(result.guessPosition, result, true);
}
}
}
}
}
}
if (this.response.finished) {
Game.transitToResultMap();
Game.showSummary();
} else {
if (this.response.restrictions) {
Game.panorama.setOptions({
clickToGo: !this.response.restrictions.noMove,
linksControl: !this.response.restrictions.noMove,
scrollwheel: !this.response.restrictions.noZoom
});
if (this.response.restrictions.noPan) {
document.getElementById('panningBlockerCover').style.display = 'block';
}
// TODO: refactor - it is necessary for mobile
if (window.getComputedStyle(document.getElementById('guess')).visibility === 'hidden') {
document.getElementById('showGuessButton').click();
}
Game.panoId = this.response.place.panoId;
Game.pov = this.response.place.pov;
if (Game.adaptGuess) {
document.getElementById('guess').classList.remove('adapt');
}
Game.startNewRound();
if (Game.guessMarker) {
Game.guessMarker.setMap(null);
Game.guessMarker = null;
}
document.getElementById('guess').classList.add('result');
Game.map.setOptions({
draggableCursor: 'grab'
});
Game.showSummary();
document.getElementById('continueButton').style.display = 'none';
return;
}
Game.panoId = this.response.place.panoId;
Game.pov = this.response.place.pov;
document.getElementById('currentRound').innerHTML = String(Game.rounds.length) + '/' + String(Game.NUMBER_OF_ROUNDS);
document.getElementById('currentScoreSum').innerHTML = String(Game.scoreSum) + '/' + String(Game.rounds.length * Game.MAX_SCORE);
Game.startNewRound();
});
},
transitToResultMap: function () {
// TODO: refactor - it is necessary for mobile
if (window.getComputedStyle(document.getElementById('guess')).visibility === 'hidden') {
document.getElementById('showGuessButton').click();
}
if (Game.adaptGuess) {
document.getElementById('guess').classList.remove('adapt');
}
if (Game.guessMarker) {
Game.guessMarker.setMap(null);
Game.guessMarker = null;
}
document.getElementById('guess').classList.add('result');
Game.map.setOptions({
draggableCursor: 'grab'
});
if (Game.rounds.length === Game.NUMBER_OF_ROUNDS) {
document.getElementById('continueButton').style.display = 'none';
document.getElementById('showSummaryButton').style.display = 'block';
} else if (Game.type == GameType.MULTI) {
if (Game.multi.owner) {
if (!Game.readyToContinue) {
document.getElementById('continueButton').disabled = true;
}
} else {
document.getElementById('continueButton').style.display = 'none';
}
}
},
loadHistory: function (history) {
if (!history)
return;
Game.history = history;
for (var i = 0; i < Game.rounds.length; ++i) {
var round = Game.rounds[i];
if (round.realMarker) {
round.realMarker.setMap(null);
}
for (var j = 0; j < round.guessMarkers.length; ++j) {
var guessMarker = round.guessMarkers[j];
guessMarker.marker.setMap(null);
guessMarker.line.setMap(null);
if (guessMarker.info) {
guessMarker.info.close();
}
}
}
Game.rounds = [];
for (var i = 0; i < Game.history.length; ++i) {
var round = Game.history[i];
if (round.result) {
Game.rounds.push({ position: round.position, guessPosition: round.result.guessPosition, realMarker: null, guessMarkers: [] });
Game.addPositionToResultMap(true);
if (round.result.guessPosition) {
Game.addGuessPositionToResultMap(round.result.guessPosition, null, true);
}
Game.scoreSum += round.result.score;
if (round.allResults !== undefined) {
for (var j = 0; j < round.allResults.length; ++j) {
var result = round.allResults[j];
if (result.guessPosition) {
Game.addGuessPositionToResultMap(result.guessPosition, result, true);
}
}
}
}
}
},
reset: function () {
if (Game.guessMarker) {
Game.guessMarker.setMap(null);
@ -452,13 +404,6 @@ const GameType = Object.freeze({ 'SINGLE': 0, 'MULTI': 1, 'CHALLENGE': 2 });
// needs to be set visible after the show guess map hid it in mobile view
document.getElementById("navigation").style.visibility = 'visible';
Game.panorama.setOptions({
clickToGo: true,
linksControl: true,
scrollwheel: true
});
document.getElementById('panningBlockerCover').style.display = null;
Game.initialize();
},
@ -531,11 +476,7 @@ const GameType = Object.freeze({ 'SINGLE': 0, 'MULTI': 1, 'CHALLENGE': 2 });
break;
case 'game_not_found':
MapGuesser.showModalWithContent('Error', 'The game was not found by this ID. Please check the link.');
break;
case 'anonymous_user':
MapGuesser.showModalWithContent('Error', 'You have to login to join a challenge!');
MapGuesser.showModalWithContent('Error', 'The game room was not found by this ID. Please check the link.');
break;
default:
@ -601,9 +542,25 @@ const GameType = Object.freeze({ 'SINGLE': 0, 'MULTI': 1, 'CHALLENGE': 2 });
},
showResultMap: function (result, resultBounds) {
// TODO: refactor - it is necessary for mobile
if (window.getComputedStyle(document.getElementById('guess')).visibility === 'hidden') {
document.getElementById('showGuessButton').click();
}
Game.transitToResultMap();
if (Game.adaptGuess) {
document.getElementById('guess').classList.remove('adapt');
}
if (Game.guessMarker) {
Game.guessMarker.setMap(null);
Game.guessMarker = null;
}
document.getElementById('guess').classList.add('result');
Game.map.setOptions({
draggableCursor: 'grab'
});
Game.map.fitBounds(resultBounds);
var distanceInfo = document.getElementById('distanceInfo');
@ -622,6 +579,19 @@ const GameType = Object.freeze({ 'SINGLE': 0, 'MULTI': 1, 'CHALLENGE': 2 });
var scoreBar = document.getElementById('scoreBar');
scoreBar.style.backgroundColor = scoreBarProperties.backgroundColor;
scoreBar.style.width = scoreBarProperties.width;
if (Game.rounds.length === Game.NUMBER_OF_ROUNDS) {
document.getElementById('continueButton').style.display = 'none';
document.getElementById('showSummaryButton').style.display = 'block';
} else if (roomId) {
if (Game.multi.owner) {
if (!Game.readyToContinue) {
document.getElementById('continueButton').disabled = true;
}
} else {
document.getElementById('continueButton').style.display = 'none';
}
}
},
guess: function () {
@ -658,6 +628,48 @@ const GameType = Object.freeze({ 'SINGLE': 0, 'MULTI': 1, 'CHALLENGE': 2 });
Game.pov = this.response.place.pov;
}
// if(this.response.history) {
// // game finished
// // delete everything but the last round
// for (var i = 0; i < Game.rounds.length -1; ++i) {
// var round = Game.rounds[i];
// if (round.realMarker) {
// round.realMarker.setMap(null);
// }
// for (var j = 0; j < round.guessMarkers.length; ++j) {
// var guessMarker = round.guessMarkers[j];
// guessMarker.marker.setMap(null);
// guessMarker.line.setMap(null);
// if (guessMarker.info) {
// guessMarker.info.close();
// }
// }
// }
// // Game.rounds = [];
// for (var i = 0; i < this.response.history.length; ++i) {
// var round = this.response.history[i];
// Game.rounds[i] = { position: round.position, guessPosition: round.result.guessPosition, realMarker: null, guessMarkers: [] };
// Game.addPositionToResultMap(true);
// if (round.result.guessPosition) {
// Game.addGuessPositionToResultMap(round.result.guessPosition, null, true);
// }
// Game.scoreSum += round.result.score;
// for (var j = 0; j < round.allResults.length; ++j) {
// var result = round.allResults[j];
// if (result.guessPosition) {
// Game.addGuessPositionToResultMap(result.guessPosition, result, true);
// }
// }
// }
// }
}, data);
},
@ -771,6 +783,43 @@ const GameType = Object.freeze({ 'SINGLE': 0, 'MULTI': 1, 'CHALLENGE': 2 });
},
showSummary: function () {
for (var i = 0; i < Game.rounds.length; ++i) {
var round = Game.rounds[i];
if (round.realMarker) {
round.realMarker.setMap(null);
}
for (var j = 0; j < round.guessMarkers.length; ++j) {
var guessMarker = round.guessMarkers[j];
guessMarker.marker.setMap(null);
guessMarker.line.setMap(null);
if (guessMarker.info) {
guessMarker.info.close();
}
}
}
Game.rounds = [];
for (var i = 0; i < Game.history.length; ++i) {
var round = Game.history[i];
Game.rounds.push({ position: round.position, guessPosition: round.result.guessPosition, realMarker: null, guessMarkers: [] });
Game.addPositionToResultMap(true);
if (round.result.guessPosition) {
Game.addGuessPositionToResultMap(round.result.guessPosition, null, true);
}
Game.scoreSum += round.result.score;
if(round.allResults !== undefined) {
for (var j = 0; j < round.allResults.length; ++j) {
var result = round.allResults[j];
if (result.guessPosition) {
Game.addGuessPositionToResultMap(result.guessPosition, result, true);
}
}
}
}
var distanceInfo = document.getElementById('distanceInfo');
distanceInfo.children[0].style.display = 'none';
distanceInfo.children[1].style.display = 'none';
@ -780,13 +829,11 @@ const GameType = Object.freeze({ 'SINGLE': 0, 'MULTI': 1, 'CHALLENGE': 2 });
scoreInfo.children[1].style.display = 'block';
document.getElementById('showSummaryButton').style.display = null;
if (Game.type == GameType.SINGLE || Game.multi.owner) {
if (!roomId || Game.multi.owner) {
document.getElementById('startNewGameButton').style.display = 'block';
if (!Game.readyToContinue) {
document.getElementById('startNewGameButton').disabled = true;
}
} else if (Game.type == GameType.CHALLENGE) {
document.getElementById('goToStart').style.display = 'block';
}
var resultBounds = new google.maps.LatLngBounds();
@ -976,9 +1023,9 @@ const GameType = Object.freeze({ 'SINGLE': 0, 'MULTI': 1, 'CHALLENGE': 2 });
document.getElementById("compass").style.transform = "translateY(-50%) rotate(" + heading + "deg)";
});
if (roomId !== null) {
if(roomId !== null) {
Game.type = GameType.MULTI;
} else if (challengeToken !== null) {
} else if(challengeToken !== null) {
Game.type = GameType.CHALLENGE;
}

View File

@ -112,15 +112,13 @@
document.getElementById('playMode').style.visibility = 'hidden';
}
if (document.getElementById('challengeButton')) {
document.getElementById('challengeButton').onclick = function () {
MapGuesser.showModal('challenge');
document.getElementById('createNewChallengeButton').href = '/challenge/new/' + this.dataset.mapId;
document.getElementById('playMode').style.visibility = 'hidden';
var timeLimit = document.getElementById('timeLimit').value;
document.getElementById('timeLimitLabel').innerText = 'Time limit of ' + timeLimit + ' seconds per round';
};
document.getElementById('challengeButton').onclick = function () {
MapGuesser.showModal('challenge');
document.getElementById('createNewChallengeButton').href = '/challenge/new/' + this.dataset.mapId;
document.getElementById('playMode').style.visibility = 'hidden';
var timeLimit = document.getElementById('timeLimit').value;
document.getElementById('timeLimitLabel').innerText = 'Time limit of ' + timeLimit + ' seconds per round';
}
document.getElementById('closePlayModeButton').onclick = function () {

View File

@ -97,7 +97,7 @@ class GameController
}
public function getChallenge(): IContent
{
{
$challengeToken = $this->request->query('challengeToken');
return new HtmlContent('game', ['challengeToken' => $challengeToken]);
@ -112,19 +112,16 @@ class GameController
$challenge->setToken($challengeToken);
$challenge->setCreatedDate(new DateTime());
if ($this->request->post('timerEnabled') !== null && $this->request->post('timeLimit') !== null) {
if($this->request->post('timerEnabled') !== null && $this->request->post('timeLimit') !== null) {
$challenge->setTimeLimit($this->request->post('timeLimit'));
}
if ($this->request->post('timeLimitType') !== null) {
$challenge->setTimeLimitType($this->request->post('timeLimitType'));
}
if ($this->request->post('noMove') !== null) {
if($this->request->post('noMove') !== null) {
$challenge->setNoMove(true);
}
if ($this->request->post('noPan') !== null) {
if($this->request->post('noPan') !== null) {
$challenge->setNoPan(true);
}
if ($this->request->post('noZoom') !== null) {
if($this->request->post('noZoom') !== null) {
$challenge->setNoZoom(true);
}
@ -242,19 +239,8 @@ class GameController
$challengeToken_str = $this->request->query('challengeToken');
$session = $this->request->session();
$userId = $session->get('userId');
if (!isset($userId))
{
return new JsonContent(['error' => 'anonymous_user']);
}
$challenge = $this->challengeRepository->getByTokenStr($challengeToken_str);
if (!isset($challenge))
{
return new JsonContent(['error' => 'game_not_found']);
}
if(!$this->userInChallengeRepository->isUserParticipatingInChallenge($userId, $challenge)) {
// new player is joining
$userInChallenge = new UserInChallenge();

View File

@ -6,13 +6,9 @@ use MapGuesser\Util\Geo\Position;
use MapGuesser\Response\JsonContent;
use MapGuesser\Interfaces\Response\IContent;
use MapGuesser\Multi\MultiConnector;
use MapGuesser\PersistentData\PersistentDataManager;
use MapGuesser\PersistentData\Model\Challenge;
use MapGuesser\PersistentData\PersistentDataManager;
use MapGuesser\PersistentData\Model\Guess;
use MapGuesser\PersistentData\Model\Map;
use MapGuesser\PersistentData\Model\Place;
use MapGuesser\PersistentData\Model\PlaceInChallenge;
use MapGuesser\PersistentData\Model\User;
use MapGuesser\PersistentData\Model\UserPlayedPlace;
use MapGuesser\Repository\ChallengeRepository;
use MapGuesser\Repository\GuessRepository;
@ -145,61 +141,41 @@ class GameFlowController
return new JsonContent(['ok' => true]);
}
private function prepareChallengeResponse(int $userId, Challenge $challenge, int $currentRound, bool $withHistory = false): array
private function prepareChallengeResponse(int $userId, Challenge $challenge, int $currentRound): array
{
$currentPlace = $this->placeRepository->getByRoundInChallenge($challenge, $currentRound);
if (!isset($currentPlace) || $withHistory) {
$withRelations = [User::class, PlaceInChallenge::class, Place::class];
foreach ($this->guessRepository->getAllInChallenge($challenge, $withRelations) as $guess) {
$round = $guess->getPlaceInChallenge()->getRound();
if ($guess->getUser()->getId() === $userId) {
$response['history'][$round]['position'] =
$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(),
$allGuessesInChallenge = iterator_to_array($this->guessRepository->getAllInChallenge($challenge));
foreach ($allGuessesInChallenge as $guess) {
$round = $guess->getPlaceInChallenge()->getRound();
if ($guess->getUser()->getId() === $userId) {
$response['history'][$round]['position'] =
$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(),
'guessPosition' => $guess->getPosition()->toArray(),
'distance' => $guess->getDistance(),
'score' => $guess->getScore()
];
}
}
if (!isset($currentPlace)) { // game finished
$currentPlace = $this->placeRepository->getByRoundInChallenge($challenge, $currentRound);
if(!isset($currentPlace)) { // game finished
$response['finished'] = true;
} else { // continue game
$response['place'] = [
'panoId' => $currentPlace->getPanoIdCached(),
'pov' => $currentPlace->getPov()->toArray()
];
$prevRound = $currentRound - 1;
if ($prevRound >= 0) {
foreach ($this->guessRepository->getAllInChallengeByRound($prevRound, $challenge, [User::class]) as $guess) {
if ($guess->getUser()->getId() != $userId) {
$response['allResults'][] = [
'userName' => $guess->getUser()->getDisplayName(),
'guessPosition' => $guess->getPosition()->toArray(),
'distance' => $guess->getDistance(),
'score' => $guess->getScore()
];
}
}
}
$response['restrictions'] = [
'timeLimit' => $challenge->getTimeLimit(),
'noMove' => $challenge->getNoMove(),
'noPan' => $challenge->getNoPan(),
'noZoom' => $challenge->getNoZoom()
'pov' => [$currentPlace->getPov()->toArray()]
];
}
@ -211,7 +187,7 @@ class GameFlowController
$session = $this->request->session();
$userId = $session->get('userId');
$challengeToken_str = $this->request->query('challengeToken');
$userInChallenge = $this->userInChallengeRepository->getByUserIdAndToken($userId, $challengeToken_str, [Challenge::class]);
$userInChallenge = $this->userInChallengeRepository->getByUserIdAndToken($userId, $challengeToken_str);
if (!isset($userInChallenge)) {
return new JsonContent(['error' => 'game_not_found']);
@ -220,13 +196,16 @@ class GameFlowController
$challenge = $userInChallenge->getChallenge();
$currentRound = $userInChallenge->getCurrentRound();
$response = $this->prepareChallengeResponse($userId, $challenge, $currentRound, true);
$response = $this->prepareChallengeResponse($userId, $challenge, $currentRound);
return new JsonContent($response);
}
public function guess(): IContent
{
// testing
$testPlace = $this->placeRepository->getByIdWithMap(5);
$mapId = (int) $this->request->query('mapId');
$session = $this->request->session();
@ -327,7 +306,7 @@ class GameFlowController
$session = $this->request->session();
$userId = $session->get('userId');
$challengeToken_str = $this->request->query('challengeToken');
$userInChallenge = $this->userInChallengeRepository->getByUserIdAndToken($userId, $challengeToken_str, [Challenge::class]);
$userInChallenge = $this->userInChallengeRepository->getByUserIdAndToken($userId, $challengeToken_str);
if (!isset($userInChallenge)) {
return new JsonContent(['error' => 'game_not_found']);
@ -335,7 +314,7 @@ class GameFlowController
$challenge = $userInChallenge->getChallenge();
$currentRound = $userInChallenge->getCurrentRound();
$currentPlaceInChallenge = $this->placeInChallengeRepository->getByRoundInChallenge($currentRound, $challenge, [Place::class, Map::class]);
$currentPlaceInChallenge = $this->placeInChallengeRepository->getByRoundInChallenge($currentRound, $challenge);
$currentPlace = $currentPlaceInChallenge->getPlace();
$map = $currentPlace->getMap();

View File

@ -5,16 +5,11 @@ use MapGuesser\Interfaces\Authentication\IUser;
use MapGuesser\Interfaces\Authorization\ISecured;
use MapGuesser\Interfaces\Request\IRequest;
use MapGuesser\Interfaces\Response\IContent;
use MapGuesser\PersistentData\Model\Challenge;
use MapGuesser\PersistentData\Model\Map;
use MapGuesser\PersistentData\Model\Place;
use MapGuesser\PersistentData\PersistentDataManager;
use MapGuesser\Repository\ChallengeRepository;
use MapGuesser\Repository\GuessRepository;
use MapGuesser\Repository\MapRepository;
use MapGuesser\Repository\PlaceInChallengeRepository;
use MapGuesser\Repository\PlaceRepository;
use MapGuesser\Repository\UserInChallengeRepository;
use MapGuesser\Repository\UserPlayedPlaceRepository;
use MapGuesser\Response\HtmlContent;
use MapGuesser\Response\JsonContent;
@ -35,14 +30,6 @@ class MapAdminController implements ISecured
private UserPlayedPlaceRepository $userPlayedPlaceRepository;
private ChallengeRepository $challengeRepository;
private GuessRepository $guessRepository;
private PlaceInChallengeRepository $placeInChallengeRepository;
private UserInChallengeRepository $userInChallengeRepository;
public function __construct(IRequest $request)
{
$this->request = $request;
@ -50,10 +37,6 @@ class MapAdminController implements ISecured
$this->mapRepository = new MapRepository();
$this->placeRepository = new PlaceRepository();
$this->userPlayedPlaceRepository = new UserPlayedPlaceRepository();
$this->challengeRepository = new ChallengeRepository();
$this->guessRepository = new GuessRepository();
$this->placeInChallengeRepository = new PlaceInChallengeRepository();
$this->userInChallengeRepository = new UserInChallengeRepository();
}
public function authorize(): bool
@ -205,10 +188,6 @@ class MapAdminController implements ISecured
$this->pdm->deleteFromDb($userPlayedPlace);
}
foreach ($this->challengeRepository->getAllByPlace($place) as $challenge) {
$this->deleteChallenge($challenge);
}
$this->pdm->deleteFromDb($place);
}
@ -219,23 +198,6 @@ class MapAdminController implements ISecured
}
}
private function deleteChallenge(Challenge $challenge): void
{
foreach ($this->userInChallengeRepository->getAllByChallenge($challenge) as $userInChallenge) {
$this->pdm->deleteFromDb($userInChallenge);
}
foreach ($this->guessRepository->getAllInChallenge($challenge, [PlaceInChallenge::class]) as $guess) {
$this->pdm->deleteFromDb($guess);
}
foreach ($this->placeInChallengeRepository->getAllByChallenge($challenge) as $placeInChallenge) {
$this->pdm->deleteFromDb($placeInChallenge);
}
$this->pdm->deleteFromDb($challenge);
}
private function calculateMapBounds(Map $map): Bounds
{
$bounds = new Bounds();

View File

@ -48,7 +48,6 @@ class MapsController
$user = $this->request->user();
return new HtmlContent('maps', [
'maps' => $maps,
'isLoggedIn' => $user !== null,
'isAdmin' => $user !== null && $user->hasPermission(IUser::PERMISSION_ADMIN)
]);
}

View File

@ -9,10 +9,7 @@ use MapGuesser\Interfaces\Response\IRedirect;
use MapGuesser\OAuth\GoogleOAuth;
use MapGuesser\PersistentData\PersistentDataManager;
use MapGuesser\PersistentData\Model\User;
use MapGuesser\PersistentData\Model\UserInChallenge;
use MapGuesser\Repository\GuessRepository;
use MapGuesser\Repository\UserConfirmationRepository;
use MapGuesser\Repository\UserInChallengeRepository;
use MapGuesser\Repository\UserPasswordResetterRepository;
use MapGuesser\Repository\UserPlayedPlaceRepository;
use MapGuesser\Response\HtmlContent;
@ -32,10 +29,6 @@ class UserController implements ISecured
private UserPlayedPlaceRepository $userPlayedPlaceRepository;
private UserInChallengeRepository $userInChallengeRepository;
private GuessRepository $guessRepository;
public function __construct(IRequest $request)
{
$this->request = $request;
@ -43,8 +36,6 @@ class UserController implements ISecured
$this->userConfirmationRepository = new UserConfirmationRepository();
$this->userPasswordResetterRepository = new UserPasswordResetterRepository();
$this->userPlayedPlaceRepository = new UserPlayedPlaceRepository();
$this->userInChallengeRepository = new UserInChallengeRepository();
$this->guessRepository = new GuessRepository();
}
public function authorize(): bool
@ -218,14 +209,6 @@ class UserController implements ISecured
$this->pdm->deleteFromDb($userPlayedPlace);
}
foreach ($this->userInChallengeRepository->getAllByUser($user) as $userInChallenge) {
$this->pdm->deleteFromDb($userInChallenge);
}
foreach ($this->guessRepository->getAllByUser($user) as $guess) {
$this->pdm->deleteFromDb($guess);
}
$this->pdm->deleteFromDb($user);
\Container::$dbConnection->commit();

View File

@ -6,7 +6,7 @@ class Challenge extends Model
{
protected static string $table = 'challenges';
protected static array $fields = ['token', 'time_limit', 'time_limit_type', 'no_move', 'no_pan', 'no_zoom', 'created'];
protected static array $fields = ['token', 'time_limit', 'no_move', 'no_pan', 'no_zoom', 'created'];
protected static array $relations = [];
@ -14,10 +14,6 @@ class Challenge extends Model
private ?int $timeLimit = null;
private static array $timeLimitTypes = ['game', 'round'];
private string $timeLimitType = 'game';
private bool $noMove = false;
private bool $noPan = false;
@ -38,26 +34,19 @@ class Challenge extends Model
}
}
public function setTimeLimitType(string $timeLimitType): void
{
if (in_array($timeLimitType, self::$timeLimitTypes)) {
$this->timeLimitType = $timeLimitType;
}
}
public function setNoMove(bool $noMove): void
{
$this->noMove = $noMove;
$this->$noMove = $noMove;
}
public function setNoPan(bool $noPan): void
{
$this->noPan = $noPan;
$this->$noPan = $noPan;
}
public function setNoZoom(bool $noZoom): void
{
$this->noZoom = $noZoom;
$this->$noZoom = $noZoom;
}
public function setCreatedDate(DateTime $created): void
@ -80,11 +69,6 @@ class Challenge extends Model
return $this->timeLimit;
}
public function getTimeLimitType(): string
{
return $this->timeLimitType;
}
public function getNoMove(): bool
{
return $this->noMove;

View File

@ -8,9 +8,9 @@ use MapGuesser\PersistentData\Model\Model;
class PersistentDataManager
{
public function selectFromDb(Select $select, string $type, bool $useRelations = false, array $withRelations = [])
public function selectFromDb(Select $select, string $type, bool $withRelations = false)
{
$select = $this->createSelect($select, $type, $useRelations, $withRelations);
$select = $this->createSelect($select, $type, $withRelations);
$data = $select->execute()->fetch(IResultSet::FETCH_ASSOC);
@ -19,38 +19,36 @@ class PersistentDataManager
}
$model = new $type();
$this->fillWithData($data, $model, $withRelations);
$this->fillWithData($data, $model);
return $model;
}
public function selectMultipleFromDb(Select $select, string $type, bool $useRelations = false, array $withRelations = []): Generator
public function selectMultipleFromDb(Select $select, string $type, bool $withRelations = false): Generator
{
$select = $this->createSelect($select, $type, $useRelations, $withRelations);
$select = $this->createSelect($select, $type, $withRelations);
$result = $select->execute();
while ($data = $result->fetch(IResultSet::FETCH_ASSOC)) {
$model = new $type();
$this->fillWithData($data, $model, $withRelations);
$this->fillWithData($data, $model);
yield $model;
}
}
public function selectFromDbById($id, string $type, bool $useRelations = false)
public function selectFromDbById($id, string $type, bool $withRelations = false)
{
$select = new Select(\Container::$dbConnection);
$select->whereId($id);
return $this->selectFromDb($select, $type, $useRelations);
return $this->selectFromDb($select, $type, $withRelations);
}
public function fillWithData(array &$data, Model $model, array $withRelations = [], ?string $modelKey = null): void
public function fillWithData(array &$data, Model $model, ?string $modelKey = null): void
{
$relations = $model::getRelations();
if (count($withRelations)) {
$relations = array_intersect($relations, $withRelations);
}
$relationData = [];
while (key($data)) {
$key = key($data);
@ -60,7 +58,7 @@ class PersistentDataManager
if (strpos($key, '__') == false) {
$method = 'set' . str_replace('_', '', ucwords($key, '_'));
if (method_exists($model, $method) && isset($value)) {
if (method_exists($model, $method)) {
$model->$method($value);
}
@ -70,15 +68,16 @@ class PersistentDataManager
$method = 'set' . str_replace('_', '', ucwords($key, '_'));
if (method_exists($model, $method) && isset($value)) {
if (method_exists($model, $method)) {
$model->$method($value);
}
next($data);
} else if (substr($key, 0, strlen($relation . '__')) === $relation . '__') {
// $relation = current($relations);
$relationType = current($relations);
$relationModel = new $relationType();
$this->fillWithData($data, $relationModel, $withRelations, $relation);
$this->fillWithData($data, $relationModel, $relation);
$method = 'set' . str_replace('_', '', ucwords($relation, '_'));
$model->$method($relationModel);
@ -89,6 +88,20 @@ class PersistentDataManager
}
}
// foreach ($data as $key => $value) {
// if ($this->extractRelationData($key, $value, $relationData, $relations)) {
// continue;
// }
// $method = 'set' . str_replace('_', '', ucwords($key, '_'));
// if (method_exists($model, $method)) {
// $model->$method($value);
// }
// }
// $this->setRelations($model, $relationData);
$model->saveSnapshot();
}
@ -153,11 +166,15 @@ class PersistentDataManager
$model->resetSnapshot();
}
private function createSelect(Select $select, string $type, bool $useRelations = false, array $withRelations = []): Select
private function createSelect(Select $select, string $type, bool $withRelations = false): Select
{
$table = call_user_func([$type, 'getTable']);
$fields = call_user_func([$type, 'getFields']);
// array_walk($fields, function (&$value, $key, $table) {
// $value = [$table, $value];
// }, $table);
$columns = [];
foreach ($fields as $field) {
@ -166,24 +183,29 @@ class PersistentDataManager
$select->from($table);
if ($useRelations) {
//TODO: only with some relations?
if ($withRelations) {
$relations = call_user_func([$type, 'getRelations']);
if (count($withRelations)) {
$relations = array_intersect($relations, $withRelations);
}
$columns = array_merge($columns, $this->getRelationColumns($relations, $withRelations));
// $columns = [];
$this->leftJoinRelations($select, $table, $relations, $withRelations);
// foreach ($fields as $field) {
// $columns[] = [$table, $field];
// }
$columns = array_merge($columns, $this->getRelationColumns($relations));
$this->leftJoinRelations($select, $table, $relations);
$select->columns($columns);
} else {
// $select->columns($fields);
$select->columns($columns);
}
return $select;
}
private function getRelationColumns(array $relations, array $withRelations): array
private function getRelationColumns(array $relations): array
{
$columns = [];
@ -194,26 +216,50 @@ class PersistentDataManager
}
$nextOrderRelations = call_user_func([$relationType, 'getRelations']);
if (count($withRelations)) {
$nextOrderRelations = array_intersect($nextOrderRelations, $withRelations);
}
$columns = array_merge($columns, $this->getRelationColumns($nextOrderRelations, $withRelations));
$columns = array_merge($columns, $this->getRelationColumns($nextOrderRelations));
}
return $columns;
}
private function leftJoinRelations(Select $select, string $table, array $relations, array $withRelations): void
private function leftJoinRelations(Select $select, string $table, array $relations): void
{
foreach ($relations as $relation => $relationType) {
$relationTable = call_user_func([$relationType, 'getTable']);
$select->leftJoin($relationTable, [$relationTable, 'id'], '=', [$table, $relation . '_id']);
$nextOrderRelations = call_user_func([$relationType, 'getRelations']);
if (count($withRelations)) {
$nextOrderRelations = array_intersect($nextOrderRelations, $withRelations);
$this->leftJoinRelations($select, $relationTable, $nextOrderRelations);
}
}
private function extractRelationData(string $key, $value, array &$relationData, array $relations): bool
{
$found = false;
foreach ($relations as $relation => $relationType) {
if (substr($key, 0, strlen($relation . '__')) === $relation . '__') {
$found = true;
$relationData[$relation][substr($key, strlen($relation . '__'))] = $value;
break;
}
}
return $found;
}
private function setRelations(Model $model, array &$relations): void
{
foreach ($model::getRelations() as $relation => $relationType) {
if (isset($relations[$relation])) {
$object = new $relationType();
$this->fillWithData($relations[$relation], $object);
$method = 'set' . str_replace('_', '', ucwords($relation, '_'));
$model->$method($object);
}
$this->leftJoinRelations($select, $relationTable, $nextOrderRelations, $withRelations);
}
}

View File

@ -3,7 +3,6 @@
use Generator;
use MapGuesser\Database\Query\Select;
use MapGuesser\PersistentData\Model\Challenge;
use MapGuesser\PersistentData\Model\Place;
use MapGuesser\PersistentData\Model\User;
use MapGuesser\PersistentData\PersistentDataManager;
@ -56,13 +55,4 @@ class ChallengeRepository
yield from $this->pdm->selectMultipleFromDb($select, Challenge::class);
}
public function getAllByPlace(Place $place): Generator
{
$select = new Select(\Container::$dbConnection);
$select->innerJoin('place_in_challenge', ['challenges', 'id'], '=', ['place_in_challenge', 'challenge_id']);
$select->where('place_id', '=', $place->getId());
yield from $this->pdm->selectMultipleFromDb($select, Challenge::class);
}
}

View File

@ -7,7 +7,6 @@ use MapGuesser\PersistentData\Model\Guess;
use MapGuesser\PersistentData\Model\User;
use MapGuesser\PersistentData\Model\UserInChallenge;
use MapGuesser\PersistentData\Model\Place;
use MapGuesser\PersistentData\Model\PlaceInChallenge;
use MapGuesser\PersistentData\PersistentDataManager;
class GuessRepository
@ -19,15 +18,7 @@ class GuessRepository
$this->pdm = new PersistentDataManager();
}
public function getAllByUser(User $user): Generator
{
$select = new Select(\Container::$dbConnection);
$select->where('user_id', '=', $user->getId());
yield from $this->pdm->selectMultipleFromDb($select, Guess::class);
}
public function getAllByUserAndChallenge(User $user, Challenge $challenge): Generator
public function getAllByUserAndChallenge(User $user, Challenge $challenge) : Generator
{
$select = new Select(\Container::$dbConnection);
$select->innerJoin('place_in_challenge', ['place_in_challenge', 'id'], '=', ['guesses', 'place_in_challenge_id']);
@ -37,7 +28,7 @@ class GuessRepository
yield from $this->pdm->selectMultipleFromDb($select, Guess::class);
}
public function getByUserAndPlaceInChallenge(User $user, Challenge $challenge, Place $place): ?Guess
public function getByUserAndPlaceInChallenge(User $user, Challenge $challenge, Place $place) : ?Guess
{
$select = new Select(\Container::$dbConnection);
$select->innerJoin('place_in_challenge', ['place_in_challenge', 'id'], '=', ['guesses', 'place_in_challenge_id']);
@ -59,40 +50,23 @@ class GuessRepository
yield from $this->pdm->selectMultipleFromDb($select, Guess::class);
}
public function getAllInChallenge(Challenge $challenge, array $withRelations = []): Generator
public function getAllInChallenge(Challenge $challenge): Generator
{
if (count($withRelations)) {
$necessaryRelations = [PlaceInChallenge::class];
$withRelations = array_unique(array_merge($withRelations, $necessaryRelations));
}
$select = new Select(\Container::$dbConnection);
// $select->innerJoin('place_in_challenge', ['guesses', 'place_in_challenge_id'], '=', ['place_in_challenge', 'id']);
$select->where('challenge_id', '=', $challenge->getId());
$select->orderBy('round');
yield from $this->pdm->selectMultipleFromDb($select, Guess::class, true, $withRelations);
yield from $this->pdm->selectMultipleFromDb($select, Guess::class, true);
}
public function getAllInChallengeByRound(int $round, Challenge $challenge, array $withRelations = []): Generator
public function getAllInChallengeByRound(int $round, Challenge $challenge): Generator
{
if (count($withRelations)) {
$necessaryRelations = [PlaceInChallenge::class];
$withRelations = array_unique(array_merge($withRelations, $necessaryRelations));
}
$select = new Select(\Container::$dbConnection);
// $select->innerJoin('place_in_challenge', ['guesses', 'place_in_challenge_id'], '=', ['place_in_challenge', 'id']);
$select->where('challenge_id', '=', $challenge->getId());
$select->where('round', '=', $round);
yield from $this->pdm->selectMultipleFromDb($select, Guess::class, true, $withRelations);
}
public function getAllByPlace(Place $place): Generator
{
$select = new Select(\Container::$dbConnection);
$select->innerJoin('place_in_challenge', ['place_in_challenge', 'id'], '=', ['guesses', 'place_in_challenge_id']);
$select->where('place_id', '=', $place->getId());
yield from $this->pdm->selectMultipleFromDb($select, Guess::class);
yield from $this->pdm->selectMultipleFromDb($select, Guess::class, true);
}
}

View File

@ -3,7 +3,6 @@
use Generator;
use MapGuesser\Database\Query\Select;
use MapGuesser\PersistentData\Model\Challenge;
use MapGuesser\PersistentData\Model\Map;
use MapGuesser\PersistentData\Model\Place;
use MapGuesser\PersistentData\Model\PlaceInChallenge;
use MapGuesser\PersistentData\PersistentDataManager;
@ -17,12 +16,12 @@ class PlaceInChallengeRepository
$this->pdm = new PersistentDataManager();
}
public function getAllByPlace(Place $place, array $withRelations = []) : Generator
public function getAllByPlace(Place $place) : Generator
{
$select = new Select(\Container::$dbConnection);
$select->where('place_id', '=', $place->getId());
yield from $this->pdm->selectMultipleFromDb($select, PlaceInChallenge::class, true, $withRelations);
yield from $this->pdm->selectMultipleFromDb($select, PlaceInChallenge::class);
}
public function getAllByChallenge(Challenge $challenge) : Generator
@ -42,13 +41,13 @@ class PlaceInChallengeRepository
return $this->pdm->selectFromDb($select, PlaceInChallenge::class);
}
public function getByRoundInChallenge(int $round, Challenge $challenge, array $withRelations = []): ?PlaceInChallenge
public function getByRoundInChallenge(int $round, Challenge $challenge): ?PlaceInChallenge
{
$select = new Select(\Container::$dbConnection);
$select->where('challenge_id', '=', $challenge->getId());
$select->orderBy('round');
$select->limit(1, $round);
return $this->pdm->selectFromDb($select, PlaceInChallenge::class, true, $withRelations);
return $this->pdm->selectFromDb($select, PlaceInChallenge::class, true);
}
}

View File

@ -198,5 +198,14 @@ class PlaceRepository
yield from $this->pdm->selectMultipleFromDb($select, Place::class);
}
public function getByIdWithMap(int $placeId): ?Place
{
$select = new Select(\Container::$dbConnection);
// $select->innerJoin('maps', ['maps', 'id'], '=', ['places', 'map_id']);
$select->where(['places', 'id'], '=', $placeId);
return $this->pdm->selectFromDb($select, Place::class, true);
}
}

View File

@ -41,20 +41,15 @@ class UserInChallengeRepository
return $this->pdm->selectFromDb($select, UserInChallenge::class);
}
public function getByUserIdAndToken(int $userId, string $token_str, array $withRelations = []): ?UserInChallenge
public function getByUserIdAndToken(int $userId, string $token_str): ?UserInChallenge
{
if (count($withRelations)) {
$necessaryRelations = [Challenge::class];
$withRelations = array_unique(array_merge($withRelations, $necessaryRelations));
}
$token = hexdec($token_str);
$select = new Select(\Container::$dbConnection);
$select->where('user_id', '=', $userId);
$select->where('token', '=', $token);
return $this->pdm->selectFromDb($select, UserInChallenge::class, true, $withRelations);
return $this->pdm->selectFromDb($select, UserInChallenge::class, true);
}
public function isUserParticipatingInChallenge(int $userId, Challenge $challenge): bool

View File

@ -38,7 +38,7 @@ class UserPlayedPlaceRepository
$select = new Select(\Container::$dbConnection);
$select->where('user_id', '=', $user->getId());
yield from $this->pdm->selectMultipleFromDb($select, UserPlayedPlace::class);
yield from $this->pdm->selectMultipleFromDb($select, UserPlayedPlace::class, true);
}
public function getByUserIdAndPlaceId(int $userId, int $placeId) : ?UserPlayedPlace

View File

@ -25,7 +25,6 @@
<p id="countdownTime" class="mono bold"></p>
</div>
<div id="panoCover"></div>
<div id="panningBlockerCover"></div>
<div id="panorama"></div>
<div id="showGuessButtonContainer">
<button id="showGuessButton" class="fullWidth">Show guess map</button>
@ -58,7 +57,6 @@
<button id="continueButton" class="fullWidth">Continue</button>
<button id="showSummaryButton" class="fullWidth">Show summary</button>
<button id="startNewGameButton" class="fullWidth">Play this map again</button>
<a href="/" id="goToStart"><button class="fullWidth">Go back to the menu</button></a>
</div>
</div>
<div id="navigation" class="circleControl">

View File

@ -11,10 +11,8 @@ TODO: condition!
<a id="singleButton" class="button fullWidth marginTop" href="" title="Single player">Single player</a>
<p class="bold center marginTop marginBottom">OR</p>
<button id="multiButton" class="fullWidth green" data-map-id="">Multiplayer (beta)</button>
<?php if ($isLoggedIn): ?>
<p class="bold center marginTop marginBottom">OR</p>
<button id="challengeButton" class="fullWidth yellow" data-map-id="" data-timer="">Challenge (gamma)</button>
<?php endif; ?>
<p class="bold center marginTop marginBottom">OR</p>
<button id="challengeButton" class="fullWidth yellow" data-map-id="" data-timer="">Challenge (gamma)</button>
<div class="right">
<button id="closePlayModeButton" class="gray marginTop" type="button">Close</button>
</div>
@ -53,25 +51,18 @@ TODO: condition!
<div>
<input type="range" id="timeLimit" name="timeLimit" min="10" max="600" value="120" />
</div>
<div id="timeLimitType">
Time limit
<input type="radio" id="timeLimitTypeGame" name="timeLimitType" value="game" checked />
<label for="timeLimitTypeGame">for the whole game</label>
<input type="radio" id="timeLimitTypeRound" name="timeLimitType" value="round" />
<label for="timeLimitTypeRound">per round</label>
</div>
</div>
<div>
<input type="checkbox" id="noMove" name="noMove" value="noMove" />
<label for="noMove">No movement allowed</label>
</div>
<div>
<input type="checkbox" id="noZoom" name="noZoom" value="noZoom" />
<label for="noMove">No zoom allowed</label>
<input type="checkbox" id="noPan" name="noPan" value="noPan" />
<label for="noPan">No camera rotation allowed</label>
</div>
<div>
<input type="checkbox" id="noPan" name="noPan" value="noPan" />
<label for="noPan">No camera change allowed</label>
<input type="checkbox" id="noZoom" name="noZoom" value="noZoom" />
<label for="noMove">No zoom allowed</label>
</div>
<input type="hidden" name="mapId" id="challengeMapId" />
</div>