Skip to content

Commit

Permalink
Quests: add option to autostart new quests
Browse files Browse the repository at this point in the history
  • Loading branch information
glaubinix committed Apr 27, 2019
1 parent a565912 commit 308b9ce
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 4 deletions.
22 changes: 20 additions & 2 deletions src/Initialization/QuestStarter.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
use LittleCubicleGames\Quests\Definition\Slot\Slot;
use LittleCubicleGames\Quests\Entity\QuestInterface;
use LittleCubicleGames\Quests\Initialization\Event\Event;
use LittleCubicleGames\Quests\QuestAdvancer;
use LittleCubicleGames\Quests\Storage\QuestStorageInterface;
use LittleCubicleGames\Quests\Workflow\QuestDefinitionInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;

class QuestStarter
Expand All @@ -19,13 +21,25 @@ class QuestStarter
private $questStorage;
/** @var EventDispatcherInterface */
private $dispatcher;
/** @var QuestAdvancer */
private $questAdvancer;
/** @var bool */
private $autoStartNewQuests;

public function __construct(RegistryInterface $registry, QuestBuilderInterface $questBuilder, QuestStorageInterface $questStorage, EventDispatcherInterface $dispatcher)
{
public function __construct(
RegistryInterface $registry,
QuestBuilderInterface $questBuilder,
QuestStorageInterface $questStorage,
EventDispatcherInterface $dispatcher,
QuestAdvancer $questAdvancer,
bool $autoStartNewQuests
) {
$this->registry = $registry;
$this->questBuilder = $questBuilder;
$this->questStorage = $questStorage;
$this->dispatcher = $dispatcher;
$this->questAdvancer = $questAdvancer;
$this->autoStartNewQuests = $autoStartNewQuests;
}

public function triggerNext(Slot $slot, int $user, ?QuestInterface $quest): void
Expand All @@ -35,6 +49,10 @@ public function triggerNext(Slot $slot, int $user, ?QuestInterface $quest): void
$quest = $this->questBuilder->buildQuest($nextQuest, $slot, $user);
$this->questStorage->save($quest);
$this->dispatcher->dispatch(Event::QUEST_ACTIVE, new Event($quest, $slot));

if ($this->autoStartNewQuests) {
$this->questAdvancer->advanceQuest($quest->getId(), $user, QuestDefinitionInterface::TRANSITION_START);
}
}
}
}
11 changes: 10 additions & 1 deletion src/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ class ServiceProvider implements ServiceProviderInterface, EventListenerProvider
{
public function register(Container $pimple): void
{
$pimple['cubicle.quests.autostart'] = false;

if (!isset($pimple['cubicle.quests.logger'])) {
$pimple['cubicle.quests.logger'] = [];
}
Expand Down Expand Up @@ -163,7 +165,14 @@ public function register(Container $pimple): void
};

$pimple['cubicle.quests.initializer.queststarter'] = function (Container $pimple) {
return new QuestStarter($pimple['cubicle.quests.registry'], $pimple['cubicle.quests.initializer.questbuilder'], $pimple['cubicle.quests.storage'], $pimple['dispatcher']);
return new QuestStarter(
$pimple['cubicle.quests.registry'],
$pimple['cubicle.quests.initializer.questbuilder'],
$pimple['cubicle.quests.storage'],
$pimple['dispatcher'],
$pimple['cubicle.quests.advancer'],
$pimple['cubicle.quests.autostart']
);
};

$pimple['cubicle.quests.initializer.questbuilder'] = function () {
Expand Down
6 changes: 5 additions & 1 deletion tests/Initialization/QuestStarterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use LittleCubicleGames\Quests\Entity\QuestInterface;
use LittleCubicleGames\Quests\Initialization\QuestBuilderInterface;
use LittleCubicleGames\Quests\Initialization\QuestStarter;
use LittleCubicleGames\Quests\QuestAdvancer;
use LittleCubicleGames\Quests\Storage\QuestStorageInterface;
use PHPUnit\Framework\TestCase;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
Expand All @@ -24,14 +25,17 @@ class QuestStarterTest extends TestCase
private $questStorage;
/** @var EventDispatcherInterface&\PHPUnit\Framework\MockObject\MockObject */
private $dispatcher;
/** @var QuestAdvancer&\PHPUnit\Framework\MockObject\MockObject */
private $questAdvancer;

protected function setUp(): void
{
$this->registry = $this->getMockBuilder(RegistryInterface::class)->getMock();
$this->questBuilder = $this->getMockBuilder(QuestBuilderInterface::class)->getMock();
$this->questStorage = $this->getMockBuilder(QuestStorageInterface::class)->getMock();
$this->dispatcher = $this->getMockBuilder(EventDispatcherInterface::class)->getMock();
$this->questStarter = new QuestStarter($this->registry, $this->questBuilder, $this->questStorage, $this->dispatcher);
$this->questAdvancer = $this->getMockBuilder(QuestAdvancer::class)->disableOriginalConstructor()->getMock();
$this->questStarter = new QuestStarter($this->registry, $this->questBuilder, $this->questStorage, $this->dispatcher, $this->questAdvancer, false);
}

public function testTriggerNext(): void
Expand Down

0 comments on commit 308b9ce

Please sign in to comment.