lotgd / core

Core functionality for Legend of the Green Dragon, a text-based RPG game.
GNU Affero General Public License v3.0
152 stars 15 forks source link

Fix/issue 94 #98

Closed Vassyli closed 7 years ago

Vassyli commented 7 years ago

Fixes issue 94 by introducing a new series of classes, EventContext and the immutable EventContextData.

EventContext contains EventContextData, as well as information about the event (event name and matching pattern for this event). handleEvent is now expected to accept only EventContext and must return it; publish expects now an event name and a EventContextDataContainer instance and returns the EventContextDataContainer.

Examples:

$contextData = NewViewpoint::create([
    'character' => $this->getCharacter(),
    'scene' => null
]);

$contextData = $this->getEventManager()->publish('h/lotgd/core/default-scene', $contextData);

$s = $contextData->get("scene");
public static function handleEvent(Game $g, EventContext $context): EventContext
{
    if ($context->getEvent() === "h/lotgd/core/default-scene") {
        if (!$context->hasDataType(NewViewpoint::class)) {
            throw new \Exception(sprintf(
                "Context was expected to be %s, %s instead.",
                NewViewpoint::class,
                get_class($context->getData())
            ));
        }

        $context->setDataField("scene", $g->getEntityManager()->getRepository(Scene::class)->find(1));
    }
    return $context;
}