Adds commands to add, show and remove characters.

This commit is contained in:
Vassyli
2021-01-26 19:23:09 +01:00
committed by Basilius Sauter
parent e3534ea88d
commit 47693f6127
5 changed files with 328 additions and 17 deletions
@@ -0,0 +1,100 @@
<?php
declare(strict_types=1);
namespace LotGD\Core\Console\Command\Character;
use Exception;
use LotGD\Core\Console\Command\BaseCommand;
use LotGD\Core\Models\Character;
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 CharacterAddCommand extends BaseCommand
{
/**
* @inheritDoc
*/
protected function configure()
{
$this->setName('character:add')
->setDescription('Add a character.')
->setDefinition(
new InputDefinition([
new InputArgument(
"name",
mode: InputArgument::REQUIRED,
description: "Character name",
),
new InputOption(
"level",
mode: InputOption::VALUE_OPTIONAL,
description: "Character level",
default: 1,
),
new InputOption(
"maxHealth",
mode: InputOption::VALUE_OPTIONAL,
description: "Maximum health of the character. 10*level if not given.",
default: null,
),
])
)
;
}
/**
* @inheritDoc
*/
protected function execute(InputInterface $input, OutputInterface $output): int
{
$em = $this->game->getEntityManager();
$logger = $this->game->getLogger();
$io = new SymfonyStyle($input, $output);
$name = $input->getArgument("name");
$level = intval($input->getOption("level"));
$maxHealth = $input->getOption("maxHealth");
if ($level <= 0) {
$io->error("Level must at least be 1.");
return Command::FAILURE;
}
// Set maxHealth in dependence of the level if not given.
if ($maxHealth === null) {
$maxHealth = $level*10;
} else {
$maxHealth = intval($maxHealth);
}
$character = Character::createAtFullHealth([
"name" => $name,
"level" => $level,
"maxHealth" => $maxHealth,
]);
try {
$em->persist($character);
// Commit changes
$em->flush();
} catch (Exception $e) {
$io->error("Creating the character was not possible. Reason: {$e->getMessage()}.");
return Command::FAILURE;
}
$io->success("{$character} was successfully created.");
$logger->info("{$character} was created.");
return Command::SUCCESS;
}
}
@@ -0,0 +1,76 @@
<?php
declare(strict_types=1);
namespace LotGD\Core\Console\Command\Character;
use Exception;
use LotGD\Core\Console\Command\BaseCommand;
use LotGD\Core\Models\Character;
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 CharacterRemoveCommand extends BaseCommand
{
/**
* @inheritDoc
*/
protected function configure()
{
$this->setName('character:remove')
->setDescription('Definitely removes a character (no soft delete).')
->setDefinition(
new InputDefinition([
new InputArgument(
"id",
mode: InputArgument::REQUIRED,
description: "Character ID",
),
])
)
;
}
/**
* @inheritDoc
*/
protected function execute(InputInterface $input, OutputInterface $output): int
{
$em = $this->game->getEntityManager();
$logger = $this->game->getLogger();
$io = new SymfonyStyle($input, $output);
$id = $input->getArgument("id");
// Find character
/** @var Character $character */
$character = $em->getRepository(Character::class)->find($id);
if (!$character) {
$io->error("The character with the id {$id} was not found.");
return Command::FAILURE;
}
try {
$em->remove($character);
// Commit changes
$em->flush();
} catch (Exception $e) {
$io->error("Removing {$character} was not possible. Reason: {$e->getMessage()}.");
return Command::FAILURE;
}
$io->success("{$character} was successfully removed.");
$logger->info("{$character} was removed.");
return Command::SUCCESS;
}
}
@@ -0,0 +1,117 @@
<?php
declare(strict_types=1);
namespace LotGD\Core\Console\Command\Character;
use Exception;
use LotGD\Core\Console\Command\BaseCommand;
use LotGD\Core\Models\Character;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\TableSeparator;
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 CharacterShowCommand extends BaseCommand
{
/**
* @inheritDoc
*/
protected function configure()
{
$this->setName('character:show')
->setDescription('Shows details about character.')
->setDefinition(
new InputDefinition([
new InputArgument(
"id",
mode: InputArgument::REQUIRED,
description: "Character ID",
),
new InputOption(
"onlyViewpoint",
mode: InputOption::VALUE_NONE,
description: "Set to true to only display viewpoint",
)
])
)
;
}
/**
* @inheritDoc
*/
protected function execute(InputInterface $input, OutputInterface $output): int
{
$em = $this->game->getEntityManager();
$logger = $this->game->getLogger();
$io = new SymfonyStyle($input, $output);
$id = $input->getArgument("id");
$onlyViewpoint = $input->getOption("onlyViewpoint");
// Find character
/** @var Character $character */
$character = $em->getRepository(Character::class)->find($id);
if (!$character) {
$io->error("The character with the id {$id} was not found.");
return Command::FAILURE;
}
if (!$onlyViewpoint) {
$io->title("About Character {$character->getName()}");
$io->listing([
"ID: {$character->getId()}",
"Display name: {$character->getDisplayName()}",
"Level: {$character->getLevel()}",
"Health: {$character->getHealth()}/{$character->getMaxHealth()}",
"Alive: ".($character->isAlive()?"yes":"no"),
"Attack: {$character->getAttack()}",
"Defense: {$character->getDefense()}",
]);
$io->section("Viewpoint");
} else {
$io->title("Viewpoint of {$character->getName()}");
}
$viewpoint = $character->getViewpoint();
if (!$viewpoint) {
$io->text("No viewpoint yet");
} else {
$io->text($viewpoint->getTitle() . "\n");
$io->text($viewpoint->getDescription());
$io->section("Viewpoint actions");
$actionGroups = $viewpoint->getActionGroups();
$rows = [];
foreach ($actionGroups as $actionGroup) {
$rows[] = [$actionGroup->getId(), $actionGroup->getTitle(), "", "", ""];
foreach ($actionGroup->getActions() as $action) {
$rows[] = ["", "", $action->getId(), $action->getTitle(), $action->getDestinationSceneId()];
}
if (count($actionGroup->getActions())) {
$rows[] = new TableSeparator();
}
}
$io->table(["Group id", "Group name", "Action id", "Action name", "Destination"], $rows);
}
return Command::SUCCESS;
}
}
+6
View File
@@ -5,8 +5,11 @@ namespace LotGD\Core\Console;
use LotGD\Core\Bootstrap;
use LotGD\Core\Console\Command\Character\CharacterAddCommand;
use LotGD\Core\Console\Command\Character\CharacterListCommand;
use LotGD\Core\Console\Command\Character\CharacterRemoveCommand;
use LotGD\Core\Console\Command\Character\CharacterResetViewpointCommand;
use LotGD\Core\Console\Command\Character\CharacterShowCommand;
use LotGD\Core\Console\Command\ConsoleCommand;
use LotGD\Core\Console\Command\Database\DatabaseInitCommand;
use LotGD\Core\Console\Command\Database\DatabaseSchemaUpdateCommand;
@@ -56,8 +59,11 @@ class Main
$this->application->add(new ConsoleCommand($this->game));
// Character commands
$this->application->add(new CharacterAddCommand($this->game));
$this->application->add(new CharacterListCommand($this->game));
$this->application->add(new CharacterRemoveCommand($this->game));
$this->application->add(new CharacterResetViewpointCommand($this->game));
$this->application->add(new CharacterShowCommand($this->game));
// Scene commands
$this->application->add(new SceneListCommand($this->game));
+29 -17
View File
@@ -42,21 +42,29 @@ class Character implements CharacterInterface, CreateableInterface, GameAwareInt
use ExtendableModel;
/** @Id @Column(type="uuid", unique=True) */
private $id;
private UuidInterface $id;
/** @Column(type="string", length=50); */
private $name = "";
private string $name = "";
/** @Column(type="text"); */
private $displayName = "";
private string $displayName = "";
/** @Column(type="integer", options={"default"=10}) */
private $maxHealth = 10;
private int $maxHealth = 10;
/** @Column(type="integer", options={"default"=10}) */
private $health = 10;
private int $health = 10;
/** @Column(type="integer", options={"default"=1})/ */
private $level = 1;
private int $level = 1;
/** @OneToMany(targetEntity="CharacterProperty", mappedBy="owner", cascade={"persist", "remove"}) */
private $properties;
private ?Collection $properties;
/** @OneToOne(targetEntity="Viewpoint", mappedBy="owner", cascade={"persist", "remove"}) */
private $viewpoint;
private ?Viewpoint $viewpoint = null;
/**
* @ManyToMany(targetEntity="MessageThread", inversedBy="participants", cascade={"persist"})
* @JoinTable(
@@ -69,20 +77,20 @@ class Character implements CharacterInterface, CreateableInterface, GameAwareInt
* }
* )
*/
private $messageThreads;
/** @OneToMany(targetEntity="Buff", mappedBy="character", cascade={"persist"}) */
private $buffs;
/** @var BuffList */
private $buffList;
private ?Collection $messageThreads;
/** @var array */
private static $fillable = [
/** @OneToMany(targetEntity="Buff", mappedBy="character", cascade={"persist"}) */
private ?Collection $buffs;
private ?BuffList $buffList;
private static array $fillable = [
"name",
"maxHealth",
"level",
];
private $propertyClass = CharacterProperty::class;
private string $propertyClass = CharacterProperty::class;
/**
* Creates a character at full health.
@@ -91,6 +99,7 @@ class Character implements CharacterInterface, CreateableInterface, GameAwareInt
{
$newCharacter = self::create($arguments);
$newCharacter->setHealth($newCharacter->getMaxHealth());
return $newCharacter;
}
@@ -296,7 +305,7 @@ class Character implements CharacterInterface, CreateableInterface, GameAwareInt
/**
* Returns the current character viewpoint or null if one is not set.
* @return \LotGD\Core\Models\Viewpoint|null
* @return Viewpoint|null
*/
public function getViewpoint(): ?Viewpoint
{
@@ -305,6 +314,7 @@ class Character implements CharacterInterface, CreateableInterface, GameAwareInt
/**
* Sets the current character viewpoint.
* @param Viewpoint|null $v
*/
public function setViewpoint(?Viewpoint $v)
{
@@ -322,6 +332,8 @@ class Character implements CharacterInterface, CreateableInterface, GameAwareInt
/**
* Adds a buff to the buffList.
* @param Buff $buff
* @param bool $override
*/
public function addBuff(Buff $buff, bool $override = false)
{