MAPG-235 new challenge can be created and is prepared
This commit is contained in:
1 parent
99244e93d2
commit
d7147b30d6
14 files changed
+289
-17
No files matched your search
@@ -8,14 +8,22 @@ use MapGuesser\Response\JsonContent;
|
||||
use MapGuesser\Interfaces\Response\IContent;
|
||||
use MapGuesser\Interfaces\Response\IRedirect;
|
||||
use MapGuesser\Multi\MultiConnector;
|
||||
use MapGuesser\PersistentData\Model\Challenge;
|
||||
use MapGuesser\PersistentData\Model\MultiRoom;
|
||||
use MapGuesser\PersistentData\Model\PlaceInChallenge;
|
||||
use MapGuesser\PersistentData\Model\UserInChallenge;
|
||||
use MapGuesser\PersistentData\PersistentDataManager;
|
||||
use MapGuesser\Repository\ChallengeRepository;
|
||||
use MapGuesser\Repository\MapRepository;
|
||||
use MapGuesser\Repository\MultiRoomRepository;
|
||||
use MapGuesser\Repository\PlaceRepository;
|
||||
use MapGuesser\Repository\UserInChallengeRepository;
|
||||
use MapGuesser\Response\Redirect;
|
||||
|
||||
class GameController
|
||||
{
|
||||
const NUMBER_OF_ROUNDS = 5;
|
||||
|
||||
private IRequest $request;
|
||||
|
||||
private PersistentDataManager $pdm;
|
||||
@@ -26,6 +34,12 @@ class GameController
|
||||
|
||||
private MapRepository $mapRepository;
|
||||
|
||||
private PlaceRepository $placeRepository;
|
||||
|
||||
private ChallengeRepository $challengeRepository;
|
||||
|
||||
private UserInChallengeRepository $userInChallengeRepository;
|
||||
|
||||
public function __construct(IRequest $request)
|
||||
{
|
||||
$this->request = $request;
|
||||
@@ -33,6 +47,9 @@ class GameController
|
||||
$this->multiConnector = new MultiConnector();
|
||||
$this->multiRoomRepository = new MultiRoomRepository();
|
||||
$this->mapRepository = new MapRepository();
|
||||
$this->placeRepository = new PlaceRepository();
|
||||
$this->challengeRepository = new ChallengeRepository();
|
||||
$this->userInChallengeRepository = new UserInChallengeRepository();
|
||||
}
|
||||
|
||||
public function getGame(): IContent
|
||||
@@ -79,6 +96,69 @@ class GameController
|
||||
return new HtmlContent('game', ['roomId' => $roomId]);
|
||||
}
|
||||
|
||||
public function getChallenge(): IContent
|
||||
{
|
||||
$challengeToken = $this->request->query('challengeToken');
|
||||
|
||||
return new HtmlContent('game', ['challengeToken' => $challengeToken]);
|
||||
}
|
||||
|
||||
public function createNewChallenge(): IContent
|
||||
{
|
||||
// create Challenge
|
||||
$challengeToken = rand();
|
||||
|
||||
$challenge = new Challenge();
|
||||
$challenge->setToken($challengeToken);
|
||||
$challenge->setCreatedDate(new DateTime());
|
||||
|
||||
if($this->request->post('timerEnabled') !== null && $this->request->post('timeLimit') !== null) {
|
||||
$challenge->setTimerSec($this->request->post('timeLimit'));
|
||||
}
|
||||
if($this->request->post('noMove') !== null) {
|
||||
$challenge->setNoMove(true);
|
||||
}
|
||||
if($this->request->post('noPan') !== null) {
|
||||
$challenge->setNoPan(true);
|
||||
}
|
||||
if($this->request->post('noZoom') !== null) {
|
||||
$challenge->setNoZoom(true);
|
||||
}
|
||||
|
||||
$this->pdm->saveToDb($challenge);
|
||||
|
||||
// save owner/creator
|
||||
|
||||
$session = $this->request->session();
|
||||
$userId = $session->get('userId');
|
||||
|
||||
$userInChallenge = new UserInChallenge();
|
||||
$userInChallenge->setUserId($userId);
|
||||
$userInChallenge->setChallenge($challenge);
|
||||
$userInChallenge->setTimeLeft($challenge->getTimerSec());
|
||||
$userInChallenge->setIsOwner(true);
|
||||
|
||||
$this->pdm->saveToDb($userInChallenge);
|
||||
|
||||
// select places
|
||||
|
||||
$mapId = (int) $this->request->post('mapId');
|
||||
$map = $this->mapRepository->getById($mapId);
|
||||
|
||||
$places = $this->placeRepository->getRandomNPlaces($mapId, static::NUMBER_OF_ROUNDS, $userId);
|
||||
|
||||
$order = 1;
|
||||
foreach ($places as $place) {
|
||||
$placeInChallenge = new PlaceInChallenge();
|
||||
$placeInChallenge->setPlace($place);
|
||||
$placeInChallenge->setChallenge($challenge);
|
||||
$placeInChallenge->setOrder($order++);
|
||||
$this->pdm->saveToDb($placeInChallenge);
|
||||
}
|
||||
|
||||
return new JsonContent(['challengeToken' => dechex($challengeToken)]);
|
||||
}
|
||||
|
||||
public function prepareGame(): IContent
|
||||
{
|
||||
$mapId = (int) $this->request->query('mapId');
|
||||
@@ -154,6 +234,28 @@ class GameController
|
||||
]);
|
||||
}
|
||||
|
||||
public function prepareChallenge(): IContent
|
||||
{
|
||||
$challengeToken_str = $this->request->query('challengeToken');
|
||||
$session = $this->request->session();
|
||||
$userId = $session->get('userId');
|
||||
$challenge = $this->challengeRepository->getByTokenStr($challengeToken_str);
|
||||
|
||||
if(!$this->userInChallengeRepository->isUserParticipatingInChallenge($userId, $challenge)) {
|
||||
$userInChallenge = new UserInChallenge();
|
||||
$userInChallenge->setUserId($userId);
|
||||
$userInChallenge->setTimeLeft($challenge->getTimerSec());
|
||||
}
|
||||
|
||||
$map = $this->mapRepository->getByChallenge($challenge);
|
||||
|
||||
return new JsonContent([
|
||||
'mapId' => $map->getId(),
|
||||
'mapName' => $map->getName(),
|
||||
'bounds' => $map->getBounds()->toArray()
|
||||
]);
|
||||
}
|
||||
|
||||
private function getMultiToken(string $roomId): string
|
||||
{
|
||||
$session = $this->request->session();
|
||||
|
||||
@@ -323,6 +323,10 @@ class Select
|
||||
$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);
|
||||
}
|
||||
|
||||
|
||||
@@ -4,7 +4,7 @@ use DateTime;
|
||||
|
||||
class Challenge extends Model
|
||||
{
|
||||
protected static string $table = 'challenge';
|
||||
protected static string $table = 'challenges';
|
||||
|
||||
protected static array $fields = ['token', 'timer_sec', 'no_move', 'no_pan', 'no_zoom', 'created'];
|
||||
|
||||
@@ -12,13 +12,13 @@ class Challenge extends Model
|
||||
|
||||
private int $token;
|
||||
|
||||
private int $timerSec;
|
||||
private ?int $timerSec = null;
|
||||
|
||||
private bool $noMove;
|
||||
private bool $noMove = false;
|
||||
|
||||
private bool $noPan;
|
||||
private bool $noPan = false;
|
||||
|
||||
private bool $noZoom;
|
||||
private bool $noZoom = false;
|
||||
|
||||
private DateTime $created;
|
||||
|
||||
@@ -27,9 +27,11 @@ class Challenge extends Model
|
||||
$this->token = $token;
|
||||
}
|
||||
|
||||
public function setTimerSec(int $timerSec): void
|
||||
public function setTimerSec(?int $timerSec): void
|
||||
{
|
||||
$this->timerSec = $timerSec;
|
||||
if(isset($timerSec)) {
|
||||
$this->timerSec = $timerSec;
|
||||
}
|
||||
}
|
||||
|
||||
public function setNoMove(bool $noMove): void
|
||||
@@ -62,7 +64,7 @@ class Challenge extends Model
|
||||
return $this->token;
|
||||
}
|
||||
|
||||
public function getTimerSec(): int
|
||||
public function getTimerSec(): ?int
|
||||
{
|
||||
return $this->timerSec;
|
||||
}
|
||||
|
||||
@@ -16,13 +16,13 @@ class UserInChallenge extends Model
|
||||
|
||||
private ?int $challengeId = null;
|
||||
|
||||
private int $round;
|
||||
private int $round = -1;
|
||||
|
||||
private int $score;
|
||||
private int $score = 0;
|
||||
|
||||
private int $timeLeft;
|
||||
private ?int $timeLeft = null;
|
||||
|
||||
private bool $isOwner;
|
||||
private bool $isOwner = false;
|
||||
|
||||
public function setUser(User $user): void
|
||||
{
|
||||
@@ -54,9 +54,11 @@ class UserInChallenge extends Model
|
||||
$this->score = $score;
|
||||
}
|
||||
|
||||
public function setTimeLeft(int $timeLeft): void
|
||||
public function setTimeLeft(?int $timeLeft): void
|
||||
{
|
||||
$this->timeLeft = $timeLeft;
|
||||
if(isset($timeLeft)) {
|
||||
$this->timeLeft = $timeLeft;
|
||||
}
|
||||
}
|
||||
|
||||
public function setIsOwner(bool $isOwner): void
|
||||
|
||||
@@ -28,6 +28,13 @@ class ChallengeRepository
|
||||
return $this->pdm->selectFromDb($select, Challenge::class);
|
||||
}
|
||||
|
||||
public function getByTokenStr(string $token_str): ?Challenge
|
||||
{
|
||||
$token = hexdec($token_str);
|
||||
|
||||
return $this->getByToken($token);
|
||||
}
|
||||
|
||||
public function getAllByParticipant(User $user): Generator
|
||||
{
|
||||
$select = new Select(\Container::$dbConnection);
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
<?php namespace MapGuesser\Repository;
|
||||
|
||||
use MapGuesser\Database\Query\Select;
|
||||
use MapGuesser\PersistentData\Model\Challenge;
|
||||
use MapGuesser\PersistentData\Model\Map;
|
||||
use MapGuesser\PersistentData\PersistentDataManager;
|
||||
|
||||
@@ -16,4 +18,15 @@ class MapRepository
|
||||
{
|
||||
return $this->pdm->selectFromDbById($mapId, Map::class);
|
||||
}
|
||||
|
||||
public function getByChallenge(Challenge $challenge): ?Map
|
||||
{
|
||||
$select = new Select(\Container::$dbConnection);
|
||||
$select->innerJoin('places', ['maps', 'id'], '=', ['places', 'map_id']);
|
||||
$select->innerJoin('place_in_challenge', ['places', 'id'], '=', ['place_in_challenge', 'place_id']);
|
||||
$select->where('challenge_id', '=', $challenge->getId());
|
||||
$select->limit(1);
|
||||
|
||||
return $this->pdm->selectFromDb($select, Map::class);
|
||||
}
|
||||
}
|
||||
@@ -31,4 +31,13 @@ class UserInChallengeRepository
|
||||
|
||||
yield from $this->pdm->selectMultipleFromDb($select, UserInChallenge::class);
|
||||
}
|
||||
|
||||
public function isUserParticipatingInChallenge(int $userId, Challenge $challenge): bool
|
||||
{
|
||||
$select = new Select(\Container::$dbConnection, 'user_in_challenge');
|
||||
$select->where('user_id', '=', $userId);
|
||||
$select->where('challenge_id', '=', $challenge->getId());
|
||||
|
||||
return $select->count();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user