From 1c52c2e0d8464d101fc07793004d833dfd8e78ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20Mi=C5=A1o=20=C4=8Cerve=C5=88=C3=A1k?= Date: Sat, 25 May 2024 21:39:08 +0200 Subject: [PATCH 1/2] remove gamephase calculator --- .../ApiResults/ApiResultsComponent.php | 5 ++- .../Countdown/CountdownComponent.php | 3 +- .../UpperHomeMap/UpperHomeMapComponent.php | 10 +----- .../UpperHomePrague/UpperHomePrague.php | 19 ++-------- .../UpperHomePrague/upperHomePrague.latte | 6 ++-- app/Models/Downloader/NetteDownloader.php | 30 +++++----------- app/Models/GamePhaseCalculator.php | 23 +++--------- .../NetteDownloader/ORM/Models/ModelEvent.php | 35 +++++++++++++------ .../NetteDownloader/ORM/Models/ModelGame.php | 21 +++++++++++ .../NetteDownloader/ORM/Models/ModelTeam.php | 4 +-- .../ORM/Services/ServiceEventList.php | 1 + app/Modules/Core/EventWebPresenter.php | 20 ++++++----- app/Modules/Dsef/Core/BasePresenter.php | 11 ++---- .../Dsef/DefaultModule/ArchivePresenter.php | 6 ++-- .../templates/Archive/default.latte | 4 +-- app/Modules/Fof/Core/BasePresenter.php | 5 +++ .../Fof/DefaultModule/ArchivePresenter.php | 2 +- app/Modules/Fol/Core/BasePresenter.php | 26 ++++++-------- .../Fol/DefaultModule/DefaultPresenter.php | 2 +- app/config/config.neon | 1 - 20 files changed, 111 insertions(+), 123 deletions(-) create mode 100644 app/Models/NetteDownloader/ORM/Models/ModelGame.php diff --git a/app/Components/ApiResults/ApiResultsComponent.php b/app/Components/ApiResults/ApiResultsComponent.php index f3e433c6..03952237 100644 --- a/app/Components/ApiResults/ApiResultsComponent.php +++ b/app/Components/ApiResults/ApiResultsComponent.php @@ -20,7 +20,10 @@ class ApiResultsComponent extends DIComponent private readonly Connector $gameServerApiConnector; private readonly FKSDBDownloader $downloader; - public function __construct(Container $container, private readonly int $eventId) + public function __construct( + Container $container, + private readonly int $eventId + ) { parent::__construct($container); } diff --git a/app/Components/Countdown/CountdownComponent.php b/app/Components/Countdown/CountdownComponent.php index b21e6d36..9ea6a3c5 100644 --- a/app/Components/Countdown/CountdownComponent.php +++ b/app/Components/Countdown/CountdownComponent.php @@ -15,7 +15,8 @@ public function __construct( Container $container, private readonly \DateTimeInterface $countdownTo, string $id = null - ) { + ) + { parent::__construct($container); $this->id = $id ?? uniqid(); } diff --git a/app/Components/UpperHomeMap/UpperHomeMapComponent.php b/app/Components/UpperHomeMap/UpperHomeMapComponent.php index 89220f01..3ead3472 100644 --- a/app/Components/UpperHomeMap/UpperHomeMapComponent.php +++ b/app/Components/UpperHomeMap/UpperHomeMapComponent.php @@ -6,15 +6,12 @@ use App\Components\Countdown\CountdownComponent; use App\Components\Map\MapComponent; -use App\Models\GamePhaseCalculator; use App\Models\NetteDownloader\ORM\Models\ModelEvent; use Fykosak\Utils\Components\DIComponent; use Nette\DI\Container; final class UpperHomeMapComponent extends DIComponent { - private readonly GamePhaseCalculator $gamePhaseCalculator; - public function __construct( Container $container, private readonly ModelEvent $event @@ -22,11 +19,6 @@ public function __construct( parent::__construct($container); } - public function inject(GamePhaseCalculator $calculator): void - { - $this->gamePhaseCalculator = $calculator; - } - /** * @throws \Throwable */ @@ -47,6 +39,6 @@ protected function createComponentMap(): MapComponent */ protected function createComponentCountdown(): CountdownComponent { - return new CountdownComponent($this->container, $this->gamePhaseCalculator->getGameBegin()); + return new CountdownComponent($this->container, $this->event->game->begin); } } diff --git a/app/Components/UpperHomePrague/UpperHomePrague.php b/app/Components/UpperHomePrague/UpperHomePrague.php index 102774a7..518691ea 100644 --- a/app/Components/UpperHomePrague/UpperHomePrague.php +++ b/app/Components/UpperHomePrague/UpperHomePrague.php @@ -5,7 +5,6 @@ namespace App\Components\UpperHomePrague; use App\Components\Countdown\CountdownComponent; -use App\Models\GamePhaseCalculator; use App\Models\NetteDownloader\ORM\Models\ModelEvent; use Fykosak\Utils\Components\DIComponent; use Fykosak\Utils\DateTime\Phase; @@ -13,8 +12,6 @@ final class UpperHomePrague extends DIComponent { - private readonly GamePhaseCalculator $gamePhaseCalculator; - public function __construct( Container $container, private readonly ModelEvent $event @@ -22,11 +19,6 @@ public function __construct( parent::__construct($container); } - public function injectGamePhaseCalculator(GamePhaseCalculator $gamePhaseCalculator): void - { - $this->gamePhaseCalculator = $gamePhaseCalculator; - } - /** * @throws \Throwable */ @@ -34,7 +26,6 @@ public function render(): void { $this->template->lang = $this->translator->lang; $this->template->event = $this->event; - $this->template->gamePhaseCalculator = $this->gamePhaseCalculator; $this->template->render(__DIR__ . DIRECTORY_SEPARATOR . 'upperHomePrague.latte'); } @@ -43,16 +34,12 @@ public function render(): void */ protected function createComponentCountdown(): CountdownComponent { - if ( - $this->gamePhaseCalculator->getFKSDBEvent() - ->getRegistrationPeriod() - ->is(Phase::before) - ) { + if ($this->event->getRegistrationPeriod()->is(Phase::before)) { return new CountdownComponent( $this->container, - $this->gamePhaseCalculator->getFKSDBEvent()->registrationBegin + $this->event->registrationBegin ); } - return new CountdownComponent($this->container, $this->gamePhaseCalculator->getGameBegin()); + return new CountdownComponent($this->container, $this->event->game->begin); } } diff --git a/app/Components/UpperHomePrague/upperHomePrague.latte b/app/Components/UpperHomePrague/upperHomePrague.latte index 02f80349..02799e06 100644 --- a/app/Components/UpperHomePrague/upperHomePrague.latte +++ b/app/Components/UpperHomePrague/upperHomePrague.latte @@ -1,5 +1,4 @@ {varType App\Models\NetteDownloader\ORM\Models\ModelEvent $event} -{varType App\Models\GamePhaseCalculator $gamePhaseCalculator} {varType App\Modules\Core\Language $lang} {block content} @@ -137,8 +136,9 @@ {elseif $event->getRegistrationPeriod()->is(Fykosak\Utils\DateTime\Phase::after)} {switch $lang->value} - {case cs} Registrace je již uzavřena. Soutěžíme {$gamePhaseCalculator->getGameBegin() |date:'j. n. G:i'}. - {default} Registration is already closed. The competition starts {$gamePhaseCalculator->getGameBegin() |date:'F j, G:i'} CET. + {case cs} Registrace je již uzavřena. Soutěžíme {$event->game->begin|date:'j. n. G:i'}. + {default} Registration is already closed. The competition starts {$event->game->begin|date:'F j, G:i'} + CET. {/switch} {elseif !$event->getRegistrationPeriod()->is(Fykosak\Utils\DateTime\Phase::before)} {switch $lang->value} diff --git a/app/Models/Downloader/NetteDownloader.php b/app/Models/Downloader/NetteDownloader.php index 1e9b036d..d4d34cbd 100644 --- a/app/Models/Downloader/NetteDownloader.php +++ b/app/Models/Downloader/NetteDownloader.php @@ -15,36 +15,22 @@ abstract class NetteDownloader use SmartObject; private Downloader $downloader; - private string $username; - private string $password; - private string $expiration; - private string $jsonApiUrl; private Cache $cache; public function __construct( string $jsonApiUrl, string $username, + #[\SensitiveParameter] string $password, - string $expiration, + private readonly string $expiration, Storage $storage ) { $this->cache = new Cache($storage, self::class); - $this->username = $username; - $this->password = $password; - $this->jsonApiUrl = $jsonApiUrl; - $this->expiration = $expiration; - } - - public function getDownloader(): Downloader - { - if (!isset($this->downloader)) { - $this->downloader = new Downloader( - $this->jsonApiUrl, - $this->username, - $this->password, - ); - } - return $this->downloader; + $this->downloader = new Downloader( + $jsonApiUrl, + $username, + $password, + ); } /** @@ -56,7 +42,7 @@ public function download(Request $request, ?string $explicitExpiration = null): $request->getCacheKey() . '-json', function (&$dependencies) use ($request, $explicitExpiration): array { $dependencies[Cache::EXPIRE] = $explicitExpiration ?? $this->expiration; - return $this->getDownloader()->download($request); + return $this->downloader->download($request); } ); } diff --git a/app/Models/GamePhaseCalculator.php b/app/Models/GamePhaseCalculator.php index 7302396c..2aff42b6 100644 --- a/app/Models/GamePhaseCalculator.php +++ b/app/Models/GamePhaseCalculator.php @@ -6,37 +6,24 @@ use App\Models\NetteDownloader\ORM\Models\ModelEvent; use App\Models\NetteDownloader\ORM\Services\ServiceEventList; -use Nette\DI\Container; use Nette\SmartObject; -use Throwable; +/** + * @deprecated + */ final class GamePhaseCalculator { use SmartObject; public function __construct( private readonly int $eventTypeId, - private readonly ServiceEventList $serviceEventList, - private readonly Container $container + private readonly ServiceEventList $serviceEventList ) { } - - /** - * @throws Throwable - */ - public function getGameBegin(): \DateTime - { - $time = new \DateTime($this->container->getParameters()['competitionBegin']); - $day = $this->getFKSDBEvent()->begin; - - $time->setDate((int)$day->format('Y'), (int)$day->format('m'), (int)$day->format('d')); - return $time; - } - /** * Returns newest FKSDB event. That means by creating a new one, the application automatically switches to the new * year. - * @throws Throwable + * @throws \Throwable */ public function getFKSDBEvent(): ?ModelEvent { diff --git a/app/Models/NetteDownloader/ORM/Models/ModelEvent.php b/app/Models/NetteDownloader/ORM/Models/ModelEvent.php index 090478e3..ec493534 100644 --- a/app/Models/NetteDownloader/ORM/Models/ModelEvent.php +++ b/app/Models/NetteDownloader/ORM/Models/ModelEvent.php @@ -8,15 +8,18 @@ class ModelEvent { - public int $eventId; - public int $eventTypeId; - public string $name; - public int $eventYear; - public int $year; - public \DateTimeImmutable $begin; - public \DateTimeImmutable $end; - public \DateTimeImmutable $registrationBegin; - public \DateTimeImmutable $registrationEnd; + public readonly int $eventId; + public readonly int $eventTypeId; + public readonly string $name; + public readonly int $eventYear; + public readonly int $year; + public readonly ?string $place; + public readonly ModelGame|null $game; + public readonly \DateTimeImmutable $begin; + public readonly \DateTimeImmutable $end; + public readonly \DateTimeImmutable $registrationBegin; + public readonly \DateTimeImmutable $registrationEnd; + public readonly Period $registration; // TODO magic? public function getRegistrationPeriod(): Period { @@ -28,9 +31,9 @@ public function getEventPeriod(): Period return new Period($this->begin, $this->end); } - public function getGamePeriod(): Period + public function getGamePeriod(): ?Period { - return $this->getEventPeriod(); // TODO!!!! + return $this->game?->getGamePeriod(); } public function getNearEventPeriod(): Period @@ -48,4 +51,14 @@ public function isLongAfterTheEvent(): bool $event = $this->end->add(new \DateInterval('P7D')); return new \DateTime() > $event; } + + public function getYear(): int + { + return (int)$this->begin->format('Y'); + } + + public function getMonth(): string + { + return $this->begin->format('m'); + } } diff --git a/app/Models/NetteDownloader/ORM/Models/ModelGame.php b/app/Models/NetteDownloader/ORM/Models/ModelGame.php new file mode 100644 index 00000000..c6c6cd45 --- /dev/null +++ b/app/Models/NetteDownloader/ORM/Models/ModelGame.php @@ -0,0 +1,21 @@ +begin, $this->end); + } +} diff --git a/app/Models/NetteDownloader/ORM/Models/ModelTeam.php b/app/Models/NetteDownloader/ORM/Models/ModelTeam.php index fe62381f..6b9b84c4 100644 --- a/app/Models/NetteDownloader/ORM/Models/ModelTeam.php +++ b/app/Models/NetteDownloader/ORM/Models/ModelTeam.php @@ -22,10 +22,10 @@ class ModelTeam /** * @var ModelMember[] */ - public $members; + public array $members; /** * @var ModelPerson[] */ - public $teachers; + public array $teachers; } diff --git a/app/Models/NetteDownloader/ORM/Services/ServiceEventList.php b/app/Models/NetteDownloader/ORM/Services/ServiceEventList.php index 8fedc36f..5ca33154 100644 --- a/app/Models/NetteDownloader/ORM/Services/ServiceEventList.php +++ b/app/Models/NetteDownloader/ORM/Services/ServiceEventList.php @@ -8,6 +8,7 @@ use App\Models\NetteDownloader\ORM\Models\ModelEvent; use Fykosak\FKSDBDownloaderCore\Requests\EventListRequest; use Nette\Caching\Storage; +use Tracy\Debugger; final class ServiceEventList extends AbstractJSONService { diff --git a/app/Modules/Core/EventWebPresenter.php b/app/Modules/Core/EventWebPresenter.php index 7e242df8..17861586 100644 --- a/app/Modules/Core/EventWebPresenter.php +++ b/app/Modules/Core/EventWebPresenter.php @@ -4,22 +4,22 @@ namespace App\Modules\Core; -use App\Models\GamePhaseCalculator; use App\Models\NetteDownloader\ORM\Models\ModelEvent; use App\Models\NetteDownloader\ORM\Services\DummyService; +use App\Models\NetteDownloader\ORM\Services\ServiceEventList; use Nette\Application\UI\Template; abstract class EventWebPresenter extends BasePresenter { - protected GamePhaseCalculator $gamePhaseCalculator; - protected DummyService $dummyService; + protected readonly DummyService $dummyService; + protected readonly ServiceEventList $serviceEventList; public function injectEventWebServices( - GamePhaseCalculator $calculator, - DummyService $dummyService + DummyService $dummyService, + ServiceEventList $serviceEventList ): void { - $this->gamePhaseCalculator = $calculator; $this->dummyService = $dummyService; + $this->serviceEventList = $serviceEventList; } /** @@ -28,7 +28,6 @@ public function injectEventWebServices( protected function createTemplate(): Template { $template = parent::createTemplate(); - $template->gamePhaseCalculator = $this->gamePhaseCalculator; $template->newestEvent = $this->getNewestEvent(); return $template; } @@ -40,8 +39,13 @@ protected function getNewestEvent(): ModelEvent { static $newestEvent; if (!isset($newestEvent)) { - $newestEvent = $this->gamePhaseCalculator->getFKSDBEvent(); + $newestEvent = $this->serviceEventList->getNewest($this->getEventIds()); } return $newestEvent; } + + /** + * @return int[] + */ + abstract protected function getEventIds(): array; } diff --git a/app/Modules/Dsef/Core/BasePresenter.php b/app/Modules/Dsef/Core/BasePresenter.php index f2514b35..2fdba4b0 100644 --- a/app/Modules/Dsef/Core/BasePresenter.php +++ b/app/Modules/Dsef/Core/BasePresenter.php @@ -13,19 +13,14 @@ abstract class BasePresenter extends EventWebPresenter { public const EVENT_IDS = [2, 14]; - public static function getEventYear(ModelEvent $event): string + protected function getEventIds(): array { - return $event->begin->format('Y'); - } - - public static function getEventMonth(ModelEvent $event): string - { - return $event->begin->format('m'); + return [2, 14]; } public static function getEventKey(ModelEvent $event): string { - return self::getEventYear($event) . '-' . self::getEventMonth($event); + return $event->getYear() . '-' . $event->getMonth(); } protected function localize(): void diff --git a/app/Modules/Dsef/DefaultModule/ArchivePresenter.php b/app/Modules/Dsef/DefaultModule/ArchivePresenter.php index f52b8f86..8f23a1b8 100644 --- a/app/Modules/Dsef/DefaultModule/ArchivePresenter.php +++ b/app/Modules/Dsef/DefaultModule/ArchivePresenter.php @@ -29,9 +29,9 @@ public function renderDefault(): void foreach ($events as $event) { $eventKeys[] = [ 'event' => $event, - 'year' => BasePresenter::getEventYear($event), - 'month' => BasePresenter::getEventMonth($event), - 'fykos-year' => $event->year, + 'year' => $event->getYear(), + 'month' => $event->getMonth(), + 'contestYear' => $event->year, ]; } diff --git a/app/Modules/Dsef/DefaultModule/templates/Archive/default.latte b/app/Modules/Dsef/DefaultModule/templates/Archive/default.latte index 59660a72..0bdac46b 100644 --- a/app/Modules/Dsef/DefaultModule/templates/Archive/default.latte +++ b/app/Modules/Dsef/DefaultModule/templates/Archive/default.latte @@ -33,9 +33,9 @@ - {if $control->getComponent('gallery')->hasPhotos('/photos/' . $eventKey['fykos-year'] . '/')} + {if $control->getComponent('gallery')->hasPhotos('/photos/' . $eventKey['contestYear'] . '/')}
- {control gallery:randomLine '/photos/' . $eventKey['fykos-year'] . '/'} + {control gallery:randomLine '/photos/' . $eventKey['contestYear'] . '/'}
{/if} diff --git a/app/Modules/Fof/Core/BasePresenter.php b/app/Modules/Fof/Core/BasePresenter.php index aafb28e8..ffee9657 100644 --- a/app/Modules/Fof/Core/BasePresenter.php +++ b/app/Modules/Fof/Core/BasePresenter.php @@ -13,4 +13,9 @@ public static function createEventKey(ModelEvent $event): string { return $event->begin->format('Y'); } + + protected function getEventIds(): array + { + return [1]; + } } diff --git a/app/Modules/Fof/DefaultModule/ArchivePresenter.php b/app/Modules/Fof/DefaultModule/ArchivePresenter.php index f325a7c4..424b32f8 100644 --- a/app/Modules/Fof/DefaultModule/ArchivePresenter.php +++ b/app/Modules/Fof/DefaultModule/ArchivePresenter.php @@ -20,7 +20,7 @@ public function injectServiceEvent(ServiceEventList $serviceEvent): void */ public function renderDefault(): void { - $events = array_reverse($this->serviceEvent->getEvents([$this->getContext()->getParameters()['eventTypeId']]));// TODO + $events = array_reverse($this->serviceEvent->getEvents($this->getEventIds()));// TODO $events = array_filter($events, function ($event) { //return true; return $event->end < new \DateTime('now'); diff --git a/app/Modules/Fol/Core/BasePresenter.php b/app/Modules/Fol/Core/BasePresenter.php index c253d78e..6a9c45db 100644 --- a/app/Modules/Fol/Core/BasePresenter.php +++ b/app/Modules/Fol/Core/BasePresenter.php @@ -4,16 +4,13 @@ namespace App\Modules\Fol\Core; -use App\Models\GamePhaseCalculator; -use App\Modules\Core\EventWebPresenter; use App\Models\NetteDownloader\ORM\Models\ModelEvent; use App\Models\NetteDownloader\ORM\Services\ServiceEventList; +use App\Modules\Core\EventWebPresenter; use Nette\Application\UI\Template; abstract class BasePresenter extends EventWebPresenter { - private readonly ServiceEventList $serviceEventList; - public static function createEventKey(ModelEvent $event): string { $year = $event->begin->format('Y'); @@ -22,24 +19,21 @@ public static function createEventKey(ModelEvent $event): string return $month < 10 ? ($year . '-' . $monthName) : $year; } - public function injectServiceEventList(ServiceEventList $serviceEventList): void - { - $this->serviceEventList = $serviceEventList; - } - /** * @throws \Throwable */ protected function createTemplate(): Template { - $fofGamePhaseCalculator = new GamePhaseCalculator( - $this->context->getParameters()['fofEventTypeId'], - $this->serviceEventList, - $this->context - ); - $template = parent::createTemplate(); - $template->fofEvent = $fofGamePhaseCalculator->getFKSDBEvent(); + $template->fofEvent = $this->serviceEventList->getNewest([ + $this->context->getParameters()['fofEventTypeId'], + ] + ); return $template; } + + protected function getEventIds(): array + { + return [9]; + } } diff --git a/app/Modules/Fol/DefaultModule/DefaultPresenter.php b/app/Modules/Fol/DefaultModule/DefaultPresenter.php index b4949604..3094fd3d 100644 --- a/app/Modules/Fol/DefaultModule/DefaultPresenter.php +++ b/app/Modules/Fol/DefaultModule/DefaultPresenter.php @@ -35,7 +35,7 @@ protected function createComponentUpperHomeBeforeRegistration(): UpperHomeBefore */ protected function createComponentCountdown(): CountdownComponent { - return new CountdownComponent($this->getContext(), $this->gamePhaseCalculator->getGameBegin()); + return new CountdownComponent($this->getContext(), $this->getNewestEvent()->game->begin); } /** diff --git a/app/config/config.neon b/app/config/config.neon index 3f706f16..a51685da 100644 --- a/app/config/config.neon +++ b/app/config/config.neon @@ -10,7 +10,6 @@ session: services: - Fykosak\Utils\Localization\GettextTranslator(App\Modules\Core\Language) - - App\Models\GamePhaseCalculator(%eventTypeId%) - App\Models\Game\Connector(%gameApi.url%, %gameApi.login%, %gameApi.password%) - App\Models\Downloader\FKSDBDownloader(%fksdbDownloader.url%,%fksdbDownloader.login%,%fksdbDownloader.password%,'10 minutes') - App\Models\Downloader\ProblemManagerDownloader(%problemManagerDownloader.url%,%problemManagerDownloader.login%,%problemManagerDownloader.password%,'10 minutes') From 5f1cbc71e172501fbfb21cc530270fcaccb8654c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20Mi=C5=A1o=20=C4=8Cerve=C5=88=C3=A1k?= Date: Sat, 25 May 2024 22:49:10 +0200 Subject: [PATCH 2/2] update downloader --- composer.json | 2 +- composer.lock | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/composer.json b/composer.json index 0cdfa138..353bd18b 100644 --- a/composer.json +++ b/composer.json @@ -13,7 +13,7 @@ "require": { "php": ">= 8.1", "nette/nette": "^v3.2.0", - "fykosak/fksdb-downloader-core": "dev-dev-json-request#c52a8fb", + "fykosak/fksdb-downloader-core": "dev-dev-json-request#596e9a1", "fykosak/nette-fks-utils": "dev-dev-datetime-period", "ext-soap": "*", "ext-dom": "*", diff --git a/composer.lock b/composer.lock index 6f3bb50a..3290c29a 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "5da0e37642b9018a43349490a1688211", + "content-hash": "392b0781c2f4dd5ece06f567efc2efa5", "packages": [ { "name": "68publishers/asset", @@ -73,12 +73,12 @@ "source": { "type": "git", "url": "https://github.com/fykosak/fksdb-downloader-core.git", - "reference": "c52a8fb" + "reference": "596e9a1" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/fykosak/fksdb-downloader-core/zipball/c52a8fb", - "reference": "c52a8fb", + "url": "https://api.github.com/repos/fykosak/fksdb-downloader-core/zipball/596e9a1", + "reference": "596e9a1", "shasum": "" }, "require": { @@ -111,7 +111,7 @@ "issues": "https://github.com/fykosak/fksdb-downloader-core/issues", "source": "https://github.com/fykosak/fksdb-downloader-core/tree/dev-json-request" }, - "time": "2024-01-18T01:40:33+00:00" + "time": "2024-05-25T20:47:40+00:00" }, { "name": "fykosak/nette-fks-utils",