Skip to content

Commit

Permalink
download series
Browse files Browse the repository at this point in the history
  • Loading branch information
Headary committed Jan 6, 2024
1 parent d0c55fa commit 2c4f1d6
Show file tree
Hide file tree
Showing 8 changed files with 158 additions and 35 deletions.
2 changes: 1 addition & 1 deletion app/Models/Problems/ProblemRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@ public function getParams(): array

final public function getMethod(): string
{
return sprintf('problem%d-%d.json', $this->series, $this->number);
return sprintf('%d/problem%d-%d.json', $this->year, $this->series, $this->number);
}
}
22 changes: 22 additions & 0 deletions app/Models/Problems/SeriesModel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace App\Models\Problems;

use DateTime;

class SeriesModel
{
public string $deadline;
public int $year;
public int $series;
/**
* @var int[]
*/
public array $problems;

public function getDeadline() {
return new DateTime($this->deadline);
}
}
36 changes: 36 additions & 0 deletions app/Models/Problems/SeriesRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

namespace App\Models\Problems;

use Fykosak\FKSDBDownloaderCore\Requests\Request;

class SeriesRequest implements Request
{
protected string $contest;
protected int $year;
protected int $series;

public function __construct(string $contest, int $year, int $series)
{
$this->contest = $contest;
$this->year = $year;
$this->series = $series;
}

public function getCacheKey(): string
{
return sprintf('series.%s.%d.%d', $this->contest, $this->year, $this->series);
}

public function getParams(): array
{
return [];
}

final public function getMethod(): string
{
return sprintf('series.json');
}
}
46 changes: 46 additions & 0 deletions app/Models/Problems/SeriesService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

declare(strict_types=1);

namespace App\Models\Problems;
use Nette\Caching\Cache;

use Fykosak\NetteFKSDBDownloader\ORM\Services\AbstractJSONService;

final class SeriesService extends AbstractJSONService
{
public function getSeries(
string $contest,
int $year,
int $series,
?string $explicitExpiration = null
): SeriesModel {
return $this->getItem(
new SeriesRequest($contest, $year, $series),
[(string)$year, (string)$series],
SeriesModel::class,
false,
$explicitExpiration
);
}

public function getLatestSeries(string $contest): SeriesModel {
return $this->cache->load(
sprintf("lastSeries_%s", $contest),
function (&$dependencies) use ($contest) {
$dependencies[Cache::EXPIRE] = $this->expiration;
$jsonText = $this->downloader->download(new SeriesRequest($contest, 0, 0)); // 0, 0 are dummy data
$json = json_decode($jsonText);

$yearArray = get_object_vars($json);
$year = end($yearArray);

$seriesArray = get_object_vars($year);
$series = end($seriesArray);

$mapper = new \JsonMapper();
return $mapper->map($series, new SeriesModel());
}
);
}
}
12 changes: 12 additions & 0 deletions app/Modules/Fykos/Core/templates/@layout.latte
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,18 @@
<script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.9.3/dist/umd/popper.min.js"
integrity="sha384-eMNCOe7tC1doHpGoWe/6oMVemdAVTMs2xqW4mwXrXsW0L84Iytr2wi5v2QjrP/xp"
crossorigin="anonymous"></script>
<script>
MathJax = {
tex: {
inlineMath: [['$', '$'], ['\\(', '\\)']],
displayMath: [['\\eq{', '}']]
}
}
</script>
<script type="text/javascript" id="MathJax-script" async
src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-svg.js">
</script>

<script src="{asset 'assets/main.js'}"></script>
{block scripts}{/block}
</head>
Expand Down
31 changes: 18 additions & 13 deletions app/Modules/Fykos/DefaultModule/ProblemsPresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,31 @@

namespace App\Modules\Fykos\DefaultModule;

use App\Models\Problems\ProblemService;
use App\Models\Problems\SeriesService;
use Nette\Utils\DateTime;

class ProblemsPresenter extends BasePresenter
{
private ProblemService $problemService;
private SeriesService $seriesService;

public function injectServiceProblem(ProblemService $problemService, SeriesService $seriesService): void
{
$this->problemService = $problemService;
$this->seriesService = $seriesService;
}

public function renderDefault(): void
{
$series = $this->seriesService->getLatestSeries('fykos');
$this->template->series = $series;

// Nahradit toto za načítání dat z databáze
$fileContents1 = file_get_contents(__DIR__ . '/temp-solution.json');
$data1 = json_decode($fileContents1, true);
$fileContents2 = file_get_contents(__DIR__ . '/temp-solution2.json');
$data2 = json_decode($fileContents2, true);
$data = [$data1, $data2];
$series = [
'number' => 1,
'year' => 37,
'deadline' => new DateTime('2023-11-25 23:59:59'),
];
$problems = [];
foreach ($series->problems as $probNum) {
$problems[] = $this->problemService->getProblem('fykos', $series->year, $series->series, $probNum);
}
$this->template->problems = $problems;

$this->template->problemIcons = [
1 => 'fas fa-smile',
Expand All @@ -33,7 +40,5 @@ public function renderDefault(): void
7 => 'fas fa-flask',
8 => 'fas fa-book'
];
$this->template->series = $series;
$this->template->problems = $data;
}
}
42 changes: 21 additions & 21 deletions app/Modules/Fykos/DefaultModule/templates/Problems/default.latte
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
<div class="col-md-8">
<div class="subheader">
{switch $lang}
{case 'cs'}Deadline pro odeslání: <strong>{$series["deadline"]|date:"d. m. Y, H:i"}</strong>.
{default}Deadline for submission: <strong>{$series["deadline"]|date:"d. m. Y, H:i"}, CET</strong>.
{case 'cs'}Deadline pro odeslání: <strong>{$series->getDeadline()|date:"d. m. Y, H:i"}</strong>.
{default}Deadline for submission: <strong>{$series->getDeadline()|date:"d. m. Y, H:i"}, CET</strong>.
{/switch}
</div>
<h1>Zadání {$series["number"]}. série {$series["year"]}. ročníku</h1>
<h1>Zadání {$series->series}. série {$series->year}. ročníku</h1>

<a class="btn btn-primary my-2 mr-4" n:href=":Events:Fykos:">
{switch $lang}
Expand Down Expand Up @@ -45,7 +45,7 @@
</div>
<div class="mt-5">
<a class="btn btn-primary my-2 mr-4" {* n:href="Rules:complete" *}>
<i class="far fa-file-pdf"></i><span class="skip-3px"></span> Text seriálu {$series["number"]}.
<i class="far fa-file-pdf"></i><span class="skip-3px"></span> Text seriálu {$series->series}.
série
</a>
{if true} {* TODO if je po deadlinu *}
Expand All @@ -59,26 +59,26 @@
{foreach $problems as $problem}
<div class="col-md-12">
<div class="card mb-3 problem-card">
<div class="card-header header-problem-{$problem['number']}">
<div class="card-header header-problem-{$problem->number}">
<div style="display: flex; justify-content: space-between;">
<h4><i class={$problemIcons[$problem["number"]]}></i><span class="skip-3px"></span>
{$problem["number"]}...
{$problem["name"][$lang]}</h4>
<h4><i class={$problemIcons[$problem->number]}></i><span class="skip-3px"></span>
{$problem->getLabel()}...
{$problem->name[$lang]}</h4>
<div>
{switch $lang}
{case 'cs'}
<h4>{$problem["points"]} bod{if $problem["points"] == 1}{elseif
$problem["points"] > 1
&& $problem["points"] < 5}y{else}ů{/if}</h4>
<h4>{$problem->points} bod{if $problem->points == 1}{elseif
$problem->points > 1
&& $problem->points < 5}y{else}ů{/if}</h4>
{default}
<h4>{$problem["points"]} point{if $problem["points"] == 1}{else}s{/if}</h4>
<h4>{$problem->points} point{if $problem->points == 1}{else}s{/if}</h4>
{/switch}
</div>
</div>
</div>
<div class="card-body">
<h5 class="card-title">
{$problem["task"]["cs"]}
{$problem->task[$lang]}
</h5>
{if true} {* TODO if je po deadlinu *}
<div style="display: flex; justify-content: space-between;" class="mt-3">
Expand All @@ -89,24 +89,24 @@
{else}
Řešení této úlohy zveřejníme brzy.
{/if}
{if $problem["number"] == 7}
<a class="btn btn-primary mr-2" n:href=":Events:Fykos:Experiments:default"
target="_blank">Návod pro řešení experimentálních úloh</a>
{if $problem->number == 7}
{*<a class="btn btn-primary mr-2" n:href=":Events:Fykos:Experiments:default"
target="_blank">Návod pro řešení experimentálních úloh</a>*}
{/if}
{if $problem["number"] == 8}
{if $problem->number == 8}
<a class="btn btn-primary my-2 mr-4" {* n:href="Rules:complete" *}>
<i class="far fa-file-pdf"></i><span class="skip-3px"></span> Text seriálu {$series["number"]}.
<i class="far fa-file-pdf"></i><span class="skip-3px"></span> Text seriálu {$series->series}.
série
</a>
{/if}
</div>
<div><i>~ {$problem["origin"][$lang]}</i></div>
<div><i>~ {$problem->origin[$lang]}</i></div>
</div>
{/if}
</div>

<div class="card-footer">
{foreach $problem["topics"] as $topic}
{foreach $problem->topics as $topic}
<span style="margin-right: 1rem;"><i class="fas fa-tag"></i> {$topic}</span>
{/foreach}
</div>
Expand All @@ -118,4 +118,4 @@
</div>
</section>
</div>
{/block}
{/block}
2 changes: 2 additions & 0 deletions app/config/config.fykos.neon
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ application:

services:
- App\RouterFactory::createFykosRouter(%domains%, %router-mapping%)
- App\Models\Problems\ProblemService('10 minutes')
- App\Models\Problems\SeriesService('10 minutes')


includes:
Expand Down

0 comments on commit 2c4f1d6

Please # to comment.