129 lines
4.7 KiB
PHP
129 lines
4.7 KiB
PHP
<?php
|
|
|
|
namespace App\Controller;
|
|
|
|
use App\Entity\Grid;
|
|
use App\Entity\Region;
|
|
use App\Kernel;
|
|
use App\Repository\GridRepository;
|
|
use App\Repository\MapRepository;
|
|
use App\Service\FileManager;
|
|
use Doctrine\ORM\EntityManagerInterface;
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
|
use Symfony\Component\HttpFoundation\Request;
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
use Symfony\Component\Routing\Annotation\Route;
|
|
|
|
#[Route('/dashboard/regions/region-{regionId}/grid')]
|
|
#[ParamConverter(data: 'region', class: Region::class, options: ['id' => 'regionId'])]
|
|
class GridController extends AbstractController {
|
|
public function __construct(private EntityManagerInterface $_em,
|
|
private FileManager $fileManager,
|
|
private GridRepository $gridRepository,
|
|
private MapRepository $mapRepository) {}
|
|
|
|
#[Route('', name: 'bo_region_grid_index', methods: ['GET'])]
|
|
public function index(Request $request, Region $region): Response {
|
|
$version = $request->query->get('v');
|
|
$version = !$version || !in_array(floatval($version), Kernel::SUPPORTED_GAME_VERSION) ? Kernel::GAME_VERSION : floatval($version);
|
|
|
|
$cells = $this->gridRepository->getGridCells($region);
|
|
$maps = $this->mapRepository->getCellsMap($cells, $version);
|
|
$grid = $this->gridRepository->buildWorldmap($version, $cells, $maps, true);
|
|
|
|
return $this->render('_dashboard/grid/index.html.twig', array(
|
|
'region' => $region,
|
|
'grid' => $grid,
|
|
'version' => $version,
|
|
));
|
|
}
|
|
|
|
#[Route('/edit', name: 'bo_region_grid_edit', methods: ['GET', 'POST'])]
|
|
public function edit(Request $request, Region $region): Response {
|
|
$logs=array();
|
|
$form = $this->gridRepository->getGridForm('bo_region_grid_edit', $region);
|
|
$form->handleRequest($request);
|
|
|
|
if($form->isSubmitted() && $form->isValid()) {
|
|
if(is_array($form->get('positions')->getData())) {
|
|
if(in_array('row_before', $form->get('positions')->getData())) {
|
|
$this->addRow($region, $this->gridRepository->getGridCells($region), true);
|
|
$logs[]='• Added one row before.';
|
|
}
|
|
|
|
if(in_array('row_after', $form->get('positions')->getData())) {
|
|
$this->addRow($region, $this->gridRepository->getGridCells($region), false);
|
|
$logs[]='• Added one row after.';
|
|
}
|
|
|
|
if(in_array('column_before', $form->get('positions')->getData())) {
|
|
$this->addColumn($region, $this->gridRepository->getGridCells($region), true);
|
|
$logs[]='• Added one column before.';
|
|
}
|
|
|
|
if(in_array('column_after', $form->get('positions')->getData())) {
|
|
$this->addColumn($region, $this->gridRepository->getGridCells($region), false);
|
|
$logs[]='• Added one column after.';
|
|
}
|
|
|
|
$form = $this->gridRepository->getGridForm('bo_region_grid_edit', $region);
|
|
}
|
|
}
|
|
|
|
return $this->renderForm('_dashboard/grid/edit.html.twig', array(
|
|
'region' => $region,
|
|
'form' => $form,
|
|
'logs' => $logs,
|
|
));
|
|
}
|
|
|
|
private function addColumn(Region $region, array $cells, bool $isBefore) {
|
|
if($isBefore) {
|
|
foreach($cells as $_cell) {
|
|
$_cell->setCol($_cell->getCol() + 1);
|
|
}
|
|
}
|
|
|
|
for($n = 1; $n <= $region->getGridHeight(); $n++) {
|
|
$cell = new Grid();
|
|
$cell->setRegion($region)
|
|
->setCol($isBefore ? 1 : $region->getGridWidth() + 1)
|
|
->setRow($n);
|
|
|
|
$this->_em->persist($cell);
|
|
}
|
|
|
|
$region->setGridWidth($region->getGridWidth() + 1);
|
|
|
|
$this->_em->flush();
|
|
}
|
|
|
|
/**
|
|
* @param Region $region
|
|
* @param Grid[] $cells
|
|
* @param bool $isBefore
|
|
* @return void
|
|
*/
|
|
private function addRow(Region $region, array $cells, bool $isBefore) {
|
|
if($isBefore) {
|
|
foreach($cells as $_cell) {
|
|
$_cell->setRow($_cell->getRow() + 1);
|
|
}
|
|
}
|
|
|
|
for($n = 1; $n <= $region->getGridWidth(); $n++) {
|
|
$cell = new Grid();
|
|
$cell->setRegion($region)
|
|
->setCol($n)
|
|
->setRow($isBefore ? 1 : $region->getGridHeight() + 1);
|
|
|
|
$this->_em->persist($cell);
|
|
}
|
|
|
|
$region->setGridHeight($region->getGridHeight() + 1);
|
|
|
|
$this->_em->flush();
|
|
}
|
|
}
|