Skip to content

Commit

Permalink
Merge pull request #512 from gereons/invert-rotation-logic
Browse files Browse the repository at this point in the history
Invert rotation logic
  • Loading branch information
plural authored Apr 11, 2021
2 parents 8f06fe9 + 659fcd4 commit 8495eb3
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 37 deletions.
11 changes: 6 additions & 5 deletions src/AppBundle/Command/ImportStdCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,9 @@ protected function execute(InputInterface $input, OutputInterface $output)
// rotation

$output->writeln("Importing Rotation...");
$mwlFileInfo = $this->getFileInfo($path, 'rotations.json');
$imported = $this->importRotationJsonFile($mwlFileInfo);
$rotationFileInfo = $this->getFileInfo($path, 'rotations.json');
$imported = $this->importRotationJsonFile($rotationFileInfo);
$output->writeln("Imported: " . count($imported));
if (!$force && count($imported)) {
$question = new ConfirmationQuestion("Do you confirm? (Y/n) ", true);
if (!$helper->ask($input, $output, $question)) {
Expand Down Expand Up @@ -459,7 +460,7 @@ protected function importRotationJsonFile(\SplFileInfo $fileinfo)
], [], []);
if ($rotation) {
$result[] = $rotation;
foreach ($rotationData['cycles'] as $cycle_code) {
foreach ($rotationData['rotated'] as $cycle_code) {
$cycle = $this->entityManager->getRepository('AppBundle:Cycle')->findOneBy(['code' => $cycle_code]);
if (!$cycle instanceof Cycle) {
continue;
Expand Down Expand Up @@ -629,10 +630,10 @@ protected function getEntityFromData(string $entityName, array $data, array $man
// we need to check that manually. If those are the same, just let the
// existing handling do its work.
if ($entityName === 'AppBundle\Entity\Rotation') {
$json_cycles = $data['cycles'];
$json_cycles = $data['rotated'];
sort($json_cycles);
$db_cycles = array();
foreach ($entity->GetCycles() as $c) {
foreach ($entity->GetRotated() as $c) {
array_push($db_cycles, $c->GetCode());
}
sort($db_cycles);
Expand Down
29 changes: 28 additions & 1 deletion src/AppBundle/Command/LegalityDecklistsRotationCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use AppBundle\Service\RotationService;

/**
*
Expand All @@ -15,11 +16,15 @@ class LegalityDecklistsRotationCommand extends ContainerAwareCommand
{
/** @var EntityManagerInterface $entityManager */
private $entityManager;

/** @var RotationService $rotationService */
private $rotationService;

public function __construct(EntityManagerInterface $entityManager)
public function __construct(EntityManagerInterface $entityManager, RotationService $rotationService)
{
parent::__construct();
$this->entityManager = $entityManager;
$this->rotationService = $rotationService;
}

protected function configure()
Expand Down Expand Up @@ -51,6 +56,28 @@ protected function execute(InputInterface $input, OutputInterface $output)
AND ny.rotated = 0);";

$this->entityManager->getConnection()->executeQuery($sql);

$rotations = $this->entityManager->getRepository('AppBundle:Rotation')->findBy([], ["dateStart" => "DESC"]);
$decklists = $this->entityManager->getRepository('AppBundle:Decklist')->findBy([]);

foreach ($rotations as $rotation) {
$output->writeln("checking " . $rotation->getName());

foreach ($decklists as $decklist) {
$confirm = $this->rotationService->isRotationCompatible($decklist, $rotation);

$oldId = null;
if ($decklist->getRotation()) {
$oldId = $decklist->getRotation()->getId();
}

if ($confirm && $oldId !== $rotation->getId()) {
$output->writeln(" updating decklist " . $decklist->getId());
$decklist->setRotation($rotation);
}
}
}
$this->entityManager->flush();

$output->writeln("<info>Done</info>");
}
Expand Down
11 changes: 8 additions & 3 deletions src/AppBundle/Controller/SearchController.php
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,7 @@ public function displayAction(

$rotationService = new RotationService($entityManager);
$currentRotation = $rotationService->findCurrentRotation();
foreach($currentRotation->getCycles()->toArray() as $cycle) {
foreach($currentRotation->getRotated()->toArray() as $cycle) {
$currentRotationCycles[$cycle->getCode()] = true;
}
$cardinfo['versions'] = [];
Expand All @@ -497,6 +497,8 @@ public function displayAction(
// Startup legality is currently hard-coded since the DB doesn't know anything about it.
$startupCycles = ['ashes' => true, 'system-gateway' => true, 'system-update-2021' => true];
$startup_legal = false;

$rotated_count = 0;

foreach ($cardVersions as $version) {
$v = $cardsData->getCardInfo($version, $locale);
Expand All @@ -511,15 +513,18 @@ public function displayAction(
if ($v['cycle_code'] == 'draft' || $v['pack_code'] == 'tdc') {
$standard_legal = false;
}
// If any version of the card is in a rotation-legal cycle, the card is considered legal.
// Count the card's occurence in the rotated cycle(s)
if (array_key_exists($v['cycle_code'], $currentRotationCycles)) {
$all_versions_rotated = false;
++$rotated_count;
}
// Any printing of this card in a valid Startup cycle means the card is Startup legal.
if (array_key_exists($v['cycle_code'], $startupCycles)) {
$startup_legal = true;
}
}

// If any version of the card is not in a rotated cycle, the card is considered legal.
$all_versions_rotated = $rotated_count == count($cardinfo['versions']);

$cardinfo['reviews'] = $cardsData->get_reviews($cardVersions);
$cardinfo['rulings'] = $cardsData->get_rulings($cardVersions);
Expand Down
2 changes: 1 addition & 1 deletion src/AppBundle/Entity/Cycle.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class Cycle implements NormalizableInterface, TimestampableInterface, CodeNameIn
{
/**
* @var Collection|Rotation[]
* @ORM\ManyToMany(targetEntity="Rotation", mappedBy="cycles")
* @ORM\ManyToMany(targetEntity="Rotation", mappedBy="rotated")
*/
protected $rotations;

Expand Down
38 changes: 19 additions & 19 deletions src/AppBundle/Entity/Rotation.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class Rotation implements NormalizableInterface, TimestampableInterface
* }
* )
*/
protected $cycles;
protected $rotated;

/**
* @var integer
Expand Down Expand Up @@ -66,7 +66,7 @@ class Rotation implements NormalizableInterface, TimestampableInterface

public function __construct()
{
$this->cycles = new ArrayCollection();
$this->rotated = new ArrayCollection();
}

public function __toString()
Expand All @@ -76,9 +76,9 @@ public function __toString()

public function normalize()
{
$cycles = [];
foreach ($this->cycles as $cycle) {
$cycles[] = $cycle->getCode();
$rotated = [];
foreach ($this->rotated as $cycle) {
$rotated[] = $cycle->getCode();
}

return [
Expand All @@ -88,7 +88,7 @@ public function normalize()
'code' => $this->code,
'name' => $this->name,
'date_start' => $this->dateStart ? $this->dateStart->format('Y-m-d') : null,
'cycles' => $cycles
'rotated' => $rotated
];
}

Expand Down Expand Up @@ -140,36 +140,36 @@ public function addDecklist(Decklist $decklist)
}

/** @return Collection|Cycle[] */
public function getCycles()
public function getRotated()
{
return $this->cycles;
return $this->rotated;
}

/** @param Collection|Cycle[] $cycles */
public function setCycles(Collection $cycles)
/** @param Collection|Cycle[] $rotated */
public function setRotated(Collection $rotated)
{
$this->clearCycles();
foreach ($cycles as $cycle) {
$this->clearRotated();
foreach ($rotated as $cycle) {
$this->addCycle($cycle);
}

return $this;
}

public function clearCycles()
public function clearRotated()
{
foreach ($this->getCycles() as $cycle) {
foreach ($this->getRotated() as $cycle) {
$this->removeCycle($cycle);
}
$this->cycles->clear();
$this->rotated->clear();

return $this;
}

public function removeCycle(Cycle $cycle)
{
if ($this->cycles->contains($cycle)) {
$this->cycles->removeElement($cycle);
if ($this->rotated->contains($cycle)) {
$this->rotated->removeElement($cycle);
$cycle->removeRotation($this);
}

Expand All @@ -178,8 +178,8 @@ public function removeCycle(Cycle $cycle)

public function addCycle(Cycle $cycle)
{
if ($this->cycles->contains($cycle) === false) {
$this->cycles->add($cycle);
if ($this->rotated->contains($cycle) === false) {
$this->rotated->add($cycle);
$cycle->addRotation($this);
}

Expand Down
2 changes: 1 addition & 1 deletion src/AppBundle/Resources/config/doctrine/Cycle.orm.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ AppBundle\Entity\Cycle:
manyToMany:
rotations:
targetEntity: Rotation
mappedBy: cycles
mappedBy: rotated
cascade: ["remove"]
fields:
id:
Expand Down
2 changes: 1 addition & 1 deletion src/AppBundle/Resources/config/doctrine/Rotation.orm.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ AppBundle\Entity\Rotation:
type: entity
table: rotation
manyToMany:
cycles:
rotated:
targetEntity: Cycle
inversedBy: rotations
joinTable:
Expand Down
4 changes: 2 additions & 2 deletions src/AppBundle/Service/CardsData.php
Original file line number Diff line number Diff line change
Expand Up @@ -618,13 +618,13 @@ public function get_search_rows(array $conditions, string $sortorder, string $lo
}
if ($rotation) {
// Add the valid cycles for the requested rotation and add them to the WHERE clause for the query.
$cycles = $rotation->normalize()["cycles"];
$cycles = $rotation->normalize()["rotated"];
$placeholders = array();
foreach($cycles as $cycle) {
array_push($placeholders, "?$i");
$parameters[$i++] = $cycle;
}
$clauses[] = "(y.code in (" . implode(", ", $placeholders) . "))";
$clauses[] = "(y.code not in (" . implode(", ", $placeholders) . "))";
}
$i++;
break;
Expand Down
8 changes: 4 additions & 4 deletions src/AppBundle/Service/RotationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,10 @@ public function isRotationCompatible(Decklist $decklist, Rotation $rotation)
foreach ($decklist->getSlots() as $slot) {
$cycles[$slot->getCard()->getPack()->getCycle()->getCode()] = 1;
}

return count(array_diff(array_keys($cycles), array_map(function (Cycle $cycle) {
return $cycle->getCode();
}, $rotation->getCycles()->toArray()))) === 0;
$usedCycles = array_keys($cycles);
$rotatedCycles = array_map(function (Cycle $cycle) { return $cycle->getCode(); }, $rotation->getRotated()->toArray());
return count(array_intersect($usedCycles, $rotatedCycles)) === 0;
}

/**
Expand Down

0 comments on commit 8495eb3

Please sign in to comment.