Change hook for setting up viewpoint to handle all actions, data and attachments

This commit is contained in:
Austen McDonald
2016-08-02 20:11:36 +00:00
parent 36c57cf9cb
commit d82f3fc588
3 changed files with 30 additions and 21 deletions
+5 -20
View File
@@ -247,8 +247,9 @@ class Game
/**
* Returns a viewpoint made from a Scene $s and the current user, complete
* with actions built from the scene's children and those modified/added by
* the hook 'h/lotgd/core/actions-for/[scene-template]'.
* with actions built from the scene's children. Further modifications to
* the viewpoint can be made by responding to the hook
* 'h/lotgd/core/viewpoint-for/[scene-template]'.
*/
private function setupViewpoint(CharacterViewpoint $v, Scene $s)
{
@@ -258,28 +259,12 @@ class Game
$as = array_map(function ($c) { return new Action($c->getId()); }, $s->getChildren()->toArray());
$ag->setActions($as);
$context = [
'g' => $this,
'viewpoint' => $v,
'actions' => [$ag]
];
$this->getEventManager()->publish('h/lotgd/core/actions-for/' . $s->getTemplate(), $context);
$v->setActions($context['actions']);
$v->setActions([$ag]);
$context = [
'g' => $this,
'viewpoint' => $v,
'attachments' => []
];
$this->getEventManager()->publish('h/lotgd/core/attachments-for/' . $s->getTemplate(), $context);
$v->setAttachments($context['attachments']);
$context = [
'g' => $this,
'viewpoint' => $v,
'data' => []
];
$this->getEventManager()->publish('h/lotgd/core/data-for/' . $s->getTemplate(), $context);
$v->setData($context['data']);
$this->getEventManager()->publish('h/lotgd/core/viewpoint-for/' . $s->getTemplate(), $context);
}
}
+21 -1
View File
@@ -25,6 +25,10 @@ use LotGD\Core\Tests\ModelTestCase;
class DefaultSceneProvider implements EventHandler
{
public static $actions = ['actions'];
public static $attachments = ['actions'];
public static $data = ['data'];
public static function handleEvent(string $event, array &$context)
{
switch ($event) {
@@ -33,6 +37,13 @@ class DefaultSceneProvider implements EventHandler
throw new \Exception("Key 'character' was expected on event h/lotgd/core/default-scene.");
}
$context['scene'] = $context['g']->getEntityManager()->getRepository(Scene::class)->find(1);
break;
case 'h/lotgd/core/viewpoint-for/lotgd/tests/village':
$v = $context['viewpoint'];
$v->setActions(self::$actions);
$v->setAttachments(self::$attachments);
$v->setData(self::$data);
break;
}
}
}
@@ -92,11 +103,20 @@ class GameTest extends ModelTestCase
$this->g->setCharacter($c);
$this->g->getEventManager()->subscribe('/h\/lotgd\/core\/default-scene/', DefaultSceneProvider::class, 'lotgd/core/tests');
$this->g->getEventManager()->subscribe('/h\/lotgd\/core\/viewpoint-for\/.*/', DefaultSceneProvider::class, 'lotgd/core/tests');
$v = $this->g->getViewpoint();
// Run it twice to make sure no additional DB operations happen.
$v = $this->g->getViewpoint();
$this->assertEquals('lotgd/tests/village', $v->getTemplate());
// Validate the changes made by the hook.
$actions = serialize(DefaultSceneProvider::$actions);
$attachments = serialize(DefaultSceneProvider::$attachments);
$data = serialize(DefaultSceneProvider::$data);
$this->assertEquals('lotgd/tests/village', $v->getTemplate());
$this->g->getEventManager()->unsubscribe('/h\/lotgd\/core\/viewpoint-for\/.*/', DefaultSceneProvider::class, 'lotgd/core/tests');
}
public function testTakeActionNonExistant()
@@ -114,7 +134,7 @@ class GameTest extends ModelTestCase
public function testTakeActionNavigate()
{
$c = $this->getEntityManager()->getRepository(Character::class)->find(1);
$c = $this->getEntityManager()->getRepository(Character::class)->find(3);
$this->g->setCharacter($c);
// For now, I cant seem to serialize a proper ActionGroup to store in
+4
View File
@@ -7,6 +7,10 @@ characters:
id: 2
name: "Char with a Scene"
displayName: "B"
-
id: 3
name: "Char without a Scene"
displayName: "C"
character_viewpoints:
-
owner_id: 2