MAPG-235 guesses of other players in the round are sent after guess

This commit is contained in:
Balázs Vigh 2021-05-13 13:06:24 +02:00
parent 93f8fc3f34
commit e7869d67f7
7 changed files with 61 additions and 14 deletions

View File

@ -325,6 +325,8 @@ const GameType = Object.freeze({'SINGLE': 0, 'MULTI': 1, 'CHALLENGE': 2});
}); });
Game.showSummary(); Game.showSummary();
document.getElementById('continueButton').style.display = 'none';
return; return;
} }

View File

@ -18,6 +18,7 @@ use MapGuesser\Repository\PlaceInChallengeRepository;
use MapGuesser\Repository\PlaceRepository; use MapGuesser\Repository\PlaceRepository;
use MapGuesser\Repository\UserInChallengeRepository; use MapGuesser\Repository\UserInChallengeRepository;
use MapGuesser\Repository\UserPlayedPlaceRepository; use MapGuesser\Repository\UserPlayedPlaceRepository;
use MapGuesser\Repository\UserRepository;
class GameFlowController class GameFlowController
{ {
@ -36,6 +37,8 @@ class GameFlowController
private MapRepository $mapRepository; private MapRepository $mapRepository;
private UserRepository $userRepository;
private UserPlayedPlaceRepository $userPlayedPlaceRepository; private UserPlayedPlaceRepository $userPlayedPlaceRepository;
private ChallengeRepository $challengeRepository; private ChallengeRepository $challengeRepository;
@ -54,6 +57,7 @@ class GameFlowController
$this->multiRoomRepository = new MultiRoomRepository(); $this->multiRoomRepository = new MultiRoomRepository();
$this->placeRepository = new PlaceRepository(); $this->placeRepository = new PlaceRepository();
$this->mapRepository = new MapRepository(); $this->mapRepository = new MapRepository();
$this->userRepository = new UserRepository();
$this->userPlayedPlaceRepository = new UserPlayedPlaceRepository(); $this->userPlayedPlaceRepository = new UserPlayedPlaceRepository();
$this->challengeRepository = new ChallengeRepository(); $this->challengeRepository = new ChallengeRepository();
$this->userInChallengeRepository = new UserInChallengeRepository(); $this->userInChallengeRepository = new UserInChallengeRepository();
@ -156,7 +160,7 @@ class GameFlowController
$response['history'] = []; $response['history'] = [];
$guesses = iterator_to_array($this->guessRepository->getAllInChallenge($userId, $challenge)); $guesses = iterator_to_array($this->guessRepository->getAllInChallengeByUser($userId, $challenge));
$places = iterator_to_array($this->placeRepository->getAllInChallenge($challenge)); $places = iterator_to_array($this->placeRepository->getAllInChallenge($challenge));
for($i = 0; $i < $currentRound; ++$i) for($i = 0; $i < $currentRound; ++$i)
@ -298,6 +302,18 @@ class GameFlowController
$map = $this->mapRepository->getByPlace($currentPlace); $map = $this->mapRepository->getByPlace($currentPlace);
$placeInChallenge = $this->placeInChallengeRepository->getByPlaceAndChallenge($currentPlace, $challenge); $placeInChallenge = $this->placeInChallengeRepository->getByPlaceAndChallenge($currentPlace, $challenge);
// 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());
@ -310,10 +326,12 @@ class GameFlowController
$guess->setScore($result['score']); $guess->setScore($result['score']);
$this->pdm->saveToDb($guess); $this->pdm->saveToDb($guess);
$response = [ // $response = [
'position' => $currentPlace->getPosition()->toArray(), // 'position' => $currentPlace->getPosition()->toArray(),
'result' => $result // 'result' => $result
]; // ];
$response['position'] = $currentPlace->getPosition()->toArray();
$response['result'] = $result;
// update round // update round
$nextRound = $currentRound + 1; $nextRound = $currentRound + 1;
@ -331,6 +349,9 @@ class GameFlowController
]; ];
} }
$this->saveVisit($currentPlace->getId()); $this->saveVisit($currentPlace->getId());
return new JsonContent($response); return new JsonContent($response);

View File

@ -323,10 +323,6 @@ class Select
$value = $this->generateColumn($value); $value = $this->generateColumn($value);
}); });
if(count($columns) > 0 && $columns[0] == Utils::backtick('id')) {
$columns[0] = Utils::backtick($this->table) . '.' . Utils::backtick('id');
}
return implode(',', $columns); return implode(',', $columns);
} }

View File

@ -133,6 +133,10 @@ class PersistentDataManager
$table = call_user_func([$type, 'getTable']); $table = call_user_func([$type, 'getTable']);
$fields = call_user_func([$type, 'getFields']); $fields = call_user_func([$type, 'getFields']);
array_walk($fields, function (&$value, $key, $table) {
$value = [$table, $value];
}, $table);
$select->from($table); $select->from($table);
//TODO: only with some relations? //TODO: only with some relations?

View File

@ -39,7 +39,7 @@ class GuessRepository
return $this->pdm->selectFromDb($select, Guess::class); return $this->pdm->selectFromDb($select, Guess::class);
} }
public function getAllInChallenge(int $userId, Challenge $challenge): Generator public function getAllInChallengeByUser(int $userId, Challenge $challenge): Generator
{ {
$select = new Select(\Container::$dbConnection); $select = new Select(\Container::$dbConnection);
$select->innerJoin('place_in_challenge', ['place_in_challenge', 'id'], '=', ['guesses', 'place_in_challenge_id']); $select->innerJoin('place_in_challenge', ['place_in_challenge', 'id'], '=', ['guesses', 'place_in_challenge_id']);
@ -49,4 +49,25 @@ class GuessRepository
yield from $this->pdm->selectMultipleFromDb($select, Guess::class); yield from $this->pdm->selectMultipleFromDb($select, Guess::class);
} }
public function getAllInChallenge(Challenge $challenge): Generator
{
$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());
yield from $this->pdm->selectMultipleFromDb($select, Guess::class);
}
public function getAllInChallengeByRound(int $round, Challenge $challenge): Generator
{
$order = $round + 1;
$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('order', '=', $order);
yield from $this->pdm->selectMultipleFromDb($select, Guess::class);
}
} }

View File

@ -22,10 +22,7 @@ class MapRepository
public function getByPlace(Place $place): ?Map public function getByPlace(Place $place): ?Map
{ {
$select = new Select(\Container::$dbConnection); return $this->getById($place->getMapId());
$select->where('id', '=', $place->getMapId());
return $this->pdm->selectFromDb($select, Map::class);
} }
public function getByChallenge(Challenge $challenge): ?Map public function getByChallenge(Challenge $challenge): ?Map

View File

@ -3,6 +3,7 @@
use DateTime; use DateTime;
use Generator; use Generator;
use MapGuesser\Database\Query\Select; use MapGuesser\Database\Query\Select;
use MapGuesser\PersistentData\Model\Guess;
use MapGuesser\PersistentData\Model\User; use MapGuesser\PersistentData\Model\User;
use MapGuesser\PersistentData\PersistentDataManager; use MapGuesser\PersistentData\PersistentDataManager;
@ -44,4 +45,9 @@ class UserRepository
yield from $this->pdm->selectMultipleFromDb($select, User::class); yield from $this->pdm->selectMultipleFromDb($select, User::class);
} }
public function getByGuess(Guess $guess): ?User
{
return $this->getById($guess->getUserId());
}
} }