Skip to content

Commit ba4b6ae

Browse files
committed
adds flag to published decks for tracking their wild-west-edition compatibility.
1 parent 6d8fe36 commit ba4b6ae

File tree

4 files changed

+109
-4
lines changed

4 files changed

+109
-4
lines changed

config/services.yaml

+6-4
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,12 @@ services:
4545
arguments:
4646
$formFactory: '@fos_user.registration.form.factory'
4747

48-
49-
# add more service definitions when explicit configuration is needed
50-
# please note that last definitions always *replace* previous ones
51-
5248
# Commands
5349
App\Command\:
5450
resource: '../src/Command/*'
51+
52+
## Event listeners
53+
App\EventListener\DecklistListener:
54+
tags:
55+
- { name: doctrine.event_listener, event: preUpdate }
56+
- { name: doctrine.event_listener, event: prePersist }

migrations/Version20230702163723.php

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace DoctrineMigrations;
6+
7+
use Doctrine\DBAL\Schema\Schema;
8+
use Doctrine\Migrations\AbstractMigration;
9+
10+
/**
11+
* Adds "is Wild West Edition" flag to deckslists
12+
*/
13+
final class Version20230702163723 extends AbstractMigration
14+
{
15+
public function getDescription(): string
16+
{
17+
return 'Adds "is Wild West Edition" flag to deckslists.';
18+
}
19+
20+
public function up(Schema $schema): void
21+
{
22+
$this->addSql('ALTER TABLE decklist ADD is_wwe TINYINT(1) DEFAULT \'0\' NOT NULL');
23+
}
24+
25+
public function down(Schema $schema): void
26+
{
27+
$this->addSql('ALTER TABLE decklist DROP is_wwe');
28+
}
29+
}

src/Entity/Decklist.php

+23
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,13 @@ class Decklist
225225
*/
226226
protected $tournament;
227227

228+
229+
/**
230+
* @var boolean isWwe;
231+
* @ORM\Column(name="is_wwe", type="boolean", nullable=false, options={"default"="0"})
232+
*/
233+
protected $isWwe;
234+
228235
public function __construct()
229236
{
230237
$this->slots = new ArrayCollection();
@@ -916,4 +923,20 @@ public function getTournament()
916923
{
917924
return $this->tournament;
918925
}
926+
927+
/**
928+
* @param bool $isWwe
929+
* @return $this
930+
*/
931+
public function setIsWwe(bool $isWwe) {
932+
$this->isWwe = $isWwe;
933+
return $this;
934+
}
935+
936+
/**
937+
* @return bool
938+
*/
939+
public function getIsWwe() {
940+
return $this->isWwe;
941+
}
919942
}
+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
namespace App\EventListener;
4+
5+
use App\Entity\Decklist;
6+
use App\Entity\Pack;
7+
use App\Services\Decks;
8+
use Doctrine\ORM\Event\LifecycleEventArgs;
9+
use Doctrine\ORM\Event\PreUpdateEventArgs;
10+
11+
class DecklistListener
12+
{
13+
public function prePersist(LifecycleEventArgs $args)
14+
{
15+
$entity = $args->getEntity();
16+
if ($entity instanceof Decklist) {
17+
$this->setFormat($entity);
18+
}
19+
}
20+
21+
public function preUpdate(PreUpdateEventArgs $args)
22+
{
23+
$entity = $args->getEntity();
24+
if ($entity instanceof Decklist) {
25+
$this->setFormat($entity);
26+
}
27+
}
28+
29+
protected function setFormat(Decklist $decklist)
30+
{
31+
$packs = [];
32+
foreach ($decklist->getSlots() as $slot) {
33+
$packs[] = $slot->getCard()->getPack();
34+
}
35+
usort($packs, function (Pack $a, Pack $b) {
36+
$rhett = $a->getCycle()->getNumber() <=> $b->getCycle()->getNumber();
37+
if ($rhett) {
38+
return $rhett;
39+
}
40+
return $a->getNumber() <=> $b->getNumber();
41+
});
42+
$earliestPack = $packs[0];
43+
$isWwe = (
44+
$earliestPack->getCycle()->getNumber() > Decks::TCAR_CYCLENUMBER
45+
|| ($earliestPack->getCycle()->getNumber() === Decks::TCAR_CYCLENUMBER
46+
&& $earliestPack->getNumber() >= Decks::TCAR_NUMBER
47+
)
48+
);
49+
$decklist->setIsWwe($isWwe);
50+
}
51+
}

0 commit comments

Comments
 (0)