Skeleton of a character manager for online games
https://github.com/Pierstoval/CharacterManagerBundle.git
This bundle is here to provide a customizable skeleton to create characters based on a list of actions representing each step of character creation.
You can configure your steps both from configuration and services.
composer require pierstoval/character-manager.<?php
class AppKernel extends Kernel
{
public function registerBundles()
{
$bundles = [
// Sorry for the very long name.
new Pierstoval\Bundle\CharacterManagerBundle\PierstovalCharacterManagerBundle(),
];
}
}
Character class, for your first character manager:<?php
namespace App\Entity;
use Pierstoval\Bundle\CharacterManagerBundle\Model\CharacterInterface;
class Character implements CharacterInterface
{
// Implement interface methods
}
Pierstoval\Bundle\CharacterManagerBundle\Entity\Character,id property.
generator_steps:
resource: "@PierstovalCharacterManagerBundle/Resources/config/routing.xml"
prefix: /character_generator/ # Or any prefix you like
This routing file is important because it is the place where character generation will be handled.{manager} route option to your url prefix, when using multiple character managers.
To generate characters, you need what are called Step Action classes.
One generation step = one class.
Each class must implement StepActionInterface, but you can also extend the abstract class AbstractStepAction which
implements the interface and adds cool logic, so you just have to implement the execute() method.
You can define it at a simple class like this:
pierstoval_character_manager:
managers:
default:
character_class: 'App\Entity\Character'
steps:
step_01:
action: App\Step\Step01
You can also refer to an already existing service:
pierstoval_character_manager:
managers:
default:
character_class: 'App\Entity\Character'
steps:
step_01:
action: app.steps.step_1
services:
app.steps.step_1:
class: App\Step\Step01
arguments:
- ...
๐ฏ Note: You should know that all action classes that are not set as service will be defined as service,
autowired and set private, because it is mandatory for the ActionRegistry to have them as services.
However, your services will be untouched, to keep consistency with your own logic.
๐ Magic
Step configuration reference:
pierstoval_character_manager:
managers:
# Prototype
name:
character_class: ~ # Required
steps:
# Prototype
name:
# Can be a class or a service. Must implement StepActionInterface or extend abstract Action class.
action: ~ # Required
label: ''
# Steps that the current step may depend on. If step is not set in session, will throw an exception.
dependencies: []
# When this step will be updated, it will clear values for specified steps.
# Only available for the abstract class
onchange_clear: []
AbstractStepAction::goToStep($stepNumber) method (see below).
onchange_clear parameter is only handled in the abstract AbstractStepAction class, but you can implement itStepActionInterface::execute() method for example.
AbstractStepAction classThis is an example of a basic action:
<?php
namespace App\Step;
use Pierstoval\Bundle\CharacterManagerBundle\Action\AbstractStepAction;
use Symfony\Component\HttpFoundation\Response;
class Step1 extends AbstractStepAction
{
/**
* {@inheritdoc}
*/
public function execute(): Response
{
// Implement your step logic here.
return new Response('This step is now rendered');
}
}
When you have defined all your character managers configurations, the StepsPass will process them and execute certain
actions:
StepActionInterface.private, autowired and lazy.AbstractStepAction class, it will also inject:RouterInterface)ObjectManager, so works with both ORM and ODM).TranslatorInterface, even if not enabled in the framework).character_class optionStep object, retrieved from the StepActionResolverStepActionResolver. They're mostly used to manage the goToStep() and nextStep() methods in the abstract action class.
ActionRegistryInterface service registered in the container.First, you must know that the AbstractStepAction has no constructor.
Then, you're free to have your own constructor without being forced to rely on the parent's logic, and inject all your necessary services and parameters in the constructor, via autowiring.
The abstract class only adds some nice stuff to use (and if someone don't extend it, send me a message, I'd like to hear why you don't want to extend it), and this cool logic resides in other methods. So you're free to implement your own constructor, especially if you define your action as a service
If you define the step action as a service and extend the abstract class, you will have access to four services:
/** @var EntityManager */
$this->em;
/** @var Twig\Environment */
$this->twig;
/** @var RouterInterface */
$this->router;
/** @var TranslatorInterface */
$this->translator;
Most of the time, you don't need many other things, but if you need other things, just add the arguments: or calls:
options to your service definition.
The abstract class adds cool methods to manage your steps:
<?php
/** @var $this \Pierstoval\Bundle\CharacterManagerBundle\Action\AbstractStepAction */
// Get the character property for specified step name (or current step by default)
$this->getCharacterProperty($stepName = null);
// Returns a RedirectResponse object to the next step
$this->nextStep();
// Returns a RedirectResponse object to the specified step, but by number, not by name
$this->goToStep($stepNumber);
// A cool tool to use flash messages. By default, the "type" is "error".
// All flash messages are translated and the translation domain is set in the StepAction::$translationDomain static property.
// Of course you can override the translation domain if you create your own abstract StepAction class and override this var.
$this->flashMessage($msg, $type = null, array $msgParams = []);
// Updates the current character step value and sets it to $value
// This is the method that makes sure that "onchange_clear" steps are cleared with a simple "unset()" in the session
$this->updateCharacterStep($value);
A list of "todos":
Character object. Abstract, obviously, because it has to be implemented manually by the user.
The project is published under MIT license. See the license file for more information.