adapt Container usage to new soko-web
This commit is contained in:
23 files changed
+248
-397
No files matched your search
@@ -2,12 +2,10 @@
|
||||
|
||||
use DateTime;
|
||||
use SokoWeb\Interfaces\Authentication\IAuthenticationRequired;
|
||||
use SokoWeb\Interfaces\Request\IRequest;
|
||||
use MapGuesser\Util\Geo\Position;
|
||||
use SokoWeb\Response\JsonContent;
|
||||
use SokoWeb\Interfaces\Response\IContent;
|
||||
use MapGuesser\Multi\MultiConnector;
|
||||
use SokoWeb\PersistentData\PersistentDataManager;
|
||||
use MapGuesser\PersistentData\Model\Challenge;
|
||||
use MapGuesser\PersistentData\Model\Guess;
|
||||
use MapGuesser\PersistentData\Model\Map;
|
||||
@@ -27,10 +25,6 @@ class GameFlowController implements IAuthenticationRequired
|
||||
const NUMBER_OF_ROUNDS = 5;
|
||||
const MAX_SCORE = 1000;
|
||||
|
||||
private IRequest $request;
|
||||
|
||||
private PersistentDataManager $pdm;
|
||||
|
||||
private MultiConnector $multiConnector;
|
||||
|
||||
private MultiRoomRepository $multiRoomRepository;
|
||||
@@ -45,10 +39,8 @@ class GameFlowController implements IAuthenticationRequired
|
||||
|
||||
private GuessRepository $guessRepository;
|
||||
|
||||
public function __construct(IRequest $request)
|
||||
public function __construct()
|
||||
{
|
||||
$this->request = $request;
|
||||
$this->pdm = new PersistentDataManager();
|
||||
$this->multiConnector = new MultiConnector();
|
||||
$this->multiRoomRepository = new MultiRoomRepository();
|
||||
$this->placeRepository = new PlaceRepository();
|
||||
@@ -65,8 +57,8 @@ class GameFlowController implements IAuthenticationRequired
|
||||
|
||||
public function initialData(): IContent
|
||||
{
|
||||
$mapId = (int) $this->request->query('mapId');
|
||||
$session = $this->request->session();
|
||||
$mapId = (int) \Container::$request->query('mapId');
|
||||
$session = \Container::$request->session();
|
||||
|
||||
if (!($state = $session->get('state')) || $state['mapId'] !== $mapId) {
|
||||
return new JsonContent(['error' => 'no_session_found']);
|
||||
@@ -103,8 +95,8 @@ class GameFlowController implements IAuthenticationRequired
|
||||
|
||||
public function multiInitialData(): IContent
|
||||
{
|
||||
$roomId = $this->request->query('roomId');
|
||||
$session = $this->request->session();
|
||||
$roomId = \Container::$request->query('roomId');
|
||||
$session = \Container::$request->session();
|
||||
|
||||
if (!($multiState = $session->get('multiState')) || $multiState['roomId'] !== $roomId) {
|
||||
return new JsonContent(['error' => 'no_session_found']);
|
||||
@@ -122,7 +114,7 @@ class GameFlowController implements IAuthenticationRequired
|
||||
$this->startNewGame($state, $state['mapId']);
|
||||
$room->setStateArray($state);
|
||||
$room->setUpdatedDate(new DateTime());
|
||||
$this->pdm->saveToDb($room);
|
||||
\Container::$persistentDataManager->saveToDb($room);
|
||||
}
|
||||
|
||||
$places = [];
|
||||
@@ -221,9 +213,9 @@ class GameFlowController implements IAuthenticationRequired
|
||||
|
||||
public function challengeInitialData(): IContent
|
||||
{
|
||||
$session = $this->request->session();
|
||||
$session = \Container::$request->session();
|
||||
$userId = $session->get('userId');
|
||||
$challengeToken_str = $this->request->query('challengeToken');
|
||||
$challengeToken_str = \Container::$request->query('challengeToken');
|
||||
$userInChallenge = $this->userInChallengeRepository->getByUserIdAndToken($userId, $challengeToken_str, [Challenge::class]);
|
||||
|
||||
if (!isset($userInChallenge)) {
|
||||
@@ -245,15 +237,15 @@ class GameFlowController implements IAuthenticationRequired
|
||||
|
||||
public function guess(): IContent
|
||||
{
|
||||
$mapId = (int) $this->request->query('mapId');
|
||||
$session = $this->request->session();
|
||||
$mapId = (int) \Container::$request->query('mapId');
|
||||
$session = \Container::$request->session();
|
||||
|
||||
if (!($state = $session->get('state')) || $state['mapId'] !== $mapId) {
|
||||
return new JsonContent(['error' => 'no_session_found']);
|
||||
}
|
||||
|
||||
$last = $state['rounds'][$state['currentRound']];
|
||||
$guessPosition = new Position((float) $this->request->post('lat'), (float) $this->request->post('lng'));
|
||||
$guessPosition = new Position((float) \Container::$request->post('lat'), (float) \Container::$request->post('lng'));
|
||||
$result = $this->evaluateGuess($last['position'], $guessPosition, $state['area']);
|
||||
|
||||
$last['guessPosition'] = $guessPosition;
|
||||
@@ -286,7 +278,7 @@ class GameFlowController implements IAuthenticationRequired
|
||||
// save the selected place for the round in UserPlayedPlace
|
||||
private function saveVisit($placeId): void
|
||||
{
|
||||
$session = $this->request->session();
|
||||
$session = \Container::$request->session();
|
||||
$userId = $session->get('userId');
|
||||
|
||||
if (isset($userId)) {
|
||||
@@ -299,14 +291,14 @@ class GameFlowController implements IAuthenticationRequired
|
||||
$userPlayedPlace->incrementOccurrences();
|
||||
}
|
||||
$userPlayedPlace->setLastTimeDate(new DateTime());
|
||||
$this->pdm->saveToDb($userPlayedPlace);
|
||||
\Container::$persistentDataManager->saveToDb($userPlayedPlace);
|
||||
}
|
||||
}
|
||||
|
||||
public function multiGuess(): IContent
|
||||
{
|
||||
$roomId = $this->request->query('roomId');
|
||||
$session = $this->request->session();
|
||||
$roomId = \Container::$request->query('roomId');
|
||||
$session = \Container::$request->session();
|
||||
|
||||
if (!($multiState = $session->get('multiState')) || $multiState['roomId'] !== $roomId) {
|
||||
return new JsonContent(['error' => 'no_session_found']);
|
||||
@@ -316,7 +308,7 @@ class GameFlowController implements IAuthenticationRequired
|
||||
$state = $room->getStateArray();
|
||||
|
||||
$last = $state['rounds'][$state['currentRound']];
|
||||
$guessPosition = new Position((float) $this->request->post('lat'), (float) $this->request->post('lng'));
|
||||
$guessPosition = new Position((float) \Container::$request->post('lat'), (float) \Container::$request->post('lng'));
|
||||
$result = $this->evaluateGuess($last['position'], $guessPosition, $state['area']);
|
||||
|
||||
$responseFromMulti = $this->multiConnector->sendMessage('guess', [
|
||||
@@ -342,9 +334,9 @@ class GameFlowController implements IAuthenticationRequired
|
||||
|
||||
public function challengeGuess(): IContent
|
||||
{
|
||||
$session = $this->request->session();
|
||||
$session = \Container::$request->session();
|
||||
$userId = $session->get('userId');
|
||||
$challengeToken_str = $this->request->query('challengeToken');
|
||||
$challengeToken_str = \Container::$request->query('challengeToken');
|
||||
$userInChallenge = $this->userInChallengeRepository->getByUserIdAndToken($userId, $challengeToken_str, [Challenge::class]);
|
||||
|
||||
if (!isset($userInChallenge)) {
|
||||
@@ -362,8 +354,8 @@ class GameFlowController implements IAuthenticationRequired
|
||||
$response = $this->prepareChallengeResponse($userId, $challenge, $nextRound);
|
||||
$response['position'] = $currentPlace->getPosition()->toArray();
|
||||
|
||||
if ($this->request->post('lat') && $this->request->post('lng')) {
|
||||
$guessPosition = new Position((float) $this->request->post('lat'), (float) $this->request->post('lng'));
|
||||
if (\Container::$request->post('lat') && \Container::$request->post('lng')) {
|
||||
$guessPosition = new Position((float) \Container::$request->post('lat'), (float) \Container::$request->post('lng'));
|
||||
$result = $this->evaluateGuess($currentPlace->getPosition(), $guessPosition, $map->getArea());
|
||||
|
||||
// save guess
|
||||
@@ -373,7 +365,7 @@ class GameFlowController implements IAuthenticationRequired
|
||||
$guess->setPosition($guessPosition);
|
||||
$guess->setDistance($result['distance']);
|
||||
$guess->setScore($result['score']);
|
||||
$this->pdm->saveToDb($guess);
|
||||
\Container::$persistentDataManager->saveToDb($guess);
|
||||
|
||||
$response['result'] = $result;
|
||||
|
||||
@@ -384,11 +376,11 @@ class GameFlowController implements IAuthenticationRequired
|
||||
|
||||
// save user relevant state of challenge
|
||||
$userInChallenge->setCurrentRound($nextRound);
|
||||
$timeLeft = $this->request->post('timeLeft');
|
||||
$timeLeft = \Container::$request->post('timeLeft');
|
||||
if (isset($timeLeft)) {
|
||||
$userInChallenge->setTimeLeft(intval($timeLeft));
|
||||
}
|
||||
$this->pdm->saveToDb($userInChallenge);
|
||||
\Container::$persistentDataManager->saveToDb($userInChallenge);
|
||||
|
||||
if ($challenge->getTimeLimitType() === 'game' && isset($timeLeft)) {
|
||||
$timeLimit = max(10, intval($timeLeft));
|
||||
@@ -406,8 +398,8 @@ class GameFlowController implements IAuthenticationRequired
|
||||
|
||||
public function multiNextRound(): IContent
|
||||
{
|
||||
$roomId = $this->request->query('roomId');
|
||||
$session = $this->request->session();
|
||||
$roomId = \Container::$request->query('roomId');
|
||||
$session = \Container::$request->session();
|
||||
|
||||
if (!($multiState = $session->get('multiState')) || $multiState['roomId'] !== $roomId) {
|
||||
return new JsonContent(['error' => 'no_session_found']);
|
||||
@@ -428,7 +420,7 @@ class GameFlowController implements IAuthenticationRequired
|
||||
|
||||
$room->setStateArray($state);
|
||||
$room->setUpdatedDate(new DateTime());
|
||||
$this->pdm->saveToDb($room);
|
||||
\Container::$persistentDataManager->saveToDb($room);
|
||||
|
||||
return new JsonContent(['ok' => true]);
|
||||
}
|
||||
@@ -443,7 +435,7 @@ class GameFlowController implements IAuthenticationRequired
|
||||
|
||||
private function startNewGame(array &$state, int $mapId): void
|
||||
{
|
||||
$session = $this->request->session();
|
||||
$session = \Container::$request->session();
|
||||
$userId = $session->get('userId');
|
||||
|
||||
$places = $this->placeRepository->getRandomNPlaces($mapId, static::NUMBER_OF_ROUNDS, $userId);
|
||||
|
||||
Reference in new issue
Block a user