Adds commands to add and remove scene connection groups.
This commit is contained in:
@@ -0,0 +1,83 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace LotGD\Core\Console\Command\Scene;
|
||||
|
||||
use LotGD\Core\Console\Command\BaseCommand;
|
||||
use LotGD\Core\Exceptions\ArgumentException;
|
||||
use LotGD\Core\Models\Character;
|
||||
use LotGD\Core\Models\Scene;
|
||||
use LotGD\Core\Models\SceneConnectable;
|
||||
use LotGD\Core\Models\SceneConnection;
|
||||
use LotGD\Core\Models\SceneConnectionGroup;
|
||||
use LotGD\Core\Models\SceneTemplate;
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
use Symfony\Component\Console\Input\InputArgument;
|
||||
use Symfony\Component\Console\Input\InputDefinition;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Input\InputOption;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
use Symfony\Component\Console\Style\SymfonyStyle;
|
||||
|
||||
/**
|
||||
* Resets the viewpoint of a given character.
|
||||
*/
|
||||
class SceneAddConnectionGroupCommand extends BaseCommand
|
||||
{
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
protected function configure()
|
||||
{
|
||||
$this->setName('scene:addConnectionGroup')
|
||||
->setDescription('Add a connection group to an existing scene.')
|
||||
->setDefinition(
|
||||
new InputDefinition([
|
||||
new InputArgument("id", InputArgument::REQUIRED, "ID of the scene"),
|
||||
new InputArgument("groupName", InputArgument::REQUIRED, "Internal id of the group."),
|
||||
new InputArgument("groupTitle", InputArgument::REQUIRED, "Title of the group (what the character can see"),
|
||||
]),
|
||||
)
|
||||
;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
protected function execute(InputInterface $input, OutputInterface $output): int
|
||||
{
|
||||
$em = $this->game->getEntityManager();
|
||||
|
||||
$io = new SymfonyStyle($input, $output);
|
||||
|
||||
$sceneId = $input->getArgument("id");
|
||||
$groupName = $input->getArgument("groupName");
|
||||
$groupTitle = $input->getArgument("groupTitle");
|
||||
|
||||
// Search scene
|
||||
/** @var ?Scene $scene */
|
||||
$scene = $em->getRepository(Scene::class)->find($sceneId);
|
||||
|
||||
if (!$scene) {
|
||||
$io->error("The requested scene with the ID {$sceneId} was not found");
|
||||
return Command::FAILURE;
|
||||
}
|
||||
|
||||
// Make scene connection group
|
||||
$connectionGroup = new SceneConnectionGroup($groupName, $groupTitle);
|
||||
|
||||
// Add
|
||||
try {
|
||||
$scene->addConnectionGroup($connectionGroup);
|
||||
} catch(ArgumentException $e) {
|
||||
$io->error($e->getMessage());
|
||||
return Command::FAILURE;
|
||||
}
|
||||
|
||||
$em->flush();
|
||||
|
||||
$io->success("Group successfully added");
|
||||
|
||||
return Command::SUCCESS;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,89 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace LotGD\Core\Console\Command\Scene;
|
||||
|
||||
use LotGD\Core\Console\Command\BaseCommand;
|
||||
use LotGD\Core\Models\Scene;
|
||||
use LotGD\Core\Models\SceneConnection;
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
use Symfony\Component\Console\Input\InputArgument;
|
||||
use Symfony\Component\Console\Input\InputDefinition;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
use Symfony\Component\Console\Style\SymfonyStyle;
|
||||
|
||||
/**
|
||||
* Resets the viewpoint of a given character.
|
||||
*/
|
||||
class SceneRemoveConnectionGroupCommand extends BaseCommand
|
||||
{
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
protected function configure()
|
||||
{
|
||||
$this->setName('scene:removeConnectionGroup')
|
||||
->setDescription('Removes a connection group from an existing scene.')
|
||||
->setDefinition(
|
||||
new InputDefinition([
|
||||
new InputArgument("id", InputArgument::REQUIRED, "ID of the scene"),
|
||||
new InputArgument("groupName", InputArgument::REQUIRED, "Internal id of the group."),
|
||||
]),
|
||||
)
|
||||
;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
protected function execute(InputInterface $input, OutputInterface $output): int
|
||||
{
|
||||
$em = $this->game->getEntityManager();
|
||||
|
||||
$io = new SymfonyStyle($input, $output);
|
||||
|
||||
$sceneId = $input->getArgument("id");
|
||||
$groupName = $input->getArgument("groupName");
|
||||
|
||||
// Search scene
|
||||
/** @var ?Scene $scene */
|
||||
$scene = $em->getRepository(Scene::class)->find($sceneId);
|
||||
|
||||
if (!$scene) {
|
||||
$io->error("The requested scene with the ID {$sceneId} was not found");
|
||||
return Command::FAILURE;
|
||||
}
|
||||
|
||||
if (!$scene->hasConnectionGroup($groupName)) {
|
||||
$io->error("The scene {$sceneId} oes not have a connection group with the name {$groupName}");
|
||||
return Command::FAILURE;
|
||||
}
|
||||
|
||||
$connectionGroup = $scene->getConnectionGroup($groupName);
|
||||
|
||||
# Mark for removal
|
||||
$em->remove($connectionGroup);
|
||||
|
||||
# Update outgoing connections if they refer to the deleted connectionGroup
|
||||
$connections = $scene->getConnections();
|
||||
/** @var SceneConnection $connection */
|
||||
foreach ($connections as $connection) {
|
||||
if ($connection->getIncomingScene() === $scene and $connection->getIncomingConnectionGroupName() === $groupName) {
|
||||
$connection->setIncomingConnectionGroupName(null);
|
||||
$io->comment("Updated connection to {$connection->getOutgoingScene()->getTitle()}");
|
||||
}
|
||||
|
||||
if ($connection->getOutgoingScene() === $scene and $connection->getOutgoingConnectionGroupName() === $groupName) {
|
||||
$connection->setOutgoingConnectionGroupName(null);
|
||||
$io->comment("Updated connection to {$connection->getIncomingScene()->getTitle()}");
|
||||
}
|
||||
}
|
||||
|
||||
$em->flush();
|
||||
|
||||
$io->success("Group successfully added");
|
||||
|
||||
return Command::SUCCESS;
|
||||
}
|
||||
}
|
||||
@@ -13,9 +13,12 @@ use LotGD\Core\Console\Command\Database\DatabaseSchemaUpdateCommand;
|
||||
use LotGD\Core\Console\Command\Module\ModuleRegisterCommand;
|
||||
use LotGD\Core\Console\Command\Module\ModuleValidateCommand;
|
||||
use LotGD\Core\Console\Command\Scene\SceneAddCommand;
|
||||
use LotGD\Core\Console\Command\Scene\SceneAddConnectionGroupCommand;
|
||||
use LotGD\Core\Console\Command\Scene\SceneConnectCommand;
|
||||
use LotGD\Core\Console\Command\Scene\SceneListCommand;
|
||||
use LotGD\Core\Console\Command\Scene\SceneDisconnectCommand;
|
||||
use LotGD\Core\Console\Command\Scene\SceneRemoveConnectionGroupCommand;
|
||||
use LotGD\Core\Console\Command\Scene\SceneShowCommand;
|
||||
use LotGD\Core\Console\Command\SceneTemplates\SceneTemplateListCommand;
|
||||
use LotGD\Core\Game;
|
||||
use Symfony\Component\Console\Application;
|
||||
@@ -56,6 +59,9 @@ class Main
|
||||
$this->application->add(new SceneAddCommand($this->game));
|
||||
$this->application->add(new SceneConnectCommand($this->game));
|
||||
$this->application->add(new SceneDisconnectCommand($this->game));
|
||||
$this->application->add(new SceneShowCommand($this->game));
|
||||
$this->application->add(new SceneAddConnectionGroupCommand($this->game));
|
||||
$this->application->add(new SceneRemoveConnectionGroupCommand($this->game));
|
||||
$this->application->add(new SceneTemplateListCommand($this->game));
|
||||
|
||||
// Add additional ones
|
||||
|
||||
@@ -152,6 +152,10 @@ class Scene implements CreateableInterface, SceneConnectable
|
||||
throw new ArgumentException("The given connection group is already owned by another scene entity.");
|
||||
}
|
||||
|
||||
if ($this->hasConnectionGroup($group->getName())) {
|
||||
throw new ArgumentException("Cannot add a second group with the same name to this scene.");
|
||||
}
|
||||
|
||||
$group->setScene($this);
|
||||
$this->connectionGroups->add($group);
|
||||
}
|
||||
|
||||
@@ -3,7 +3,11 @@ declare(strict_types=1);
|
||||
|
||||
namespace LotGD\Core\Models;
|
||||
|
||||
use Doctrine\ORM\Mapping\Column;
|
||||
use Doctrine\ORM\Mapping\Entity;
|
||||
use Doctrine\ORM\Mapping\Id;
|
||||
use Doctrine\ORM\Mapping\JoinColumn;
|
||||
use Doctrine\ORM\Mapping\ManyToOne;
|
||||
use Doctrine\ORM\Mapping\Table;
|
||||
|
||||
/**
|
||||
@@ -18,34 +22,34 @@ class SceneConnection
|
||||
* @ManyToOne(targetEntity="Scene", inversedBy="outgoingConnections")
|
||||
* @JoinColumn(name="outgoingScene", referencedColumnName="id")
|
||||
*/
|
||||
private $outgoingScene;
|
||||
private ?Scene $outgoingScene = null;
|
||||
|
||||
/**
|
||||
* @Id
|
||||
* @ManyToOne(targetEntity="Scene", inversedBy="incomingConnections")
|
||||
* @JoinColumn(name="incomingScene", referencedColumnName="id")
|
||||
*/
|
||||
private $incomingScene;
|
||||
private ?Scene $incomingScene = null;
|
||||
|
||||
/**
|
||||
* @Column(type="integer", options={"default"=0})
|
||||
*/
|
||||
private $directionality = 0;
|
||||
private int $directionality = 0;
|
||||
|
||||
/**
|
||||
* @Column(type="string", nullable=True)
|
||||
*/
|
||||
private $outgoingConnectionGroupName;
|
||||
private ?string $outgoingConnectionGroupName;
|
||||
|
||||
/**
|
||||
* @Column(type="string", nullable=True)
|
||||
*/
|
||||
private $incomingConnectionGroupName;
|
||||
private ?string $incomingConnectionGroupName;
|
||||
|
||||
/**
|
||||
*
|
||||
* @param \LotGD\Core\Models\Scene $outgoing
|
||||
* @param \LotGD\Core\Models\Scene $incoming
|
||||
* @param Scene $outgoing
|
||||
* @param Scene $incoming
|
||||
* @param int $directionality
|
||||
*/
|
||||
public function __construct(
|
||||
|
||||
@@ -3,8 +3,13 @@ declare(strict_types=1);
|
||||
|
||||
namespace LotGD\Core\Models;
|
||||
|
||||
use Doctrine\ORM\Mapping\Column;
|
||||
use Doctrine\ORM\Mapping\Entity;
|
||||
use Doctrine\ORM\Mapping\Id;
|
||||
use Doctrine\ORM\Mapping\JoinColumn;
|
||||
use Doctrine\ORM\Mapping\ManyToOne;
|
||||
use Doctrine\ORM\Mapping\Table;
|
||||
use LotGD\Core\Exceptions\ArgumentException;
|
||||
|
||||
/**
|
||||
*
|
||||
@@ -18,18 +23,18 @@ class SceneConnectionGroup implements SceneConnectable
|
||||
* @ManyToOne(targetEntity="Scene", inversedBy="outgoingConnections", cascade={"persist"})
|
||||
* @JoinColumn(name="scene", referencedColumnName="id")
|
||||
*/
|
||||
private $scene;
|
||||
private ?Scene $scene = null;
|
||||
|
||||
/**
|
||||
* @Id
|
||||
* @Column(type="string")
|
||||
*/
|
||||
private $name;
|
||||
private string $name;
|
||||
|
||||
/**
|
||||
* @Column(type="string", length=255)
|
||||
*/
|
||||
private $title;
|
||||
private string $title;
|
||||
|
||||
/**
|
||||
* SceneConnectionGroup constructor.
|
||||
@@ -44,7 +49,7 @@ class SceneConnectionGroup implements SceneConnectable
|
||||
|
||||
/**
|
||||
* Returns the scene associated with this connection group.
|
||||
* @return \LotGD\Core\Models\Scene
|
||||
* @return Scene
|
||||
*/
|
||||
public function getScene(): ?Scene
|
||||
{
|
||||
@@ -53,7 +58,7 @@ class SceneConnectionGroup implements SceneConnectable
|
||||
|
||||
/**
|
||||
* Sets the scene associated with this connection group.
|
||||
* @param \LotGD\Core\Models\Scene $scene
|
||||
* @param Scene $scene
|
||||
*/
|
||||
public function setScene(Scene $scene): void
|
||||
{
|
||||
@@ -100,6 +105,7 @@ class SceneConnectionGroup implements SceneConnectable
|
||||
* @param SceneConnectable $connectable
|
||||
* @param int|null $directionality
|
||||
* @return SceneConnection
|
||||
* @throws ArgumentException
|
||||
*/
|
||||
public function connect(SceneConnectable $connectable, int $directionality = null): SceneConnection
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user