MAPG-235 reduced number of queries for init and guess in challenge
This commit is contained in:
parent
7792f1c3ff
commit
45ddb7f56a
@ -297,8 +297,11 @@ const GameType = Object.freeze({'SINGLE': 0, 'MULTI': 1, 'CHALLENGE': 2});
|
|||||||
|
|
||||||
Game.history = this.response.history;
|
Game.history = this.response.history;
|
||||||
|
|
||||||
|
if (this.response.history !== undefined) {
|
||||||
for (var i = 0; i < this.response.history.length; ++i) {
|
for (var i = 0; i < this.response.history.length; ++i) {
|
||||||
var round = this.response.history[i];
|
var round = this.response.history[i];
|
||||||
|
|
||||||
|
if (round.result) {
|
||||||
Game.rounds.push({ position: round.position, guessPosition: round.result.guessPosition, realMarker: null, guessMarkers: [] });
|
Game.rounds.push({ position: round.position, guessPosition: round.result.guessPosition, realMarker: null, guessMarkers: [] });
|
||||||
Game.addPositionToResultMap(true);
|
Game.addPositionToResultMap(true);
|
||||||
if (round.result.guessPosition) {
|
if (round.result.guessPosition) {
|
||||||
@ -306,6 +309,8 @@ const GameType = Object.freeze({'SINGLE': 0, 'MULTI': 1, 'CHALLENGE': 2});
|
|||||||
}
|
}
|
||||||
Game.scoreSum += round.result.score;
|
Game.scoreSum += round.result.score;
|
||||||
|
|
||||||
|
|
||||||
|
if (round.allResults !== undefined) {
|
||||||
for (var j = 0; j < round.allResults.length; ++j) {
|
for (var j = 0; j < round.allResults.length; ++j) {
|
||||||
var result = round.allResults[j];
|
var result = round.allResults[j];
|
||||||
if (result.guessPosition) {
|
if (result.guessPosition) {
|
||||||
@ -314,6 +319,10 @@ const GameType = Object.freeze({'SINGLE': 0, 'MULTI': 1, 'CHALLENGE': 2});
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (this.response.finished) {
|
if (this.response.finished) {
|
||||||
|
|
||||||
// TODO: refactor - it is necessary for mobile
|
// TODO: refactor - it is necessary for mobile
|
||||||
@ -800,6 +809,7 @@ const GameType = Object.freeze({'SINGLE': 0, 'MULTI': 1, 'CHALLENGE': 2});
|
|||||||
}
|
}
|
||||||
Game.scoreSum += round.result.score;
|
Game.scoreSum += round.result.score;
|
||||||
|
|
||||||
|
if(round.allResults !== undefined) {
|
||||||
for (var j = 0; j < round.allResults.length; ++j) {
|
for (var j = 0; j < round.allResults.length; ++j) {
|
||||||
var result = round.allResults[j];
|
var result = round.allResults[j];
|
||||||
if (result.guessPosition) {
|
if (result.guessPosition) {
|
||||||
@ -807,6 +817,7 @@ const GameType = Object.freeze({'SINGLE': 0, 'MULTI': 1, 'CHALLENGE': 2});
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
var distanceInfo = document.getElementById('distanceInfo');
|
var distanceInfo = document.getElementById('distanceInfo');
|
||||||
|
@ -6,7 +6,7 @@ use MapGuesser\Util\Geo\Position;
|
|||||||
use MapGuesser\Response\JsonContent;
|
use MapGuesser\Response\JsonContent;
|
||||||
use MapGuesser\Interfaces\Response\IContent;
|
use MapGuesser\Interfaces\Response\IContent;
|
||||||
use MapGuesser\Multi\MultiConnector;
|
use MapGuesser\Multi\MultiConnector;
|
||||||
use MapGuesser\PersistentData\Model\UserInChallenge;
|
use MapGuesser\PersistentData\Model\Challenge;
|
||||||
use MapGuesser\PersistentData\PersistentDataManager;
|
use MapGuesser\PersistentData\PersistentDataManager;
|
||||||
use MapGuesser\PersistentData\Model\Guess;
|
use MapGuesser\PersistentData\Model\Guess;
|
||||||
use MapGuesser\PersistentData\Model\UserPlayedPlace;
|
use MapGuesser\PersistentData\Model\UserPlayedPlace;
|
||||||
@ -141,6 +141,47 @@ class GameFlowController
|
|||||||
return new JsonContent(['ok' => true]);
|
return new JsonContent(['ok' => true]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private function prepareChallengeResponse(int $userId, Challenge $challenge, int $currentRound): array
|
||||||
|
{
|
||||||
|
$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()
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
$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()]
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
return $response;
|
||||||
|
}
|
||||||
|
|
||||||
public function challengeInitialData(): IContent
|
public function challengeInitialData(): IContent
|
||||||
{
|
{
|
||||||
$session = $this->request->session();
|
$session = $this->request->session();
|
||||||
@ -154,52 +195,8 @@ class GameFlowController
|
|||||||
|
|
||||||
$challenge = $userInChallenge->getChallenge();
|
$challenge = $userInChallenge->getChallenge();
|
||||||
$currentRound = $userInChallenge->getCurrentRound();
|
$currentRound = $userInChallenge->getCurrentRound();
|
||||||
$currentPlace = $this->placeRepository->getByRoundInChallenge($challenge, $currentRound);
|
|
||||||
|
|
||||||
$response = [];
|
$response = $this->prepareChallengeResponse($userId, $challenge, $currentRound);
|
||||||
|
|
||||||
$response['history'] = [];
|
|
||||||
|
|
||||||
$allGuessesInChallenge = iterator_to_array($this->guessRepository->getAllInChallenge($challenge));
|
|
||||||
$guessesByUser = iterator_to_array($this->guessRepository->getAllInChallengeByUser($userId, $challenge));
|
|
||||||
$places = iterator_to_array($this->placeRepository->getAllInChallenge($challenge));
|
|
||||||
|
|
||||||
|
|
||||||
for($i = 0; $i < $currentRound; ++$i) {
|
|
||||||
|
|
||||||
$response['history'][] = [
|
|
||||||
'position' => $places[$i]->getPosition()->toArray(),
|
|
||||||
'result' => [
|
|
||||||
'guessPosition' => $guessesByUser[$i]->getPosition()->toArray(),
|
|
||||||
'distance' => $guessesByUser[$i]->getDistance(),
|
|
||||||
'score' => $guessesByUser[$i]->getScore()
|
|
||||||
],
|
|
||||||
'allResults' => []
|
|
||||||
];
|
|
||||||
|
|
||||||
foreach($this->guessRepository->getAllInChallengeByRound($i, $challenge) as $guess) {
|
|
||||||
if($guess->getUserId() != $userId) {
|
|
||||||
$user = $this->userRepository->getByGuess($guess);
|
|
||||||
|
|
||||||
$response['history'][$i]['allResults'][] = [
|
|
||||||
'userName' => $user->getDisplayName(),
|
|
||||||
'guessPosition' => $guess->getPosition()->toArray(),
|
|
||||||
'distance' => $guess->getDistance(),
|
|
||||||
'score' => $guess->getScore()
|
|
||||||
];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if(!isset($currentPlace)) { // game finished
|
|
||||||
$response['finished'] = true;
|
|
||||||
|
|
||||||
} else { // continue game
|
|
||||||
$response['place'] = [
|
|
||||||
'panoId' => $currentPlace->getPanoIdCached(),
|
|
||||||
'pov' => [$currentPlace->getPov()->toArray()]
|
|
||||||
];
|
|
||||||
}
|
|
||||||
|
|
||||||
return new JsonContent($response);
|
return new JsonContent($response);
|
||||||
}
|
}
|
||||||
@ -317,21 +314,10 @@ class GameFlowController
|
|||||||
|
|
||||||
$challenge = $userInChallenge->getChallenge();
|
$challenge = $userInChallenge->getChallenge();
|
||||||
$currentRound = $userInChallenge->getCurrentRound();
|
$currentRound = $userInChallenge->getCurrentRound();
|
||||||
$currentPlace = $this->placeRepository->getByRoundInChallenge($challenge, $currentRound);
|
$currentPlaceInChallenge = $this->placeInChallengeRepository->getByRoundInChallenge($currentRound, $challenge);
|
||||||
$map = $this->mapRepository->getByPlace($currentPlace);
|
$currentPlace = $currentPlaceInChallenge->getPlace();
|
||||||
$placeInChallenge = $this->placeInChallengeRepository->getByPlaceAndChallenge($currentPlace, $challenge);
|
$map = $currentPlace->getMap();
|
||||||
|
|
||||||
// fill in all other results for the round
|
|
||||||
$response['allResults'] = [];
|
|
||||||
foreach($this->guessRepository->getAllInChallengeByRound($currentRound, $challenge) as $guess) {
|
|
||||||
$user = $this->userRepository->getByGuess($guess);
|
|
||||||
$response['allResults'][] = [
|
|
||||||
'userName' => $user->getDisplayName(),
|
|
||||||
'guessPosition' => $guess->getPosition()->toArray(),
|
|
||||||
'distance' => $guess->getDistance(),
|
|
||||||
'score' => $guess->getScore()
|
|
||||||
];
|
|
||||||
}
|
|
||||||
|
|
||||||
$guessPosition = new Position((float) $this->request->post('lat'), (float) $this->request->post('lng'));
|
$guessPosition = new Position((float) $this->request->post('lat'), (float) $this->request->post('lng'));
|
||||||
$result = $this->evaluateGuess($currentPlace->getPosition(), $guessPosition, $map->getArea());
|
$result = $this->evaluateGuess($currentPlace->getPosition(), $guessPosition, $map->getArea());
|
||||||
@ -339,18 +325,13 @@ class GameFlowController
|
|||||||
// save guess
|
// save guess
|
||||||
$guess = new Guess();
|
$guess = new Guess();
|
||||||
$guess->setUserId($userId);
|
$guess->setUserId($userId);
|
||||||
$guess->setPlaceInChallenge($placeInChallenge);
|
$guess->setPlaceInChallenge($currentPlaceInChallenge);
|
||||||
$guess->setPosition($guessPosition);
|
$guess->setPosition($guessPosition);
|
||||||
$guess->setDistance($result['distance']);
|
$guess->setDistance($result['distance']);
|
||||||
$guess->setScore($result['score']);
|
$guess->setScore($result['score']);
|
||||||
$this->pdm->saveToDb($guess);
|
$this->pdm->saveToDb($guess);
|
||||||
|
|
||||||
// $response = [
|
|
||||||
// 'position' => $currentPlace->getPosition()->toArray(),
|
|
||||||
// 'result' => $result
|
|
||||||
// ];
|
|
||||||
$response['position'] = $currentPlace->getPosition()->toArray();
|
|
||||||
$response['result'] = $result;
|
|
||||||
|
|
||||||
// update round
|
// update round
|
||||||
$nextRound = $currentRound + 1;
|
$nextRound = $currentRound + 1;
|
||||||
@ -358,47 +339,18 @@ class GameFlowController
|
|||||||
$userInChallenge->setCurrentRound($nextRound);
|
$userInChallenge->setCurrentRound($nextRound);
|
||||||
$this->pdm->saveToDb($userInChallenge);
|
$this->pdm->saveToDb($userInChallenge);
|
||||||
|
|
||||||
if ($nextRound < static::NUMBER_OF_ROUNDS) {
|
|
||||||
|
|
||||||
$nextPlace = $this->placeRepository->getByRoundInChallenge($challenge, $nextRound);
|
// creating response
|
||||||
|
$response = $this->prepareChallengeResponse($userId, $challenge, $nextRound);
|
||||||
|
|
||||||
$response['place'] = [
|
$response['position'] = $currentPlace->getPosition()->toArray();
|
||||||
'panoId' => $nextPlace->getPanoIdCached(),
|
$response['result'] = $result;
|
||||||
'pov' => $nextPlace->getPov()->toArray()
|
|
||||||
];
|
|
||||||
|
|
||||||
} else {
|
if(isset($response['history'][$currentRound]['allResults'])) {
|
||||||
|
$response['allResults'] = $response['history'][$currentRound]['allResults'];
|
||||||
$guessesByUser = iterator_to_array($this->guessRepository->getAllInChallengeByUser($userId, $challenge));
|
|
||||||
$places = iterator_to_array($this->placeRepository->getAllInChallenge($challenge));
|
|
||||||
|
|
||||||
for($i = 0; $i < $nextRound; ++$i) {
|
|
||||||
|
|
||||||
$response['history'][] = [
|
|
||||||
'position' => $places[$i]->getPosition()->toArray(),
|
|
||||||
'result' => [
|
|
||||||
'guessPosition' => $guessesByUser[$i]->getPosition()->toArray(),
|
|
||||||
'distance' => $guessesByUser[$i]->getDistance(),
|
|
||||||
'score' => $guessesByUser[$i]->getScore()
|
|
||||||
],
|
|
||||||
'allResults' => []
|
|
||||||
];
|
|
||||||
|
|
||||||
foreach($this->guessRepository->getAllInChallengeByRound($i, $challenge) as $guess) {
|
|
||||||
if($guess->getUserId() != $userId) {
|
|
||||||
$user = $this->userRepository->getByGuess($guess);
|
|
||||||
|
|
||||||
$response['history'][$i]['allResults'][] = [
|
|
||||||
'userName' => $user->getDisplayName(),
|
|
||||||
'guessPosition' => $guess->getPosition()->toArray(),
|
|
||||||
'distance' => $guess->getDistance(),
|
|
||||||
'score' => $guess->getScore()
|
|
||||||
];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
$this->saveVisit($currentPlace->getId());
|
$this->saveVisit($currentPlace->getId());
|
||||||
|
|
||||||
|
@ -63,10 +63,10 @@ class GuessRepository
|
|||||||
public function getAllInChallengeByRound(int $round, Challenge $challenge): Generator
|
public function getAllInChallengeByRound(int $round, Challenge $challenge): Generator
|
||||||
{
|
{
|
||||||
$select = new Select(\Container::$dbConnection);
|
$select = new Select(\Container::$dbConnection);
|
||||||
$select->innerJoin('place_in_challenge', ['guesses', 'place_in_challenge_id'], '=', ['place_in_challenge', 'id']);
|
// $select->innerJoin('place_in_challenge', ['guesses', 'place_in_challenge_id'], '=', ['place_in_challenge', 'id']);
|
||||||
$select->where('challenge_id', '=', $challenge->getId());
|
$select->where('challenge_id', '=', $challenge->getId());
|
||||||
$select->where('round', '=', $round);
|
$select->where('round', '=', $round);
|
||||||
|
|
||||||
yield from $this->pdm->selectMultipleFromDb($select, Guess::class);
|
yield from $this->pdm->selectMultipleFromDb($select, Guess::class, true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -40,4 +40,14 @@ class PlaceInChallengeRepository
|
|||||||
|
|
||||||
return $this->pdm->selectFromDb($select, PlaceInChallenge::class);
|
return $this->pdm->selectFromDb($select, PlaceInChallenge::class);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user