Skip to content

Commit

Permalink
adds flag to published decks for tracking their wild-west-edition com…
Browse files Browse the repository at this point in the history
…patibility.
  • Loading branch information
stopfstedt committed Jul 2, 2023
1 parent 6d8fe36 commit ba4b6ae
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 4 deletions.
10 changes: 6 additions & 4 deletions config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,12 @@ services:
arguments:
$formFactory: '@fos_user.registration.form.factory'


# add more service definitions when explicit configuration is needed
# please note that last definitions always *replace* previous ones

# Commands
App\Command\:
resource: '../src/Command/*'

## Event listeners
App\EventListener\DecklistListener:
tags:
- { name: doctrine.event_listener, event: preUpdate }
- { name: doctrine.event_listener, event: prePersist }
29 changes: 29 additions & 0 deletions migrations/Version20230702163723.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

namespace DoctrineMigrations;

use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;

/**
* Adds "is Wild West Edition" flag to deckslists
*/
final class Version20230702163723 extends AbstractMigration
{
public function getDescription(): string
{
return 'Adds "is Wild West Edition" flag to deckslists.';
}

public function up(Schema $schema): void
{
$this->addSql('ALTER TABLE decklist ADD is_wwe TINYINT(1) DEFAULT \'0\' NOT NULL');
}

public function down(Schema $schema): void
{
$this->addSql('ALTER TABLE decklist DROP is_wwe');
}
}
23 changes: 23 additions & 0 deletions src/Entity/Decklist.php
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,13 @@ class Decklist
*/
protected $tournament;


/**
* @var boolean isWwe;
* @ORM\Column(name="is_wwe", type="boolean", nullable=false, options={"default"="0"})
*/
protected $isWwe;

public function __construct()
{
$this->slots = new ArrayCollection();
Expand Down Expand Up @@ -916,4 +923,20 @@ public function getTournament()
{
return $this->tournament;
}

/**
* @param bool $isWwe
* @return $this
*/
public function setIsWwe(bool $isWwe) {
$this->isWwe = $isWwe;
return $this;
}

/**
* @return bool
*/
public function getIsWwe() {
return $this->isWwe;
}
}
51 changes: 51 additions & 0 deletions src/EventListener/DecklistListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace App\EventListener;

use App\Entity\Decklist;
use App\Entity\Pack;
use App\Services\Decks;
use Doctrine\ORM\Event\LifecycleEventArgs;
use Doctrine\ORM\Event\PreUpdateEventArgs;

class DecklistListener
{
public function prePersist(LifecycleEventArgs $args)
{
$entity = $args->getEntity();
if ($entity instanceof Decklist) {
$this->setFormat($entity);
}
}

public function preUpdate(PreUpdateEventArgs $args)
{
$entity = $args->getEntity();
if ($entity instanceof Decklist) {
$this->setFormat($entity);
}
}

protected function setFormat(Decklist $decklist)
{
$packs = [];
foreach ($decklist->getSlots() as $slot) {
$packs[] = $slot->getCard()->getPack();
}
usort($packs, function (Pack $a, Pack $b) {
$rhett = $a->getCycle()->getNumber() <=> $b->getCycle()->getNumber();
if ($rhett) {
return $rhett;
}
return $a->getNumber() <=> $b->getNumber();
});
$earliestPack = $packs[0];
$isWwe = (
$earliestPack->getCycle()->getNumber() > Decks::TCAR_CYCLENUMBER
|| ($earliestPack->getCycle()->getNumber() === Decks::TCAR_CYCLENUMBER
&& $earliestPack->getNumber() >= Decks::TCAR_NUMBER
)
);
$decklist->setIsWwe($isWwe);
}
}

0 comments on commit ba4b6ae

Please sign in to comment.