89 lines
3.2 KiB
PHP
89 lines
3.2 KiB
PHP
<?php
|
||
|
||
namespace Database\Seeders;
|
||
|
||
use App\Models\Contest;
|
||
use App\Models\Round;
|
||
use App\Models\Band;
|
||
use Illuminate\Database\Seeder;
|
||
use Illuminate\Support\Facades\DB;
|
||
use Illuminate\Support\Facades\Schema;
|
||
|
||
class RoundSeeder extends Seeder
|
||
{
|
||
/**
|
||
* Run the database seeds.
|
||
*/
|
||
public function run(): void
|
||
{
|
||
Schema::disableForeignKeyConstraints();
|
||
DB::table('rounds')->truncate();
|
||
DB::table('rounds_bands')->truncate();
|
||
DB::table('rounds_categories')->truncate();
|
||
DB::table('rounds_power_categories')->truncate();
|
||
DB::table('evaluation_runs')->truncate();
|
||
Schema::enableForeignKeyConstraints();
|
||
|
||
$bands = Band::all()->pluck('id')->toArray();
|
||
|
||
$contests = Contest::with(['categories', 'powerCategories'])->get();
|
||
if ($contests->isEmpty()) {
|
||
// contest se zakládá přes ContestSeeder – pokud chybí, nic neseedujeme
|
||
return;
|
||
}
|
||
|
||
$globalStart = now()->startOfMonth()->setTime(14, 0);
|
||
|
||
foreach ($contests as $contestIndex => $contest) {
|
||
$baseStart = (clone $globalStart)->addMonths($contestIndex * 2);
|
||
|
||
for ($i = 0; $i < 2; $i++) {
|
||
$start = (clone $baseStart)->addMonths($i);
|
||
$end = (clone $start)->addHours(24);
|
||
$deadline = (clone $end)->addDays(3);
|
||
|
||
$round = new Round();
|
||
|
||
$round->contest_id = $contest->id;
|
||
$round->rule_set_id = $contest->rule_set_id;
|
||
|
||
$round->setTranslations('name', [
|
||
'cs' => 'Kolo ' . ($i + 1) . ' – ' . $contest->getTranslation('name', 'cs'),
|
||
'en' => 'Round ' . ($i + 1) . ' – ' . $contest->getTranslation('name', 'en'),
|
||
]);
|
||
|
||
$round->setTranslations('description', [
|
||
'cs' => 'Testovací kolo číslo ' . ($i + 1) . ' pro závod ' . $contest->getTranslation('name', 'cs'),
|
||
'en' => 'Test round number ' . ($i + 1) . ' for contest ' . $contest->getTranslation('name', 'en'),
|
||
]);
|
||
|
||
$round->is_active = $i < 8; // poslední dvě kola jako neaktivní
|
||
$round->is_test = $i === 0; // první kolo jako testovací
|
||
$round->is_sixhr = $i % 2 === 1; // lichá kola jako 6h
|
||
|
||
$round->start_time = $start;
|
||
$round->end_time = $end;
|
||
$round->logs_deadline = $deadline;
|
||
|
||
$round->save();
|
||
|
||
// napojení pásem stejné jako u contestu (všechna dostupná)
|
||
if (!empty($bands)) {
|
||
$round->bands()->attach($bands);
|
||
}
|
||
// napojení kategorií stejné jako u contestu
|
||
$categoryIds = $contest->categories->pluck('id')->toArray();
|
||
if (!empty($categoryIds)) {
|
||
$round->categories()->attach($categoryIds);
|
||
}
|
||
// výkonové kategorie stejně jako u contestu
|
||
$powerCategoryIds = $contest->powerCategories->pluck('id')->toArray();
|
||
if (!empty($powerCategoryIds)) {
|
||
$round->powerCategories()->attach($powerCategoryIds);
|
||
}
|
||
|
||
}
|
||
}
|
||
}
|
||
}
|